-
Notifications
You must be signed in to change notification settings - Fork 70
/
command_document.go
238 lines (196 loc) · 8.89 KB
/
command_document.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
package nerdstorage
import (
"encoding/json"
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/newrelic/newrelic-cli/internal/client"
configAPI "github.com/newrelic/newrelic-cli/internal/config/api"
"github.com/newrelic/newrelic-cli/internal/output"
"github.com/newrelic/newrelic-cli/internal/utils"
"github.com/newrelic/newrelic-client-go/pkg/nerdstorage"
)
var cmdDocument = &cobra.Command{
Use: "document",
Short: "Read, write, and delete NerdStorage documents.",
Example: "newrelic nerdstorage document --help",
Long: `Read write, and delete NerdStorage documents`,
}
var cmdDocumentGet = &cobra.Command{
Use: "get",
Short: "Retrieve a NerdStorage document.",
Long: `Retrieve a NerdStorage document
Retrieve a NerdStorage document. Valid scopes are ACCOUNT, ENTITY, and USER.
ACCOUNT scope requires a valid account ID and ENTITY scope requires a valid entity
GUID. A valid Nerdpack package ID is required.
`,
Example: `
# Account scope
newrelic nerdstorage document get --scope ACCOUNT --packageId b0dee5a1-e809-4d6f-bd3c-0682cd079612 --accountId 12345678 --collection myCol --documentId myDoc
# Entity scope
newrelic nerdstorage document get --scope ENTITY --packageId b0dee5a1-e809-4d6f-bd3c-0682cd079612 --entityId MjUyMDUyOHxFUE18QVBQTElDQVRJT058MjE1MDM3Nzk1 --collection myCol --documentId myDoc
# User scope
newrelic nerdstorage document get --scope USER --packageId b0dee5a1-e809-4d6f-bd3c-0682cd079612 --collection myCol --documentId myDoc
`,
PreRun: client.RequireClient,
Run: func(cmd *cobra.Command, args []string) {
var document interface{}
var err error
input := nerdstorage.GetDocumentInput{
PackageID: packageID,
Collection: collection,
DocumentID: documentID,
}
switch strings.ToLower(scope) {
case "account":
accountID := configAPI.RequireActiveProfileAccountID()
document, err = client.NRClient.NerdStorage.GetDocumentWithAccountScopeWithContext(utils.SignalCtx, accountID, input)
case "entity":
document, err = client.NRClient.NerdStorage.GetDocumentWithEntityScopeWithContext(utils.SignalCtx, entityGUID, input)
case "user":
document, err = client.NRClient.NerdStorage.GetDocumentWithUserScopeWithContext(utils.SignalCtx, input)
default:
log.Fatal("scope must be one of ACCOUNT, ENTITY, or USER")
}
if err != nil {
log.Fatal(err)
}
utils.LogIfFatal(output.Print(document))
log.Info("success")
},
}
var cmdDocumentWrite = &cobra.Command{
Use: "write",
Short: "Write a NerdStorage document.",
Long: `Write a NerdStorage document
Write a NerdStorage document. Valid scopes are ACCOUNT, ENTITY, and USER.
ACCOUNT scope requires a valid account ID and ENTITY scope requires a valid entity
GUID. A valid Nerdpack package ID is required.
`,
Example: `
# Account scope
newrelic nerdstorage document write --scope ACCOUNT --packageId b0dee5a1-e809-4d6f-bd3c-0682cd079612 --accountId 12345678 --collection myCol --documentId myDoc --document '{"field": "myValue"}'
# Entity scope
newrelic nerdstorage document write --scope ENTITY --packageId b0dee5a1-e809-4d6f-bd3c-0682cd079612 --entityId MjUyMDUyOHxFUE18QVBQTElDQVRJT058MjE1MDM3Nzk1 --collection myCol --documentId myDoc --document '{"field": "myValue"}'
# User scope
newrelic nerdstorage document write --scope USER --packageId b0dee5a1-e809-4d6f-bd3c-0682cd079612 --collection myCol --documentId myDoc --document '{"field": "myValue"}'
`,
PreRun: client.RequireClient,
Run: func(cmd *cobra.Command, args []string) {
var unmarshaled map[string]interface{}
err := json.Unmarshal([]byte(document), &unmarshaled)
if err != nil {
log.Fatalf("error parsing provided document: %s", err)
}
input := nerdstorage.WriteDocumentInput{
PackageID: packageID,
Collection: collection,
DocumentID: documentID,
Document: unmarshaled,
}
switch strings.ToLower(scope) {
case "account":
accountID := configAPI.RequireActiveProfileAccountID()
_, err = client.NRClient.NerdStorage.WriteDocumentWithAccountScopeWithContext(utils.SignalCtx, accountID, input)
case "entity":
_, err = client.NRClient.NerdStorage.WriteDocumentWithEntityScopeWithContext(utils.SignalCtx, entityGUID, input)
case "user":
_, err = client.NRClient.NerdStorage.WriteDocumentWithUserScopeWithContext(utils.SignalCtx, input)
default:
log.Fatal("scope must be one of ACCOUNT, ENTITY, or USER")
}
if err != nil {
log.Fatal(err)
}
log.Info("success")
},
}
var cmdDocumentDelete = &cobra.Command{
Use: "delete",
Short: "Delete a NerdStorage document.",
Long: `Delete a NerdStorage document
Delete a NerdStorage document. Valid scopes are ACCOUNT, ENTITY, and USER.
ACCOUNT scope requires a valid account ID and ENTITY scope requires a valid entity
GUID. A valid Nerdpack package ID is required.
`,
Example: `
# Account scope
newrelic nerdstorage document delete --scope ACCOUNT --packageId b0dee5a1-e809-4d6f-bd3c-0682cd079612 --accountId 12345678 --collection myCol --documentId myDoc
# Entity scope
newrelic nerdstorage document delete --scope ENTITY --packageId b0dee5a1-e809-4d6f-bd3c-0682cd079612 --entityId MjUyMDUyOHxFUE18QVBQTElDQVRJT058MjE1MDM3Nzk1 --collection myCol --documentId myDoc
# User scope
newrelic nerdstorage document delete --scope USER --packageId b0dee5a1-e809-4d6f-bd3c-0682cd079612 --collection myCol --documentId myDoc
`,
PreRun: client.RequireClient,
Run: func(cmd *cobra.Command, args []string) {
var err error
input := nerdstorage.DeleteDocumentInput{
PackageID: packageID,
Collection: collection,
DocumentID: documentID,
}
switch strings.ToLower(scope) {
case "account":
accountID := configAPI.RequireActiveProfileAccountID()
_, err = client.NRClient.NerdStorage.DeleteDocumentWithAccountScopeWithContext(utils.SignalCtx, accountID, input)
case "entity":
_, err = client.NRClient.NerdStorage.DeleteDocumentWithEntityScopeWithContext(utils.SignalCtx, entityGUID, input)
case "user":
_, err = client.NRClient.NerdStorage.DeleteDocumentWithUserScopeWithContext(utils.SignalCtx, input)
default:
log.Fatal("scope must be one of ACCOUNT, ENTITY, or USER")
}
if err != nil {
log.Fatal(err)
}
log.Info("success")
},
}
func init() {
Command.AddCommand(cmdDocument)
cmdDocument.AddCommand(cmdDocumentGet)
cmdDocumentGet.Flags().StringVarP(&entityGUID, "entityGuid", "e", "", "the entity GUID")
cmdDocumentGet.Flags().StringVarP(&packageID, "packageId", "p", "", "the external package ID")
cmdDocumentGet.Flags().StringVarP(&collection, "collection", "c", "", "the collection name to get the document from")
cmdDocumentGet.Flags().StringVarP(&documentID, "documentId", "d", "", "the document ID")
cmdDocumentGet.Flags().StringVarP(&scope, "scope", "s", "USER", "the scope to get the document from")
err := cmdDocumentGet.MarkFlagRequired("packageId")
utils.LogIfError(err)
err = cmdDocumentGet.MarkFlagRequired("scope")
utils.LogIfError(err)
err = cmdDocumentGet.MarkFlagRequired("collection")
utils.LogIfError(err)
err = cmdDocumentGet.MarkFlagRequired("documentId")
utils.LogIfError(err)
cmdDocument.AddCommand(cmdDocumentWrite)
cmdDocumentWrite.Flags().StringVarP(&entityGUID, "entityGuid", "e", "", "the entity GUID")
cmdDocumentWrite.Flags().StringVarP(&packageID, "packageId", "p", "", "the external package ID")
cmdDocumentWrite.Flags().StringVarP(&collection, "collection", "c", "", "the collection name to write the document to")
cmdDocumentWrite.Flags().StringVarP(&documentID, "documentId", "d", "", "the document ID")
cmdDocumentWrite.Flags().StringVarP(&document, "document", "o", "{}", "the document to be written, in JSON format")
cmdDocumentWrite.Flags().StringVarP(&scope, "scope", "s", "USER", "the scope to write the document to")
err = cmdDocumentWrite.MarkFlagRequired("packageId")
utils.LogIfError(err)
err = cmdDocumentWrite.MarkFlagRequired("scope")
utils.LogIfError(err)
err = cmdDocumentWrite.MarkFlagRequired("document")
utils.LogIfError(err)
err = cmdDocumentWrite.MarkFlagRequired("collection")
utils.LogIfError(err)
err = cmdDocumentWrite.MarkFlagRequired("documentId")
utils.LogIfError(err)
cmdDocument.AddCommand(cmdDocumentDelete)
cmdDocumentDelete.Flags().StringVarP(&entityGUID, "entityGuid", "e", "", "the entity GUID")
cmdDocumentDelete.Flags().StringVarP(&packageID, "packageId", "p", "", "the external package ID")
cmdDocumentDelete.Flags().StringVarP(&collection, "collection", "c", "", "the collection name to delete the document from")
cmdDocumentDelete.Flags().StringVarP(&documentID, "documentId", "d", "", "the document ID")
cmdDocumentDelete.Flags().StringVarP(&scope, "scope", "s", "USER", "the scope to delete the document from")
err = cmdDocumentDelete.MarkFlagRequired("packageId")
utils.LogIfError(err)
err = cmdDocumentDelete.MarkFlagRequired("scope")
utils.LogIfError(err)
err = cmdDocumentDelete.MarkFlagRequired("collection")
utils.LogIfError(err)
err = cmdDocumentDelete.MarkFlagRequired("documentId")
utils.LogIfError(err)
}