|
80 | 80 | Subnet struct { |
81 | 81 | Name string `json:"name,omitempty"` |
82 | 82 | Cidr string `json:"cidr,omitempty"` |
83 | | - EnableDhcp bool `json:"enableDhcp,omitempty"` |
84 | | - EnableGatewayIp bool `json:"enableGatewayIp,omitempty"` |
| 83 | + EnableDhcp bool `json:"enableDhcp"` |
| 84 | + EnableGatewayIp bool `json:"enableGatewayIp"` |
85 | 85 | GatewayIp string `json:"gatewayIp,omitempty"` |
86 | 86 | DnsNameServers []string `json:"dnsNameServers,omitempty"` |
87 | 87 | UseDefaultPublicDNSResolver bool `json:"useDefaultPublicDNSResolver,omitempty"` |
@@ -115,6 +115,16 @@ type ( |
115 | 115 | Destination string `json:"destination,omitempty"` |
116 | 116 | NextHop string `json:"nextHop,omitempty"` |
117 | 117 | } |
| 118 | + |
| 119 | + NetworkRegionDetails struct { |
| 120 | + OpenstackID string `json:"openstackId"` |
| 121 | + Region string `json:"region"` |
| 122 | + } |
| 123 | + |
| 124 | + PrivateNetwork struct { |
| 125 | + ID string `json:"id"` |
| 126 | + Regions []NetworkRegionDetails `json:"regions"` |
| 127 | + } |
118 | 128 | ) |
119 | 129 |
|
120 | 130 | func ListPrivateNetworks(_ *cobra.Command, _ []string) { |
@@ -283,29 +293,81 @@ You can check the status of the operation with: 'ovhcloud cloud operation get %[ |
283 | 293 | } |
284 | 294 |
|
285 | 295 | // Fetch all private networks |
286 | | - var networks []struct { |
287 | | - ID string `json:"id"` |
288 | | - Regions []struct { |
289 | | - OpenstackID string `json:"openstackId"` |
290 | | - Region string `json:"region"` |
291 | | - } `json:"regions"` |
292 | | - } |
| 296 | + var networks []PrivateNetwork |
293 | 297 | if err := httpLib.Client.Get(fmt.Sprintf("/cloud/project/%s/network/private", projectID), &networks); err != nil { |
294 | 298 | display.OutputError(&flags.OutputFormatConfig, "failed to fetch private networks: %s", err) |
295 | 299 | return |
296 | 300 | } |
297 | 301 |
|
298 | 302 | // Find the created network |
| 303 | + var ( |
| 304 | + foundNetwork *PrivateNetwork |
| 305 | + foundRegionNetwork *NetworkRegionDetails |
| 306 | + ) |
| 307 | + |
| 308 | +eachNetwork: |
299 | 309 | for _, network := range networks { |
300 | 310 | for _, regionDetails := range network.Regions { |
301 | 311 | if regionDetails.OpenstackID == networkID && regionDetails.Region == region { |
302 | | - display.OutputInfo(&flags.OutputFormatConfig, regionDetails, "✅ Network %s created successfully (Openstack ID: %s)", network.ID, regionDetails.OpenstackID) |
303 | | - return |
| 312 | + foundNetwork = &network |
| 313 | + foundRegionNetwork = ®ionDetails |
| 314 | + break eachNetwork |
| 315 | + } |
| 316 | + } |
| 317 | + } |
| 318 | + |
| 319 | + if foundNetwork == nil { |
| 320 | + display.OutputError(&flags.OutputFormatConfig, "created network not found, this is unexpected") |
| 321 | + return |
| 322 | + } |
| 323 | + |
| 324 | + // Fetch subnets of created network |
| 325 | + endpoint = fmt.Sprintf("/cloud/project/%s/region/%s/network/%s/subnet", projectID, url.PathEscape(region), url.PathEscape(foundRegionNetwork.OpenstackID)) |
| 326 | + var subnets []map[string]any |
| 327 | + if err := httpLib.Client.Get(endpoint, &subnets); err != nil { |
| 328 | + display.OutputError(&flags.OutputFormatConfig, "failed to fetch subnets of created network: %s", err) |
| 329 | + return |
| 330 | + } |
| 331 | + |
| 332 | + // Fetch gateway of created subnets and prepare output |
| 333 | + var outputSubnets []map[string]any |
| 334 | + for _, subnet := range subnets { |
| 335 | + endpoint = fmt.Sprintf("/cloud/project/%s/region/%s/gateway?subnetId=%s", |
| 336 | + projectID, |
| 337 | + url.PathEscape(region), |
| 338 | + url.PathEscape(subnet["id"].(string)), |
| 339 | + ) |
| 340 | + |
| 341 | + var gateways []map[string]any |
| 342 | + if err := httpLib.Client.Get(endpoint, &gateways); err != nil { |
| 343 | + display.OutputError(&flags.OutputFormatConfig, "failed to fetch gateways of created network: %s", err) |
| 344 | + return |
| 345 | + } |
| 346 | + |
| 347 | + var outputGateways []map[string]any |
| 348 | + for _, gateway := range gateways { |
| 349 | + outputGateway := map[string]any{ |
| 350 | + "id": gateway["id"], |
| 351 | + "name": gateway["name"], |
304 | 352 | } |
| 353 | + outputGateways = append(outputGateways, outputGateway) |
305 | 354 | } |
| 355 | + |
| 356 | + outputSubnets = append(outputSubnets, map[string]any{ |
| 357 | + "id": subnet["id"], |
| 358 | + "name": subnet["name"], |
| 359 | + "gateways": outputGateways, |
| 360 | + }) |
| 361 | + } |
| 362 | + |
| 363 | + networkObject := map[string]any{ |
| 364 | + "id": foundNetwork.ID, |
| 365 | + "openstackId": foundRegionNetwork.OpenstackID, |
| 366 | + "region": foundRegionNetwork.Region, |
| 367 | + "subnets": outputSubnets, |
306 | 368 | } |
307 | 369 |
|
308 | | - display.OutputError(&flags.OutputFormatConfig, "created network not found, this is unexpected") |
| 370 | + display.OutputInfo(&flags.OutputFormatConfig, networkObject, "✅ Network %s created successfully (Openstack ID: %s)", foundNetwork.ID, foundRegionNetwork.OpenstackID) |
309 | 371 | } |
310 | 372 |
|
311 | 373 | func DeletePrivateNetwork(_ *cobra.Command, args []string) { |
|
0 commit comments