diff --git a/examples/iaasalpha/attach_nic/attach_nic.go b/examples/iaasalpha/attach_nic/attach_nic.go new file mode 100644 index 000000000..8a2ee94fb --- /dev/null +++ b/examples/iaasalpha/attach_nic/attach_nic.go @@ -0,0 +1,67 @@ +package main + +import ( + "context" + "fmt" + "net/http" + "os" + + "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/runtime" + "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" + serverId := "SERVER_ID" + nicId := "NIC_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) + } + + // Attach an existing network interface to an existing server + var httpResp *http.Response + ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(context.Background(), &httpResp) + err = iaasalphaClient.AddNICToServer(ctxWithHTTPResp, projectId, serverId, nicId).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `AddNICToServer`: %v\n", err) + } else { + fmt.Printf("[iaasalpha API] Triggered attachment of nic with ID %q.\n", nicId) + } + requestId := httpResp.Header[wait.XRequestIDHeader][0] + + // Wait for attachment of the nic + _, err = wait.ProjectRequestWaitHandler(context.Background(), iaasalphaClient, projectId, requestId).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for attachment: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[iaasalpha API] Nic %q has been successfully attached to the server %s.\n", nicId, serverId) + + err = iaasalphaClient.RemoveNICFromServer(ctxWithHTTPResp, projectId, serverId, nicId).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `RemoveNICFromServer`: %v\n", err) + } else { + fmt.Printf("[iaasalpha API] Triggered removal of attachment of nic with ID %q.\n", nicId) + } + + requestId = httpResp.Header[wait.XRequestIDHeader][0] + + // Wait for dettachment of the nic + _, err = wait.ProjectRequestWaitHandler(context.Background(), iaasalphaClient, projectId, requestId).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for removal of attachment of NIC: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[iaasalpha API] NIC %q has been successfully detacched from the server %s.\n", nicId, serverId) +} diff --git a/examples/iaasalpha/attach_public_ip/attach_public_ip.go b/examples/iaasalpha/attach_public_ip/attach_public_ip.go new file mode 100644 index 000000000..e1f1f9725 --- /dev/null +++ b/examples/iaasalpha/attach_public_ip/attach_public_ip.go @@ -0,0 +1,67 @@ +package main + +import ( + "context" + "fmt" + "net/http" + "os" + + "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/runtime" + "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" + serverId := "SERVER_ID" + publicIpId := "PUBLIC_IP_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) + } + + // Attach an existing network interface to an existing server + var httpResp *http.Response + ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(context.Background(), &httpResp) + err = iaasalphaClient.AddPublicIpToServer(ctxWithHTTPResp, projectId, serverId, publicIpId).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `AddPublicIpToServer`: %v\n", err) + } else { + fmt.Printf("[iaasalpha API] Triggered attachment of public ip with ID %q.\n", publicIpId) + } + requestId := httpResp.Header[wait.XRequestIDHeader][0] + + // Wait for attachment of the public ip + _, err = wait.ProjectRequestWaitHandler(context.Background(), iaasalphaClient, projectId, requestId).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for attachment: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[iaasalpha API] Public IP %q has been successfully attached to the server %s.\n", publicIpId, serverId) + + err = iaasalphaClient.RemovePublicIpFromServer(ctxWithHTTPResp, projectId, serverId, publicIpId).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `RemovePublicIpFromServer`: %v\n", err) + } else { + fmt.Printf("[iaasalpha API] Triggered removal of attachment of public ip with ID %q.\n", publicIpId) + } + + requestId = httpResp.Header[wait.XRequestIDHeader][0] + + // Wait for dettachment of the public ip + _, err = wait.ProjectRequestWaitHandler(context.Background(), iaasalphaClient, projectId, requestId).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for removal of attachment of PublicIp: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[iaasalpha API] PublicIp %q has been successfully detacched from the server %s.\n", publicIpId, serverId) +} diff --git a/examples/iaasalpha/attach_security_group/attach_security_group.go b/examples/iaasalpha/attach_security_group/attach_security_group.go new file mode 100644 index 000000000..994e50782 --- /dev/null +++ b/examples/iaasalpha/attach_security_group/attach_security_group.go @@ -0,0 +1,67 @@ +package main + +import ( + "context" + "fmt" + "net/http" + "os" + + "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/runtime" + "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" + serverId := "SERVER_ID" + securityGroupId := "SECURITY_GROUP_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) + } + + // Attach an existing network interface to an existing server + var httpResp *http.Response + ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(context.Background(), &httpResp) + err = iaasalphaClient.AddSecurityGroupToServer(ctxWithHTTPResp, projectId, serverId, securityGroupId).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `AddSecurityGroupToServer`: %v\n", err) + } else { + fmt.Printf("[iaasalpha API] Triggered attachment of security group with ID %q.\n", securityGroupId) + } + requestId := httpResp.Header[wait.XRequestIDHeader][0] + + // Wait for attachment of the security group + _, err = wait.ProjectRequestWaitHandler(context.Background(), iaasalphaClient, projectId, requestId).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for attachment: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[iaasalpha API] Security group %q has been successfully attached to the server %s.\n", securityGroupId, serverId) + + err = iaasalphaClient.RemoveSecurityGroupFromServer(ctxWithHTTPResp, projectId, serverId, securityGroupId).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `RemoveSecurityGroupFromServer`: %v\n", err) + } else { + fmt.Printf("[iaasalpha API] Triggered removal of attachment of security group with ID %q.\n", securityGroupId) + } + + requestId = httpResp.Header[wait.XRequestIDHeader][0] + + // Wait for dettachment of the security group + _, err = wait.ProjectRequestWaitHandler(context.Background(), iaasalphaClient, projectId, requestId).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for removal of attachment of SecurityGroup: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[iaasalpha API] SecurityGroup %q has been successfully detacched from the server %s.\n", securityGroupId, serverId) +} diff --git a/examples/iaasalpha/attach_service_account/attach_service_account.go b/examples/iaasalpha/attach_service_account/attach_service_account.go new file mode 100644 index 000000000..52037635b --- /dev/null +++ b/examples/iaasalpha/attach_service_account/attach_service_account.go @@ -0,0 +1,67 @@ +package main + +import ( + "context" + "fmt" + "net/http" + "os" + + "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/runtime" + "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" + serverId := "SERVER_ID" + serviceAccountMail := "SERVICE_ACCOUNT_MAIL" + + // 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) + } + + // Attach an existing service account to an existing server + var httpResp *http.Response + ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(context.Background(), &httpResp) + _, err = iaasalphaClient.AddServiceAccountToServer(ctxWithHTTPResp, projectId, serverId, serviceAccountMail).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `AddServiceAccountToServer`: %v\n", err) + } else { + fmt.Printf("[iaasalpha API] Triggered attachment of service account with mail %q.\n", serviceAccountMail) + } + requestId := httpResp.Header[wait.XRequestIDHeader][0] + + // Wait for attachment of the service account + _, err = wait.ProjectRequestWaitHandler(context.Background(), iaasalphaClient, projectId, requestId).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for attachment: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[iaasalpha API] Service account %q has been successfully attached to the server %s.\n", serviceAccountMail, serverId) + + _, err = iaasalphaClient.RemoveServiceAccountFromServer(ctxWithHTTPResp, projectId, serverId, serviceAccountMail).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `RemoveServiceAccountFromServer`: %v\n", err) + } else { + fmt.Printf("[iaasalpha API] Triggered removal of attachment of service account with mail %q.\n", serviceAccountMail) + } + + requestId = httpResp.Header[wait.XRequestIDHeader][0] + + // Wait for dettachment of the service account + _, err = wait.ProjectRequestWaitHandler(context.Background(), iaasalphaClient, projectId, requestId).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for removal of attachment of service account: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[iaasalpha API] Service account %q has been successfully detacched from the server %s.\n", serviceAccountMail, serverId) +} diff --git a/examples/iaasalpha/attach_volume/attach_volume.go b/examples/iaasalpha/attach_volume/attach_volume.go new file mode 100644 index 000000000..50cbaea3e --- /dev/null +++ b/examples/iaasalpha/attach_volume/attach_volume.go @@ -0,0 +1,60 @@ +package main + +import ( + "context" + "fmt" + "os" + + "github.com/stackitcloud/stackit-sdk-go/core/config" + "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" + serverId := "SERVER_ID" + volumeId := "VOLUME_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) + } + + payload := iaasalpha.AddVolumeToServerPayload{} + _, err = iaasalphaClient.AddVolumeToServer(context.Background(), projectId, serverId, volumeId).AddVolumeToServerPayload(payload).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `AddVolumeToServer`: %v\n", err) + } else { + fmt.Printf("[iaasalpha API] Triggered attachment of volume with ID %q.\n", volumeId) + } + + // Wait for attachment of the volume + _, err = wait.AddVolumeToServerWaitHandler(context.Background(), iaasalphaClient, projectId, serverId, volumeId).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for attachment: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[iaasalpha API] Volume %q has been successfully attached to the server %s.\n", volumeId, serverId) + + err = iaasalphaClient.RemoveVolumeFromServer(context.Background(), projectId, serverId, volumeId).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `RemoveVolumeFromServer`: %v\n", err) + } else { + fmt.Printf("[iaasalpha API] Triggered removal of attachment of volume with ID %q.\n", volumeId) + } + + // Wait for dettachment of the volume + _, err = wait.RemoveVolumeFromServerWaitHandler(context.Background(), iaasalphaClient, projectId, serverId, volumeId).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for removal of attachment of volume: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[iaasalpha API] Volume %q has been successfully detacched from the server %s.\n", volumeId, serverId) +} diff --git a/examples/iaasalpha/go.mod b/examples/iaasalpha/go.mod index d9501de05..4bbd49db2 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.14.0 - github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.8-alpha + github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.11-alpha ) require ( diff --git a/examples/iaasalpha/go.sum b/examples/iaasalpha/go.sum index 680523dce..0f5e5a461 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.14.0 h1:oBwwzrEHDTlZpRoQwmMQpNA8bWciTrtGkiN29nx14Z0= github.com/stackitcloud/stackit-sdk-go/core v0.14.0/go.mod h1:mDX1mSTsB3mP+tNBGcFNx6gH1mGBN4T+dVt+lcw7nlw= -github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.8-alpha h1:s7HprLITChqzYk1sO605q0Ivhge7oNmZ4sVBfySlgw8= -github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.8-alpha/go.mod h1:b4KR6r+yWS2hsDkz6ebRqxgadB+ZsAZcG0oDfv5jeaY= +github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.11-alpha h1:uYslPwq0euGkuH6kHIhoXUaB9w+yrrDKLd80eo/5GZU= +github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.11-alpha/go.mod h1:nW/6vvumUHA7o1/JOOqsrEOBNrRHombEKB1U4jmg2wU=