|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/stackitcloud/stackit-sdk-go/core/utils" |
| 9 | + postgresflex "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3beta1api" |
| 10 | + "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3beta1api/wait" |
| 11 | +) |
| 12 | + |
| 13 | +func main() { |
| 14 | + const ( |
| 15 | + projectId = "PROJECT_ID" // the uuid of your STACKIT project |
| 16 | + region = "eu01" // Specify the region |
| 17 | + // Specify instance configuration options |
| 18 | + version = "VERSION" |
| 19 | + // You can find a valid flavorId, by calling this API https://docs.api.stackit.cloud/documentation/postgres-flex-service/version/v3alpha1#tag/Flavor/operation/getFlavorsRequest |
| 20 | + // or using postgresflexClient.DefaultAPI.ListFlavors(ctx, projectId, region).Execute() |
| 21 | + flavorId = "FLAVOR_ID" |
| 22 | + ) |
| 23 | + |
| 24 | + ctx := context.Background() |
| 25 | + |
| 26 | + // Create a new API client, that uses default authentication and configuration |
| 27 | + postgresflexClient, err := postgresflex.NewAPIClient() |
| 28 | + if err != nil { |
| 29 | + fmt.Fprintf(os.Stderr, "Creating API client: %v\n", err) |
| 30 | + os.Exit(1) |
| 31 | + } |
| 32 | + |
| 33 | + // List the PostgreSQL Flex instances for your project |
| 34 | + getInstancesResp, err := postgresflexClient.DefaultAPI.ListInstances(ctx, projectId, region).Execute() |
| 35 | + |
| 36 | + if err != nil { |
| 37 | + fmt.Fprintf(os.Stderr, "Error when calling `ListInstances`: %v\n", err) |
| 38 | + os.Exit(1) |
| 39 | + } |
| 40 | + fmt.Printf("Number of instances: %v\n", len(getInstancesResp.Instances)) |
| 41 | + |
| 42 | + // Create an instance |
| 43 | + createInstancePayload := postgresflex.CreateInstancePayload{ |
| 44 | + Name: "my-instance", |
| 45 | + FlavorId: flavorId, |
| 46 | + Version: version, |
| 47 | + BackupSchedule: "0 2 * * *", |
| 48 | + RetentionDays: *postgresflex.NewNullableInt32(utils.Ptr(int32(35))), |
| 49 | + Network: postgresflex.InstanceNetworkCreate{ |
| 50 | + Acl: []string{"1.2.3.4/32"}, |
| 51 | + }, |
| 52 | + Storage: postgresflex.StorageCreate{ |
| 53 | + Class: utils.Ptr("premium-perf2-stackit"), |
| 54 | + Size: 5, |
| 55 | + }, |
| 56 | + } |
| 57 | + instance, err := postgresflexClient.DefaultAPI.CreateInstance(ctx, projectId, region).CreateInstancePayload(createInstancePayload).Execute() |
| 58 | + if err != nil { |
| 59 | + fmt.Fprintf(os.Stderr, "Error creating PostgreSQL Flex instance: %v\n", err) |
| 60 | + os.Exit(1) |
| 61 | + } |
| 62 | + fmt.Printf("Triggered PostgreSQL Flex instance creation %q.\n", instance.Id) |
| 63 | + |
| 64 | + // Wait for the instance to become active |
| 65 | + _, err = wait.CreateInstanceWaitHandler(ctx, postgresflexClient.DefaultAPI, projectId, region, instance.Id).WaitWithContext(ctx) |
| 66 | + if err != nil { |
| 67 | + fmt.Fprintf(os.Stderr, "Error when waiting for PostgreSQL Flex instance creation: %v\n", err) |
| 68 | + os.Exit(1) |
| 69 | + } |
| 70 | + |
| 71 | + fmt.Printf("Created PostgreSQL Flex instance %q.\n", instance.Id) |
| 72 | + |
| 73 | + // Get an instance |
| 74 | + instanceResp, err := postgresflexClient.DefaultAPI.GetInstance(ctx, projectId, region, instance.Id).Execute() |
| 75 | + if err != nil { |
| 76 | + fmt.Fprintf(os.Stderr, "Error when calling `GetInstance`: %v\n", err) |
| 77 | + os.Exit(1) |
| 78 | + } |
| 79 | + |
| 80 | + fmt.Printf("Get instance %+v.\n", instanceResp) |
| 81 | + |
| 82 | + // Create a user associated to an instance |
| 83 | + instanceId := instanceResp.Id |
| 84 | + username := "myUser" |
| 85 | + createUserPayload := postgresflex.CreateUserPayload{ |
| 86 | + Name: username, |
| 87 | + Roles: []string{"login"}, |
| 88 | + } |
| 89 | + user, err := postgresflexClient.DefaultAPI.CreateUser(ctx, projectId, region, instanceId).CreateUserPayload(createUserPayload).Execute() |
| 90 | + if err != nil { |
| 91 | + fmt.Fprintf(os.Stderr, "Error when calling `CreateUser`: %v\n", err) |
| 92 | + os.Exit(1) |
| 93 | + } |
| 94 | + |
| 95 | + fmt.Printf("Triggered creation of user %q associated to instance %q.\n", username, instanceResp.Name) |
| 96 | + |
| 97 | + // Wait for the user to become active |
| 98 | + _, err = wait.CreateUserWaitHandler(ctx, postgresflexClient.DefaultAPI, projectId, region, instance.Id, user.Id).WaitWithContext(ctx) |
| 99 | + if err != nil { |
| 100 | + fmt.Fprintf(os.Stderr, "Error when waiting for PostgreSQL Flex user creation: %v\n", err) |
| 101 | + os.Exit(1) |
| 102 | + } |
| 103 | + |
| 104 | + fmt.Printf("Created PostgreSQL Flex user %q.\n", user.Id) |
| 105 | + |
| 106 | + // Get a user |
| 107 | + userResp, err := postgresflexClient.DefaultAPI.GetUser(ctx, projectId, region, instance.Id, user.Id).Execute() |
| 108 | + if err != nil { |
| 109 | + fmt.Fprintf(os.Stderr, "Error when calling `GetUser`: %v\n", err) |
| 110 | + os.Exit(1) |
| 111 | + } |
| 112 | + |
| 113 | + fmt.Printf("Get user %+v.\n", userResp) |
| 114 | + |
| 115 | + // Delete a user |
| 116 | + err = postgresflexClient.DefaultAPI.DeleteUser(ctx, projectId, region, instanceId, user.Id).Execute() |
| 117 | + |
| 118 | + if err != nil { |
| 119 | + fmt.Fprintf(os.Stderr, "Error when deleting PostgreSQL Flex user: %v", err) |
| 120 | + os.Exit(1) |
| 121 | + } |
| 122 | + |
| 123 | + _, err = wait.DeleteUserWaitHandler(ctx, postgresflexClient.DefaultAPI, projectId, region, instanceId, user.Id).WaitWithContext(ctx) |
| 124 | + if err != nil { |
| 125 | + fmt.Fprintf(os.Stderr, "Error when waiting for PostgreSQL Flex user deletion: %v", err) |
| 126 | + } |
| 127 | + |
| 128 | + fmt.Printf("Deleted PostgreSQL Flex user %q.\n", instanceId) |
| 129 | + |
| 130 | + // Update an instance |
| 131 | + updateInstancePayload := postgresflex.PartialUpdateInstancePayload{ |
| 132 | + Name: utils.Ptr("updated-instance"), |
| 133 | + } |
| 134 | + err = postgresflexClient.DefaultAPI.PartialUpdateInstance(ctx, projectId, region, instanceId).PartialUpdateInstancePayload(updateInstancePayload).Execute() |
| 135 | + if err != nil { |
| 136 | + fmt.Fprintf(os.Stderr, "Error updating PostgreSQL Flex instance: %v\n", err) |
| 137 | + os.Exit(1) |
| 138 | + } |
| 139 | + fmt.Printf("Triggered PostgreSQL Flex instance update %q.\n", instance.Id) |
| 140 | + |
| 141 | + // Wait for the instance to become active |
| 142 | + _, err = wait.PartialUpdateInstanceWaitHandler(ctx, postgresflexClient.DefaultAPI, projectId, region, instanceId).WaitWithContext(ctx) |
| 143 | + if err != nil { |
| 144 | + fmt.Fprintf(os.Stderr, "Error when waiting for PostgreSQL Flex instance update: %v\n", err) |
| 145 | + os.Exit(1) |
| 146 | + } |
| 147 | + |
| 148 | + fmt.Printf("Updated PostgreSQL Flex instance %q.\n", instance.Id) |
| 149 | + |
| 150 | + // Delete an instance |
| 151 | + err = postgresflexClient.DefaultAPI.DeleteInstance(ctx, projectId, region, instanceId).Execute() |
| 152 | + |
| 153 | + if err != nil { |
| 154 | + fmt.Fprintf(os.Stderr, "Error when deleting PostgreSQL Flex instance: %v", err) |
| 155 | + os.Exit(1) |
| 156 | + } |
| 157 | + |
| 158 | + _, err = wait.DeleteInstanceWaitHandler(ctx, postgresflexClient.DefaultAPI, projectId, region, instanceId).WaitWithContext(ctx) |
| 159 | + if err != nil { |
| 160 | + fmt.Fprintf(os.Stderr, "Error when waiting for PostgreSQL Flex instance deletion: %v", err) |
| 161 | + os.Exit(1) |
| 162 | + } |
| 163 | + |
| 164 | + fmt.Printf("Deleted PostgreSQL Flex instance %q.\n", instanceId) |
| 165 | +} |
0 commit comments