Skip to content

Commit cd02eec

Browse files
authored
chore(sfs): Add examples (#4117)
Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent a7ed8fd commit cd02eec

File tree

5 files changed

+261
-0
lines changed

5 files changed

+261
-0
lines changed

examples/sfs/go.mod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module github.com/stackitcloud/stackit-sdk-go/examples/sfs
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/sfs v0.1.0
8+
)
9+
10+
require (
11+
github.com/golang-jwt/jwt/v5 v5.3.0 // indirect
12+
github.com/google/uuid v1.6.0 // indirect
13+
)

examples/sfs/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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/sfs v0.1.0 h1:RRzwDicugcWtEEBRSVVz+Cwb2OAsxodmO4ZJoftGfB0=
10+
github.com/stackitcloud/stackit-sdk-go/services/sfs v0.1.0/go.mod h1:XHOtGgBwwCqPSoQt2ojIRb/BeOd4kICwb9RuMXXFGt8=
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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/sfs"
11+
"github.com/stackitcloud/stackit-sdk-go/services/sfs/wait"
12+
)
13+
14+
func main() {
15+
// Specify the project ID and region
16+
projectId := "PROJECT_ID" // the uuid of your STACKIT project
17+
region := "eu01"
18+
19+
// Create a new API client, that uses default authentication and configuration
20+
sfsClient, err := sfs.NewAPIClient(config.WithRegion(region))
21+
if err != nil {
22+
fmt.Fprintf(os.Stderr, "[sfs API] Creating API client: %v\n", err)
23+
os.Exit(1)
24+
}
25+
26+
// Create a resource pool
27+
createResourcePoolPayload := sfs.CreateResourcePoolPayload{
28+
AvailabilityZone: utils.Ptr("eu01-m"),
29+
Name: utils.Ptr("example-resourcepool-test"),
30+
PerformanceClass: utils.Ptr("Standard"),
31+
IpAcl: &[]string{"192.168.42.1/32", "192.168.42.2/32"},
32+
SizeGigabytes: utils.Ptr(int64(512)),
33+
}
34+
35+
resourcePool, err := sfsClient.CreateResourcePool(context.Background(), projectId, region).CreateResourcePoolPayload(createResourcePoolPayload).Execute()
36+
if err != nil {
37+
fmt.Fprintf(os.Stderr, "[sfs API] Error when calling `CreateResourcePool`: %v\n", err)
38+
os.Exit(1)
39+
}
40+
41+
if resourcePool == nil || resourcePool.ResourcePool == nil || resourcePool.ResourcePool.Id == nil {
42+
fmt.Fprintf(os.Stderr, "[sfs API] Error creating resource pool. Calling API: Incomplete response")
43+
}
44+
45+
fmt.Printf("[sfs API] Triggered creation of resource pool with ID %q.\n", *resourcePool.ResourcePool.Id)
46+
fmt.Printf("[sfs API] Current state of the resource pool: %q\n", *resourcePool.ResourcePool.State)
47+
fmt.Println("[sfs API] Waiting for resource pool to be created...")
48+
49+
resourcePoolResp, err := wait.CreateResourcePoolWaitHandler(context.Background(), sfsClient, projectId, region, *resourcePool.ResourcePool.Id).WaitWithContext(context.Background())
50+
if err != nil {
51+
fmt.Fprintf(os.Stderr, "[sfs API] Error when waiting for creation: %v\n", err)
52+
os.Exit(1)
53+
}
54+
55+
fmt.Printf("[sfs API] Resource pool has been successfully created.\n")
56+
57+
// Describe the resource pool
58+
getResourcePoolResp, err := sfsClient.GetResourcePool(context.Background(), projectId, region, *resourcePoolResp.ResourcePool.Id).Execute()
59+
if err != nil {
60+
fmt.Fprintf(os.Stderr, "[sfs API] Error when calling `GetResoucePool`: %v\n", err)
61+
os.Exit(1)
62+
}
63+
64+
fmt.Printf("[sfs API] Resource pool %s with ID %d was received.\n", *getResourcePoolResp.ResourcePool.Name, getResourcePoolResp.ResourcePool.Id)
65+
66+
// Update the resource pool
67+
updateResourcePoolPayload := sfs.UpdateResourcePoolPayload{
68+
SizeGigabytes: utils.Ptr(int64(600)),
69+
}
70+
71+
updateResourcePool, err := sfsClient.UpdateResourcePool(context.Background(), projectId, region, *resourcePoolResp.ResourcePool.Id).UpdateResourcePoolPayload(updateResourcePoolPayload).Execute()
72+
if err != nil {
73+
fmt.Fprintf(os.Stderr, "[sfs API] Error when calling `UpdateResourcePool`: %v\n", err)
74+
os.Exit(1)
75+
}
76+
77+
_, err = wait.UpdateResourcePoolWaitHandler(context.Background(), sfsClient, projectId, region, *resourcePoolResp.ResourcePool.Id).WaitWithContext(context.Background())
78+
if err != nil {
79+
fmt.Fprintf(os.Stderr, "[sfs API] Error when waiting for update: %v\n", err)
80+
os.Exit(1)
81+
}
82+
83+
fmt.Printf("[sfs API] Resource pool has been successfully updated to new size %d.\n", *updateResourcePool.ResourcePool.Space.SizeGigabytes)
84+
85+
// Delete the resource pool
86+
_, err = sfsClient.DeleteResourcePool(context.Background(), projectId, region, *resourcePoolResp.ResourcePool.Id).Execute()
87+
if err != nil {
88+
fmt.Fprintf(os.Stderr, "[sfs API] Error when calling `DeleteResourcePool`: %v\n", err)
89+
os.Exit(1)
90+
}
91+
92+
_, err = wait.DeleteResourcePoolWaitHandler(context.Background(), sfsClient, projectId, region, *resourcePoolResp.ResourcePool.Id).WaitWithContext(context.Background())
93+
if err != nil {
94+
fmt.Fprintf(os.Stderr, "[sfs API] Error when waiting for deletion: %v\n", err)
95+
os.Exit(1)
96+
}
97+
98+
fmt.Printf("[sfs API] Resource pool has been successfully deleted.\n")
99+
}

examples/sfs/share/share.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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/sfs"
11+
"github.com/stackitcloud/stackit-sdk-go/services/sfs/wait"
12+
)
13+
14+
func main() {
15+
// Specify the project ID and region
16+
projectId := "PROJECT_ID" // the uuid of your STACKIT project
17+
region := "eu01"
18+
19+
// Create a new API client, that uses default authentication and configuration
20+
sfsClient, err := sfs.NewAPIClient(config.WithRegion(region))
21+
if err != nil {
22+
fmt.Fprintf(os.Stderr, "[sfs API] Creating API client: %v\n", err)
23+
os.Exit(1)
24+
}
25+
26+
// Create a resource pool in order to create a share
27+
createResourcePoolPayload := sfs.CreateResourcePoolPayload{
28+
AvailabilityZone: utils.Ptr("eu01-m"),
29+
Name: utils.Ptr("example-resourcepool-share-test"),
30+
PerformanceClass: utils.Ptr("Standard"),
31+
IpAcl: &[]string{"192.168.42.1/32", "192.168.42.2/32"},
32+
SizeGigabytes: utils.Ptr(int64(512)),
33+
}
34+
35+
resourcePool, err := sfsClient.CreateResourcePool(context.Background(), projectId, region).CreateResourcePoolPayload(createResourcePoolPayload).Execute()
36+
if err != nil {
37+
fmt.Fprintf(os.Stderr, "[sfs API] Error when calling `CreateResourcePool`: %v\n", err)
38+
os.Exit(1)
39+
}
40+
41+
if resourcePool == nil || resourcePool.ResourcePool == nil || resourcePool.ResourcePool.Id == nil {
42+
fmt.Fprintf(os.Stderr, "[sfs API] Error creating resource pool. Calling API: Incomplete response")
43+
}
44+
45+
fmt.Printf("[sfs API] Triggered creation of resource pool with ID %q.\n", *resourcePool.ResourcePool.Id)
46+
fmt.Printf("[sfs API] Current state of the resource pool: %q\n", *resourcePool.ResourcePool.State)
47+
fmt.Println("[sfs API] Waiting for resource pool to be created...")
48+
49+
_, err = wait.CreateResourcePoolWaitHandler(context.Background(), sfsClient, projectId, region, *resourcePool.ResourcePool.Id).WaitWithContext(context.Background())
50+
if err != nil {
51+
fmt.Fprintf(os.Stderr, "[sfs API] Error when waiting for creation of resource pool: %v\n", err)
52+
os.Exit(1)
53+
}
54+
55+
fmt.Printf("[sfs API] Resource pool has been successfully created.\n")
56+
57+
// Create a share for the resource pool
58+
createSharePayload := sfs.CreateSharePayload{
59+
Name: utils.Ptr("example-share-name"),
60+
SpaceHardLimitGigabytes: utils.Ptr(int64(100)),
61+
}
62+
63+
share, err := sfsClient.CreateShare(context.Background(), projectId, region, *resourcePool.ResourcePool.Id).CreateSharePayload(createSharePayload).Execute()
64+
if err != nil {
65+
fmt.Fprintf(os.Stderr, "[sfs API] Error when waiting calling `CreateShare`: %v\n", err)
66+
os.Exit(1)
67+
}
68+
69+
if share == nil || share.Share == nil || share.Share.Id == nil {
70+
fmt.Fprintf(os.Stderr, "[sfs API] Error creating share. Calling API: Incomplete response")
71+
}
72+
73+
_, err = wait.CreateShareWaitHandler(context.Background(), sfsClient, projectId, region, *resourcePool.ResourcePool.Id, *share.Share.Id).WaitWithContext(context.Background())
74+
if err != nil {
75+
fmt.Fprintf(os.Stderr, "[sfs API] Error when waiting for creation: %v\n", err)
76+
os.Exit(1)
77+
}
78+
79+
fmt.Printf("[sfs API] Share has been successfully created.\n")
80+
81+
// Describe the share
82+
getShareResp, err := sfsClient.GetShare(context.Background(), projectId, region, *resourcePool.ResourcePool.Id, *share.Share.Id).Execute()
83+
if err != nil {
84+
fmt.Fprintf(os.Stderr, "[sfs API] Error when calling `GetShare`: %v\n", err)
85+
os.Exit(1)
86+
}
87+
88+
fmt.Printf("[sfs API] Share %s with ID %d was received.\n", *getShareResp.Share.Name, getShareResp.Share.Id)
89+
90+
// Update the share
91+
updateSharePayload := sfs.UpdateSharePayload{
92+
SpaceHardLimitGigabytes: utils.Ptr(int64(150)),
93+
}
94+
95+
updateShare, err := sfsClient.UpdateShare(context.Background(), projectId, region, *resourcePool.ResourcePool.Id, *share.Share.Id).UpdateSharePayload(updateSharePayload).Execute()
96+
if err != nil {
97+
fmt.Fprintf(os.Stderr, "[sfs API] Error when calling `UpdateShare`: %v\n", err)
98+
os.Exit(1)
99+
}
100+
101+
_, err = wait.UpdateShareWaitHandler(context.Background(), sfsClient, projectId, region, *resourcePool.ResourcePool.Id, *share.Share.Id).WaitWithContext(context.Background())
102+
if err != nil {
103+
fmt.Fprintf(os.Stderr, "[sfs API] Error when waiting for update: %v\n", err)
104+
os.Exit(1)
105+
}
106+
107+
fmt.Printf("[sfs API] Share has been successfully updated to new limit %d.\n", *updateShare.Share.SpaceHardLimitGigabytes)
108+
109+
// Delete share
110+
_, err = sfsClient.DeleteShare(context.Background(), projectId, region, *resourcePool.ResourcePool.Id, *share.Share.Id).Execute()
111+
if err != nil {
112+
fmt.Fprintf(os.Stderr, "[sfs API] Error when calling `DeleteShare`: %v\n", err)
113+
os.Exit(1)
114+
}
115+
116+
_, err = wait.DeleteShareWaitHandler(context.Background(), sfsClient, projectId, region, *resourcePool.ResourcePool.Id, *share.Share.Id).WaitWithContext(context.Background())
117+
if err != nil {
118+
fmt.Fprintf(os.Stderr, "[sfs API] Error when waiting for deletion: %v\n", err)
119+
os.Exit(1)
120+
}
121+
122+
fmt.Printf("[sfs API] Share has been successfully deleted.\n")
123+
124+
// Delete resource pool
125+
_, err = sfsClient.DeleteResourcePool(context.Background(), projectId, region, *resourcePool.ResourcePool.Id).Execute()
126+
if err != nil {
127+
fmt.Fprintf(os.Stderr, "[sfs API] Error when calling `DeleteResourcePool`: %v\n", err)
128+
os.Exit(1)
129+
}
130+
131+
_, err = wait.DeleteResourcePoolWaitHandler(context.Background(), sfsClient, projectId, region, *resourcePool.ResourcePool.Id).WaitWithContext(context.Background())
132+
if err != nil {
133+
fmt.Fprintf(os.Stderr, "[sfs API] Error when waiting for deletion: %v\n", err)
134+
os.Exit(1)
135+
}
136+
137+
fmt.Printf("[sfs API] Resource pool has been successfully deleted.\n")
138+
}

go.work

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use (
2929
./examples/secretsmanager
3030
./examples/serviceaccount
3131
./examples/serviceenablement
32+
./examples/sfs
3233
./examples/ske
3334
./examples/sqlserverflex
3435
./examples/waiter

0 commit comments

Comments
 (0)