-
Notifications
You must be signed in to change notification settings - Fork 73
/
command_workload.go
234 lines (198 loc) · 8.7 KB
/
command_workload.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
package workload
import (
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/utils"
"github.com/newrelic/newrelic-client-go/newrelic"
"github.com/newrelic/newrelic-client-go/pkg/workloads"
)
var (
accountID int
name string
entityGUIDs []string
entitySearchQueries []string
scopeAccountIDs []int
guid string
)
var cmdGet = &cobra.Command{
Use: "get",
Short: "Get a New Relic One workload.",
Long: `Get a New Relic One workload
The get command retrieves a specific workload by its account ID and workload GUID.
`,
Example: `newrelic workload create --accountId 12345678 --guid MjUyMDUyOHxOUjF8V09SS0xPQUR8MTI4Myt`,
Run: func(cmd *cobra.Command, args []string) {
client.WithClient(func(nrClient *newrelic.NewRelic) {
workload, err := nrClient.Workloads.GetWorkload(accountID, guid)
utils.LogIfFatal(err)
utils.LogIfFatal(output.Print(workload))
})
},
}
var cmdList = &cobra.Command{
Use: "list",
Short: "List the New Relic One workloads for an account.",
Long: `List the New Relic One workloads for an account
The list command retrieves the workloads for the given account ID.
`,
Example: `newrelic workload list --accountId 12345678`,
Run: func(cmd *cobra.Command, args []string) {
client.WithClient(func(nrClient *newrelic.NewRelic) {
workload, err := nrClient.Workloads.ListWorkloads(accountID)
utils.LogIfFatal(err)
utils.LogIfFatal(output.Print(workload))
})
},
}
var cmdCreate = &cobra.Command{
Use: "create",
Short: "Create a New Relic One workload.",
Long: `Create a New Relic One workload
The create command accepts several different arguments for explicit and dynamic
workloads. Multiple entity GUIDs can be provided for explicit inclusion of entities,
or multiple entity search queries can be provided for dynamic inclusion of entities.
Multiple queries will be aggregated together with an OR. Multiple account scope
IDs can optionally be provided to include entities from different sub-accounts that
you also have access to.
`,
Example: `newrelic workload create --name 'Example workload' --accountId 12345678 --entitySearchQuery "name like 'Example application'"`,
Run: func(cmd *cobra.Command, args []string) {
client.WithClient(func(nrClient *newrelic.NewRelic) {
createInput := workloads.CreateInput{
Name: name,
}
if len(entityGUIDs) > 0 {
createInput.EntityGUIDs = entityGUIDs
}
if len(entitySearchQueries) > 0 {
var queryInputs []workloads.EntitySearchQueryInput
for _, q := range entitySearchQueries {
queryInputs = append(queryInputs, workloads.EntitySearchQueryInput{Query: q})
}
createInput.EntitySearchQueries = queryInputs
}
if len(scopeAccountIDs) > 0 {
createInput.ScopeAccountsInput = &workloads.ScopeAccountsInput{AccountIDs: scopeAccountIDs}
}
workload, err := nrClient.Workloads.CreateWorkload(accountID, createInput)
utils.LogIfFatal(err)
utils.LogIfFatal(output.Print(workload))
log.Info("success")
})
},
}
var cmdUpdate = &cobra.Command{
Use: "update",
Short: "Update a New Relic One workload.",
Long: `Update a New Relic One workload
The update command targets an existing workload by its entity GUID, and accepts
several different arguments for explicit and dynamic workloads. Multiple entity GUIDs can
be provided for explicit inclusion of entities, or multiple entity search queries can be
provided for dynamic inclusion of entities. Multiple queries will be aggregated
together with an OR. Multiple account scope IDs can optionally be provided to include
entities from different sub-accounts that you also have access to.
`,
Example: `newrelic workload update --guid 'MjUyMDUyOHxBOE28QVBQTElDQVRDT058MjE1MDM3Nzk1' --name 'Updated workflow'`,
Run: func(cmd *cobra.Command, args []string) {
client.WithClient(func(nrClient *newrelic.NewRelic) {
updateInput := workloads.UpdateInput{
Name: name,
}
if len(entityGUIDs) > 0 {
updateInput.EntityGUIDs = entityGUIDs
}
if len(entitySearchQueries) > 0 {
var queryInputs []workloads.EntitySearchQueryInput
for _, q := range entitySearchQueries {
queryInputs = append(queryInputs, workloads.EntitySearchQueryInput{Query: q})
}
updateInput.EntitySearchQueries = queryInputs
}
if len(scopeAccountIDs) > 0 {
updateInput.ScopeAccountsInput = &workloads.ScopeAccountsInput{AccountIDs: scopeAccountIDs}
}
_, err := nrClient.Workloads.UpdateWorkload(guid, updateInput)
utils.LogIfFatal(err)
log.Info("success")
})
},
}
var cmdDuplicate = &cobra.Command{
Use: "duplicate",
Short: "Duplicate a New Relic One workload.",
Long: `Duplicate a New Relic One workload
The duplicate command targets an existing workload by its entity GUID, and clones
it to the provided account ID. An optional name can be provided for the new workload.
If the name isn't specified, the name + ' copy' of the source workload is used to
compose the new name.
`,
Example: `newrelic workload duplicate --guid 'MjUyMDUyOHxBOE28QVBQTElDQVRDT058MjE1MDM3Nzk1' --accountID 12345678 --name 'New Workload'`,
Run: func(cmd *cobra.Command, args []string) {
client.WithClient(func(nrClient *newrelic.NewRelic) {
duplicateInput := &workloads.DuplicateInput{
Name: name,
}
workload, err := nrClient.Workloads.DuplicateWorkload(accountID, guid, duplicateInput)
utils.LogIfFatal(err)
utils.LogIfFatal(output.Print(workload))
log.Info("success")
})
},
}
var cmdDelete = &cobra.Command{
Use: "delete",
Short: "Delete a New Relic One workload.",
Long: `Delete a New Relic One workload
The delete command accepts a workload's entity GUID.
`,
Example: `newrelic workload delete --guid 'MjUyMDUyOHxBOE28QVBQTElDQVRDT058MjE1MDM3Nzk1'`,
Run: func(cmd *cobra.Command, args []string) {
client.WithClient(func(nrClient *newrelic.NewRelic) {
_, err := nrClient.Workloads.DeleteWorkload(guid)
utils.LogIfFatal(err)
log.Info("success")
})
},
}
func init() {
// Get
Command.AddCommand(cmdGet)
cmdGet.Flags().IntVarP(&accountID, "accountId", "a", 0, "the New Relic account ID where the workload is located")
cmdGet.Flags().StringVarP(&guid, "guid", "g", "", "the GUID of the workload")
utils.LogIfError(cmdGet.MarkFlagRequired("accountId"))
utils.LogIfError(cmdGet.MarkFlagRequired("guid"))
// List
Command.AddCommand(cmdList)
cmdList.Flags().IntVarP(&accountID, "accountId", "a", 0, "the New Relic account ID you want to list workloads for")
utils.LogIfError(cmdList.MarkFlagRequired("accountId"))
// Create
Command.AddCommand(cmdCreate)
cmdCreate.Flags().IntVarP(&accountID, "accountId", "a", 0, "the New Relic account ID where you want to create the workload")
cmdCreate.Flags().StringVarP(&name, "name", "n", "", "the name of the workload")
cmdCreate.Flags().StringSliceVarP(&entityGUIDs, "entityGuid", "e", []string{}, "the list of entity Guids composing the workload")
cmdCreate.Flags().StringSliceVarP(&entitySearchQueries, "entitySearchQuery", "q", []string{}, "a list of search queries, combined using an OR operator")
cmdCreate.Flags().IntSliceVarP(&scopeAccountIDs, "scopeAccountIds", "s", []int{}, "accounts that will be used to get entities from")
utils.LogIfError(cmdCreate.MarkFlagRequired("accountId"))
utils.LogIfError(cmdCreate.MarkFlagRequired("name"))
// Update
Command.AddCommand(cmdUpdate)
cmdUpdate.Flags().StringVarP(&guid, "guid", "g", "", "the GUID of the workload you want to update")
cmdUpdate.Flags().StringVarP(&name, "name", "n", "", "the name of the workload")
cmdUpdate.Flags().StringSliceVarP(&entityGUIDs, "entityGuid", "e", []string{}, "the list of entity Guids composing the workload")
cmdUpdate.Flags().StringSliceVarP(&entitySearchQueries, "entitySearchQuery", "q", []string{}, "a list of search queries, combined using an OR operator")
cmdUpdate.Flags().IntSliceVarP(&scopeAccountIDs, "scopeAccountIds", "s", []int{}, "accounts that will be used to get entities from")
utils.LogIfError(cmdUpdate.MarkFlagRequired("guid"))
// Duplicate
Command.AddCommand(cmdDuplicate)
cmdDuplicate.Flags().StringVarP(&guid, "guid", "g", "", "the GUID of the workload you want to duplicate")
cmdDuplicate.Flags().IntVarP(&accountID, "accountId", "a", 0, "the New Relic Account ID where you want to create the new workload")
cmdDuplicate.Flags().StringVarP(&name, "name", "n", "", "the name of the workload to duplicate")
utils.LogIfError(cmdDuplicate.MarkFlagRequired("accountId"))
utils.LogIfError(cmdDuplicate.MarkFlagRequired("guid"))
// Delete
Command.AddCommand(cmdDelete)
cmdDelete.Flags().StringVarP(&guid, "guid", "g", "", "the GUID of the workload to delete")
utils.LogIfError(cmdDelete.MarkFlagRequired("guid"))
}