diff --git a/examples/iaasalpha/go.mod b/examples/iaasalpha/go.mod index f392e5fbd..05da82a7d 100644 --- a/examples/iaasalpha/go.mod +++ b/examples/iaasalpha/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( github.com/stackitcloud/stackit-sdk-go/core v0.13.0 - github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.3-alpha + github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.6-alpha ) require ( diff --git a/examples/iaasalpha/go.sum b/examples/iaasalpha/go.sum index 1c184b03c..f7580a342 100644 --- a/examples/iaasalpha/go.sum +++ b/examples/iaasalpha/go.sum @@ -5,5 +5,5 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/stackitcloud/stackit-sdk-go/core v0.13.0 h1:BtJT2WXqZdexPPQi/HPUIr8g4JUPOCheh6J9dxiCQ4Q= github.com/stackitcloud/stackit-sdk-go/core v0.13.0/go.mod h1:mDX1mSTsB3mP+tNBGcFNx6gH1mGBN4T+dVt+lcw7nlw= -github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.3-alpha h1:cwNZwtvb7diYJhjNmxuX8IxaqOq1BNDSjtG3c9Ne1ig= -github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.3-alpha/go.mod h1:b4KR6r+yWS2hsDkz6ebRqxgadB+ZsAZcG0oDfv5jeaY= +github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.6-alpha h1:XUYncbRKaqbG76OzoSugfvPHp6+0A86JJxW2T3CLT2E= +github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.6-alpha/go.mod h1:b4KR6r+yWS2hsDkz6ebRqxgadB+ZsAZcG0oDfv5jeaY= diff --git a/examples/iaasalpha/server/server.go b/examples/iaasalpha/server/server.go new file mode 100644 index 000000000..853c912fc --- /dev/null +++ b/examples/iaasalpha/server/server.go @@ -0,0 +1,111 @@ +package main + +import ( + "context" + "fmt" + "os" + + "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/utils" + "github.com/stackitcloud/stackit-sdk-go/services/iaasalpha" + "github.com/stackitcloud/stackit-sdk-go/services/iaasalpha/wait" +) + +func main() { + // Specify the organization ID and project ID + projectId := "PROJECT_ID" + + // Create a new API client, that uses default authentication and configuration + iaasalphaClient, err := iaasalpha.NewAPIClient( + config.WithRegion("eu01"), + ) + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Creating API client: %v\n", err) + os.Exit(1) + } + + servers, err := iaasalphaClient.ListServers(context.Background(), projectId).Execute() + + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `ListServers`: %v\n", err) + } else { + fmt.Printf("[iaasalpha API] Number of servers: %v\n", len(*servers.Items)) + } + + // Create a server + createServerPayload := iaasalpha.CreateServerPayload{ + Name: utils.Ptr("example-server"), + AvailabilityZone: utils.Ptr("eu01-1"), + MachineType: utils.Ptr("g1.1"), + BootVolume: &iaasalpha.CreateServerPayloadBootVolume{ + Size: utils.Ptr(int64(64)), + Source: &iaasalpha.BootVolumeSource{ + Id: utils.Ptr("IMAGE_ID"), + Type: utils.Ptr("image"), + }, + }, + } + server, err := iaasalphaClient.CreateServer(context.Background(), projectId).CreateServerPayload(createServerPayload).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `CreateServer`: %v\n", err) + } else { + fmt.Printf("[iaasalpha API] Triggered creation of server with ID %q.\n", *server.Id) + } + + // Wait for creation of the server + server, err = wait.CreateServerWaitHandler(context.Background(), iaasalphaClient, projectId, *server.Id).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for creation: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[iaasalpha API] Server %q has been successfully created.\n", *server.Id) + + // Update a server + updateServerPayload := iaasalpha.V1alpha1UpdateServerPayload{ + Name: utils.Ptr("renamed"), + } + server, err = iaasalphaClient.V1alpha1UpdateServer(context.Background(), projectId, *server.Id).V1alpha1UpdateServerPayload(updateServerPayload).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `UpdateServer`: %v\n", err) + } + + fmt.Printf("[iaasalpha API] Server %q has been successfully updated.\n", *server.Id) + + // Resize a server + resizeServerPayload := iaasalpha.ResizeServerPayload{ + MachineType: utils.Ptr("c1.2"), + } + + err = iaasalphaClient.ResizeServer(context.Background(), projectId, *server.Id).ResizeServerPayload(resizeServerPayload).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `ResizeServer`: %v\n", err) + } else { + fmt.Printf("[iaasalpha API] Triggered resize of server with ID %q.\n", *server.Id) + } + + server, err = wait.ResizeServerWaitHandler(context.Background(), iaasalphaClient, projectId, *server.Id).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for resize: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[iaasalpha API] Server %q has been successfully resized.\n", *server.Id) + + // Delete a server + err = iaasalphaClient.DeleteServer(context.Background(), projectId, *server.Id).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `DeleteServer`: %v\n", err) + } else { + fmt.Printf("[iaasalpha API] Triggered deletion of server with ID %q.\n", *server.Id) + } + + // Wait for deletion of the server + _, err = wait.DeleteServerWaitHandler(context.Background(), iaasalphaClient, projectId, *server.Id).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for deletion: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[iaasalpha API] Server %q has been successfully deleted.\n", *server.Id) +} diff --git a/examples/iaasalpha/iaas.go b/examples/iaasalpha/volume/volume.go similarity index 89% rename from examples/iaasalpha/iaas.go rename to examples/iaasalpha/volume/volume.go index 31bcb8b73..f07411cbe 100644 --- a/examples/iaasalpha/iaas.go +++ b/examples/iaasalpha/volume/volume.go @@ -52,7 +52,7 @@ func main() { os.Exit(1) } - fmt.Printf("[iaasalpha API] Network volume %q has been successfully created.\n", *volume.Id) + fmt.Printf("[iaasalpha API] Volume %q has been successfully created.\n", *volume.Id) // Update a volume updateVolumePayload := iaasalpha.UpdateVolumePayload{ @@ -63,7 +63,7 @@ func main() { fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `UpdateVolume`: %v\n", err) } - fmt.Printf("[iaasalpha API] Network volume %q has been successfully updated.\n", *volume.Id) + fmt.Printf("[iaasalpha API] Volume %q has been successfully updated.\n", *volume.Id) // Resize a volume resizeVolumePayload := iaasalpha.ResizeVolumePayload{ @@ -74,7 +74,7 @@ func main() { fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `ResizeVolume`: %v\n", err) } - fmt.Printf("[iaasalpha API] Network volume %q has been successfully resized.\n", *volume.Id) + fmt.Printf("[iaasalpha API] Volume %q has been successfully resized.\n", *volume.Id) // Delete a volume err = iaasalphaClient.DeleteVolume(context.Background(), projectId, *volume.Id).Execute() @@ -91,5 +91,5 @@ func main() { os.Exit(1) } - fmt.Printf("[iaasalpha API] Network volume %q has been successfully deleted.\n", *volume.Id) + fmt.Printf("[iaasalpha API] Volume %q has been successfully deleted.\n", *volume.Id) }