|
| 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 | +} |
0 commit comments