Skip to content

Commit

Permalink
feat: adapt object storage to new regional api
Browse files Browse the repository at this point in the history
  • Loading branch information
bahkauv70 committed Feb 3, 2025
1 parent b9c414f commit abcf88b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
1 change: 1 addition & 0 deletions core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func WithUserAgent(userAgent string) ConfigurationOption {
}

// WithRegion returns a ConfigurationOption that specifies the region to be used
// [Deprecated]: The region should be set on the individual calls
func WithRegion(region string) ConfigurationOption {
return func(config *Configuration) error {
config.Region = region
Expand Down
10 changes: 4 additions & 6 deletions examples/objectstorage/objectstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,31 @@ import (
"fmt"
"os"

"github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
)

func main() {
// Specify the project ID
projectId := "PROJECT_ID"
region := "eu01"

// Create a new API client, that uses default authentication and configuration
objectStorageClient, err := objectstorage.NewAPIClient(
config.WithRegion("eu01"),
)
objectStorageClient, err := objectstorage.NewAPIClient()
if err != nil {
fmt.Fprintf(os.Stderr, "Creating API client: %v\n", err)
os.Exit(1)
}

// Get the object storage buckets for your project
getBucketsResp, err := objectStorageClient.ListBuckets(context.Background(), projectId).Execute()
getBucketsResp, err := objectStorageClient.ListBuckets(context.Background(), projectId, region).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `GetBuckets`: %v\n", err)
} else {
fmt.Printf("Number of buckets: %v\n", len(*getBucketsResp.Buckets))
}

// Create an object storage bucket
createBucketResp, err := objectStorageClient.CreateBucket(context.Background(), projectId, "example-bucket").Execute()
createBucketResp, err := objectStorageClient.CreateBucket(context.Background(), projectId, region, "example-bucket").Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `CreateBucket`: %v\n", err)
} else {
Expand Down
10 changes: 5 additions & 5 deletions services/objectstorage/wait/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (

// Interface needed for tests
type APIClientBucketInterface interface {
GetBucketExecute(ctx context.Context, projectId string, bucketName string) (*objectstorage.GetBucketResponse, error)
GetBucketExecute(ctx context.Context, projectId string, region, bucketName string) (*objectstorage.GetBucketResponse, error)
}

// CreateBucketWaitHandler will wait for bucket creation
func CreateBucketWaitHandler(ctx context.Context, a APIClientBucketInterface, projectId, bucketName string) *wait.AsyncActionHandler[objectstorage.GetBucketResponse] {
func CreateBucketWaitHandler(ctx context.Context, a APIClientBucketInterface, projectId, region, bucketName string) *wait.AsyncActionHandler[objectstorage.GetBucketResponse] {
handler := wait.New(func() (waitFinished bool, response *objectstorage.GetBucketResponse, err error) {
s, err := a.GetBucketExecute(ctx, projectId, bucketName)
s, err := a.GetBucketExecute(ctx, projectId, region, bucketName)
if err != nil {
return false, nil, err
}
Expand All @@ -30,9 +30,9 @@ func CreateBucketWaitHandler(ctx context.Context, a APIClientBucketInterface, pr
}

// DeleteBucketWaitHandler will wait for bucket deletion
func DeleteBucketWaitHandler(ctx context.Context, a APIClientBucketInterface, projectId, bucketName string) *wait.AsyncActionHandler[struct{}] {
func DeleteBucketWaitHandler(ctx context.Context, a APIClientBucketInterface, projectId, region, bucketName string) *wait.AsyncActionHandler[struct{}] {
handler := wait.New(func() (waitFinished bool, response *struct{}, err error) {
_, err = a.GetBucketExecute(ctx, projectId, bucketName)
_, err = a.GetBucketExecute(ctx, projectId, region, bucketName)
if err == nil {
return false, nil, nil
}
Expand Down
6 changes: 3 additions & 3 deletions services/objectstorage/wait/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type apiClientBucketMocked struct {
bucketGetFails bool
}

func (a *apiClientBucketMocked) GetBucketExecute(_ context.Context, _, _ string) (*objectstorage.GetBucketResponse, error) {
func (a *apiClientBucketMocked) GetBucketExecute(_ context.Context, _, _, _ string) (*objectstorage.GetBucketResponse, error) {
if a.bucketGetFails {
return nil, &oapierror.GenericOpenAPIError{
StatusCode: 500,
Expand Down Expand Up @@ -63,7 +63,7 @@ func TestCreateBucketWaitHandler(t *testing.T) {
wantRes = &objectstorage.GetBucketResponse{}
}

handler := CreateBucketWaitHandler(context.Background(), apiClient, "", "")
handler := CreateBucketWaitHandler(context.Background(), apiClient, "", "", "")

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

Expand Down Expand Up @@ -101,7 +101,7 @@ func TestDeleteBucketWaitHandler(t *testing.T) {
bucketGetFails: tt.bucketGetFails,
}

handler := DeleteBucketWaitHandler(context.Background(), apiClient, "", "")
handler := DeleteBucketWaitHandler(context.Background(), apiClient, "", "", "")

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

Expand Down

0 comments on commit abcf88b

Please sign in to comment.