-
Notifications
You must be signed in to change notification settings - Fork 70
/
command_tags.go
248 lines (195 loc) · 7.67 KB
/
command_tags.go
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package entities
import (
"errors"
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/newrelic/newrelic-cli/internal/client"
"github.com/newrelic/newrelic-cli/internal/output"
"github.com/newrelic/newrelic-cli/internal/pipe"
"github.com/newrelic/newrelic-cli/internal/utils"
"github.com/newrelic/newrelic-client-go/newrelic"
"github.com/newrelic/newrelic-client-go/pkg/entities"
)
var (
entityTag string
entityTags []string
)
var cmdTags = &cobra.Command{
Use: "tags",
Short: "Manage tags on New Relic entities",
Long: `Manage entity tags
The tag command allows users to manage the tags applied on the requested
entity. Use --help for more information.
`,
Example: "newrelic entity tags get --guid <guid>",
}
var cmdTagsGet = &cobra.Command{
Use: "get",
Short: "Get the tags for a given entity",
Long: `Get the tags for a given entity
The get command returns JSON output of the tags for the requested entity.
`,
Example: "newrelic entity tags get --guid <entityGUID>",
Run: func(cmd *cobra.Command, args []string) {
client.WithClient(func(nrClient *newrelic.NewRelic) {
// Temporary until bulk actions can be build into newrelic-client-go
if value, ok := pipe.Get("guid"); ok {
tags, err := nrClient.Entities.GetTagsForEntity(entities.EntityGUID(value[0]))
utils.LogIfFatal(err)
utils.LogIfError(output.Print(tags))
} else {
tags, err := nrClient.Entities.GetTagsForEntity(entities.EntityGUID(entityGUID))
utils.LogIfFatal(err)
utils.LogIfError(output.Print(tags))
}
})
},
}
var cmdTagsDelete = &cobra.Command{
Use: "delete",
Short: "Delete the given tag:value pairs from the given entity",
Long: `Delete the given tag:value pairs from the given entity
The delete command deletes all tags on the given entity
that match the specified keys.
`,
Example: "newrelic entity tags delete --guid <entityGUID> --tag tag1 --tag tag2 --tag tag3,tag4",
Run: func(cmd *cobra.Command, args []string) {
client.WithClient(func(nrClient *newrelic.NewRelic) {
_, err := nrClient.Entities.TaggingDeleteTagFromEntity(entities.EntityGUID(entityGUID), entityTags)
utils.LogIfFatal(err)
log.Info("success")
})
},
}
var cmdTagsDeleteValues = &cobra.Command{
Use: "delete-values",
Short: "Delete the given tag/value pairs from the given entity",
Long: `Delete the given tag/value pairs from the given entity
The delete-values command deletes the specified tag:value pairs on a given entity.
`,
Example: "newrelic entity tags delete-values --guid <guid> --tag tag1:value1",
Run: func(cmd *cobra.Command, args []string) {
client.WithClient(func(nrClient *newrelic.NewRelic) {
tagValues, err := assembleTagValuesInput(entityValues)
utils.LogIfFatal(err)
_, err = nrClient.Entities.TaggingDeleteTagValuesFromEntity(entities.EntityGUID(entityGUID), tagValues)
utils.LogIfFatal(err)
log.Info("success")
})
},
}
var cmdTagsCreate = &cobra.Command{
Use: "create",
Short: "Create tag:value pairs for the given entity",
Long: `Create tag:value pairs for the given entity
The create command adds tag:value pairs to the given entity.
`,
Example: "newrelic entity tags create --guid <entityGUID> --tag tag1:value1",
Run: func(cmd *cobra.Command, args []string) {
client.WithClient(func(nrClient *newrelic.NewRelic) {
tags, err := assembleTagsInput(entityTags)
utils.LogIfFatal(err)
_, err = nrClient.Entities.TaggingAddTagsToEntity(entities.EntityGUID(entityGUID), tags)
utils.LogIfFatal(err)
log.Info("success")
})
},
}
var cmdTagsReplace = &cobra.Command{
Use: "replace",
Short: "Replace tag:value pairs for the given entity",
Long: `Replace tag:value pairs for the given entity
The replace command replaces any existing tag:value pairs with those
provided for the given entity.
`,
Example: "newrelic entity tags replace --guid <entityGUID> --tag tag1:value1",
Run: func(cmd *cobra.Command, args []string) {
client.WithClient(func(nrClient *newrelic.NewRelic) {
tags, err := assembleTagsInput(entityTags)
utils.LogIfFatal(err)
_, err = nrClient.Entities.TaggingReplaceTagsOnEntity(entities.EntityGUID(entityGUID), tags)
utils.LogIfFatal(err)
log.Info("success")
})
},
}
func assembleTagsInput(tags []string) ([]entities.TaggingTagInput, error) {
var t []entities.TaggingTagInput
tagBuilder := make(map[string][]string)
for _, x := range tags {
if !strings.Contains(x, ":") {
return []entities.TaggingTagInput{}, errors.New("tags must be specified as colon separated key:value pairs")
}
v := strings.SplitN(x, ":", 2)
tagBuilder[v[0]] = append(tagBuilder[v[0]], v[1])
}
for k, v := range tagBuilder {
tag := entities.TaggingTagInput{
Key: k,
Values: v,
}
t = append(t, tag)
}
return t, nil
}
func assembleTagValues(values []string) ([]entities.EntitySearchQueryBuilderTag, error) {
var tagValues []entities.EntitySearchQueryBuilderTag
for _, x := range values {
key, value, err := assembleTagValue(x)
if err != nil {
return []entities.EntitySearchQueryBuilderTag{}, err
}
tagValues = append(tagValues, entities.EntitySearchQueryBuilderTag{Key: key, Value: value})
}
return tagValues, nil
}
// assembleTagValuesInput is the same as assembleTagValues
func assembleTagValuesInput(values []string) ([]entities.TaggingTagValueInput, error) {
var tagValues []entities.TaggingTagValueInput
for _, x := range values {
key, value, err := assembleTagValue(x)
if err != nil {
return []entities.TaggingTagValueInput{}, err
}
tagValues = append(tagValues, entities.TaggingTagValueInput{Key: key, Value: value})
}
return tagValues, nil
}
func assembleTagValue(tagValueString string) (string, string, error) {
tagFormatError := errors.New("tag values must be specified as colon separated key:value pairs")
if !strings.Contains(tagValueString, ":") {
return "", "", tagFormatError
}
v := strings.SplitN(tagValueString, ":", 2)
// Handle incomplete tag where the value portion is empty
if v[1] == "" {
return "", "", tagFormatError
}
return v[0], v[1], nil
}
func init() {
Command.AddCommand(cmdTags)
cmdTags.AddCommand(cmdTagsGet)
cmdTagsGet.Flags().StringVarP(&entityGUID, "guid", "g", "", "the entity GUID to retrieve tags for")
cmdTags.AddCommand(cmdTagsDelete)
cmdTagsDelete.Flags().StringVarP(&entityGUID, "guid", "g", "", "the entity GUID to delete tags on")
cmdTagsDelete.Flags().StringSliceVarP(&entityTags, "tag", "t", []string{}, "the tag keys to delete from the entity")
utils.LogIfError(cmdTagsDelete.MarkFlagRequired("guid"))
utils.LogIfError(cmdTagsDelete.MarkFlagRequired("tag"))
cmdTags.AddCommand(cmdTagsDeleteValues)
cmdTagsDeleteValues.Flags().StringVarP(&entityGUID, "guid", "g", "", "the entity GUID to delete tag values on")
cmdTagsDeleteValues.Flags().StringSliceVarP(&entityValues, "value", "v", []string{}, "the tag key:value pairs to delete from the entity")
utils.LogIfError(cmdTagsDeleteValues.MarkFlagRequired("guid"))
utils.LogIfError(cmdTagsDeleteValues.MarkFlagRequired("value"))
cmdTags.AddCommand(cmdTagsCreate)
cmdTagsCreate.Flags().StringVarP(&entityGUID, "guid", "g", "", "the entity GUID to create tag values on")
cmdTagsCreate.Flags().StringSliceVarP(&entityTags, "tag", "t", []string{}, "the tag names to add to the entity")
utils.LogIfError(cmdTagsCreate.MarkFlagRequired("guid"))
utils.LogIfError(cmdTagsCreate.MarkFlagRequired("tag"))
cmdTags.AddCommand(cmdTagsReplace)
cmdTagsReplace.Flags().StringVarP(&entityGUID, "guid", "g", "", "the entity GUID to replace tag values on")
cmdTagsReplace.Flags().StringSliceVarP(&entityTags, "tag", "t", []string{}, "the tag names to replace on the entity")
utils.LogIfError(cmdTagsReplace.MarkFlagRequired("guid"))
utils.LogIfError(cmdTagsReplace.MarkFlagRequired("tag"))
}