Skip to content

Commit 4604ef4

Browse files
committed
Update tests and waiter with the new region parameter
1 parent e9b9a8c commit 4604ef4

File tree

5 files changed

+37
-39
lines changed

5 files changed

+37
-39
lines changed

examples/postgresflex/go.mod

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ module github.com/stackitcloud/stackit-sdk-go/examples/postgresflex
22

33
go 1.21
44

5-
require (
6-
github.com/stackitcloud/stackit-sdk-go/core v0.16.0
7-
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.18.0
8-
)
5+
require github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.18.0
96

107
require (
118
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
129
github.com/google/go-cmp v0.7.0 // indirect
1310
github.com/google/uuid v1.6.0 // indirect
11+
github.com/stackitcloud/stackit-sdk-go/core v0.16.0 // indirect
1412
)

examples/postgresflex/postgresflex.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"os"
77

8-
"github.com/stackitcloud/stackit-sdk-go/core/config"
98
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex"
109
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex/wait"
1110
)
@@ -14,17 +13,18 @@ func main() {
1413
// Specify the project ID
1514
projectId := "PROJECT_ID"
1615

16+
// Specify the region
17+
region := "REGION"
18+
1719
// Create a new API client, that uses default authentication and configuration
18-
postgresflexClient, err := postgresflex.NewAPIClient(
19-
config.WithRegion("eu01"),
20-
)
20+
postgresflexClient, err := postgresflex.NewAPIClient()
2121
if err != nil {
2222
fmt.Fprintf(os.Stderr, "Creating API client: %v\n", err)
2323
os.Exit(1)
2424
}
2525

2626
// Get the postgresql instances for your project
27-
getInstancesResp, err := postgresflexClient.ListInstances(context.Background(), projectId).Execute()
27+
getInstancesResp, err := postgresflexClient.ListInstances(context.Background(), projectId, region).Execute()
2828
if err != nil {
2929
fmt.Fprintf(os.Stderr, "Error when calling `ListInstances`: %v\n", err)
3030
os.Exit(1)
@@ -44,7 +44,7 @@ func main() {
4444
Username: &username,
4545
Roles: &[]string{"login"},
4646
}
47-
_, err = postgresflexClient.CreateUser(context.Background(), projectId, instanceId).CreateUserPayload(createUserPayload).Execute()
47+
_, err = postgresflexClient.CreateUser(context.Background(), projectId, region, instanceId).CreateUserPayload(createUserPayload).Execute()
4848
if err != nil {
4949
fmt.Fprintf(os.Stderr, "Error when calling `CreateUser`: %v\n", err)
5050
os.Exit(1)
@@ -53,27 +53,27 @@ func main() {
5353
fmt.Printf("Created user \"%s\" associated to instance \"%s\".\n", username, *items[0].Name)
5454

5555
// Delete an instance
56-
err = postgresflexClient.DeleteInstance(context.Background(), projectId, instanceId).Execute()
56+
err = postgresflexClient.DeleteInstance(context.Background(), projectId, region, instanceId).Execute()
5757

5858
if err != nil {
5959
fmt.Fprintf(os.Stderr, "Error when delete PostgreSQL Flex instance: %v", err)
6060
}
6161

62-
_, err = wait.DeleteInstanceWaitHandler(context.Background(), postgresflexClient, projectId, instanceId).WaitWithContext(context.Background())
62+
_, err = wait.DeleteInstanceWaitHandler(context.Background(), postgresflexClient, projectId, region, instanceId).WaitWithContext(context.Background())
6363
if err != nil {
6464
fmt.Fprintf(os.Stderr, "Error when waiting for PostgreSQL Flex instance deletion: %v", err)
6565
}
6666

6767
fmt.Printf("Deleted PostgreSQL Flex instance \"%s\".\n", instanceId)
6868

6969
// Force delete an instance
70-
err = postgresflexClient.ForceDeleteInstance(context.Background(), projectId, instanceId).Execute()
70+
err = postgresflexClient.ForceDeleteInstance(context.Background(), projectId, region, instanceId).Execute()
7171

7272
if err != nil {
7373
fmt.Fprintf(os.Stderr, "Error when force delete PostgreSQL Flex instance: %v", err)
7474
}
7575

76-
_, err = wait.ForceDeleteInstanceWaitHandler(context.Background(), postgresflexClient, projectId, instanceId).WaitWithContext(context.Background())
76+
_, err = wait.ForceDeleteInstanceWaitHandler(context.Background(), postgresflexClient, projectId, region, instanceId).WaitWithContext(context.Background())
7777
if err != nil {
7878
fmt.Fprintf(os.Stderr, "Error when waiting for PostgreSQL Flex instance force deletion: %v", err)
7979
}

examples/runtime/runtime.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net/http"
77
"os"
88

9-
"github.com/stackitcloud/stackit-sdk-go/core/config"
109
"github.com/stackitcloud/stackit-sdk-go/core/runtime"
1110
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex"
1211
)
@@ -15,30 +14,31 @@ func main() {
1514
// Specify the project ID
1615
projectId := "PROJECT_ID"
1716

17+
// Specify the region
18+
region := "REGION"
19+
1820
// Create a new API client, that uses default authentication and configuration
19-
postgresflexClient, err := postgresflex.NewAPIClient(
20-
config.WithRegion("eu01"),
21-
)
21+
postgresflexClient, err := postgresflex.NewAPIClient()
2222
if err != nil {
2323
fmt.Fprintf(os.Stderr, "Creating API client: %v\n", err)
2424
os.Exit(1)
2525
}
2626

27-
// Get the MongoDB Flex instances for your project and capture the HTTP response using the context
27+
// Get the PostgreSQL Flex instances for your project and capture the HTTP response using the context
2828
var httpResp *http.Response
2929
ctxWithHTTPResp := runtime.WithCaptureHTTPResponse(context.Background(), &httpResp)
30-
getInstancesResp, err := postgresflexClient.ListInstances(ctxWithHTTPResp, projectId).Execute()
30+
getInstancesResp, err := postgresflexClient.ListInstances(ctxWithHTTPResp, projectId, region).Execute()
3131
if err != nil {
3232
fmt.Fprintf(os.Stderr, "Error when calling `ListInstances`: %v\n", err)
3333
os.Exit(1)
3434
}
3535
fmt.Printf("Number of instances: %v\n\n", len(*getInstancesResp.Items))
3636
fmt.Printf("HTTP response: %+v\n\n", *httpResp)
3737

38-
// Get the MongoDB Flex instances for your project and capture the HTTP request using the context
38+
// Get the PostgreSQL Flex instances for your project and capture the HTTP request using the context
3939
var httpReq *http.Request
4040
ctxWithHTTPReq := runtime.WithCaptureHTTPRequest(context.Background(), &httpReq)
41-
getInstancesResp, err = postgresflexClient.ListInstances(ctxWithHTTPReq, projectId).Execute()
41+
getInstancesResp, err = postgresflexClient.ListInstances(ctxWithHTTPReq, projectId, region).Execute()
4242
if err != nil {
4343
fmt.Fprintf(os.Stderr, "Error when calling `ListInstances`: %v\n", err)
4444
os.Exit(1)

services/postgresflex/wait/wait.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ const (
2020

2121
// Interface needed for tests
2222
type APIClientInstanceInterface interface {
23-
GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*postgresflex.InstanceResponse, error)
24-
ListUsersExecute(ctx context.Context, projectId string, instanceId string) (*postgresflex.ListUsersResponse, error)
23+
GetInstanceExecute(ctx context.Context, projectId, region, instanceId string) (*postgresflex.InstanceResponse, error)
24+
ListUsersExecute(ctx context.Context, projectId, region, instanceId string) (*postgresflex.ListUsersResponse, error)
2525
}
2626

2727
// Interface needed for tests
@@ -30,13 +30,13 @@ type APIClientUserInterface interface {
3030
}
3131

3232
// CreateInstanceWaitHandler will wait for instance creation
33-
func CreateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.AsyncActionHandler[postgresflex.InstanceResponse] {
33+
func CreateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, region, instanceId string) *wait.AsyncActionHandler[postgresflex.InstanceResponse] {
3434
instanceCreated := false
3535
var instanceGetResponse *postgresflex.InstanceResponse
3636

3737
handler := wait.New(func() (waitFinished bool, response *postgresflex.InstanceResponse, err error) {
3838
if !instanceCreated {
39-
s, err := a.GetInstanceExecute(ctx, projectId, instanceId)
39+
s, err := a.GetInstanceExecute(ctx, projectId, region, instanceId)
4040
if err != nil {
4141
return false, nil, err
4242
}
@@ -60,7 +60,7 @@ func CreateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface
6060

6161
// User operations aren't available right after an instance is deemed successful
6262
// To check if they are, perform a users request
63-
_, err = a.ListUsersExecute(ctx, projectId, instanceId)
63+
_, err = a.ListUsersExecute(ctx, projectId, region, instanceId)
6464
if err == nil {
6565
return true, instanceGetResponse, nil
6666
}
@@ -79,9 +79,9 @@ func CreateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface
7979
}
8080

8181
// PartialUpdateInstanceWaitHandler will wait for instance update
82-
func PartialUpdateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.AsyncActionHandler[postgresflex.InstanceResponse] {
82+
func PartialUpdateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, region, instanceId string) *wait.AsyncActionHandler[postgresflex.InstanceResponse] {
8383
handler := wait.New(func() (waitFinished bool, response *postgresflex.InstanceResponse, err error) {
84-
s, err := a.GetInstanceExecute(ctx, projectId, instanceId)
84+
s, err := a.GetInstanceExecute(ctx, projectId, region, instanceId)
8585
if err != nil {
8686
return false, nil, err
8787
}
@@ -106,9 +106,9 @@ func PartialUpdateInstanceWaitHandler(ctx context.Context, a APIClientInstanceIn
106106
}
107107

108108
// DeleteInstanceWaitHandler will wait for instance deletion
109-
func DeleteInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.AsyncActionHandler[struct{}] {
109+
func DeleteInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, region, instanceId string) *wait.AsyncActionHandler[struct{}] {
110110
handler := wait.New(func() (waitFinished bool, response *struct{}, err error) {
111-
s, err := a.GetInstanceExecute(ctx, projectId, instanceId)
111+
s, err := a.GetInstanceExecute(ctx, projectId, region, instanceId)
112112
if err != nil {
113113
return false, nil, err
114114
}
@@ -129,9 +129,9 @@ func DeleteInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface
129129
}
130130

131131
// ForceDeleteInstanceWaitHandler will wait for instance deletion
132-
func ForceDeleteInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.AsyncActionHandler[struct{}] {
132+
func ForceDeleteInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, region, instanceId string) *wait.AsyncActionHandler[struct{}] {
133133
handler := wait.New(func() (waitFinished bool, response *struct{}, err error) {
134-
_, err = a.GetInstanceExecute(ctx, projectId, instanceId)
134+
_, err = a.GetInstanceExecute(ctx, projectId, region, instanceId)
135135
if err == nil {
136136
return false, nil, nil
137137
}

services/postgresflex/wait/wait_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type apiClientInstanceMocked struct {
2020
usersGetErrorStatus int
2121
}
2222

23-
func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ string) (*postgresflex.InstanceResponse, error) {
23+
func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _, _ string) (*postgresflex.InstanceResponse, error) {
2424
if a.instanceGetFails {
2525
return nil, &oapierror.GenericOpenAPIError{
2626
StatusCode: 500,
@@ -41,7 +41,7 @@ func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ str
4141
}, nil
4242
}
4343

44-
func (a *apiClientInstanceMocked) ListUsersExecute(_ context.Context, _, _ string) (*postgresflex.ListUsersResponse, error) {
44+
func (a *apiClientInstanceMocked) ListUsersExecute(_ context.Context, _, _, _ string) (*postgresflex.ListUsersResponse, error) {
4545
if a.usersGetErrorStatus != 0 {
4646
return nil, &oapierror.GenericOpenAPIError{
4747
StatusCode: a.usersGetErrorStatus,
@@ -163,7 +163,7 @@ func TestCreateInstanceWaitHandler(t *testing.T) {
163163
}
164164
}
165165

166-
handler := CreateInstanceWaitHandler(context.Background(), apiClient, "", instanceId)
166+
handler := CreateInstanceWaitHandler(context.Background(), apiClient, "", "", instanceId)
167167

168168
gotRes, err := handler.SetTimeout(10 * time.Millisecond).SetSleepBeforeWait(1 * time.Millisecond).WaitWithContext(context.Background())
169169

@@ -240,7 +240,7 @@ func TestUpdateInstanceWaitHandler(t *testing.T) {
240240
}
241241
}
242242

243-
handler := PartialUpdateInstanceWaitHandler(context.Background(), apiClient, "", instanceId)
243+
handler := PartialUpdateInstanceWaitHandler(context.Background(), apiClient, "", "", instanceId)
244244

245245
gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
246246

@@ -289,7 +289,7 @@ func TestDeleteInstanceWaitHandler(t *testing.T) {
289289
instanceState: tt.instanceState,
290290
}
291291

292-
handler := DeleteInstanceWaitHandler(context.Background(), apiClient, "", instanceId)
292+
handler := DeleteInstanceWaitHandler(context.Background(), apiClient, "", "", instanceId)
293293

294294
_, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
295295

@@ -336,7 +336,7 @@ func TestForceDeleteInstanceWaitHandler(t *testing.T) {
336336
instanceState: tt.instanceState,
337337
}
338338

339-
handler := ForceDeleteInstanceWaitHandler(context.Background(), apiClient, "", instanceId)
339+
handler := ForceDeleteInstanceWaitHandler(context.Background(), apiClient, "", "", instanceId)
340340

341341
_, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background())
342342

0 commit comments

Comments
 (0)