-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathcreate.go
More file actions
322 lines (290 loc) · 12.4 KB
/
create.go
File metadata and controls
322 lines (290 loc) · 12.4 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package create
import (
"context"
"fmt"
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api"
"github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api/wait"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
cliErr "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/spf13/cobra"
)
const (
nameFlag = "name"
ipv4DnsNameServersFlag = "ipv4-dns-name-servers"
ipv4PrefixLengthFlag = "ipv4-prefix-length"
ipv4PrefixFlag = "ipv4-prefix"
ipv4GatewayFlag = "ipv4-gateway"
ipv6DnsNameServersFlag = "ipv6-dns-name-servers"
ipv6PrefixLengthFlag = "ipv6-prefix-length"
ipv6PrefixFlag = "ipv6-prefix"
ipv6GatewayFlag = "ipv6-gateway"
nonRoutedFlag = "non-routed"
noIpv4GatewayFlag = "no-ipv4-gateway"
noIpv6GatewayFlag = "no-ipv6-gateway"
routingTableIdFlag = "routing-table-id"
labelFlag = "labels"
)
type inputModel struct {
*globalflags.GlobalFlagModel
Name string
IPv4DnsNameServers []string
IPv4PrefixLength *int64
IPv4Prefix *string
IPv4Gateway *string
IPv6DnsNameServers []string
IPv6PrefixLength *int64
IPv6Prefix *string
IPv6Gateway *string
NonRouted bool
NoIPv4Gateway bool
NoIPv6Gateway bool
RoutingTableID *string
Labels map[string]any
}
func NewCmd(params *types.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Creates a network",
Long: "Creates a network.",
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`Create a network with name "network-1"`,
`$ stackit network create --name network-1`,
),
examples.NewExample(
`Create a non-routed network with name "network-1"`,
`$ stackit network create --name network-1 --non-routed`,
),
examples.NewExample(
`Create a network with name "network-1" and no gateway`,
`$ stackit network create --name network-1 --no-ipv4-gateway`,
),
examples.NewExample(
`Create a network with name "network-1" and labels "key=value,key1=value1"`,
`$ stackit network create --name network-1 --labels key=value,key1=value1`,
),
examples.NewExample(
`Create an IPv4 network with name "network-1" with DNS name servers, a prefix and a gateway`,
`$ stackit network create --name network-1 --non-routed --ipv4-dns-name-servers "1.1.1.1,8.8.8.8,9.9.9.9" --ipv4-prefix "10.1.2.0/24" --ipv4-gateway "10.1.2.3"`,
),
examples.NewExample(
`Create an IPv6 network with name "network-1" with DNS name servers, a prefix and a gateway`,
`$ stackit network create --name network-1 --ipv6-dns-name-servers "2001:4860:4860::8888,2001:4860:4860::8844" --ipv6-prefix "2001:4860:4860::8888" --ipv6-gateway "2001:4860:4860::8888"`,
),
examples.NewExample(
`Create a network with name "network-1" and attach routing-table "xxx"`,
`$ stackit network create --name network-1 --routing-table-id xxx`,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(params.Printer, cmd, args)
if err != nil {
return err
}
// Configure API client
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
} else if projectLabel == "" {
projectLabel = model.ProjectId
}
prompt := fmt.Sprintf("Are you sure you want to create a network for project %q?", projectLabel)
err = params.Printer.PromptForConfirmation(prompt)
if err != nil {
return err
}
// Call API
req := buildRequest(ctx, model, apiClient)
resp, err := req.Execute()
if err != nil {
return fmt.Errorf("create network : %w", err)
}
if resp == nil {
return fmt.Errorf("create network : empty response")
}
networkId := resp.Id
// Wait for async operation, if async mode not enabled
if !model.Async {
err := spinner.Run(params.Printer, "Creating network", func() error {
_, err = wait.CreateNetworkWaitHandler(ctx, apiClient.DefaultAPI, model.ProjectId, model.Region, networkId).WaitWithContext(ctx)
return err
})
if err != nil {
return fmt.Errorf("wait for network creation: %w", err)
}
}
return outputResult(params.Printer, model.OutputFormat, model.Async, projectLabel, resp)
},
}
configureFlags(cmd)
return cmd
}
func configureFlags(cmd *cobra.Command) {
cmd.Flags().StringP(nameFlag, "n", "", "Network name")
cmd.Flags().StringSlice(ipv4DnsNameServersFlag, []string{}, "List of DNS name servers for IPv4. Nameservers cannot be defined for routed networks")
cmd.Flags().Int64(ipv4PrefixLengthFlag, 0, "The prefix length of the IPv4 network")
cmd.Flags().String(ipv4PrefixFlag, "", "The IPv4 prefix of the network (CIDR)")
cmd.Flags().String(ipv4GatewayFlag, "", "The IPv4 gateway of a network. If not specified, the first IP of the network will be assigned as the gateway")
cmd.Flags().StringSlice(ipv6DnsNameServersFlag, []string{}, "List of DNS name servers for IPv6. Nameservers cannot be defined for routed networks")
cmd.Flags().Int64(ipv6PrefixLengthFlag, 0, "The prefix length of the IPv6 network")
cmd.Flags().String(ipv6PrefixFlag, "", "The IPv6 prefix of the network (CIDR)")
cmd.Flags().String(ipv6GatewayFlag, "", "The IPv6 gateway of a network. If not specified, the first IP of the network will be assigned as the gateway")
cmd.Flags().Bool(nonRoutedFlag, false, "If set to true, the network is not routed and therefore not accessible from other networks")
cmd.Flags().Bool(noIpv4GatewayFlag, false, "If set to true, the network doesn't have an IPv4 gateway")
cmd.Flags().Bool(noIpv6GatewayFlag, false, "If set to true, the network doesn't have an IPv6 gateway")
cmd.Flags().Var(flags.UUIDFlag(), routingTableIdFlag, "The ID of the routing-table for the network")
cmd.Flags().StringToString(labelFlag, nil, "Labels are key-value string pairs which can be attached to a network. E.g. '--labels key1=value1,key2=value2,...'")
// IPv4 checks
cmd.MarkFlagsMutuallyExclusive(ipv4PrefixFlag, ipv4PrefixLengthFlag)
cmd.MarkFlagsMutuallyExclusive(ipv4GatewayFlag, ipv4PrefixLengthFlag)
cmd.MarkFlagsMutuallyExclusive(ipv4GatewayFlag, noIpv4GatewayFlag)
cmd.MarkFlagsMutuallyExclusive(noIpv4GatewayFlag, ipv4PrefixLengthFlag)
// IPv6 checks
cmd.MarkFlagsMutuallyExclusive(ipv6PrefixFlag, ipv6PrefixLengthFlag)
cmd.MarkFlagsMutuallyExclusive(ipv6GatewayFlag, ipv6PrefixLengthFlag)
cmd.MarkFlagsMutuallyExclusive(ipv6GatewayFlag, noIpv6GatewayFlag)
err := flags.MarkFlagsRequired(cmd, nameFlag)
cobra.CheckErr(err)
}
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &cliErr.ProjectIdError{}
}
model := inputModel{
GlobalFlagModel: globalFlags,
Name: flags.FlagToStringValue(p, cmd, nameFlag),
IPv4DnsNameServers: flags.FlagToStringSliceValue(p, cmd, ipv4DnsNameServersFlag),
IPv4PrefixLength: flags.FlagToInt64Pointer(p, cmd, ipv4PrefixLengthFlag),
IPv4Prefix: flags.FlagToStringPointer(p, cmd, ipv4PrefixFlag),
IPv4Gateway: flags.FlagToStringPointer(p, cmd, ipv4GatewayFlag),
IPv6DnsNameServers: flags.FlagToStringSliceValue(p, cmd, ipv6DnsNameServersFlag),
IPv6PrefixLength: flags.FlagToInt64Pointer(p, cmd, ipv6PrefixLengthFlag),
IPv6Prefix: flags.FlagToStringPointer(p, cmd, ipv6PrefixFlag),
IPv6Gateway: flags.FlagToStringPointer(p, cmd, ipv6GatewayFlag),
NonRouted: flags.FlagToBoolValue(p, cmd, nonRoutedFlag),
NoIPv4Gateway: flags.FlagToBoolValue(p, cmd, noIpv4GatewayFlag),
NoIPv6Gateway: flags.FlagToBoolValue(p, cmd, noIpv6GatewayFlag),
RoutingTableID: flags.FlagToStringPointer(p, cmd, routingTableIdFlag),
Labels: flags.FlagToStringToAny(p, cmd, labelFlag),
}
// IPv4 nameserver can not be set alone. IPv4 Prefix || IPv4 Prefix length must be set as well
isIPv4NameserverSet := len(model.IPv4DnsNameServers) > 0
isIPv4PrefixOrPrefixLengthSet := model.IPv4Prefix != nil || model.IPv4PrefixLength != nil
if isIPv4NameserverSet && !isIPv4PrefixOrPrefixLengthSet {
return nil, &cliErr.OneOfFlagsIsMissing{
MissingFlags: []string{ipv4PrefixLengthFlag, ipv4PrefixFlag},
SetFlag: ipv4DnsNameServersFlag,
}
}
isIPv4GatewaySet := model.IPv4Gateway != nil
isIPv4PrefixSet := model.IPv4Prefix != nil
if isIPv4GatewaySet && !isIPv4PrefixSet {
return nil, &cliErr.DependingFlagIsMissing{
MissingFlag: ipv4PrefixFlag,
SetFlag: ipv4GatewayFlag,
}
}
// IPv6 nameserver can not be set alone. IPv6 Prefix || IPv6 Prefix length must be set as well
isIPv6NameserverSet := len(model.IPv6DnsNameServers) > 0
isIPv6PrefixOrPrefixLengthSet := model.IPv6Prefix != nil || model.IPv6PrefixLength != nil
if isIPv6NameserverSet && !isIPv6PrefixOrPrefixLengthSet {
return nil, &cliErr.OneOfFlagsIsMissing{
MissingFlags: []string{ipv6PrefixLengthFlag, ipv6PrefixFlag},
SetFlag: ipv6DnsNameServersFlag,
}
}
isIPv6GatewaySet := model.IPv6Gateway != nil
isIPv6PrefixSet := model.IPv6Prefix != nil
if isIPv6GatewaySet && !isIPv6PrefixSet {
return nil, &cliErr.DependingFlagIsMissing{
MissingFlag: ipv6PrefixFlag,
SetFlag: ipv6GatewayFlag,
}
}
p.DebugInputModel(model)
return &model, nil
}
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateNetworkRequest {
req := apiClient.DefaultAPI.CreateNetwork(ctx, model.ProjectId, model.Region)
var ipv4Network *iaas.CreateNetworkIPv4
var ipv6Network *iaas.CreateNetworkIPv6
if model.IPv6Prefix != nil {
ipv6Network = &iaas.CreateNetworkIPv6{
CreateNetworkIPv6WithPrefix: &iaas.CreateNetworkIPv6WithPrefix{
Prefix: *model.IPv6Prefix,
Nameservers: model.IPv6DnsNameServers,
},
}
if model.NoIPv6Gateway {
ipv6Network.CreateNetworkIPv6WithPrefix.Gateway = *iaas.NewNullableString(nil)
} else if model.IPv6Gateway != nil {
ipv6Network.CreateNetworkIPv6WithPrefix.Gateway = *iaas.NewNullableString(model.IPv6Gateway)
}
} else if model.IPv6PrefixLength != nil {
ipv6Network = &iaas.CreateNetworkIPv6{
CreateNetworkIPv6WithPrefixLength: &iaas.CreateNetworkIPv6WithPrefixLength{
PrefixLength: *model.IPv6PrefixLength,
Nameservers: model.IPv6DnsNameServers,
},
}
}
if model.IPv4Prefix != nil {
ipv4Network = &iaas.CreateNetworkIPv4{
CreateNetworkIPv4WithPrefix: &iaas.CreateNetworkIPv4WithPrefix{
Prefix: *model.IPv4Prefix,
Nameservers: model.IPv4DnsNameServers,
},
}
if model.NoIPv4Gateway {
ipv4Network.CreateNetworkIPv4WithPrefix.Gateway = *iaas.NewNullableString(nil)
} else if model.IPv4Gateway != nil {
ipv4Network.CreateNetworkIPv4WithPrefix.Gateway = *iaas.NewNullableString(model.IPv4Gateway)
}
} else if model.IPv4PrefixLength != nil {
ipv4Network = &iaas.CreateNetworkIPv4{
CreateNetworkIPv4WithPrefixLength: &iaas.CreateNetworkIPv4WithPrefixLength{
PrefixLength: *model.IPv4PrefixLength,
Nameservers: model.IPv4DnsNameServers,
},
}
}
payload := iaas.CreateNetworkPayload{
Name: model.Name,
Labels: model.Labels,
Routed: utils.Ptr(!model.NonRouted),
Ipv4: ipv4Network,
Ipv6: ipv6Network,
RoutingTableId: model.RoutingTableID,
}
return req.CreateNetworkPayload(payload)
}
func outputResult(p *print.Printer, outputFormat string, async bool, projectLabel string, network *iaas.Network) error {
if network == nil {
return fmt.Errorf("network cannot be nil")
}
return p.OutputResult(outputFormat, network, func() error {
operationState := "Created"
if async {
operationState = "Triggered creation of"
}
p.Outputf("%s network for project %q.\nNetwork ID: %s\n", operationState, projectLabel, network.Id)
return nil
})
}