Skip to content

Commit d649653

Browse files
authored
Merge pull request #207 from ans-group/tag-management
Add tag management commands
2 parents 06a4268 + 67db8b9 commit d649653

6 files changed

Lines changed: 553 additions & 11 deletions

File tree

cmd/ecloud/ecloud.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func ECloudRootCmd(f factory.ClientFactory, fs afero.Fs) *cobra.Command {
6161
cmd.AddCommand(ecloudRouterRootCmd(f))
6262
cmd.AddCommand(ecloudRouterThroughputRootCmd(f))
6363
cmd.AddCommand(ecloudSSHKeyPairRootCmd(f, fs))
64+
cmd.AddCommand(ecloudTagRootCmd(f))
6465
cmd.AddCommand(ecloudTaskRootCmd(f))
6566
cmd.AddCommand(ecloudVIPRootCmd(f))
6667
cmd.AddCommand(ecloudVolumeRootCmd(f))

cmd/ecloud/ecloud_tag.go

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
package ecloud
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
"github.com/ans-group/cli/internal/pkg/factory"
8+
"github.com/ans-group/cli/internal/pkg/helper"
9+
"github.com/ans-group/cli/internal/pkg/output"
10+
"github.com/ans-group/sdk-go/pkg/service/ecloud"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
func ecloudTagRootCmd(f factory.ClientFactory) *cobra.Command {
15+
cmd := &cobra.Command{
16+
Use: "tag",
17+
Short: "sub-commands relating to tags",
18+
}
19+
20+
// Child commands
21+
cmd.AddCommand(ecloudTagListCmd(f))
22+
cmd.AddCommand(ecloudTagShowCmd(f))
23+
cmd.AddCommand(ecloudTagCreateCmd(f))
24+
cmd.AddCommand(ecloudTagUpdateCmd(f))
25+
cmd.AddCommand(ecloudTagDeleteCmd(f))
26+
27+
return cmd
28+
}
29+
30+
func ecloudTagListCmd(f factory.ClientFactory) *cobra.Command {
31+
cmd := &cobra.Command{
32+
Use: "list",
33+
Short: "Lists tags",
34+
Long: "This command lists tags",
35+
Example: "ans ecloud tag list",
36+
RunE: ecloudCobraRunEFunc(f, ecloudTagList),
37+
}
38+
39+
cmd.Flags().String("name", "", "Tag name for filtering")
40+
cmd.Flags().String("scope", "", "Tag scope for filtering")
41+
42+
return cmd
43+
}
44+
45+
func ecloudTagList(service ecloud.ECloudService, cmd *cobra.Command, args []string) error {
46+
params, err := helper.GetAPIRequestParametersFromFlags(cmd,
47+
helper.NewStringFilterFlagOption("name", "name"),
48+
helper.NewStringFilterFlagOption("scope", "scope"),
49+
)
50+
if err != nil {
51+
return err
52+
}
53+
54+
tags, err := service.GetTags(params)
55+
if err != nil {
56+
return fmt.Errorf("ecloud: Error retrieving tags: %s", err)
57+
}
58+
59+
return output.CommandOutput(cmd, TagCollection(tags))
60+
}
61+
62+
func ecloudTagShowCmd(f factory.ClientFactory) *cobra.Command {
63+
return &cobra.Command{
64+
Use: "show <tag: id>...",
65+
Short: "Shows a tag",
66+
Long: "This command shows one or more tags",
67+
Example: "ans ecloud tag show tag-abcdef12",
68+
Args: func(cmd *cobra.Command, args []string) error {
69+
if len(args) < 1 {
70+
return errors.New("Missing tag")
71+
}
72+
73+
return nil
74+
},
75+
RunE: ecloudCobraRunEFunc(f, ecloudTagShow),
76+
}
77+
}
78+
79+
func ecloudTagShow(service ecloud.ECloudService, cmd *cobra.Command, args []string) error {
80+
var tags []ecloud.Tag
81+
for _, arg := range args {
82+
tag, err := service.GetTag(arg)
83+
if err != nil {
84+
output.OutputWithErrorLevelf("Error retrieving tag [%s]: %s", arg, err)
85+
continue
86+
}
87+
88+
tags = append(tags, tag)
89+
}
90+
91+
return output.CommandOutput(cmd, TagCollection(tags))
92+
}
93+
94+
func ecloudTagCreateCmd(f factory.ClientFactory) *cobra.Command {
95+
cmd := &cobra.Command{
96+
Use: "create",
97+
Short: "Creates a tag",
98+
Long: "This command creates a tag",
99+
Example: "ans ecloud tag create --name \"production\" --scope \"environment\"",
100+
RunE: ecloudCobraRunEFunc(f, ecloudTagCreate),
101+
}
102+
103+
// Setup flags
104+
cmd.Flags().String("name", "", "Name of tag")
105+
cmd.MarkFlagRequired("name")
106+
cmd.Flags().String("scope", "", "Scope of tag")
107+
cmd.MarkFlagRequired("scope")
108+
109+
return cmd
110+
}
111+
112+
func ecloudTagCreate(service ecloud.ECloudService, cmd *cobra.Command, args []string) error {
113+
createRequest := ecloud.CreateTagRequest{}
114+
createRequest.Name, _ = cmd.Flags().GetString("name")
115+
createRequest.Scope, _ = cmd.Flags().GetString("scope")
116+
117+
tagID, err := service.CreateTag(createRequest)
118+
if err != nil {
119+
return fmt.Errorf("ecloud: Error creating tag: %s", err)
120+
}
121+
122+
tag, err := service.GetTag(tagID)
123+
if err != nil {
124+
return fmt.Errorf("ecloud: Error retrieving new tag: %s", err)
125+
}
126+
127+
return output.CommandOutput(cmd, TagCollection([]ecloud.Tag{tag}))
128+
}
129+
130+
func ecloudTagUpdateCmd(f factory.ClientFactory) *cobra.Command {
131+
cmd := &cobra.Command{
132+
Use: "update <tag: id>...",
133+
Short: "Updates a tag",
134+
Long: "This command updates one or more tags",
135+
Example: "ans ecloud tag update tag-abcdef12 --name \"staging\"",
136+
Args: func(cmd *cobra.Command, args []string) error {
137+
if len(args) < 1 {
138+
return errors.New("Missing tag")
139+
}
140+
141+
return nil
142+
},
143+
RunE: ecloudCobraRunEFunc(f, ecloudTagUpdate),
144+
}
145+
146+
cmd.Flags().String("name", "", "Name of tag")
147+
cmd.Flags().String("scope", "", "Scope of tag")
148+
149+
return cmd
150+
}
151+
152+
func ecloudTagUpdate(service ecloud.ECloudService, cmd *cobra.Command, args []string) error {
153+
patchRequest := ecloud.PatchTagRequest{}
154+
155+
if cmd.Flags().Changed("name") {
156+
patchRequest.Name, _ = cmd.Flags().GetString("name")
157+
}
158+
159+
if cmd.Flags().Changed("scope") {
160+
patchRequest.Scope, _ = cmd.Flags().GetString("scope")
161+
}
162+
163+
var tags []ecloud.Tag
164+
for _, arg := range args {
165+
err := service.PatchTag(arg, patchRequest)
166+
if err != nil {
167+
output.OutputWithErrorLevelf("Error updating tag [%s]: %s", arg, err)
168+
continue
169+
}
170+
171+
tag, err := service.GetTag(arg)
172+
if err != nil {
173+
output.OutputWithErrorLevelf("Error retrieving updated tag [%s]: %s", arg, err)
174+
continue
175+
}
176+
177+
tags = append(tags, tag)
178+
}
179+
180+
return output.CommandOutput(cmd, TagCollection(tags))
181+
}
182+
183+
func ecloudTagDeleteCmd(f factory.ClientFactory) *cobra.Command {
184+
cmd := &cobra.Command{
185+
Use: "delete <tag: id>...",
186+
Short: "Removes a tag",
187+
Long: "This command removes one or more tags",
188+
Example: "ans ecloud tag delete tag-abcdef12",
189+
Args: func(cmd *cobra.Command, args []string) error {
190+
if len(args) < 1 {
191+
return errors.New("Missing tag")
192+
}
193+
194+
return nil
195+
},
196+
RunE: ecloudCobraRunEFunc(f, ecloudTagDelete),
197+
}
198+
199+
return cmd
200+
}
201+
202+
func ecloudTagDelete(service ecloud.ECloudService, cmd *cobra.Command, args []string) error {
203+
for _, arg := range args {
204+
err := service.DeleteTag(arg)
205+
if err != nil {
206+
output.OutputWithErrorLevelf("Error removing tag [%s]: %s", arg, err)
207+
continue
208+
}
209+
}
210+
return nil
211+
}

0 commit comments

Comments
 (0)