|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stackitcloud/stackit-sdk-go/core/utils" |
| 10 | + "github.com/stackitcloud/stackit-sdk-go/services/edge" |
| 11 | + "github.com/stackitcloud/stackit-sdk-go/services/edge/wait" |
| 12 | + "gopkg.in/yaml.v3" |
| 13 | +) |
| 14 | + |
| 15 | +func main() { |
| 16 | + // Mandatory parameters |
| 17 | + projectId := "PROJECT_ID" |
| 18 | + region := "eu01" |
| 19 | + |
| 20 | + // Create a new API client, that uses default authentication and configuration |
| 21 | + client, err := edge.NewAPIClient() |
| 22 | + if err != nil { |
| 23 | + fmt.Fprintf(os.Stderr, "[Edge API] Failed to create API client: %v\n", err) |
| 24 | + os.Exit(1) |
| 25 | + } |
| 26 | + |
| 27 | + // Create an Edge Instance with default values |
| 28 | + var ( |
| 29 | + payload = edge.NewPostInstancesPayloadWithDefaults() |
| 30 | + instance *edge.Instance |
| 31 | + ctx = context.Background() |
| 32 | + ) |
| 33 | + payload.DisplayName = utils.Ptr("example") |
| 34 | + instance, err = client.PostInstances(ctx, projectId, region).PostInstancesPayload(*payload).Execute() |
| 35 | + if err != nil { |
| 36 | + fmt.Fprintf(os.Stderr, "[Edge API] Failed to create Instance: %v\n", err) |
| 37 | + os.Exit(1) |
| 38 | + } |
| 39 | + fmt.Printf("[Edge API] Instance creation started, Id: %s\n", instance.GetId()) |
| 40 | + |
| 41 | + // Wait for Edge Instance to become active |
| 42 | + waitResult, err := wait.CreateOrUpdateInstanceWaitHandler( |
| 43 | + ctx, |
| 44 | + client, |
| 45 | + projectId, |
| 46 | + region, |
| 47 | + instance.GetId(), |
| 48 | + ).SetTimeout(10 * time.Minute).WaitWithContext(ctx) |
| 49 | + if err != nil { |
| 50 | + fmt.Fprintf(os.Stderr, "[Edge API] Failed wait for Instance creation: %v\n", err) |
| 51 | + os.Exit(1) |
| 52 | + } |
| 53 | + fmt.Printf("[Edge API] Instance created, Id: %s, Status: %s\n, URL: %s\n", instance.GetId(), waitResult.GetStatus(), instance.GetFrontendUrl()) |
| 54 | + |
| 55 | + // Create a service token to login to the instance UI and wait for the instance to become ready |
| 56 | + token, err := wait.TokenWaitHandler( |
| 57 | + ctx, |
| 58 | + client, |
| 59 | + projectId, |
| 60 | + region, |
| 61 | + instance.GetId(), |
| 62 | + utils.Ptr(int64(600)), |
| 63 | + ).WaitWithContext(ctx) |
| 64 | + if err != nil { |
| 65 | + fmt.Fprintf(os.Stderr, "[Edge API] Failed wait for token creation: %v\n", err) |
| 66 | + os.Exit(1) |
| 67 | + } |
| 68 | + if token != nil && token.Token != nil { |
| 69 | + fmt.Printf("Token: %s\n", *token.Token) |
| 70 | + } |
| 71 | + |
| 72 | + // Create a kubeconfig to interact with the instances kubernetes API and wait for the instance to become ready |
| 73 | + kubeconfig, err := wait.KubeconfigWaitHandler( |
| 74 | + ctx, |
| 75 | + client, |
| 76 | + projectId, |
| 77 | + region, |
| 78 | + instance.GetId(), |
| 79 | + utils.Ptr(int64(600)), |
| 80 | + ).WaitWithContext(ctx) |
| 81 | + if err != nil { |
| 82 | + fmt.Fprintf(os.Stderr, "[Edge API] Failed wait for kubeconfig creation: %v\n", err) |
| 83 | + os.Exit(1) |
| 84 | + } |
| 85 | + if kubeconfig != nil && kubeconfig.Kubeconfig != nil { |
| 86 | + yamlData, err := yaml.Marshal(kubeconfig.Kubeconfig) |
| 87 | + if err != nil { |
| 88 | + fmt.Fprintf(os.Stderr, "[Edge API] Failed to marshal kubeconfig: %v\n", err) |
| 89 | + os.Exit(1) |
| 90 | + } |
| 91 | + |
| 92 | + fmt.Println("Kubeconfig:") |
| 93 | + fmt.Println(string(yamlData)) |
| 94 | + } |
| 95 | + |
| 96 | + // Delete Edge Instance |
| 97 | + err = client.DeleteInstance(ctx, projectId, region, instance.GetId()).Execute() |
| 98 | + if err != nil { |
| 99 | + fmt.Fprintf(os.Stderr, "[Edge API] Failed to delete instance: %v\n", err) |
| 100 | + os.Exit(1) |
| 101 | + } |
| 102 | + fmt.Printf("[Edge API] Instance deletion started, Id: %s\n", instance.GetId()) |
| 103 | + |
| 104 | + // Wait for Edge instance deletion |
| 105 | + _, err = wait.DeleteInstanceWaitHandler(ctx, client, projectId, region, instance.GetId()).SetTimeout(10 * time.Minute).WaitWithContext(ctx) |
| 106 | + if err != nil { |
| 107 | + fmt.Fprintf(os.Stderr, "[Edge API] Failed wait for Instance deletion: %v\n", err) |
| 108 | + os.Exit(1) |
| 109 | + } |
| 110 | + fmt.Println("[Edge API] Instance deleted") |
| 111 | +} |
0 commit comments