Skip to content

Commit 3394364

Browse files
IaaS Server example (#866)
* Add server waiters * Add waiter unit tests * Fix litn * Add iaasalpha server example * Import latest iaasalpha! * Re-structure examples
1 parent b5e9485 commit 3394364

File tree

4 files changed

+118
-7
lines changed

4 files changed

+118
-7
lines changed

examples/iaasalpha/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.18
44

55
require (
66
github.com/stackitcloud/stackit-sdk-go/core v0.13.0
7-
github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.3-alpha
7+
github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.6-alpha
88
)
99

1010
require (

examples/iaasalpha/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
55
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
66
github.com/stackitcloud/stackit-sdk-go/core v0.13.0 h1:BtJT2WXqZdexPPQi/HPUIr8g4JUPOCheh6J9dxiCQ4Q=
77
github.com/stackitcloud/stackit-sdk-go/core v0.13.0/go.mod h1:mDX1mSTsB3mP+tNBGcFNx6gH1mGBN4T+dVt+lcw7nlw=
8-
github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.3-alpha h1:cwNZwtvb7diYJhjNmxuX8IxaqOq1BNDSjtG3c9Ne1ig=
9-
github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.3-alpha/go.mod h1:b4KR6r+yWS2hsDkz6ebRqxgadB+ZsAZcG0oDfv5jeaY=
8+
github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.6-alpha h1:XUYncbRKaqbG76OzoSugfvPHp6+0A86JJxW2T3CLT2E=
9+
github.com/stackitcloud/stackit-sdk-go/services/iaasalpha v0.1.6-alpha/go.mod h1:b4KR6r+yWS2hsDkz6ebRqxgadB+ZsAZcG0oDfv5jeaY=
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+
8+
"github.com/stackitcloud/stackit-sdk-go/core/config"
9+
"github.com/stackitcloud/stackit-sdk-go/core/utils"
10+
"github.com/stackitcloud/stackit-sdk-go/services/iaasalpha"
11+
"github.com/stackitcloud/stackit-sdk-go/services/iaasalpha/wait"
12+
)
13+
14+
func main() {
15+
// Specify the organization ID and project ID
16+
projectId := "PROJECT_ID"
17+
18+
// Create a new API client, that uses default authentication and configuration
19+
iaasalphaClient, err := iaasalpha.NewAPIClient(
20+
config.WithRegion("eu01"),
21+
)
22+
if err != nil {
23+
fmt.Fprintf(os.Stderr, "[iaasalpha API] Creating API client: %v\n", err)
24+
os.Exit(1)
25+
}
26+
27+
servers, err := iaasalphaClient.ListServers(context.Background(), projectId).Execute()
28+
29+
if err != nil {
30+
fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `ListServers`: %v\n", err)
31+
} else {
32+
fmt.Printf("[iaasalpha API] Number of servers: %v\n", len(*servers.Items))
33+
}
34+
35+
// Create a server
36+
createServerPayload := iaasalpha.CreateServerPayload{
37+
Name: utils.Ptr("example-server"),
38+
AvailabilityZone: utils.Ptr("eu01-1"),
39+
MachineType: utils.Ptr("g1.1"),
40+
BootVolume: &iaasalpha.CreateServerPayloadBootVolume{
41+
Size: utils.Ptr(int64(64)),
42+
Source: &iaasalpha.BootVolumeSource{
43+
Id: utils.Ptr("IMAGE_ID"),
44+
Type: utils.Ptr("image"),
45+
},
46+
},
47+
}
48+
server, err := iaasalphaClient.CreateServer(context.Background(), projectId).CreateServerPayload(createServerPayload).Execute()
49+
if err != nil {
50+
fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `CreateServer`: %v\n", err)
51+
} else {
52+
fmt.Printf("[iaasalpha API] Triggered creation of server with ID %q.\n", *server.Id)
53+
}
54+
55+
// Wait for creation of the server
56+
server, err = wait.CreateServerWaitHandler(context.Background(), iaasalphaClient, projectId, *server.Id).WaitWithContext(context.Background())
57+
if err != nil {
58+
fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for creation: %v\n", err)
59+
os.Exit(1)
60+
}
61+
62+
fmt.Printf("[iaasalpha API] Server %q has been successfully created.\n", *server.Id)
63+
64+
// Update a server
65+
updateServerPayload := iaasalpha.V1alpha1UpdateServerPayload{
66+
Name: utils.Ptr("renamed"),
67+
}
68+
server, err = iaasalphaClient.V1alpha1UpdateServer(context.Background(), projectId, *server.Id).V1alpha1UpdateServerPayload(updateServerPayload).Execute()
69+
if err != nil {
70+
fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `UpdateServer`: %v\n", err)
71+
}
72+
73+
fmt.Printf("[iaasalpha API] Server %q has been successfully updated.\n", *server.Id)
74+
75+
// Resize a server
76+
resizeServerPayload := iaasalpha.ResizeServerPayload{
77+
MachineType: utils.Ptr("c1.2"),
78+
}
79+
80+
err = iaasalphaClient.ResizeServer(context.Background(), projectId, *server.Id).ResizeServerPayload(resizeServerPayload).Execute()
81+
if err != nil {
82+
fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `ResizeServer`: %v\n", err)
83+
} else {
84+
fmt.Printf("[iaasalpha API] Triggered resize of server with ID %q.\n", *server.Id)
85+
}
86+
87+
server, err = wait.ResizeServerWaitHandler(context.Background(), iaasalphaClient, projectId, *server.Id).WaitWithContext(context.Background())
88+
if err != nil {
89+
fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for resize: %v\n", err)
90+
os.Exit(1)
91+
}
92+
93+
fmt.Printf("[iaasalpha API] Server %q has been successfully resized.\n", *server.Id)
94+
95+
// Delete a server
96+
err = iaasalphaClient.DeleteServer(context.Background(), projectId, *server.Id).Execute()
97+
if err != nil {
98+
fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `DeleteServer`: %v\n", err)
99+
} else {
100+
fmt.Printf("[iaasalpha API] Triggered deletion of server with ID %q.\n", *server.Id)
101+
}
102+
103+
// Wait for deletion of the server
104+
_, err = wait.DeleteServerWaitHandler(context.Background(), iaasalphaClient, projectId, *server.Id).WaitWithContext(context.Background())
105+
if err != nil {
106+
fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when waiting for deletion: %v\n", err)
107+
os.Exit(1)
108+
}
109+
110+
fmt.Printf("[iaasalpha API] Server %q has been successfully deleted.\n", *server.Id)
111+
}

examples/iaasalpha/iaas.go renamed to examples/iaasalpha/volume/volume.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func main() {
5252
os.Exit(1)
5353
}
5454

55-
fmt.Printf("[iaasalpha API] Network volume %q has been successfully created.\n", *volume.Id)
55+
fmt.Printf("[iaasalpha API] Volume %q has been successfully created.\n", *volume.Id)
5656

5757
// Update a volume
5858
updateVolumePayload := iaasalpha.UpdateVolumePayload{
@@ -63,7 +63,7 @@ func main() {
6363
fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `UpdateVolume`: %v\n", err)
6464
}
6565

66-
fmt.Printf("[iaasalpha API] Network volume %q has been successfully updated.\n", *volume.Id)
66+
fmt.Printf("[iaasalpha API] Volume %q has been successfully updated.\n", *volume.Id)
6767

6868
// Resize a volume
6969
resizeVolumePayload := iaasalpha.ResizeVolumePayload{
@@ -74,7 +74,7 @@ func main() {
7474
fmt.Fprintf(os.Stderr, "[iaasalpha API] Error when calling `ResizeVolume`: %v\n", err)
7575
}
7676

77-
fmt.Printf("[iaasalpha API] Network volume %q has been successfully resized.\n", *volume.Id)
77+
fmt.Printf("[iaasalpha API] Volume %q has been successfully resized.\n", *volume.Id)
7878

7979
// Delete a volume
8080
err = iaasalphaClient.DeleteVolume(context.Background(), projectId, *volume.Id).Execute()
@@ -91,5 +91,5 @@ func main() {
9191
os.Exit(1)
9292
}
9393

94-
fmt.Printf("[iaasalpha API] Network volume %q has been successfully deleted.\n", *volume.Id)
94+
fmt.Printf("[iaasalpha API] Volume %q has been successfully deleted.\n", *volume.Id)
9595
}

0 commit comments

Comments
 (0)