-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathcloud_network.go
More file actions
455 lines (352 loc) · 20 KB
/
cloud_network.go
File metadata and controls
455 lines (352 loc) · 20 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// SPDX-FileCopyrightText: 2025 OVH SAS <opensource@ovh.net>
//
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"github.com/ovh/ovhcloud-cli/internal/assets"
"github.com/ovh/ovhcloud-cli/internal/flags"
"github.com/ovh/ovhcloud-cli/internal/services/cloud"
"github.com/spf13/cobra"
)
func initCloudNetworkCommand(cloudCmd *cobra.Command) {
networkCmd := &cobra.Command{
Use: "network",
Short: "Manage networks in the given cloud project",
}
networkCmd.PersistentFlags().StringVar(&cloud.CloudProject, "cloud-project", "", "Cloud project ID")
cloudCmd.AddCommand(networkCmd)
// Private network commands
privateNetworkCmd := &cobra.Command{
Use: "private",
Short: "Manage private networks in the given cloud project",
}
networkCmd.AddCommand(privateNetworkCmd)
privateNetworkListCmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List your private networks",
Run: cloud.ListPrivateNetworks,
}
privateNetworkCmd.AddCommand(withFilterFlag(privateNetworkListCmd))
privateNetworkCmd.AddCommand(&cobra.Command{
Use: "get <network_id>",
Short: "Get a specific private network",
Run: cloud.GetPrivateNetwork,
Args: cobra.ExactArgs(1),
})
privateNetworkEditCmd := &cobra.Command{
Use: "edit <network_id>",
Short: "Edit the given private network",
Args: cobra.ExactArgs(1),
Run: cloud.EditPrivateNetwork,
}
privateNetworkEditCmd.Flags().StringVar(&cloud.CloudNetworkName, "name", "", "Name of the private network")
addInteractiveEditorFlag(privateNetworkEditCmd)
privateNetworkCmd.AddCommand(privateNetworkEditCmd)
privateNetworkCmd.AddCommand(getPrivateNetworkCreationCmd())
privateNetworkCmd.AddCommand(&cobra.Command{
Use: "delete <network_id>",
Short: "Delete a specific private network",
Run: cloud.DeletePrivateNetwork,
Args: cobra.ExactArgs(1),
})
// Private network region commands
privateNetworkRegionCmd := &cobra.Command{
Use: "region",
Short: "Manage regions in a specific private network",
}
privateNetworkCmd.AddCommand(privateNetworkRegionCmd)
privateNetworkRegionCmd.AddCommand(&cobra.Command{
Use: "delete <network_id> <region>",
Short: "Delete the given region from a private network",
Run: cloud.DeletePrivateNetworkRegion,
Args: cobra.ExactArgs(2),
})
privateNetworkRegionCmd.AddCommand(&cobra.Command{
Use: "add <network_id> <region>",
Short: "Add a region to a private network",
Run: cloud.AddPrivateNetworkRegion,
Args: cobra.ExactArgs(2),
})
// Private network subnet commands
privateNetworkSubnetCmd := &cobra.Command{
Use: "subnet",
Short: "Manage subnets in a specific private network",
}
privateNetworkCmd.AddCommand(privateNetworkSubnetCmd)
privateNetworkSubnetCmd.AddCommand(withFilterFlag(&cobra.Command{
Use: "list <network_id>",
Aliases: []string{"ls"},
Short: "List subnets in a private network",
Run: cloud.ListPrivateNetworkSubnets,
Args: cobra.ExactArgs(1),
}))
privateNetworkSubnetCmd.AddCommand(&cobra.Command{
Use: "get <network_id> <subnet_id>",
Short: "Get a specific subnet in a private network",
Run: cloud.GetPrivateNetworkSubnet,
Args: cobra.ExactArgs(2),
})
privateNetworkSubnetCmd.AddCommand(getSubnetCreationCmd())
privateNetworkSubnetEditCmd := &cobra.Command{
Use: "edit <network_id> <subnet_id>",
Short: "Edit a specific subnet in a private network",
Run: cloud.EditPrivateNetworkSubnet,
Args: cobra.ExactArgs(2),
}
privateNetworkSubnetEditCmd.Flags().BoolVar(&cloud.CloudNetworkSubnetEditSpec.Dhcp, "enable-dhcp", false, "Enable DHCP (set to true if you don't want to set a default gateway IP)")
privateNetworkSubnetEditCmd.Flags().BoolVar(&cloud.CloudNetworkSubnetEditSpec.DisableGateway, "disable-gateway", false, "Set to true if you want to disable the default gateway")
privateNetworkSubnetEditCmd.Flags().StringVar(&cloud.CloudNetworkSubnetEditSpec.GatewayIp, "gateway-ip", "", "Gateway IP address")
addInteractiveEditorFlag(privateNetworkSubnetEditCmd)
privateNetworkSubnetCmd.AddCommand(privateNetworkSubnetEditCmd)
privateNetworkSubnetCmd.AddCommand(&cobra.Command{
Use: "delete <network_id> <subnet_id>",
Short: "Delete a specific subnet in a private network",
Run: cloud.DeletePrivateNetworkSubnet,
Args: cobra.ExactArgs(2),
})
// Public network commands
publicNetworkCmd := &cobra.Command{
Use: "public",
Short: "Manage public networks in the given cloud project",
}
networkCmd.AddCommand(publicNetworkCmd)
publicNetworkListCmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List your public networks",
Run: cloud.ListPublicNetworks,
}
publicNetworkCmd.AddCommand(withFilterFlag(publicNetworkListCmd))
publicNetworkCmd.AddCommand(&cobra.Command{
Use: "get <network_id>",
Short: "Get a specific public network",
Run: cloud.GetPublicNetwork,
Args: cobra.ExactArgs(1),
})
// Gateway commands
gatewayCmd := &cobra.Command{
Use: "gateway",
Short: "Manage gateways in the given cloud project",
}
networkCmd.AddCommand(gatewayCmd)
gatewayCmd.AddCommand(getGatewayCreationCmd())
gatewayListCmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List your gateways",
Run: cloud.ListGateways,
}
gatewayCmd.AddCommand(withFilterFlag(gatewayListCmd))
gatewayCmd.AddCommand(&cobra.Command{
Use: "get <gateway_id>",
Short: "Get a specific gateway",
Run: cloud.GetGateway,
Args: cobra.ExactArgs(1),
})
gatewayEditCmd := &cobra.Command{
Use: "edit <gateway_id>",
Short: "Edit the given gateway",
Run: cloud.EditGateway,
Args: cobra.ExactArgs(1),
}
gatewayEditCmd.Flags().StringVar(&cloud.CloudGatewaySpec.Name, "name", "", "Name of the gateway")
gatewayEditCmd.Flags().StringVar(&cloud.CloudGatewaySpec.Model, "model", "", "Model of the gateway (s, m, l, xl, 2xl, 3xl)")
addInteractiveEditorFlag(gatewayEditCmd)
gatewayCmd.AddCommand(gatewayEditCmd)
gatewayCmd.AddCommand(&cobra.Command{
Use: "delete <gateway_id>",
Short: "Delete a specific gateway",
Run: cloud.DeleteGateway,
Args: cobra.ExactArgs(1),
})
gatewayCmd.AddCommand(&cobra.Command{
Use: "expose <gateway_id>",
Short: "Expose gateway to public network by adding a public port on it",
Run: cloud.ExposeGateway,
Args: cobra.ExactArgs(1),
})
// Gateway interfaces commands
gatewayInterfaceCmd := &cobra.Command{
Use: "interface",
Short: "Manage interfaces of a specific gateway",
}
gatewayCmd.AddCommand(gatewayInterfaceCmd)
gatewayInterfaceCmd.AddCommand(withFilterFlag(&cobra.Command{
Use: "list <gateway_id>",
Aliases: []string{"ls"},
Short: "List interfaces of a specific gateway",
Run: cloud.ListGatewayInterfaces,
Args: cobra.ExactArgs(1),
}))
gatewayInterfaceCmd.AddCommand(&cobra.Command{
Use: "get <gateway_id> <interface_id>",
Short: "Get a specific interface of a gateway",
Run: cloud.GetGatewayInterface,
Args: cobra.ExactArgs(2),
})
gatewayInterfaceCreateCmd := &cobra.Command{
Use: "create <gateway_id>",
Short: "Create a new interface for the given gateway",
Run: cloud.CreateGatewayInterface,
Args: cobra.ExactArgs(1),
}
gatewayInterfaceCreateCmd.Flags().StringVar(&cloud.GatewayInterfaceSpec.SubnetID, "subnet-id", "", "ID of the subnet to attach the interface to")
gatewayInterfaceCreateCmd.MarkFlagRequired("subnet-id")
gatewayInterfaceCmd.AddCommand(gatewayInterfaceCreateCmd)
gatewayInterfaceCmd.AddCommand(&cobra.Command{
Use: "delete <gateway_id> <interface_id>",
Short: "Delete a specific interface of a gateway",
Run: cloud.DeleteGatewayInterface,
Args: cobra.ExactArgs(2),
})
}
func getPrivateNetworkCreationCmd() *cobra.Command {
privateNetworkCreateCmd := &cobra.Command{
Use: "create <region>",
Short: "Create a private network in the given cloud project",
Long: `Use this command to create a private network.
There are three ways to define the parameters:
1. Using only CLI flags:
ovhcloud cloud network private create <region> --name MyNetwork
2. Using a configuration file:
First you can generate an example of parameters file using the following command:
ovhcloud cloud network private create <region> --init-file ./params.json
You will be able to choose from several examples of parameters. Once an example has been selected, the content is written in the given file.
After editing the file to set the correct creation parameters, run:
ovhcloud cloud network private create <region> --from-file ./params.json
Note that you can also pipe the content of the parameters file, like the following:
cat ./params.json | ovhcloud cloud network private create <region>
In both cases, you can override the parameters in the given file using command line flags, for example:
ovhcloud cloud network private create <region> --from-file ./params.json --name MyNetwork
3. Using your default text editor:
ovhcloud cloud network private create <region> --editor
You will be able to choose from several examples of parameters. Once an example has been selected, the CLI will open your
default text editor to update the parameters. When saving the file, the creation will start.
Note that it is also possible to override values in the presented examples using command line flags like the following:
ovhcloud cloud network private create <region> --editor --name MyNetwork
`,
Run: cloud.CreatePrivateNetwork,
Args: cobra.ExactArgs(1),
}
privateNetworkCreateCmd.Flags().StringVar(&cloud.CloudNetworkSpec.Name, "name", "", "Name of the private network")
privateNetworkCreateCmd.Flags().IntVar(&cloud.CloudNetworkSpec.VlanId, "vlan-id", 0, "VLAN ID for the private network")
privateNetworkCreateCmd.Flags().StringVar(&cloud.CloudNetworkSpec.Gateway.Model, "gateway-model", "", "Gateway model (s, m, l, xl, 2xl, 3xl)")
privateNetworkCreateCmd.Flags().StringVar(&cloud.CloudNetworkSpec.Gateway.Name, "gateway-name", "", "Name of the gateway")
privateNetworkCreateCmd.Flags().StringVar(&cloud.CloudNetworkSpec.Subnet.Name, "subnet-name", "", "Name of the subnet")
privateNetworkCreateCmd.Flags().StringVar(&cloud.CloudNetworkSpec.Subnet.Cidr, "subnet-cidr", "", "CIDR of the subnet")
privateNetworkCreateCmd.Flags().IntVar(&cloud.CloudNetworkSpec.Subnet.IPVersion, "subnet-ip-version", 0, "IP version (4 or 6)")
privateNetworkCreateCmd.Flags().BoolVar(&cloud.CloudNetworkSpec.Subnet.EnableDhcp, "subnet-enable-dhcp", false, "Enable DHCP for the subnet")
privateNetworkCreateCmd.Flags().BoolVar(&cloud.CloudNetworkSpec.Subnet.EnableGatewayIp, "subnet-enable-gateway-ip", false, "Set a gateway ip for the subnet")
privateNetworkCreateCmd.Flags().StringVar(&cloud.CloudNetworkSpec.Subnet.GatewayIp, "subnet-gateway-ip", "", "Gateway IP address for the subnet")
privateNetworkCreateCmd.Flags().BoolVar(&cloud.CloudNetworkSpec.Subnet.UseDefaultPublicDNSResolver, "subnet-use-default-public-dns-resolver", false, "Use default DNS resolver for the subnet")
privateNetworkCreateCmd.Flags().StringSliceVar(&cloud.CloudNetworkSpec.Subnet.DnsNameServers, "subnet-dns-name-servers", nil, "DNS name servers for the subnet")
privateNetworkCreateCmd.Flags().StringSliceVar(&cloud.CloudNetworkSpec.Subnet.CliAllocationPools, "subnet-allocation-pools", nil, "Allocation pools for the subnet in format start:end")
privateNetworkCreateCmd.Flags().StringSliceVar(&cloud.CloudNetworkSpec.Subnet.CliHostRoutes, "subnet-host-routes", nil, "Host routes for the subnet in format destination:nextHop")
// Common flags for other means to define parameters
addParameterFileFlags(privateNetworkCreateCmd, false, assets.CloudOpenapiSchema, "/cloud/project/{serviceName}/region/{regionName}/network", "post", cloud.PrivateNetworkCreationExample, nil)
addInteractiveEditorFlag(privateNetworkCreateCmd)
privateNetworkCreateCmd.Flags().BoolVar(&flags.WaitForTask, "wait", false, "Wait for network creation to be done before exiting")
privateNetworkCreateCmd.MarkFlagsMutuallyExclusive("from-file", "editor")
return privateNetworkCreateCmd
}
func getSubnetCreationCmd() *cobra.Command {
privateNetworkSubnetCreateCmd := &cobra.Command{
Use: "create <network_id>",
Short: "Create a subnet in the given private network",
Long: `Use this command to create a new subnet in a private network.
There are three ways to define the parameters:
1. Using only CLI flags:
ovhcloud cloud network private subnet create <network_id> --network 192.168.1.0/24 --start 192.168.1.12 --end 192.168.1.24 --region GRA9
2. Using a configuration file:
First you can generate an example of parameters file using the following command:
ovhcloud cloud network private subnet create <network_id> --init-file ./params.json
You will be able to choose from several examples of parameters. Once an example has been selected, the content is written in the given file.
After editing the file to set the correct creation parameters, run:
ovhcloud cloud network private subnet create <network_id> --from-file ./params.json
Note that you can also pipe the content of the parameters file, like the following:
cat ./params.json | ovhcloud cloud network private subnet create <network_id>
In both cases, you can override the parameters in the given file using command line flags, for example:
ovhcloud cloud network private subnet create <network_id> --from-file ./params.json --region BHS5
3. Using your default text editor:
ovhcloud cloud network private subnet create <network_id> --editor
You will be able to choose from several examples of parameters. Once an example has been selected, the CLI will open your
default text editor to update the parameters. When saving the file, the creation will start.
Note that it is also possible to override values in the presented examples using command line flags like the following:
ovhcloud cloud network private subnet create <network_id> --editor --region DE1
`,
Run: cloud.CreatePrivateNetworkSubnet,
Args: cobra.ExactArgs(1),
}
privateNetworkSubnetCreateCmd.Flags().BoolVar(&cloud.CloudNetworkSubnetSpec.DHCP, "dhcp", false, "Enable DHCP for the subnet")
privateNetworkSubnetCreateCmd.Flags().BoolVar(&cloud.CloudNetworkSubnetSpec.NoGateway, "no-gateway", false, "Use this flag if you don't want to set a default gateway IP")
privateNetworkSubnetCreateCmd.Flags().StringVar(&cloud.CloudNetworkSubnetSpec.Network, "network", "", "Global network CIDR (eg: 192.168.1.0/24)")
privateNetworkSubnetCreateCmd.Flags().StringVar(&cloud.CloudNetworkSubnetSpec.Start, "start", "", "First IP for this region (eg: 192.168.1.12)")
privateNetworkSubnetCreateCmd.Flags().StringVar(&cloud.CloudNetworkSubnetSpec.End, "end", "", "Last IP for this region (eg: 192.168.1.24)")
privateNetworkSubnetCreateCmd.Flags().StringVar(&cloud.CloudNetworkSubnetSpec.Region, "region", "", "Region for the subnet")
// Common flags for other means to define parameters
addParameterFileFlags(privateNetworkSubnetCreateCmd, false, assets.CloudOpenapiSchema, "/cloud/project/{serviceName}/network/private/{networkId}/subnet", "post", cloud.PrivateNetworkSubnetCreationExample, nil)
addInteractiveEditorFlag(privateNetworkSubnetCreateCmd)
privateNetworkSubnetCreateCmd.MarkFlagsMutuallyExclusive("from-file", "editor")
return privateNetworkSubnetCreateCmd
}
func getGatewayCreationCmd() *cobra.Command {
gatewayCreateCmd := &cobra.Command{
Use: "create <region>",
Short: "Create a gateway in the given cloud project",
Long: `Use this command to create a new gateway.
Two options are available to create a gateway:
- Create a gateway in an existing private network
- Create a gateway in a new private network
When creating a gateway in an existing private network, you must specify the network ID and subnet ID
using the flags --network-id and --subnet-id.
In this case, only two parameters are supported and required: the gateway model and its name (respectively
--model and --name flags).
There are three ways to define the parameters:
1. Using only CLI flags:
ovhcloud cloud network gateway create <region> --name MyGateway --model xl
2. Using a configuration file:
First you can generate an example of parameters file using the following command:
ovhcloud cloud network gateway create <region> --init-file ./params.json
You will be able to choose from several examples of parameters. Once an example has been selected, the content is written in the given file.
After editing the file to set the correct creation parameters, run:
ovhcloud cloud network gateway create <region> --from-file ./params.json
Note that you can also pipe the content of the parameters file, like the following:
cat ./params.json | ovhcloud cloud network gateway create <region>
In both cases, you can override the parameters in the given file using command line flags, for example:
ovhcloud cloud network gateway create <region> --from-file ./params.json --name MyGateway
3. Using your default text editor:
ovhcloud cloud network gateway create <region> --editor
You will be able to choose from several examples of parameters. Once an example has been selected, the CLI will open your
default text editor to update the parameters. When saving the file, the creation will start.
Note that it is also possible to override values in the presented examples using command line flags like the following:
ovhcloud cloud network gateway create <region> --editor --name MyGateway
`,
Run: cloud.CreateGateway,
Args: cobra.ExactArgs(1),
}
gatewayCreateCmd.Flags().StringVar(&cloud.CloudGatewaySpec.Model, "model", "", "Gateway model (s, m, l, xl, 2xl, 3xl)")
gatewayCreateCmd.Flags().StringVar(&cloud.CloudGatewaySpec.Name, "name", "", "Name of the gateway")
gatewayCreateCmd.Flags().StringVar(&cloud.CloudGatewaySpec.Network.Name, "network-name", "", "Name of the private network")
gatewayCreateCmd.Flags().IntVar(&cloud.CloudGatewaySpec.Network.VlanId, "network-vlan-id", 0, "VLAN ID for the private network")
gatewayCreateCmd.Flags().StringVar(&cloud.CloudGatewaySpec.Network.Subnet.Name, "subnet-name", "", "Name of the subnet")
gatewayCreateCmd.Flags().StringVar(&cloud.CloudGatewaySpec.Network.Subnet.Cidr, "subnet-cidr", "", "CIDR of the subnet")
gatewayCreateCmd.Flags().IntVar(&cloud.CloudGatewaySpec.Network.Subnet.IPVersion, "subnet-ip-version", 0, "IP version (4 or 6)")
gatewayCreateCmd.Flags().BoolVar(&cloud.CloudGatewaySpec.Network.Subnet.EnableDhcp, "subnet-enable-dhcp", false, "Enable DHCP for the subnet")
gatewayCreateCmd.Flags().StringVar(&cloud.CloudGatewaySpec.Network.Subnet.GatewayIp, "subnet-gateway-ip", "", "Gateway IP address for the subnet")
gatewayCreateCmd.Flags().BoolVar(&cloud.CloudGatewaySpec.Network.Subnet.UseDefaultPublicDNSResolver, "subnet-use-default-public-dns-resolver", false, "Use default DNS resolver for the subnet")
gatewayCreateCmd.Flags().StringSliceVar(&cloud.CloudGatewaySpec.Network.Subnet.DnsNameServers, "subnet-dns-name-servers", nil, "DNS name servers for the subnet")
gatewayCreateCmd.Flags().StringSliceVar(&cloud.CloudGatewaySpec.Network.Subnet.CliAllocationPools, "subnet-allocation-pools", nil, "Allocation pools for the subnet in format start:end")
gatewayCreateCmd.Flags().StringSliceVar(&cloud.CloudGatewaySpec.Network.Subnet.CliHostRoutes, "subnet-host-routes", nil, "Host routes for the subnet in format destination:nextHop")
// Common flags for other means to define parameters
addParameterFileFlags(gatewayCreateCmd, false, assets.CloudOpenapiSchema, "/cloud/project/{serviceName}/region/{regionName}/gateway", "post", cloud.GatewayCreationExample, nil)
addInteractiveEditorFlag(gatewayCreateCmd)
gatewayCreateCmd.Flags().BoolVar(&flags.WaitForTask, "wait", false, "Wait for gateway creation to be done before exiting")
gatewayCreateCmd.MarkFlagsMutuallyExclusive("from-file", "editor")
// Add a flag to specify the network ID if creating in an existing private network
gatewayCreateCmd.Flags().StringVar(&cloud.CloudGatewaySpec.ExistingNetworkID, "network-id", "", "ID of the existing private network to create the gateway in")
gatewayCreateCmd.Flags().StringVar(&cloud.CloudGatewaySpec.ExistingSubnetID, "subnet-id", "", "ID of the existing subnet to create the gateway in")
gatewayCreateCmd.MarkFlagsMutuallyExclusive("network-name", "network-id")
gatewayCreateCmd.MarkFlagsMutuallyExclusive("subnet-name", "subnet-id")
return gatewayCreateCmd
}