-
Notifications
You must be signed in to change notification settings - Fork 70
/
command_collection.go
151 lines (124 loc) · 5.42 KB
/
command_collection.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
package nerdstorage
import (
"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 cmdCollection = &cobra.Command{
Use: "collection",
Short: "Read, write, and delete NerdStorage collections.",
Example: "newrelic nerdstorage collection --help",
Long: `Read write, and delete NerdStorage collections`,
}
var cmdCollectionGet = &cobra.Command{
Use: "get",
Short: "Retrieve a NerdStorage collection.",
Long: `Retrieve a NerdStorage collection
Retrieve a NerdStorage collection. 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 collection get --scope ACCOUNT --packageId b0dee5a1-e809-4d6f-bd3c-0682cd079612 --accountId 12345678 --collection myCol
# Entity scope
newrelic nerdstorage collection get --scope ENTITY --packageId b0dee5a1-e809-4d6f-bd3c-0682cd079612 --entityId MjUyMDUyOHxFUE18QVBQTElDQVRJT058MjE1MDM3Nzk1 --collection myCol
# User scope
newrelic nerdstorage collection get --scope USER --packageId b0dee5a1-e809-4d6f-bd3c-0682cd079612 --collection myCol
`,
PreRun: client.RequireClient,
Run: func(cmd *cobra.Command, args []string) {
var resp []interface{}
var err error
input := nerdstorage.GetCollectionInput{
PackageID: packageID,
Collection: collection,
}
switch strings.ToLower(scope) {
case "account":
accountID := configAPI.RequireActiveProfileAccountID()
resp, err = client.NRClient.NerdStorage.GetCollectionWithAccountScopeWithContext(utils.SignalCtx, accountID, input)
case "entity":
resp, err = client.NRClient.NerdStorage.GetCollectionWithEntityScopeWithContext(utils.SignalCtx, entityGUID, input)
case "user":
resp, err = client.NRClient.NerdStorage.GetCollectionWithUserScopeWithContext(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(resp))
log.Info("success")
},
}
var cmdCollectionDelete = &cobra.Command{
Use: "delete",
Short: "Delete a NerdStorage collection.",
Long: `Delete a NerdStorage collection
Delete a NerdStorage collection. 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 collection delete --scope ACCOUNT --packageId b0dee5a1-e809-4d6f-bd3c-0682cd079612 --accountId 12345678 --collection myCol
# Entity scope
newrelic nerdstorage collection delete --scope ENTITY --packageId b0dee5a1-e809-4d6f-bd3c-0682cd079612 --entityId MjUyMDUyOHxFUE18QVBQTElDQVRJT058MjE1MDM3Nzk1 --collection myCol
# User scope
newrelic nerdstorage collection delete --scope USER --packageId b0dee5a1-e809-4d6f-bd3c-0682cd079612 --collection myCol
`,
PreRun: client.RequireClient,
Run: func(cmd *cobra.Command, args []string) {
var err error
input := nerdstorage.DeleteCollectionInput{
PackageID: packageID,
Collection: collection,
}
switch strings.ToLower(scope) {
case "account":
accountID := configAPI.RequireActiveProfileAccountID()
_, err = client.NRClient.NerdStorage.DeleteCollectionWithAccountScopeWithContext(utils.SignalCtx, accountID, input)
case "entity":
_, err = client.NRClient.NerdStorage.DeleteCollectionWithEntityScopeWithContext(utils.SignalCtx, entityGUID, input)
case "user":
_, err = client.NRClient.NerdStorage.DeleteCollectionWithUserScopeWithContext(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(cmdCollection)
cmdCollection.AddCommand(cmdCollectionGet)
cmdCollectionGet.Flags().StringVarP(&entityGUID, "entityGuid", "e", "", "the entity GUID")
cmdCollectionGet.Flags().StringVarP(&packageID, "packageId", "p", "", "the external package ID")
cmdCollectionGet.Flags().StringVarP(&collection, "collection", "c", "", "the collection name to get the document from")
cmdCollectionGet.Flags().StringVarP(&scope, "scope", "s", "USER", "the scope to get the document from")
err := cmdCollectionGet.MarkFlagRequired("packageId")
utils.LogIfError(err)
err = cmdCollectionGet.MarkFlagRequired("scope")
utils.LogIfError(err)
err = cmdCollectionGet.MarkFlagRequired("collection")
utils.LogIfError(err)
cmdCollection.AddCommand(cmdCollectionDelete)
cmdCollectionDelete.Flags().StringVarP(&entityGUID, "entityGuid", "e", "", "the entity GUID")
cmdCollectionDelete.Flags().StringVarP(&packageID, "packageId", "", "p", "the external package ID")
cmdCollectionDelete.Flags().StringVarP(&collection, "collection", "c", "", "the collection name to delete the document from")
cmdCollectionDelete.Flags().StringVarP(&scope, "scope", "s", "USER", "the scope to delete the document from")
err = cmdCollectionDelete.MarkFlagRequired("packageId")
utils.LogIfError(err)
err = cmdCollectionDelete.MarkFlagRequired("scope")
utils.LogIfError(err)
err = cmdCollectionDelete.MarkFlagRequired("collection")
utils.LogIfError(err)
}