Skip to content

Commit 737b9f3

Browse files
feat(edge): add edge-cloud examples (#4179)
1 parent 3a9e84c commit 737b9f3

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

examples/edge/edge.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}

examples/edge/go.mod

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/stackitcloud/stackit-sdk-go/examples/edge
2+
3+
go 1.21
4+
5+
require (
6+
github.com/stackitcloud/stackit-sdk-go/core v0.20.1
7+
github.com/stackitcloud/stackit-sdk-go/services/edge v0.2.0
8+
gopkg.in/yaml.v3 v3.0.1
9+
)
10+
11+
require (
12+
github.com/golang-jwt/jwt/v5 v5.3.0 // indirect
13+
github.com/google/uuid v1.6.0 // indirect
14+
)

examples/edge/go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
2+
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
3+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
4+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
5+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
6+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
7+
github.com/stackitcloud/stackit-sdk-go/core v0.20.1 h1:odiuhhRXmxvEvnVTeZSN9u98edvw2Cd3DcnkepncP3M=
8+
github.com/stackitcloud/stackit-sdk-go/core v0.20.1/go.mod h1:fqto7M82ynGhEnpZU6VkQKYWYoFG5goC076JWXTUPRQ=
9+
github.com/stackitcloud/stackit-sdk-go/services/edge v0.2.0 h1:ElmnEg3V4MisAgqqJFxl3nCmKraxbHtN+vv1DNiWYfM=
10+
github.com/stackitcloud/stackit-sdk-go/services/edge v0.2.0/go.mod h1:tFDkVkK+ESBTiH2XIcMPPR/pJJmeqT1VNDghg+ZxfMI=
11+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
12+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
13+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
14+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

go.work

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use (
88
./examples/backgroundrefresh
99
./examples/configuration
1010
./examples/dns
11+
./examples/edge
1112
./examples/errorhandling
1213
./examples/iaas
1314
./examples/intake

0 commit comments

Comments
 (0)