Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions examples/iaas/attach_nic/attach_nic.go
Original file line number Diff line number Diff line change
@@ -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/iaas"
"github.com/stackitcloud/stackit-sdk-go/services/iaas/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
iaasClient, err := iaas.NewAPIClient(
config.WithRegion("eu01"),
)
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas 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 = iaasClient.AddNICToServer(ctxWithHTTPResp, projectId, serverId, nicId).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when calling `AddNICToServer`: %v\n", err)
} else {
fmt.Printf("[iaas 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(), iaasClient, projectId, requestId).WaitWithContext(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when waiting for attachment: %v\n", err)
os.Exit(1)
}

fmt.Printf("[iaas API] Nic %q has been successfully attached to the server %s.\n", nicId, serverId)

err = iaasClient.RemoveNICFromServer(ctxWithHTTPResp, projectId, serverId, nicId).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when calling `RemoveNICFromServer`: %v\n", err)
} else {
fmt.Printf("[iaas 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(), iaasClient, projectId, requestId).WaitWithContext(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when waiting for removal of attachment of NIC: %v\n", err)
os.Exit(1)
}

fmt.Printf("[iaas API] NIC %q has been successfully detached from the server %s.\n", nicId, serverId)
}
67 changes: 67 additions & 0 deletions examples/iaas/attach_public_ip/attach_public_ip.go
Original file line number Diff line number Diff line change
@@ -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/iaas"
"github.com/stackitcloud/stackit-sdk-go/services/iaas/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
iaasClient, err := iaas.NewAPIClient(
config.WithRegion("eu01"),
)
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas 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 = iaasClient.AddPublicIpToServer(ctxWithHTTPResp, projectId, serverId, publicIpId).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when calling `AddPublicIpToServer`: %v\n", err)
} else {
fmt.Printf("[iaas 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(), iaasClient, projectId, requestId).WaitWithContext(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when waiting for attachment: %v\n", err)
os.Exit(1)
}

fmt.Printf("[iaas API] Public IP %q has been successfully attached to the server %s.\n", publicIpId, serverId)

err = iaasClient.RemovePublicIpFromServer(ctxWithHTTPResp, projectId, serverId, publicIpId).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when calling `RemovePublicIpFromServer`: %v\n", err)
} else {
fmt.Printf("[iaas 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(), iaasClient, projectId, requestId).WaitWithContext(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when waiting for removal of attachment of PublicIp: %v\n", err)
os.Exit(1)
}

fmt.Printf("[iaas API] PublicIp %q has been successfully detached from the server %s.\n", publicIpId, serverId)
}
67 changes: 67 additions & 0 deletions examples/iaas/attach_security_group/attach_security_group.go
Original file line number Diff line number Diff line change
@@ -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/iaas"
"github.com/stackitcloud/stackit-sdk-go/services/iaas/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
iaasClient, err := iaas.NewAPIClient(
config.WithRegion("eu01"),
)
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas 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 = iaasClient.AddSecurityGroupToServer(ctxWithHTTPResp, projectId, serverId, securityGroupId).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when calling `AddSecurityGroupToServer`: %v\n", err)
} else {
fmt.Printf("[iaas 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(), iaasClient, projectId, requestId).WaitWithContext(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when waiting for attachment: %v\n", err)
os.Exit(1)
}

fmt.Printf("[iaas API] Security group %q has been successfully attached to the server %s.\n", securityGroupId, serverId)

err = iaasClient.RemoveSecurityGroupFromServer(ctxWithHTTPResp, projectId, serverId, securityGroupId).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when calling `RemoveSecurityGroupFromServer`: %v\n", err)
} else {
fmt.Printf("[iaas 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(), iaasClient, projectId, requestId).WaitWithContext(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when waiting for removal of attachment of SecurityGroup: %v\n", err)
os.Exit(1)
}

fmt.Printf("[iaas API] SecurityGroup %q has been successfully detached from the server %s.\n", securityGroupId, serverId)
}
67 changes: 67 additions & 0 deletions examples/iaas/attach_service_account/attach_service_account.go
Original file line number Diff line number Diff line change
@@ -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/iaas"
"github.com/stackitcloud/stackit-sdk-go/services/iaas/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
iaasClient, err := iaas.NewAPIClient(
config.WithRegion("eu01"),
)
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas 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 = iaasClient.AddServiceAccountToServer(ctxWithHTTPResp, projectId, serverId, serviceAccountMail).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when calling `AddServiceAccountToServer`: %v\n", err)
} else {
fmt.Printf("[iaas 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(), iaasClient, projectId, requestId).WaitWithContext(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when waiting for attachment: %v\n", err)
os.Exit(1)
}

fmt.Printf("[iaas API] Service account %q has been successfully attached to the server %s.\n", serviceAccountMail, serverId)

_, err = iaasClient.RemoveServiceAccountFromServer(ctxWithHTTPResp, projectId, serverId, serviceAccountMail).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when calling `RemoveServiceAccountFromServer`: %v\n", err)
} else {
fmt.Printf("[iaas 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(), iaasClient, projectId, requestId).WaitWithContext(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when waiting for removal of attachment of service account: %v\n", err)
os.Exit(1)
}

fmt.Printf("[iaas API] Service account %q has been successfully detached from the server %s.\n", serviceAccountMail, serverId)
}
60 changes: 60 additions & 0 deletions examples/iaas/attach_volume/attach_volume.go
Original file line number Diff line number Diff line change
@@ -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/iaas"
"github.com/stackitcloud/stackit-sdk-go/services/iaas/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
iaasClient, err := iaas.NewAPIClient(
config.WithRegion("eu01"),
)
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Creating API client: %v\n", err)
os.Exit(1)
}

payload := iaas.AddVolumeToServerPayload{}
_, err = iaasClient.AddVolumeToServer(context.Background(), projectId, serverId, volumeId).AddVolumeToServerPayload(payload).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when calling `AddVolumeToServer`: %v\n", err)
} else {
fmt.Printf("[iaas API] Triggered attachment of volume with ID %q.\n", volumeId)
}

// Wait for attachment of the volume
_, err = wait.AddVolumeToServerWaitHandler(context.Background(), iaasClient, projectId, serverId, volumeId).WaitWithContext(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when waiting for attachment: %v\n", err)
os.Exit(1)
}

fmt.Printf("[iaas API] Volume %q has been successfully attached to the server %s.\n", volumeId, serverId)

err = iaasClient.RemoveVolumeFromServer(context.Background(), projectId, serverId, volumeId).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when calling `RemoveVolumeFromServer`: %v\n", err)
} else {
fmt.Printf("[iaas API] Triggered removal of attachment of volume with ID %q.\n", volumeId)
}

// Wait for dettachment of the volume
_, err = wait.RemoveVolumeFromServerWaitHandler(context.Background(), iaasClient, projectId, serverId, volumeId).WaitWithContext(context.Background())
if err != nil {
fmt.Fprintf(os.Stderr, "[iaas API] Error when waiting for removal of attachment of volume: %v\n", err)
os.Exit(1)
}

fmt.Printf("[iaas API] Volume %q has been successfully detached from the server %s.\n", volumeId, serverId)
}
Loading
Loading