diff --git a/examples/sfs/go.mod b/examples/sfs/go.mod new file mode 100644 index 000000000..d3700d10c --- /dev/null +++ b/examples/sfs/go.mod @@ -0,0 +1,13 @@ +module github.com/stackitcloud/stackit-sdk-go/examples/sfs + +go 1.21 + +require ( + github.com/stackitcloud/stackit-sdk-go/core v0.20.1 + github.com/stackitcloud/stackit-sdk-go/services/sfs v0.1.0 +) + +require ( + github.com/golang-jwt/jwt/v5 v5.3.0 // indirect + github.com/google/uuid v1.6.0 // indirect +) diff --git a/examples/sfs/go.sum b/examples/sfs/go.sum new file mode 100644 index 000000000..a705126dc --- /dev/null +++ b/examples/sfs/go.sum @@ -0,0 +1,10 @@ +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/sfs v0.1.0 h1:RRzwDicugcWtEEBRSVVz+Cwb2OAsxodmO4ZJoftGfB0= +github.com/stackitcloud/stackit-sdk-go/services/sfs v0.1.0/go.mod h1:XHOtGgBwwCqPSoQt2ojIRb/BeOd4kICwb9RuMXXFGt8= diff --git a/examples/sfs/resourcepool/resourcepool.go b/examples/sfs/resourcepool/resourcepool.go new file mode 100644 index 000000000..a0a4235de --- /dev/null +++ b/examples/sfs/resourcepool/resourcepool.go @@ -0,0 +1,99 @@ +package main + +import ( + "context" + "fmt" + "os" + + "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/utils" + "github.com/stackitcloud/stackit-sdk-go/services/sfs" + "github.com/stackitcloud/stackit-sdk-go/services/sfs/wait" +) + +func main() { + // Specify the project ID and region + projectId := "PROJECT_ID" // the uuid of your STACKIT project + region := "eu01" + + // Create a new API client, that uses default authentication and configuration + sfsClient, err := sfs.NewAPIClient(config.WithRegion(region)) + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Creating API client: %v\n", err) + os.Exit(1) + } + + // Create a resource pool + createResourcePoolPayload := sfs.CreateResourcePoolPayload{ + AvailabilityZone: utils.Ptr("eu01-m"), + Name: utils.Ptr("example-resourcepool-test"), + PerformanceClass: utils.Ptr("Standard"), + IpAcl: &[]string{"192.168.42.1/32", "192.168.42.2/32"}, + SizeGigabytes: utils.Ptr(int64(512)), + } + + resourcePool, err := sfsClient.CreateResourcePool(context.Background(), projectId, region).CreateResourcePoolPayload(createResourcePoolPayload).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error when calling `CreateResourcePool`: %v\n", err) + os.Exit(1) + } + + if resourcePool == nil || resourcePool.ResourcePool == nil || resourcePool.ResourcePool.Id == nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error creating resource pool. Calling API: Incomplete response") + } + + fmt.Printf("[sfs API] Triggered creation of resource pool with ID %q.\n", *resourcePool.ResourcePool.Id) + fmt.Printf("[sfs API] Current state of the resource pool: %q\n", *resourcePool.ResourcePool.State) + fmt.Println("[sfs API] Waiting for resource pool to be created...") + + resourcePoolResp, err := wait.CreateResourcePoolWaitHandler(context.Background(), sfsClient, projectId, region, *resourcePool.ResourcePool.Id).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error when waiting for creation: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[sfs API] Resource pool has been successfully created.\n") + + // Describe the resource pool + getResourcePoolResp, err := sfsClient.GetResourcePool(context.Background(), projectId, region, *resourcePoolResp.ResourcePool.Id).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error when calling `GetResoucePool`: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[sfs API] Resource pool %s with ID %d was received.\n", *getResourcePoolResp.ResourcePool.Name, getResourcePoolResp.ResourcePool.Id) + + // Update the resource pool + updateResourcePoolPayload := sfs.UpdateResourcePoolPayload{ + SizeGigabytes: utils.Ptr(int64(600)), + } + + updateResourcePool, err := sfsClient.UpdateResourcePool(context.Background(), projectId, region, *resourcePoolResp.ResourcePool.Id).UpdateResourcePoolPayload(updateResourcePoolPayload).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error when calling `UpdateResourcePool`: %v\n", err) + os.Exit(1) + } + + _, err = wait.UpdateResourcePoolWaitHandler(context.Background(), sfsClient, projectId, region, *resourcePoolResp.ResourcePool.Id).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error when waiting for update: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[sfs API] Resource pool has been successfully updated to new size %d.\n", *updateResourcePool.ResourcePool.Space.SizeGigabytes) + + // Delete the resource pool + _, err = sfsClient.DeleteResourcePool(context.Background(), projectId, region, *resourcePoolResp.ResourcePool.Id).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error when calling `DeleteResourcePool`: %v\n", err) + os.Exit(1) + } + + _, err = wait.DeleteResourcePoolWaitHandler(context.Background(), sfsClient, projectId, region, *resourcePoolResp.ResourcePool.Id).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error when waiting for deletion: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[sfs API] Resource pool has been successfully deleted.\n") +} diff --git a/examples/sfs/share/share.go b/examples/sfs/share/share.go new file mode 100644 index 000000000..0ca1d7849 --- /dev/null +++ b/examples/sfs/share/share.go @@ -0,0 +1,138 @@ +package main + +import ( + "context" + "fmt" + "os" + + "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/utils" + "github.com/stackitcloud/stackit-sdk-go/services/sfs" + "github.com/stackitcloud/stackit-sdk-go/services/sfs/wait" +) + +func main() { + // Specify the project ID and region + projectId := "PROJECT_ID" // the uuid of your STACKIT project + region := "eu01" + + // Create a new API client, that uses default authentication and configuration + sfsClient, err := sfs.NewAPIClient(config.WithRegion(region)) + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Creating API client: %v\n", err) + os.Exit(1) + } + + // Create a resource pool in order to create a share + createResourcePoolPayload := sfs.CreateResourcePoolPayload{ + AvailabilityZone: utils.Ptr("eu01-m"), + Name: utils.Ptr("example-resourcepool-share-test"), + PerformanceClass: utils.Ptr("Standard"), + IpAcl: &[]string{"192.168.42.1/32", "192.168.42.2/32"}, + SizeGigabytes: utils.Ptr(int64(512)), + } + + resourcePool, err := sfsClient.CreateResourcePool(context.Background(), projectId, region).CreateResourcePoolPayload(createResourcePoolPayload).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error when calling `CreateResourcePool`: %v\n", err) + os.Exit(1) + } + + if resourcePool == nil || resourcePool.ResourcePool == nil || resourcePool.ResourcePool.Id == nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error creating resource pool. Calling API: Incomplete response") + } + + fmt.Printf("[sfs API] Triggered creation of resource pool with ID %q.\n", *resourcePool.ResourcePool.Id) + fmt.Printf("[sfs API] Current state of the resource pool: %q\n", *resourcePool.ResourcePool.State) + fmt.Println("[sfs API] Waiting for resource pool to be created...") + + _, err = wait.CreateResourcePoolWaitHandler(context.Background(), sfsClient, projectId, region, *resourcePool.ResourcePool.Id).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error when waiting for creation of resource pool: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[sfs API] Resource pool has been successfully created.\n") + + // Create a share for the resource pool + createSharePayload := sfs.CreateSharePayload{ + Name: utils.Ptr("example-share-name"), + SpaceHardLimitGigabytes: utils.Ptr(int64(100)), + } + + share, err := sfsClient.CreateShare(context.Background(), projectId, region, *resourcePool.ResourcePool.Id).CreateSharePayload(createSharePayload).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error when waiting calling `CreateShare`: %v\n", err) + os.Exit(1) + } + + if share == nil || share.Share == nil || share.Share.Id == nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error creating share. Calling API: Incomplete response") + } + + _, err = wait.CreateShareWaitHandler(context.Background(), sfsClient, projectId, region, *resourcePool.ResourcePool.Id, *share.Share.Id).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error when waiting for creation: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[sfs API] Share has been successfully created.\n") + + // Describe the share + getShareResp, err := sfsClient.GetShare(context.Background(), projectId, region, *resourcePool.ResourcePool.Id, *share.Share.Id).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error when calling `GetShare`: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[sfs API] Share %s with ID %d was received.\n", *getShareResp.Share.Name, getShareResp.Share.Id) + + // Update the share + updateSharePayload := sfs.UpdateSharePayload{ + SpaceHardLimitGigabytes: utils.Ptr(int64(150)), + } + + updateShare, err := sfsClient.UpdateShare(context.Background(), projectId, region, *resourcePool.ResourcePool.Id, *share.Share.Id).UpdateSharePayload(updateSharePayload).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error when calling `UpdateShare`: %v\n", err) + os.Exit(1) + } + + _, err = wait.UpdateShareWaitHandler(context.Background(), sfsClient, projectId, region, *resourcePool.ResourcePool.Id, *share.Share.Id).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error when waiting for update: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[sfs API] Share has been successfully updated to new limit %d.\n", *updateShare.Share.SpaceHardLimitGigabytes) + + // Delete share + _, err = sfsClient.DeleteShare(context.Background(), projectId, region, *resourcePool.ResourcePool.Id, *share.Share.Id).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error when calling `DeleteShare`: %v\n", err) + os.Exit(1) + } + + _, err = wait.DeleteShareWaitHandler(context.Background(), sfsClient, projectId, region, *resourcePool.ResourcePool.Id, *share.Share.Id).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error when waiting for deletion: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[sfs API] Share has been successfully deleted.\n") + + // Delete resource pool + _, err = sfsClient.DeleteResourcePool(context.Background(), projectId, region, *resourcePool.ResourcePool.Id).Execute() + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error when calling `DeleteResourcePool`: %v\n", err) + os.Exit(1) + } + + _, err = wait.DeleteResourcePoolWaitHandler(context.Background(), sfsClient, projectId, region, *resourcePool.ResourcePool.Id).WaitWithContext(context.Background()) + if err != nil { + fmt.Fprintf(os.Stderr, "[sfs API] Error when waiting for deletion: %v\n", err) + os.Exit(1) + } + + fmt.Printf("[sfs API] Resource pool has been successfully deleted.\n") +} diff --git a/go.work b/go.work index 159a66756..633580965 100644 --- a/go.work +++ b/go.work @@ -29,6 +29,7 @@ use ( ./examples/secretsmanager ./examples/serviceaccount ./examples/serviceenablement + ./examples/sfs ./examples/ske ./examples/sqlserverflex ./examples/waiter