|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/stackitcloud/stackit-sdk-go/core/config" |
| 9 | + "github.com/stackitcloud/stackit-sdk-go/core/utils" |
| 10 | + "github.com/stackitcloud/stackit-sdk-go/services/iaas" |
| 11 | + "github.com/stackitcloud/stackit-sdk-go/services/iaas/wait" |
| 12 | +) |
| 13 | + |
| 14 | +func main() { |
| 15 | + // Specify the organization ID and project ID |
| 16 | + projectId := "PROJECT_ID" |
| 17 | + |
| 18 | + // Create a new API client, that uses default authentication and configuration |
| 19 | + iaasClient, err := iaas.NewAPIClient( |
| 20 | + config.WithRegion("eu01"), |
| 21 | + ) |
| 22 | + if err != nil { |
| 23 | + fmt.Fprintf(os.Stderr, "[IaaS API] Creating API client: %v\n", err) |
| 24 | + os.Exit(1) |
| 25 | + } |
| 26 | + |
| 27 | + // Create a network |
| 28 | + createNetworkPayload := iaas.CreateNetworkPayload{ |
| 29 | + Name: utils.Ptr("example-network"), |
| 30 | + AddressFamily: &iaas.CreateNetworkAddressFamily{ |
| 31 | + Ipv4: &iaas.CreateNetworkIPv4Body{ |
| 32 | + PrefixLength: utils.Ptr(int64(24)), |
| 33 | + Nameservers: &[]string{"1.2.3.4"}, |
| 34 | + }, |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + network, err := iaasClient.CreateNetwork(context.Background(), projectId).CreateNetworkPayload(createNetworkPayload).Execute() |
| 39 | + if err != nil { |
| 40 | + fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `CreateNetwork`: %v\n", err) |
| 41 | + os.Exit(1) |
| 42 | + } |
| 43 | + |
| 44 | + fmt.Printf("[IaaS API] Triggered creation of network with ID %q.\n", *network.NetworkId) |
| 45 | + fmt.Printf("[Iaas API] Current state of the network: %q\n", *network.State) |
| 46 | + fmt.Println("[Iaas API] Waiting for network to be created...") |
| 47 | + |
| 48 | + network, err = wait.CreateNetworkWaitHandler(context.Background(), iaasClient, projectId, *network.NetworkId).WaitWithContext(context.Background()) |
| 49 | + if err != nil { |
| 50 | + fmt.Fprintf(os.Stderr, "[IaaS API] Error when waiting for creation: %v\n", err) |
| 51 | + os.Exit(1) |
| 52 | + } |
| 53 | + |
| 54 | + fmt.Printf("[IaaS API] Network has been successfully created.\n") |
| 55 | + |
| 56 | + // Update a network |
| 57 | + updateNetworkPayload := iaas.PartialUpdateNetworkPayload{ |
| 58 | + Name: utils.Ptr("example-network-test-renamed"), |
| 59 | + } |
| 60 | + |
| 61 | + err = iaasClient.PartialUpdateNetwork(context.Background(), projectId, *network.NetworkId).PartialUpdateNetworkPayload(updateNetworkPayload).Execute() |
| 62 | + if err != nil { |
| 63 | + fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `PartialUpdateNetwork`: %v\n", err) |
| 64 | + os.Exit(1) |
| 65 | + } |
| 66 | + |
| 67 | + _, err = wait.UpdateNetworkWaitHandler(context.Background(), iaasClient, projectId, *network.NetworkId).WaitWithContext(context.Background()) |
| 68 | + if err != nil { |
| 69 | + fmt.Fprintf(os.Stderr, "[IaaS API] Error when waiting for update: %v\n", err) |
| 70 | + os.Exit(1) |
| 71 | + } |
| 72 | + |
| 73 | + fmt.Printf("[IaaS API] Network has been successfully updated.\n") |
| 74 | + |
| 75 | + // Delete a network |
| 76 | + err = iaasClient.DeleteNetwork(context.Background(), projectId, *network.NetworkId).Execute() |
| 77 | + if err != nil { |
| 78 | + fmt.Fprintf(os.Stderr, "[IaaS API] Error when calling `DeleteNetwork`: %v\n", err) |
| 79 | + os.Exit(1) |
| 80 | + } |
| 81 | + |
| 82 | + _, err = wait.DeleteNetworkWaitHandler(context.Background(), iaasClient, projectId, *network.NetworkId).WaitWithContext(context.Background()) |
| 83 | + if err != nil { |
| 84 | + fmt.Fprintf(os.Stderr, "[IaaS API] Error when waiting for deletion: %v\n", err) |
| 85 | + os.Exit(1) |
| 86 | + } |
| 87 | + |
| 88 | + fmt.Printf("[IaaS API] Network has been successfully deleted.\n") |
| 89 | +} |
0 commit comments