From 1a457303376aebd423a3792d1292ba0a621753d8 Mon Sep 17 00:00:00 2001 From: Ruben Aleman Date: Mon, 12 Jan 2026 14:41:29 +0100 Subject: [PATCH] improvement(edge): add usage examples --- examples/edge/edge.go | 111 ++++++++++++++++++++++++++++++++++++++++++ examples/edge/go.mod | 14 ++++++ examples/edge/go.sum | 14 ++++++ go.work | 1 + 4 files changed, 140 insertions(+) create mode 100644 examples/edge/edge.go create mode 100644 examples/edge/go.mod create mode 100644 examples/edge/go.sum diff --git a/examples/edge/edge.go b/examples/edge/edge.go new file mode 100644 index 000000000..72a88eef4 --- /dev/null +++ b/examples/edge/edge.go @@ -0,0 +1,111 @@ +package main + +import ( + "context" + "fmt" + "os" + "time" + + "github.com/stackitcloud/stackit-sdk-go/core/utils" + "github.com/stackitcloud/stackit-sdk-go/services/edge" + "github.com/stackitcloud/stackit-sdk-go/services/edge/wait" + "gopkg.in/yaml.v3" +) + +func main() { + // Mandatory parameters + projectId := "PROJECT_ID" + region := "eu01" + + // Create a new API client, that uses default authentication and configuration + client, err := edge.NewAPIClient() + if err != nil { + fmt.Fprintf(os.Stderr, "[Edge API] Failed to create API client: %v\n", err) + os.Exit(1) + } + + // Create an Edge Instance with default values + var ( + payload = edge.NewPostInstancesPayloadWithDefaults() + instance *edge.Instance + ctx = context.Background() + ) + payload.DisplayName = utils.Ptr("example") + instance, err = client.PostInstances(ctx, projectId, region).PostInstancesPayload(*payload).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[Edge API] Failed to create Instance: %v\n", err) + os.Exit(1) + } + fmt.Printf("[Edge API] Instance creation started, Id: %s\n", instance.GetId()) + + // Wait for Edge Instance to become active + waitResult, err := wait.CreateOrUpdateInstanceWaitHandler( + ctx, + client, + projectId, + region, + instance.GetId(), + ).SetTimeout(10 * time.Minute).WaitWithContext(ctx) + if err != nil { + fmt.Fprintf(os.Stderr, "[Edge API] Failed wait for Instance creation: %v\n", err) + os.Exit(1) + } + fmt.Printf("[Edge API] Instance created, Id: %s, Status: %s\n, URL: %s\n", instance.GetId(), waitResult.GetStatus(), instance.GetFrontendUrl()) + + // Create a service token to login to the instance UI and wait for the instance to become ready + token, err := wait.TokenWaitHandler( + ctx, + client, + projectId, + region, + instance.GetId(), + utils.Ptr(int64(600)), + ).WaitWithContext(ctx) + if err != nil { + fmt.Fprintf(os.Stderr, "[Edge API] Failed wait for token creation: %v\n", err) + os.Exit(1) + } + if token != nil && token.Token != nil { + fmt.Printf("Token: %s\n", *token.Token) + } + + // Create a kubeconfig to interact with the instances kubernetes API and wait for the instance to become ready + kubeconfig, err := wait.KubeconfigWaitHandler( + ctx, + client, + projectId, + region, + instance.GetId(), + utils.Ptr(int64(600)), + ).WaitWithContext(ctx) + if err != nil { + fmt.Fprintf(os.Stderr, "[Edge API] Failed wait for kubeconfig creation: %v\n", err) + os.Exit(1) + } + if kubeconfig != nil && kubeconfig.Kubeconfig != nil { + yamlData, err := yaml.Marshal(kubeconfig.Kubeconfig) + if err != nil { + fmt.Fprintf(os.Stderr, "[Edge API] Failed to marshal kubeconfig: %v\n", err) + os.Exit(1) + } + + fmt.Println("Kubeconfig:") + fmt.Println(string(yamlData)) + } + + // Delete Edge Instance + err = client.DeleteInstance(ctx, projectId, region, instance.GetId()).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[Edge API] Failed to delete instance: %v\n", err) + os.Exit(1) + } + fmt.Printf("[Edge API] Instance deletion started, Id: %s\n", instance.GetId()) + + // Wait for Edge instance deletion + _, err = wait.DeleteInstanceWaitHandler(ctx, client, projectId, region, instance.GetId()).SetTimeout(10 * time.Minute).WaitWithContext(ctx) + if err != nil { + fmt.Fprintf(os.Stderr, "[Edge API] Failed wait for Instance deletion: %v\n", err) + os.Exit(1) + } + fmt.Println("[Edge API] Instance deleted") +} diff --git a/examples/edge/go.mod b/examples/edge/go.mod new file mode 100644 index 000000000..8418b7d0a --- /dev/null +++ b/examples/edge/go.mod @@ -0,0 +1,14 @@ +module github.com/stackitcloud/stackit-sdk-go/examples/edge + +go 1.21 + +require ( + github.com/stackitcloud/stackit-sdk-go/core v0.20.1 + github.com/stackitcloud/stackit-sdk-go/services/edge v0.2.0 + gopkg.in/yaml.v3 v3.0.1 +) + +require ( + github.com/golang-jwt/jwt/v5 v5.3.0 // indirect + github.com/google/uuid v1.6.0 // indirect +) diff --git a/examples/edge/go.sum b/examples/edge/go.sum new file mode 100644 index 000000000..4eb9c09ac --- /dev/null +++ b/examples/edge/go.sum @@ -0,0 +1,14 @@ +github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo= +github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +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.20.1 h1:odiuhhRXmxvEvnVTeZSN9u98edvw2Cd3DcnkepncP3M= +github.com/stackitcloud/stackit-sdk-go/core v0.20.1/go.mod h1:fqto7M82ynGhEnpZU6VkQKYWYoFG5goC076JWXTUPRQ= +github.com/stackitcloud/stackit-sdk-go/services/edge v0.2.0 h1:ElmnEg3V4MisAgqqJFxl3nCmKraxbHtN+vv1DNiWYfM= +github.com/stackitcloud/stackit-sdk-go/services/edge v0.2.0/go.mod h1:tFDkVkK+ESBTiH2XIcMPPR/pJJmeqT1VNDghg+ZxfMI= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/go.work b/go.work index 913390529..edfc1f700 100644 --- a/go.work +++ b/go.work @@ -8,6 +8,7 @@ use ( ./examples/backgroundrefresh ./examples/configuration ./examples/dns + ./examples/edge ./examples/errorhandling ./examples/iaas ./examples/intake