Skip to content

Commit ad62c1e

Browse files
GokceGKjoaopalet
andauthored
Extend network commands with new fields (#517)
* extend network commands with new fields * change payload handling for create and update * edit flag descriptions in create and update command * fix flag names * remove routed from the updated * change routed handling in describe output * update descriptions * replace routed flag with non-routed to align with api values * set default values of routed to true * Update internal/cmd/beta/network/create/create_test.go Co-authored-by: João Palet <[email protected]> * extend unit tests * set routed values to false * fix: Fix output of async network creation --------- Co-authored-by: João Palet <[email protected]> Co-authored-by: [email protected] <[email protected]>
1 parent 9859f26 commit ad62c1e

File tree

8 files changed

+372
-40
lines changed

8 files changed

+372
-40
lines changed

docs/stackit_beta_network_create.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,35 @@ stackit beta network create [flags]
1616
Create a network with name "network-1"
1717
$ stackit beta network create --name network-1
1818
19-
Create an IPv4 network with name "network-1" with DNS name servers and a prefix length
20-
$ stackit beta network create --name network-1 --ipv4-dns-name-servers "1.1.1.1,8.8.8.8,9.9.9.9" --ipv4-prefix-length 25
19+
Create a non-routed network with name "network-1"
20+
$ stackit beta network create --name network-1 --non-routed
2121
22-
Create an IPv6 network with name "network-1" with DNS name servers and a prefix length
23-
$ stackit beta network create --name network-1 --ipv6-dns-name-servers "2001:4860:4860::8888,2001:4860:4860::8844" --ipv6-prefix-length 56
22+
Create a network with name "network-1" and no gateway
23+
$ stackit beta network create --name network-1 --no-ipv4-gateway
24+
25+
Create an IPv4 network with name "network-1" with DNS name servers, a prefix and a gateway
26+
$ stackit beta 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"
27+
28+
Create an IPv6 network with name "network-1" with DNS name servers, a prefix and a gateway
29+
$ stackit beta 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"
2430
```
2531

2632
### Options
2733

2834
```
2935
-h, --help Help for "stackit beta network create"
30-
--ipv4-dns-name-servers strings List of DNS name servers for IPv4
36+
--ipv4-dns-name-servers strings List of DNS name servers for IPv4. Nameservers cannot be defined for routed networks
37+
--ipv4-gateway string The IPv4 gateway of a network. If not specified, the first IP of the network will be assigned as the gateway
38+
--ipv4-prefix string The IPv4 prefix of the network (CIDR)
3139
--ipv4-prefix-length int The prefix length of the IPv4 network
32-
--ipv6-dns-name-servers strings List of DNS name servers for IPv6
40+
--ipv6-dns-name-servers strings List of DNS name servers for IPv6. Nameservers cannot be defined for routed networks
41+
--ipv6-gateway string The IPv6 gateway of a network. If not specified, the first IP of the network will be assigned as the gateway
42+
--ipv6-prefix string The IPv6 prefix of the network (CIDR)
3343
--ipv6-prefix-length int The prefix length of the IPv6 network
3444
-n, --name string Network name
45+
--no-ipv4-gateway If set to true, the network doesn't have an IPv4 gateway
46+
--no-ipv6-gateway If set to true, the network doesn't have an IPv6 gateway
47+
--non-routed If set to true, the network is not routed and therefore not accessible from other networks
3548
```
3649

3750
### Options inherited from parent commands

docs/stackit_beta_network_update.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,27 @@ stackit beta network update [flags]
1616
Update network with ID "xxx" with new name "network-1-new"
1717
$ stackit beta network update xxx --name network-1-new
1818
19-
Update IPv4 network with ID "xxx" with new name "network-1-new" and new DNS name servers
20-
$ stackit beta network update xxx --name network-1-new --ipv4-dns-name-servers "2.2.2.2"
19+
Update network with ID "xxx" with no gateway
20+
$ stackit beta network update --no-ipv4-gateway
2121
22-
Update IPv6 network with ID "xxx" with new name "network-1-new" and new DNS name servers
23-
$ stackit beta network update xxx --name network-1-new --ipv6-dns-name-servers "2001:4860:4860::8888"
22+
Update IPv4 network with ID "xxx" with new name "network-1-new", new gateway and new DNS name servers
23+
$ stackit beta network update xxx --name network-1-new --ipv4-dns-name-servers "2.2.2.2" --ipv4-gateway "10.1.2.3"
24+
25+
Update IPv6 network with ID "xxx" with new name "network-1-new", new gateway and new DNS name servers
26+
$ stackit beta network update xxx --name network-1-new --ipv6-dns-name-servers "2001:4860:4860::8888" --ipv6-gateway "2001:4860:4860::8888"
2427
```
2528

2629
### Options
2730

2831
```
2932
-h, --help Help for "stackit beta network update"
30-
--ipv4-dns-name-servers strings List of DNS name servers IPv4
31-
--ipv6-dns-name-servers strings List of DNS name servers for IPv6
33+
--ipv4-dns-name-servers strings List of DNS name servers IPv4. Nameservers cannot be defined for routed networks
34+
--ipv4-gateway string The IPv4 gateway of a network. If not specified, the first IP of the network will be assigned as the gateway
35+
--ipv6-dns-name-servers strings List of DNS name servers for IPv6. Nameservers cannot be defined for routed networks
36+
--ipv6-gateway string The IPv6 gateway of a network. If not specified, the first IP of the network will be assigned as the gateway
3237
-n, --name string Network name
38+
--no-ipv4-gateway If set to true, the network doesn't have an IPv4 gateway
39+
--no-ipv6-gateway If set to true, the network doesn't have an IPv6 gateway
3340
```
3441

3542
### Options inherited from parent commands

internal/cmd/beta/network/create/create.go

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,31 @@ const (
2525
nameFlag = "name"
2626
ipv4DnsNameServersFlag = "ipv4-dns-name-servers"
2727
ipv4PrefixLengthFlag = "ipv4-prefix-length"
28+
ipv4PrefixFlag = "ipv4-prefix"
29+
ipv4GatewayFlag = "ipv4-gateway"
2830
ipv6DnsNameServersFlag = "ipv6-dns-name-servers"
2931
ipv6PrefixLengthFlag = "ipv6-prefix-length"
32+
ipv6PrefixFlag = "ipv6-prefix"
33+
ipv6GatewayFlag = "ipv6-gateway"
34+
nonRoutedFlag = "non-routed"
35+
noIpv4GatewayFlag = "no-ipv4-gateway"
36+
noIpv6GatewayFlag = "no-ipv6-gateway"
3037
)
3138

3239
type inputModel struct {
3340
*globalflags.GlobalFlagModel
3441
Name *string
3542
IPv4DnsNameServers *[]string
3643
IPv4PrefixLength *int64
44+
IPv4Prefix *string
45+
IPv4Gateway *string
3746
IPv6DnsNameServers *[]string
3847
IPv6PrefixLength *int64
48+
IPv6Prefix *string
49+
IPv6Gateway *string
50+
NonRouted bool
51+
NoIPv4Gateway bool
52+
NoIPv6Gateway bool
3953
}
4054

4155
func NewCmd(p *print.Printer) *cobra.Command {
@@ -50,12 +64,20 @@ func NewCmd(p *print.Printer) *cobra.Command {
5064
`$ stackit beta network create --name network-1`,
5165
),
5266
examples.NewExample(
53-
`Create an IPv4 network with name "network-1" with DNS name servers and a prefix length`,
54-
`$ stackit beta network create --name network-1 --ipv4-dns-name-servers "1.1.1.1,8.8.8.8,9.9.9.9" --ipv4-prefix-length 25`,
67+
`Create a non-routed network with name "network-1"`,
68+
`$ stackit beta network create --name network-1 --non-routed`,
5569
),
5670
examples.NewExample(
57-
`Create an IPv6 network with name "network-1" with DNS name servers and a prefix length`,
58-
`$ stackit beta network create --name network-1 --ipv6-dns-name-servers "2001:4860:4860::8888,2001:4860:4860::8844" --ipv6-prefix-length 56`,
71+
`Create a network with name "network-1" and no gateway`,
72+
`$ stackit beta network create --name network-1 --no-ipv4-gateway`,
73+
),
74+
examples.NewExample(
75+
`Create an IPv4 network with name "network-1" with DNS name servers, a prefix and a gateway`,
76+
`$ stackit beta 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"`,
77+
),
78+
examples.NewExample(
79+
`Create an IPv6 network with name "network-1" with DNS name servers, a prefix and a gateway`,
80+
`$ stackit beta 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"`,
5981
),
6082
),
6183
RunE: func(cmd *cobra.Command, _ []string) error {
@@ -113,10 +135,17 @@ func NewCmd(p *print.Printer) *cobra.Command {
113135

114136
func configureFlags(cmd *cobra.Command) {
115137
cmd.Flags().StringP(nameFlag, "n", "", "Network name")
116-
cmd.Flags().StringSlice(ipv4DnsNameServersFlag, []string{}, "List of DNS name servers for IPv4")
138+
cmd.Flags().StringSlice(ipv4DnsNameServersFlag, []string{}, "List of DNS name servers for IPv4. Nameservers cannot be defined for routed networks")
117139
cmd.Flags().Int64(ipv4PrefixLengthFlag, 0, "The prefix length of the IPv4 network")
118-
cmd.Flags().StringSlice(ipv6DnsNameServersFlag, []string{}, "List of DNS name servers for IPv6")
140+
cmd.Flags().String(ipv4PrefixFlag, "", "The IPv4 prefix of the network (CIDR)")
141+
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")
142+
cmd.Flags().StringSlice(ipv6DnsNameServersFlag, []string{}, "List of DNS name servers for IPv6. Nameservers cannot be defined for routed networks")
119143
cmd.Flags().Int64(ipv6PrefixLengthFlag, 0, "The prefix length of the IPv6 network")
144+
cmd.Flags().String(ipv6PrefixFlag, "", "The IPv6 prefix of the network (CIDR)")
145+
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")
146+
cmd.Flags().Bool(nonRoutedFlag, false, "If set to true, the network is not routed and therefore not accessible from other networks")
147+
cmd.Flags().Bool(noIpv4GatewayFlag, false, "If set to true, the network doesn't have an IPv4 gateway")
148+
cmd.Flags().Bool(noIpv6GatewayFlag, false, "If set to true, the network doesn't have an IPv6 gateway")
120149

121150
err := flags.MarkFlagsRequired(cmd, nameFlag)
122151
cobra.CheckErr(err)
@@ -133,8 +162,15 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
133162
Name: flags.FlagToStringPointer(p, cmd, nameFlag),
134163
IPv4DnsNameServers: flags.FlagToStringSlicePointer(p, cmd, ipv4DnsNameServersFlag),
135164
IPv4PrefixLength: flags.FlagToInt64Pointer(p, cmd, ipv4PrefixLengthFlag),
165+
IPv4Prefix: flags.FlagToStringPointer(p, cmd, ipv4PrefixFlag),
166+
IPv4Gateway: flags.FlagToStringPointer(p, cmd, ipv4GatewayFlag),
136167
IPv6DnsNameServers: flags.FlagToStringSlicePointer(p, cmd, ipv6DnsNameServersFlag),
137168
IPv6PrefixLength: flags.FlagToInt64Pointer(p, cmd, ipv6PrefixLengthFlag),
169+
IPv6Prefix: flags.FlagToStringPointer(p, cmd, ipv6PrefixFlag),
170+
IPv6Gateway: flags.FlagToStringPointer(p, cmd, ipv6GatewayFlag),
171+
NonRouted: flags.FlagToBoolValue(p, cmd, nonRoutedFlag),
172+
NoIPv4Gateway: flags.FlagToBoolValue(p, cmd, noIpv4GatewayFlag),
173+
NoIPv6Gateway: flags.FlagToBoolValue(p, cmd, noIpv6GatewayFlag),
138174
}
139175

140176
if p.IsVerbosityDebug() {
@@ -153,22 +189,42 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
153189
req := apiClient.CreateNetwork(ctx, model.ProjectId)
154190
addressFamily := &iaas.CreateNetworkAddressFamily{}
155191

156-
if model.IPv6DnsNameServers != nil || model.IPv6PrefixLength != nil {
192+
if model.IPv6DnsNameServers != nil || model.IPv6PrefixLength != nil || model.IPv6Prefix != nil || model.NoIPv6Gateway || model.IPv6Gateway != nil {
157193
addressFamily.Ipv6 = &iaas.CreateNetworkIPv6Body{
158194
Nameservers: model.IPv6DnsNameServers,
159195
PrefixLength: model.IPv6PrefixLength,
196+
Prefix: model.IPv6Prefix,
197+
}
198+
199+
if model.NoIPv6Gateway {
200+
addressFamily.Ipv6.Gateway = iaas.NewNullableString(nil)
201+
} else if model.IPv6Gateway != nil {
202+
addressFamily.Ipv6.Gateway = iaas.NewNullableString(model.IPv6Gateway)
160203
}
161204
}
162205

163-
if model.IPv4DnsNameServers != nil || model.IPv4PrefixLength != nil {
206+
if model.IPv4DnsNameServers != nil || model.IPv4PrefixLength != nil || model.IPv4Prefix != nil || model.NoIPv4Gateway || model.IPv4Gateway != nil {
164207
addressFamily.Ipv4 = &iaas.CreateNetworkIPv4Body{
165208
Nameservers: model.IPv4DnsNameServers,
166209
PrefixLength: model.IPv4PrefixLength,
210+
Prefix: model.IPv4Prefix,
211+
}
212+
213+
if model.NoIPv4Gateway {
214+
addressFamily.Ipv4.Gateway = iaas.NewNullableString(nil)
215+
} else if model.IPv4Gateway != nil {
216+
addressFamily.Ipv4.Gateway = iaas.NewNullableString(model.IPv4Gateway)
167217
}
168218
}
169219

220+
routed := true
221+
if model.NonRouted {
222+
routed = false
223+
}
224+
170225
payload := iaas.CreateNetworkPayload{
171-
Name: model.Name,
226+
Name: model.Name,
227+
Routed: &routed,
172228
}
173229

174230
if addressFamily.Ipv4 != nil || addressFamily.Ipv6 != nil {
@@ -197,7 +253,11 @@ func outputResult(p *print.Printer, model *inputModel, projectLabel string, netw
197253

198254
return nil
199255
default:
200-
p.Outputf("Created network for project %q.\nNetwork ID: %s\n", projectLabel, *network.NetworkId)
256+
operationState := "Created"
257+
if model.Async {
258+
operationState = "Triggered creation of"
259+
}
260+
p.Outputf("%s network for project %q.\nNetwork ID: %s\n", operationState, projectLabel, *network.NetworkId)
201261
return nil
202262
}
203263
}

0 commit comments

Comments
 (0)