-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtag.go
More file actions
234 lines (198 loc) · 6.03 KB
/
tag.go
File metadata and controls
234 lines (198 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package cmd
import (
"encoding/json"
"fmt"
"strings"
"github.com/opslevel/cli/common"
"github.com/opslevel/opslevel-go/v2025"
"golang.org/x/exp/slices"
"github.com/spf13/cobra"
)
var resourceType string
var exampleTagCmd = &cobra.Command{
Use: "tag",
Short: "Example tag to assign to a resource",
Long: `Example tag to assign to a resource`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(getExample(opslevel.TagInput{
Key: "example_key",
Value: "example_value",
}))
},
}
var createTagCmd = &cobra.Command{
Use: "tag --type=RESOURCE_TYPE [--assign] ID|ALIAS KEY VALUE",
Short: "Create/assign a tag",
Long: "Create/assign a tag",
Example: `
opslevel create tag --type=Service my-service-alias foo bar
opslevel create tag --type=Team my-team-alias foo bar
opslevel create tag --type=Infra my-infra-alias foo bar
`,
Args: cobra.ExactArgs(3),
ArgAliases: []string{"RESOURCE_ID", "KEY", "VALUE"},
Run: func(cmd *cobra.Command, args []string) {
err := validateResourceTypeArg()
cobra.CheckErr(err)
resource := args[0]
key := args[1]
value := args[2]
if cmd.Flag("assign").Changed {
tagInput := opslevel.TagInput{
Key: key,
Value: value,
}
input := opslevel.TagAssignInput{Tags: []opslevel.TagInput{tagInput}}
if opslevel.IsID(resource) {
input.Id = opslevel.RefOf(opslevel.ID(resource))
} else {
input.Alias = opslevel.RefOf(resource)
resourceType := opslevel.TaggableResource(resourceType)
input.Type = &resourceType
}
result, err := getClientGQL().AssignTag(input)
cobra.CheckErr(err)
fmt.Printf("assigned tag for %s: %s\n", resource, result[0].Id)
} else {
input := opslevel.TagCreateInput{
Key: key,
Value: value,
}
if opslevel.IsID(resource) {
input.Id = opslevel.NewID(resource)
} else {
input.Alias = &resource
resourceType := opslevel.TaggableResource(resourceType)
input.Type = &resourceType
}
result, err := getClientGQL().CreateTag(input)
cobra.CheckErr(err)
fmt.Printf("created tag for %s: %s\n", resource, result.Id)
}
},
}
var getObjectTagCmd = &cobra.Command{
Use: "tag --type=RESOURCE_TYPE ID|ALIAS KEY",
Short: "Get tags on an object matching key",
Long: "Get tags on an object matching key",
Example: `
opslevel get tag --type=Service my-service-alias foo
`,
Args: cobra.ExactArgs(2),
ArgAliases: []string{"RESOURCE_ID", "KEY"},
Run: func(cmd *cobra.Command, args []string) {
err := validateResourceTypeArg()
cobra.CheckErr(err)
resourceIdentifier := args[0]
tagKey := args[1]
client := getClientGQL()
result, err := client.GetTaggableResource(opslevel.TaggableResource(resourceType), resourceIdentifier)
cobra.CheckErr(err)
tags, err := result.GetTags(client, nil)
cobra.CheckErr(err)
output := make([]opslevel.Tag, 0)
for _, tag := range tags.Nodes {
if tagKey == tag.Key {
output = append(output, tag)
}
}
common.PrettyPrint(output)
},
}
var listObjectTagCmd = &cobra.Command{
Use: "tag --type=RESOURCE_TYPE ID|ALIAS",
Aliases: []string{"tags"},
Short: "Get all tags on an object",
Long: "Get all tags on an object",
Example: `
opslevel list tag --type=Service my-service-alias -o json | jq
`,
Args: cobra.ExactArgs(1),
ArgAliases: []string{"RESOURCE_ID"},
Run: func(cmd *cobra.Command, args []string) {
err := validateResourceTypeArg()
cobra.CheckErr(err)
resourceIdentifier := args[0]
client := getClientGQL()
result, err := client.GetTaggableResource(opslevel.TaggableResource(resourceType), resourceIdentifier)
cobra.CheckErr(err)
tags, err := result.GetTags(client, nil)
cobra.CheckErr(err)
common.PrettyPrint(tags.Nodes)
},
}
var updateTagCmd = &cobra.Command{
Use: "tag TAG_ID KEY VALUE",
Short: "Update a tag",
Long: "Update a tag",
Example: `
opslevel update tag XXX_TAG_ID_XXX foo baz
`,
Args: cobra.ExactArgs(3),
ArgAliases: []string{"TAG_ID", "KEY", "VALUE"},
Run: func(cmd *cobra.Command, args []string) {
tag := args[0]
key := args[1]
value := args[2]
input := opslevel.TagUpdateInput{
Id: opslevel.ID(tag),
Key: &key,
Value: &value,
}
result, err := getClientGQL().UpdateTag(input)
cobra.CheckErr(err)
common.JsonPrint(json.MarshalIndent(result, "", " "))
},
}
var deleteTagCmd = &cobra.Command{
Use: "tag TAG_ID",
Short: "Delete a tag",
Long: "Delete a tag",
Example: `
opslevel delete tag XXX_TAG_ID_XXX
`,
Args: cobra.ExactArgs(1),
ArgAliases: []string{"TAG_ID"},
Run: func(cmd *cobra.Command, args []string) {
tag := opslevel.ID(args[0])
err := getClientGQL().DeleteTag(tag)
cobra.CheckErr(err)
fmt.Printf("deleted a tag: %s\n", tag)
},
}
func init() {
exampleCmd.AddCommand(exampleTagCmd)
createCmd.AddCommand(createTagCmd)
createTagCmd.Flags().StringVar(&resourceType, "type", "", "resource type")
createTagCmd.Flags().Bool("assign", false, "if a tag with the same key already exists it will be updated, otherwise a new tag will be created.")
getCmd.AddCommand(getObjectTagCmd)
getObjectTagCmd.Flags().StringVar(&resourceType, "type", "", "resource type")
listCmd.AddCommand(listObjectTagCmd)
listObjectTagCmd.Flags().StringVar(&resourceType, "type", "", "resource type")
updateCmd.AddCommand(updateTagCmd)
deleteCmd.AddCommand(deleteTagCmd)
}
func validateResourceTypeArg() error {
if resourceType == "" {
return fmt.Errorf("must specify a taggable resource type using --type=RESOURCE_TYPE")
}
// if ProperCase, continue
if slices.Contains(opslevel.AllTaggableResource, resourceType) {
return nil
}
// if lowercase, check if it exists. if not, error out
lowercaseInput := strings.ToLower(resourceType)
if lowercaseInput == "infra" {
resourceType = string(opslevel.TaggableResourceInfrastructureresource)
return nil
}
lowercaseMap := make(map[string]string)
for _, s := range opslevel.AllTaggableResource {
lowercaseMap[strings.ToLower(s)] = s
}
if lowercaseMap[lowercaseInput] == "" {
return fmt.Errorf("not a taggable resource type: '%s'", resourceType)
}
resourceType = lowercaseMap[lowercaseInput]
return nil
}