|
| 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/iaasalpha" |
| 11 | + "github.com/stackitcloud/stackit-sdk-go/services/iaasalpha/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 | + iaasalphaClient, err := iaasalpha.NewAPIClient( |
| 20 | + config.WithRegion("eu01"), |
| 21 | + ) |
| 22 | + if err != nil { |
| 23 | + fmt.Fprintf(os.Stderr, "[iaasalpha API] Creating API client: %v\n", err) |
| 24 | + os.Exit(1) |
| 25 | + } |
| 26 | + |
| 27 | + servers, err := iaasalphaClient.ListServers(context.Background(), projectId).Execute() |
| 28 | + |
| 29 | + if err != nil { |
| 30 | + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `ListServers`: %v\n", err) |
| 31 | + } else { |
| 32 | + fmt.Printf("[iaasalpha API] Number of servers: %v\n", len(*servers.Items)) |
| 33 | + } |
| 34 | + |
| 35 | + // Create a server |
| 36 | + createServerPayload := iaasalpha.CreateServerPayload{ |
| 37 | + Name: utils.Ptr("example-server"), |
| 38 | + AvailabilityZone: utils.Ptr("eu01-1"), |
| 39 | + MachineType: utils.Ptr("g1.1"), |
| 40 | + BootVolume: &iaasalpha.CreateServerPayloadBootVolume{ |
| 41 | + Size: utils.Ptr(int64(64)), |
| 42 | + Source: &iaasalpha.BootVolumeSource{ |
| 43 | + Id: utils.Ptr("IMAGE_ID"), |
| 44 | + Type: utils.Ptr("image"), |
| 45 | + }, |
| 46 | + }, |
| 47 | + } |
| 48 | + server, err := iaasalphaClient.CreateServer(context.Background(), projectId).CreateServerPayload(createServerPayload).Execute() |
| 49 | + if err != nil { |
| 50 | + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `CreateServer`: %v\n", err) |
| 51 | + } else { |
| 52 | + fmt.Printf("[iaasalpha API] Triggered creation of server with ID %q.\n", *server.Id) |
| 53 | + } |
| 54 | + |
| 55 | + // Wait for creation of the server |
| 56 | + server, err = wait.CreateServerWaitHandler(context.Background(), iaasalphaClient, projectId, *server.Id).WaitWithContext(context.Background()) |
| 57 | + if err != nil { |
| 58 | + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for creation: %v\n", err) |
| 59 | + os.Exit(1) |
| 60 | + } |
| 61 | + |
| 62 | + fmt.Printf("[iaasalpha API] Server %q has been successfully created.\n", *server.Id) |
| 63 | + |
| 64 | + // Update a server |
| 65 | + updateServerPayload := iaasalpha.V1alpha1UpdateServerPayload{ |
| 66 | + Name: utils.Ptr("renamed"), |
| 67 | + } |
| 68 | + server, err = iaasalphaClient.V1alpha1UpdateServer(context.Background(), projectId, *server.Id).V1alpha1UpdateServerPayload(updateServerPayload).Execute() |
| 69 | + if err != nil { |
| 70 | + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `UpdateServer`: %v\n", err) |
| 71 | + } |
| 72 | + |
| 73 | + fmt.Printf("[iaasalpha API] Server %q has been successfully updated.\n", *server.Id) |
| 74 | + |
| 75 | + // Resize a server |
| 76 | + resizeServerPayload := iaasalpha.ResizeServerPayload{ |
| 77 | + MachineType: utils.Ptr("c1.2"), |
| 78 | + } |
| 79 | + |
| 80 | + err = iaasalphaClient.ResizeServer(context.Background(), projectId, *server.Id).ResizeServerPayload(resizeServerPayload).Execute() |
| 81 | + if err != nil { |
| 82 | + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `ResizeServer`: %v\n", err) |
| 83 | + } else { |
| 84 | + fmt.Printf("[iaasalpha API] Triggered resize of server with ID %q.\n", *server.Id) |
| 85 | + } |
| 86 | + |
| 87 | + server, err = wait.ResizeServerWaitHandler(context.Background(), iaasalphaClient, projectId, *server.Id).WaitWithContext(context.Background()) |
| 88 | + if err != nil { |
| 89 | + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for resize: %v\n", err) |
| 90 | + os.Exit(1) |
| 91 | + } |
| 92 | + |
| 93 | + fmt.Printf("[iaasalpha API] Server %q has been successfully resized.\n", *server.Id) |
| 94 | + |
| 95 | + // Delete a server |
| 96 | + err = iaasalphaClient.DeleteServer(context.Background(), projectId, *server.Id).Execute() |
| 97 | + if err != nil { |
| 98 | + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `DeleteServer`: %v\n", err) |
| 99 | + } else { |
| 100 | + fmt.Printf("[iaasalpha API] Triggered deletion of server with ID %q.\n", *server.Id) |
| 101 | + } |
| 102 | + |
| 103 | + // Wait for deletion of the server |
| 104 | + _, err = wait.DeleteServerWaitHandler(context.Background(), iaasalphaClient, projectId, *server.Id).WaitWithContext(context.Background()) |
| 105 | + if err != nil { |
| 106 | + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for deletion: %v\n", err) |
| 107 | + os.Exit(1) |
| 108 | + } |
| 109 | + |
| 110 | + fmt.Printf("[iaasalpha API] Server %q has been successfully deleted.\n", *server.Id) |
| 111 | +} |
0 commit comments