Skip to content

Commit 38805b0

Browse files
committed
adjust network commands
1 parent 95cf0d1 commit 38805b0

File tree

12 files changed

+610
-242
lines changed

12 files changed

+610
-242
lines changed

internal/cmd/network/create/create.go

Lines changed: 92 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
7979
),
8080
examples.NewExample(
8181
`Create an IPv4 network with name "network-1" with DNS name servers, a prefix and a gateway`,
82-
`$ stackit network create --name network-1 --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"`,
82+
`$ 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"`,
8383
),
8484
examples.NewExample(
8585
`Create an IPv6 network with name "network-1" with DNS name servers, a prefix and a gateway`,
@@ -121,13 +121,17 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
121121
if err != nil {
122122
return fmt.Errorf("create network : %w", err)
123123
}
124-
networkId := *resp.NetworkId
124+
125+
if resp == nil || resp.Id == nil {
126+
return fmt.Errorf("create network : empty response")
127+
}
128+
networkId := *resp.Id
125129

126130
// Wait for async operation, if async mode not enabled
127131
if !model.Async {
128132
s := spinner.New(params.Printer)
129133
s.Start("Creating network")
130-
_, err = wait.CreateNetworkWaitHandler(ctx, apiClient, model.ProjectId, networkId).WaitWithContext(ctx)
134+
_, err = wait.CreateNetworkWaitHandler(ctx, apiClient, model.ProjectId, model.Region, networkId).WaitWithContext(ctx)
131135
if err != nil {
132136
return fmt.Errorf("wait for network creation: %w", err)
133137
}
@@ -156,6 +160,17 @@ func configureFlags(cmd *cobra.Command) {
156160
cmd.Flags().Bool(noIpv6GatewayFlag, false, "If set to true, the network doesn't have an IPv6 gateway")
157161
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,...'")
158162

163+
// IPv4 checks
164+
cmd.MarkFlagsMutuallyExclusive(ipv4PrefixFlag, ipv4PrefixLengthFlag)
165+
cmd.MarkFlagsMutuallyExclusive(ipv4GatewayFlag, ipv4PrefixLengthFlag)
166+
cmd.MarkFlagsMutuallyExclusive(ipv4GatewayFlag, noIpv4GatewayFlag)
167+
cmd.MarkFlagsMutuallyExclusive(noIpv4GatewayFlag, ipv4PrefixLengthFlag)
168+
169+
// IPv6 checks
170+
cmd.MarkFlagsMutuallyExclusive(ipv6PrefixFlag, ipv6PrefixLengthFlag)
171+
cmd.MarkFlagsMutuallyExclusive(ipv6GatewayFlag, ipv6PrefixLengthFlag)
172+
cmd.MarkFlagsMutuallyExclusive(ipv6GatewayFlag, noIpv6GatewayFlag)
173+
159174
err := flags.MarkFlagsRequired(cmd, nameFlag)
160175
cobra.CheckErr(err)
161176
}
@@ -173,6 +188,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
173188
IPv4PrefixLength: flags.FlagToInt64Pointer(p, cmd, ipv4PrefixLengthFlag),
174189
IPv4Prefix: flags.FlagToStringPointer(p, cmd, ipv4PrefixFlag),
175190
IPv4Gateway: flags.FlagToStringPointer(p, cmd, ipv4GatewayFlag),
191+
176192
IPv6DnsNameServers: flags.FlagToStringSlicePointer(p, cmd, ipv6DnsNameServersFlag),
177193
IPv6PrefixLength: flags.FlagToInt64Pointer(p, cmd, ipv6PrefixLengthFlag),
178194
IPv6Prefix: flags.FlagToStringPointer(p, cmd, ipv6PrefixFlag),
@@ -183,39 +199,92 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
183199
Labels: flags.FlagToStringToStringPointer(p, cmd, labelFlag),
184200
}
185201

202+
// IPv4 nameserver can not be set alone. IPv4 Prefix || IPv4 Prefix length must be set as well
203+
isIPv4NameserverSet := model.IPv4DnsNameServers != nil && len(*model.IPv4DnsNameServers) > 0
204+
isIPv4PrefixOrPrefixLengthSet := model.IPv4Prefix != nil || model.IPv4PrefixLength != nil
205+
if isIPv4NameserverSet && !isIPv4PrefixOrPrefixLengthSet {
206+
return nil, &cliErr.OneOfFlagsIsMissing{
207+
MissingFlags: []string{ipv4PrefixLengthFlag, ipv4PrefixFlag},
208+
SetFlag: ipv4DnsNameServersFlag,
209+
}
210+
}
211+
isIPv4GatewaySet := model.IPv4Gateway != nil
212+
isIPv4PrefixSet := model.IPv4Prefix != nil
213+
if isIPv4GatewaySet && !isIPv4PrefixSet {
214+
return nil, &cliErr.DependingFlagIsMissing{
215+
MissingFlag: ipv4PrefixFlag,
216+
SetFlag: ipv4GatewayFlag,
217+
}
218+
}
219+
220+
// IPv6 nameserver can not be set alone. IPv6 Prefix || IPv6 Prefix length must be set as well
221+
isIPv6NameserverSet := model.IPv6DnsNameServers != nil && len(*model.IPv6DnsNameServers) > 0
222+
isIPv6PrefixOrPrefixLengthSet := model.IPv6Prefix != nil || model.IPv6PrefixLength != nil
223+
if isIPv6NameserverSet && !isIPv6PrefixOrPrefixLengthSet {
224+
return nil, &cliErr.OneOfFlagsIsMissing{
225+
MissingFlags: []string{ipv6PrefixLengthFlag, ipv6PrefixFlag},
226+
SetFlag: ipv6DnsNameServersFlag,
227+
}
228+
}
229+
isIPv6GatewaySet := model.IPv6Gateway != nil
230+
isIPv6PrefixSet := model.IPv6Prefix != nil
231+
if isIPv6GatewaySet && !isIPv6PrefixSet {
232+
return nil, &cliErr.DependingFlagIsMissing{
233+
MissingFlag: ipv6PrefixFlag,
234+
SetFlag: ipv6GatewayFlag,
235+
}
236+
}
237+
186238
p.DebugInputModel(model)
187239
return &model, nil
188240
}
189241

190242
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateNetworkRequest {
191-
req := apiClient.CreateNetwork(ctx, model.ProjectId)
192-
addressFamily := &iaas.CreateNetworkAddressFamily{}
193-
194-
if model.IPv6DnsNameServers != nil || model.IPv6PrefixLength != nil || model.IPv6Prefix != nil || model.NoIPv6Gateway || model.IPv6Gateway != nil {
195-
addressFamily.Ipv6 = &iaas.CreateNetworkIPv6Body{
196-
Nameservers: model.IPv6DnsNameServers,
197-
PrefixLength: model.IPv6PrefixLength,
198-
Prefix: model.IPv6Prefix,
243+
req := apiClient.CreateNetwork(ctx, model.ProjectId, model.Region)
244+
var ipv4Network *iaas.CreateNetworkIPv4
245+
var ipv6Network *iaas.CreateNetworkIPv6
246+
247+
if model.IPv6Prefix != nil {
248+
ipv6Network = &iaas.CreateNetworkIPv6{
249+
CreateNetworkIPv6WithPrefix: &iaas.CreateNetworkIPv6WithPrefix{
250+
Prefix: model.IPv6Prefix,
251+
Nameservers: model.IPv6DnsNameServers,
252+
},
199253
}
200254

201255
if model.NoIPv6Gateway {
202-
addressFamily.Ipv6.Gateway = iaas.NewNullableString(nil)
256+
ipv6Network.CreateNetworkIPv6WithPrefix.Gateway = iaas.NewNullableString(nil)
203257
} else if model.IPv6Gateway != nil {
204-
addressFamily.Ipv6.Gateway = iaas.NewNullableString(model.IPv6Gateway)
258+
ipv6Network.CreateNetworkIPv6WithPrefix.Gateway = iaas.NewNullableString(model.IPv6Gateway)
259+
}
260+
} else if model.IPv6PrefixLength != nil {
261+
ipv6Network = &iaas.CreateNetworkIPv6{
262+
CreateNetworkIPv6WithPrefixLength: &iaas.CreateNetworkIPv6WithPrefixLength{
263+
PrefixLength: model.IPv6PrefixLength,
264+
Nameservers: model.IPv6DnsNameServers,
265+
},
205266
}
206267
}
207268

208-
if model.IPv4DnsNameServers != nil || model.IPv4PrefixLength != nil || model.IPv4Prefix != nil || model.NoIPv4Gateway || model.IPv4Gateway != nil {
209-
addressFamily.Ipv4 = &iaas.CreateNetworkIPv4Body{
210-
Nameservers: model.IPv4DnsNameServers,
211-
PrefixLength: model.IPv4PrefixLength,
212-
Prefix: model.IPv4Prefix,
269+
if model.IPv4Prefix != nil {
270+
ipv4Network = &iaas.CreateNetworkIPv4{
271+
CreateNetworkIPv4WithPrefix: &iaas.CreateNetworkIPv4WithPrefix{
272+
Prefix: model.IPv4Prefix,
273+
Nameservers: model.IPv4DnsNameServers,
274+
},
213275
}
214276

215277
if model.NoIPv4Gateway {
216-
addressFamily.Ipv4.Gateway = iaas.NewNullableString(nil)
278+
ipv4Network.CreateNetworkIPv4WithPrefix.Gateway = iaas.NewNullableString(nil)
217279
} else if model.IPv4Gateway != nil {
218-
addressFamily.Ipv4.Gateway = iaas.NewNullableString(model.IPv4Gateway)
280+
ipv4Network.CreateNetworkIPv4WithPrefix.Gateway = iaas.NewNullableString(model.IPv4Gateway)
281+
}
282+
} else if model.IPv4PrefixLength != nil {
283+
ipv4Network = &iaas.CreateNetworkIPv4{
284+
CreateNetworkIPv4WithPrefixLength: &iaas.CreateNetworkIPv4WithPrefixLength{
285+
PrefixLength: model.IPv4PrefixLength,
286+
Nameservers: model.IPv4DnsNameServers,
287+
},
219288
}
220289
}
221290

@@ -228,10 +297,8 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
228297
Name: model.Name,
229298
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
230299
Routed: &routed,
231-
}
232-
233-
if addressFamily.Ipv4 != nil || addressFamily.Ipv6 != nil {
234-
payload.AddressFamily = addressFamily
300+
Ipv4: ipv4Network,
301+
Ipv6: ipv6Network,
235302
}
236303

237304
return req.CreateNetworkPayload(payload)
@@ -246,7 +313,7 @@ func outputResult(p *print.Printer, outputFormat string, async bool, projectLabe
246313
if async {
247314
operationState = "Triggered creation of"
248315
}
249-
p.Outputf("%s network for project %q.\nNetwork ID: %s\n", operationState, projectLabel, utils.PtrString(network.NetworkId))
316+
p.Outputf("%s network for project %q.\nNetwork ID: %s\n", operationState, projectLabel, utils.PtrString(network.Id))
250317
return nil
251318
})
252319
}

0 commit comments

Comments
 (0)