|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/stackitcloud/stackit-sdk-go/core/config" |
| 10 | + "github.com/stackitcloud/stackit-sdk-go/core/runtime" |
| 11 | + "github.com/stackitcloud/stackit-sdk-go/services/iaasalpha" |
| 12 | + "github.com/stackitcloud/stackit-sdk-go/services/iaasalpha/wait" |
| 13 | +) |
| 14 | + |
| 15 | +func main() { |
| 16 | + // Specify the organization ID and project ID |
| 17 | + projectId := "PROJECT_ID" |
| 18 | + serverId := "SERVER_ID" |
| 19 | + serviceAccountMail := "SERVICE_ACCOUNT_MAIL" |
| 20 | + |
| 21 | + // Create a new API client, that uses default authentication and configuration |
| 22 | + iaasalphaClient, err := iaasalpha.NewAPIClient( |
| 23 | + config.WithRegion("eu01"), |
| 24 | + ) |
| 25 | + if err != nil { |
| 26 | + fmt.Fprintf(os.Stderr, "[iaasalpha API] Creating API client: %v\n", err) |
| 27 | + os.Exit(1) |
| 28 | + } |
| 29 | + |
| 30 | + // Attach an existing service account to an existing server |
| 31 | + var httpResp *http.Response |
| 32 | + ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(context.Background(), &httpResp) |
| 33 | + _, err = iaasalphaClient.AddServiceAccountToServer(ctxWithHTTPResp, projectId, serverId, serviceAccountMail).Execute() |
| 34 | + if err != nil { |
| 35 | + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `AddServiceAccountToServer`: %v\n", err) |
| 36 | + } else { |
| 37 | + fmt.Printf("[iaasalpha API] Triggered attachment of service account with mail %q.\n", serviceAccountMail) |
| 38 | + } |
| 39 | + requestId := httpResp.Header[wait.XRequestIDHeader][0] |
| 40 | + |
| 41 | + // Wait for attachment of the service account |
| 42 | + _, err = wait.ProjectRequestWaitHandler(context.Background(), iaasalphaClient, projectId, requestId).WaitWithContext(context.Background()) |
| 43 | + if err != nil { |
| 44 | + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for attachment: %v\n", err) |
| 45 | + os.Exit(1) |
| 46 | + } |
| 47 | + |
| 48 | + fmt.Printf("[iaasalpha API] Service account %q has been successfully attached to the server %s.\n", serviceAccountMail, serverId) |
| 49 | + |
| 50 | + _, err = iaasalphaClient.RemoveServiceAccountFromServer(ctxWithHTTPResp, projectId, serverId, serviceAccountMail).Execute() |
| 51 | + if err != nil { |
| 52 | + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `RemoveServiceAccountFromServer`: %v\n", err) |
| 53 | + } else { |
| 54 | + fmt.Printf("[iaasalpha API] Triggered removal of attachment of service account with mail %q.\n", serviceAccountMail) |
| 55 | + } |
| 56 | + |
| 57 | + requestId = httpResp.Header[wait.XRequestIDHeader][0] |
| 58 | + |
| 59 | + // Wait for dettachment of the service account |
| 60 | + _, err = wait.ProjectRequestWaitHandler(context.Background(), iaasalphaClient, projectId, requestId).WaitWithContext(context.Background()) |
| 61 | + if err != nil { |
| 62 | + fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for removal of attachment of service account: %v\n", err) |
| 63 | + os.Exit(1) |
| 64 | + } |
| 65 | + |
| 66 | + fmt.Printf("[iaasalpha API] Service account %q has been successfully detacched from the server %s.\n", serviceAccountMail, serverId) |
| 67 | +} |
0 commit comments