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
111 changes: 111 additions & 0 deletions examples/edge/edge.go
Original file line number Diff line number Diff line change
@@ -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")
}
14 changes: 14 additions & 0 deletions examples/edge/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
14 changes: 14 additions & 0 deletions examples/edge/go.sum
Original file line number Diff line number Diff line change
@@ -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=
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use (
./examples/backgroundrefresh
./examples/configuration
./examples/dns
./examples/edge
./examples/errorhandling
./examples/iaas
./examples/intake
Expand Down
Loading