/
resource.go
227 lines (197 loc) · 8.94 KB
/
resource.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
package commands
import (
"context"
"fmt"
"github.com/ionos-cloud/ionosctl/v6/commands/cloudapi-v6/completer"
"github.com/ionos-cloud/ionosctl/v6/commands/cloudapi-v6/query"
"github.com/ionos-cloud/ionosctl/v6/internal/constants"
"github.com/ionos-cloud/ionosctl/v6/internal/core"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/json2table/jsonpaths"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/jsontabwriter"
"github.com/ionos-cloud/ionosctl/v6/internal/printer/tabheaders"
cloudapiv6 "github.com/ionos-cloud/ionosctl/v6/services/cloudapi-v6"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
defaultResourceCols = []string{"ResourceId", "Name", "SecAuthProtection", "Type", "State"}
)
func ResourceCmd() *core.Command {
ctx := context.TODO()
resourceCmd := &core.Command{
Command: &cobra.Command{
Use: "resource",
Aliases: []string{"res"},
Short: "Resource Operations",
Long: "The sub-commands of `ionosctl resource` allow you to list, get Resources.",
TraverseChildren: true,
},
}
globalFlags := resourceCmd.GlobalFlags()
globalFlags.StringSliceP(constants.ArgCols, "", defaultResourceCols, tabheaders.ColsMessage(defaultResourceCols))
_ = viper.BindPFlag(core.GetFlagName(resourceCmd.Name(), constants.ArgCols), globalFlags.Lookup(constants.ArgCols))
_ = resourceCmd.Command.RegisterFlagCompletionFunc(constants.ArgCols, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return defaultResourceCols, cobra.ShellCompDirectiveNoFileComp
})
/*
List Command
*/
list := core.NewCommand(ctx, resourceCmd, core.CommandBuilder{
Namespace: "resource",
Resource: "resource",
Verb: "list",
Aliases: []string{"l", "ls"},
ShortDesc: "List Resources",
LongDesc: "Use this command to get a full list of existing Resources. To sort list by Resource Type, use `ionosctl resource get` command.",
Example: listResourcesExample,
PreCmdRun: core.NoPreRun,
CmdRun: RunResourceList,
InitClient: true,
})
list.AddInt32Flag(constants.FlagMaxResults, constants.FlagMaxResultsShort, cloudapiv6.DefaultMaxResults, constants.DescMaxResults)
list.AddInt32Flag(cloudapiv6.ArgDepth, cloudapiv6.ArgDepthShort, cloudapiv6.DefaultListDepth, cloudapiv6.ArgDepthDescription)
/*
Get Command
*/
getRsc := core.NewCommand(ctx, resourceCmd, core.CommandBuilder{
Namespace: "resource",
Resource: "resource",
Verb: "get",
Aliases: []string{"g"},
ShortDesc: "Get all Resources of a Type or a specific Resource Type",
LongDesc: "Use this command to get all Resources of a Type or a specific Resource Type using its Type and ID.\n\nRequired values to run command:\n\n* Type",
Example: getResourceExample,
PreCmdRun: PreRunResourceType,
CmdRun: RunResourceGet,
InitClient: true,
})
getRsc.AddStringFlag(constants.FlagType, "", "", "The specific Type of Resources to retrieve information about", core.RequiredFlagOption())
_ = getRsc.Command.RegisterFlagCompletionFunc(constants.FlagType, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"datacenter", "snapshot", "image", "ipblock", "pcc", "backupunit", "k8s"}, cobra.ShellCompDirectiveNoFileComp
})
getRsc.AddUUIDFlag(cloudapiv6.ArgResourceId, cloudapiv6.ArgIdShort, "", "The ID of the specific Resource to retrieve information about")
_ = getRsc.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgResourceId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.ResourcesIds(), cobra.ShellCompDirectiveNoFileComp
})
return resourceCmd
}
func PreRunResourceType(c *core.PreCommandConfig) error {
return core.CheckRequiredFlags(c.Command, c.NS, constants.FlagType)
}
func RunResourceList(c *core.CommandConfig) error {
resourcesListed, resp, err := c.CloudApiV6Services.Users().ListResources()
if resp != nil {
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput(constants.MessageRequestTime, resp.RequestTime))
}
if err != nil {
return err
}
cols := viper.GetStringSlice(core.GetFlagName(c.Resource, constants.ArgCols))
out, err := jsontabwriter.GenerateOutput("items", jsonpaths.Resource, resourcesListed.Resources,
tabheaders.GetHeadersAllDefault(defaultResourceCols, cols))
if err != nil {
return err
}
fmt.Fprintf(c.Command.Command.OutOrStdout(), out)
return nil
}
func RunResourceGet(c *core.CommandConfig) error {
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput(
"Resource with id: %v is getting...", viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgResourceId))))
cols := viper.GetStringSlice(core.GetFlagName(c.Resource, constants.ArgCols))
if viper.IsSet(core.GetFlagName(c.NS, cloudapiv6.ArgResourceId)) {
resourceListed, resp, err := c.CloudApiV6Services.Users().GetResourceByTypeAndId(
viper.GetString(core.GetFlagName(c.NS, constants.FlagType)),
viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgResourceId)),
)
if resp != nil {
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput(constants.MessageRequestTime, resp.RequestTime))
}
if err != nil {
return err
}
out, err := jsontabwriter.GenerateOutput("", jsonpaths.Resource, resourceListed.Resource,
tabheaders.GetHeadersAllDefault(defaultResourceCols, cols))
if err != nil {
return err
}
fmt.Fprintf(c.Command.Command.OutOrStdout(), out)
return nil
}
resourcesListed, resp, err := c.CloudApiV6Services.Users().GetResourcesByType(viper.GetString(core.GetFlagName(c.NS, constants.FlagType)))
if resp != nil {
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput(constants.MessageRequestTime, resp.RequestTime))
}
if err != nil {
return err
}
out, err := jsontabwriter.GenerateOutput("items", jsonpaths.Resource, resourcesListed.Resources,
tabheaders.GetHeadersAllDefault(defaultResourceCols, cols))
if err != nil {
return err
}
fmt.Fprintf(c.Command.Command.OutOrStdout(), out)
return nil
}
// Group Resources Commands
func GroupResourceCmd() *core.Command {
ctx := context.TODO()
resourceCmd := &core.Command{
Command: &cobra.Command{
Use: "resource",
Aliases: []string{"res"},
Short: "Group Resource Operations",
Long: "The sub-command of `ionosctl group resource` allows you to list Resources from a Group.",
TraverseChildren: true,
},
}
/*
List Resources Command
*/
listResources := core.NewCommand(ctx, resourceCmd, core.CommandBuilder{
Namespace: "group",
Resource: "resource",
Verb: "list",
Aliases: []string{"l", "ls"},
ShortDesc: "List Resources from a Group",
LongDesc: "Use this command to get a list of Resources assigned to a Group. To see more details about existing Resources, use `ionosctl resource` commands.\n\nRequired values to run command:\n\n* Group Id",
Example: listGroupResourcesExample,
PreCmdRun: PreRunGroupId,
CmdRun: RunGroupResourceList,
InitClient: true,
})
listResources.AddInt32Flag(constants.FlagMaxResults, constants.FlagMaxResultsShort, cloudapiv6.DefaultMaxResults, constants.DescMaxResults)
listResources.AddStringSliceFlag(constants.ArgCols, "", defaultResourceCols, tabheaders.ColsMessage(defaultResourceCols))
_ = listResources.Command.RegisterFlagCompletionFunc(constants.ArgCols, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return defaultResourceCols, cobra.ShellCompDirectiveNoFileComp
})
listResources.AddUUIDFlag(cloudapiv6.ArgGroupId, "", "", cloudapiv6.GroupId, core.RequiredFlagOption())
_ = listResources.Command.RegisterFlagCompletionFunc(cloudapiv6.ArgGroupId, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return completer.GroupsIds(), cobra.ShellCompDirectiveNoFileComp
})
return resourceCmd
}
func RunGroupResourceList(c *core.CommandConfig) error {
// Add Query Parameters for GET Requests
listQueryParams, err := query.GetListQueryParams(c)
if err != nil {
return err
}
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput(
"Listing Resources from Group with ID: %v...", viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgGroupId))))
resourcesListed, resp, err := c.CloudApiV6Services.Groups().ListResources(viper.GetString(core.GetFlagName(c.NS, cloudapiv6.ArgGroupId)), listQueryParams)
if resp != nil {
fmt.Fprintf(c.Command.Command.ErrOrStderr(), jsontabwriter.GenerateVerboseOutput(constants.MessageRequestTime, resp.RequestTime))
}
if err != nil {
return err
}
cols := viper.GetStringSlice(core.GetFlagName(c.Resource, constants.ArgCols))
out, err := jsontabwriter.GenerateOutput("items", jsonpaths.Resource, resourcesListed.ResourceGroups,
tabheaders.GetHeadersAllDefault(defaultResourceCols, cols))
if err != nil {
return err
}
fmt.Fprintf(c.Command.Command.OutOrStdout(), out)
return nil
}