diff --git a/CONTRIBUTION.md b/CONTRIBUTION.md index 6cca7b6ea..f33e751e5 100644 --- a/CONTRIBUTION.md +++ b/CONTRIBUTION.md @@ -8,7 +8,7 @@ Your contribution is welcome! Thank you for your interest in contributing to the ## Developer Guide ### Repository structure -The SDK STACKIT service modules are located under `services`, which are automatically generated from the [REST API specs](https://github.com/stackitcloud/stackit-api-specifications), except for the `wait.go` and `wait_test.go` files. Therefore, changes to these modules besides the `wait.go` and `wait_test.go` files will not be accepted. Instead, consider proposing changes to the generation process in the [Generator repository](https://github.com/stackitcloud/stackit-sdk-generator). +The SDK STACKIT service modules are located under `services`. The files located in `services/[service]` are automatically generated from the [REST API specs](https://github.com/stackitcloud/stackit-api-specifications), whereas the ones located in subfolders (like `wait`) are manually maintained. Therefore, changes to files located in `services/[service]` will not be accepted. Instead, consider proposing changes to the generation process in the [Generator repository](https://github.com/stackitcloud/stackit-sdk-generator). Inside `core` you can find several packages that are used by all service modules, such as `auth`, `config` and `wait`. Examples of usage of the SDK are located under the `examples` folder. diff --git a/examples/waiter/go.mod b/examples/waiter/go.mod deleted file mode 100644 index 4759f8ffa..000000000 --- a/examples/waiter/go.mod +++ /dev/null @@ -1,20 +0,0 @@ -module github.com/stackitcloud/stackit-sdk-go/examples/waiter - -go 1.18 - -require ( - github.com/stackitcloud/stackit-sdk-go/core v0.2.0 - github.com/stackitcloud/stackit-sdk-go/services/dns v0.1.0 -) - -require ( - github.com/go-logr/logr v1.2.4 // indirect - github.com/golang/protobuf v1.5.3 // indirect - golang.org/x/net v0.15.0 // indirect - golang.org/x/oauth2 v0.12.0 // indirect - google.golang.org/appengine v1.6.7 // indirect - google.golang.org/protobuf v1.31.0 // indirect - k8s.io/apimachinery v0.28.2 // indirect - k8s.io/klog/v2 v2.100.1 // indirect - k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect -) diff --git a/examples/waiter/go.sum b/examples/waiter/go.sum deleted file mode 100644 index cf8316e15..000000000 --- a/examples/waiter/go.sum +++ /dev/null @@ -1,36 +0,0 @@ -github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= -github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= -github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= -github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/stackitcloud/stackit-sdk-go/core v0.2.0 h1:dv9pMtGN6p5HbbpaB1PjncOdDPDyP4mXfXBxk7j9x0c= -github.com/stackitcloud/stackit-sdk-go/core v0.2.0/go.mod h1:Bne56SlX8V2GbONcofEmZz5lVLuuZgsc015FPIUYXy4= -github.com/stackitcloud/stackit-sdk-go/services/dns v0.1.0 h1:PEfzbpktaK7Fj8O8zxLM3bpXwazmGhG59D15Vi8z/sE= -github.com/stackitcloud/stackit-sdk-go/services/dns v0.1.0/go.mod h1:eD6fRbn/pS9sdRYeq3EBKvFj2qlDu25Ir/doWfPaBcI= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= -golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8= -golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= -golang.org/x/oauth2 v0.12.0 h1:smVPGxink+n1ZI5pkQa8y6fZT0RW0MgCO5bFpepy4B4= -golang.org/x/oauth2 v0.12.0/go.mod h1:A74bZ3aGXgCY0qaIC9Ahg6Lglin4AMAco8cIv9baba4= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= -google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= -google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= -k8s.io/apimachinery v0.28.2 h1:KCOJLrc6gu+wV1BYgwik4AF4vXOlVJPdiqn0yAWWwXQ= -k8s.io/apimachinery v0.28.2/go.mod h1:RdzF87y/ngqk9H4z3EL2Rppv5jj95vGS/HaFXrLDApU= -k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg= -k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= -k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI= -k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= diff --git a/examples/waiter/waiter.go b/examples/waiter/waiter.go deleted file mode 100644 index cb783bf28..000000000 --- a/examples/waiter/waiter.go +++ /dev/null @@ -1,56 +0,0 @@ -package main - -import ( - "context" - "fmt" - "os" - "time" - - "github.com/stackitcloud/stackit-sdk-go/core/utils" - "github.com/stackitcloud/stackit-sdk-go/services/dns" -) - -func main() { - ctx := context.Background() - - // Specify the project ID and DNS name (must be a valid and unique FQDN) - projectId := "PROJECT_ID" - dnsName := "zoneTest.com" - - // Create a new API client, that uses default authentication. - dnsClient, err := dns.NewAPIClient() - if err != nil { - fmt.Fprintf(os.Stderr, "[DNS API] Creating API client: %v\n", err) - os.Exit(1) - } - - // Create a DNS Zone - createZonePayload := dns.CreateZonePayload{ - Name: utils.Ptr("myZone"), - DnsName: utils.Ptr(dnsName), - } - - createZoneResp, err := dnsClient.CreateZone(ctx, projectId).CreateZonePayload(createZonePayload).Execute() - if err != nil { - fmt.Fprintf(os.Stderr, "[DNS API] Error when calling `ZoneApi.CreateZone`: %v\n", err) - os.Exit(1) - } - - zoneId := *createZoneResp.Zone.Id - wres, err := dns.CreateZoneWaitHandler(ctx, dnsClient, projectId, zoneId).SetTimeout(15 * time.Minute).WaitWithContext(ctx) - if err != nil { - fmt.Fprintf(os.Stderr, "[DNS API] Waiting for zone update: %v\n", err) - os.Exit(1) - } - - // At this stage the waiter is waiting for an update to the zone - // You can make a manual request to the DNS API updating the zone that was just created - // The waiter will finish, and you will get the output below - got, ok := wres.(*dns.ZoneResponse) - if !ok { - fmt.Fprintf(os.Stderr, "[DNS API] Returned response has unexpected type: %v\n", err) - os.Exit(1) - } - - fmt.Fprintf(os.Stderr, "[DNS API] Zone with id %s update (state: %s)\n", *got.Zone.Id, *got.Zone.State) -} diff --git a/go.work b/go.work index a91dfea96..0b205502c 100644 --- a/go.work +++ b/go.work @@ -18,7 +18,6 @@ use ( ./examples/redis ./examples/secretsmanager ./examples/ske - ./examples/waiter ./services/argus ./services/dns ./services/loadbalancer diff --git a/services/argus/wait.go b/services/argus/wait/wait.go similarity index 96% rename from services/argus/wait.go rename to services/argus/wait/wait.go index 0a2251daa..164b57c1f 100644 --- a/services/argus/wait.go +++ b/services/argus/wait/wait.go @@ -1,10 +1,11 @@ -package argus +package wait import ( "context" "fmt" "github.com/stackitcloud/stackit-sdk-go/core/wait" + "github.com/stackitcloud/stackit-sdk-go/services/argus" ) const ( @@ -18,8 +19,8 @@ const ( // APIClientInterface Interfaces needed for tests type APIClientInterface interface { - GetInstanceExecute(ctx context.Context, instanceId, projectId string) (*InstanceResponse, error) - GetScrapeConfigsExecute(ctx context.Context, instanceId, projectId string) (*ScrapeConfigsResponse, error) + GetInstanceExecute(ctx context.Context, instanceId, projectId string) (*argus.InstanceResponse, error) + GetScrapeConfigsExecute(ctx context.Context, instanceId, projectId string) (*argus.ScrapeConfigsResponse, error) } // will wait for creation diff --git a/services/argus/wait_test.go b/services/argus/wait/wait_test.go similarity index 86% rename from services/argus/wait_test.go rename to services/argus/wait/wait_test.go index cccf2a465..91e7e8374 100644 --- a/services/argus/wait_test.go +++ b/services/argus/wait/wait_test.go @@ -1,4 +1,4 @@ -package argus +package wait import ( "context" @@ -9,35 +9,36 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/utils" + "github.com/stackitcloud/stackit-sdk-go/services/argus" ) type apiClientMocked struct { getFails bool resourceState *string - jobs []Job + jobs []argus.Job } -func (a *apiClientMocked) GetInstanceExecute(_ context.Context, _, _ string) (*InstanceResponse, error) { +func (a *apiClientMocked) GetInstanceExecute(_ context.Context, _, _ string) (*argus.InstanceResponse, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, } } - return &InstanceResponse{ + return &argus.InstanceResponse{ Id: utils.Ptr("iid"), Status: a.resourceState, }, nil } -func (a *apiClientMocked) GetScrapeConfigsExecute(_ context.Context, _, _ string) (*ScrapeConfigsResponse, error) { +func (a *apiClientMocked) GetScrapeConfigsExecute(_ context.Context, _, _ string) (*argus.ScrapeConfigsResponse, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, } } - return &ScrapeConfigsResponse{ + return &argus.ScrapeConfigsResponse{ Data: &a.jobs, }, nil } @@ -87,9 +88,9 @@ func TestCreateInstanceWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *InstanceResponse + var wantRes *argus.InstanceResponse if !tt.getFails { - wantRes = &InstanceResponse{ + wantRes = &argus.InstanceResponse{ Id: utils.Ptr("iid"), Status: tt.resourceState, } @@ -107,7 +108,7 @@ func TestCreateInstanceWaitHandler(t *testing.T) { if wantRes == nil && gotRes != nil { t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) } - if wantRes != nil && !cmp.Equal(gotRes, wantRes, cmpopts.IgnoreUnexported(NullableString{})) { + if wantRes != nil && !cmp.Equal(gotRes, wantRes, cmpopts.IgnoreUnexported(argus.NullableString{})) { t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) } }) @@ -153,9 +154,9 @@ func TestUpdateInstanceWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *InstanceResponse + var wantRes *argus.InstanceResponse if !tt.getFails { - wantRes = &InstanceResponse{ + wantRes = &argus.InstanceResponse{ Status: tt.resourceState, Id: utils.Ptr("iid"), } @@ -173,7 +174,7 @@ func TestUpdateInstanceWaitHandler(t *testing.T) { if wantRes == nil && gotRes != nil { t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) } - if wantRes != nil && !cmp.Equal(gotRes, wantRes, cmpopts.IgnoreUnexported(NullableString{})) { + if wantRes != nil && !cmp.Equal(gotRes, wantRes, cmpopts.IgnoreUnexported(argus.NullableString{})) { t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) } }) @@ -219,9 +220,9 @@ func TestDeleteInstanceWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *InstanceResponse + var wantRes *argus.InstanceResponse if !tt.getFails { - wantRes = &InstanceResponse{ + wantRes = &argus.InstanceResponse{ Status: tt.resourceState, Id: utils.Ptr("iid"), } @@ -239,7 +240,7 @@ func TestDeleteInstanceWaitHandler(t *testing.T) { if wantRes == nil && gotRes != nil { t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) } - if wantRes != nil && !cmp.Equal(gotRes, wantRes, cmpopts.IgnoreUnexported(NullableString{})) { + if wantRes != nil && !cmp.Equal(gotRes, wantRes, cmpopts.IgnoreUnexported(argus.NullableString{})) { t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) } }) @@ -250,25 +251,25 @@ func TestCreateScrapeConfigWaitHandler(t *testing.T) { tests := []struct { desc string getFails bool - jobs []Job + jobs []argus.Job wantErr bool }{ { desc: "create_succeeded", getFails: false, - jobs: []Job{{JobName: utils.Ptr("job")}, {JobName: utils.Ptr("other-job")}}, + jobs: []argus.Job{{JobName: utils.Ptr("job")}, {JobName: utils.Ptr("other-job")}}, wantErr: false, }, { desc: "create_failed and timeout", getFails: false, - jobs: []Job{{JobName: utils.Ptr("other-job")}}, + jobs: []argus.Job{{JobName: utils.Ptr("other-job")}}, wantErr: true, }, { desc: "get_fails", getFails: true, - jobs: []Job{}, + jobs: []argus.Job{}, wantErr: true, }, } @@ -279,9 +280,9 @@ func TestCreateScrapeConfigWaitHandler(t *testing.T) { jobs: tt.jobs, } - var wantRes *ScrapeConfigsResponse + var wantRes *argus.ScrapeConfigsResponse if !tt.getFails { - wantRes = &ScrapeConfigsResponse{ + wantRes = &argus.ScrapeConfigsResponse{ Data: &tt.jobs, } } else { @@ -298,7 +299,7 @@ func TestCreateScrapeConfigWaitHandler(t *testing.T) { if wantRes == nil && gotRes != nil { t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) } - if wantRes != nil && !cmp.Equal(gotRes, wantRes, cmpopts.IgnoreUnexported(NullableString{})) { + if wantRes != nil && !cmp.Equal(gotRes, wantRes, cmpopts.IgnoreUnexported(argus.NullableString{})) { t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) } }) @@ -309,25 +310,25 @@ func TestDeleteScrapeConfigWaitHandler(t *testing.T) { tests := []struct { desc string getFails bool - jobs []Job + jobs []argus.Job wantErr bool }{ { desc: "delete_succeeded", getFails: false, - jobs: []Job{{JobName: utils.Ptr("other-job")}}, + jobs: []argus.Job{{JobName: utils.Ptr("other-job")}}, wantErr: false, }, { desc: "timeout", getFails: false, - jobs: []Job{{JobName: utils.Ptr("job")}}, + jobs: []argus.Job{{JobName: utils.Ptr("job")}}, wantErr: true, }, { desc: "get_fails", getFails: true, - jobs: []Job{}, + jobs: []argus.Job{}, wantErr: true, }, } @@ -338,9 +339,9 @@ func TestDeleteScrapeConfigWaitHandler(t *testing.T) { jobs: tt.jobs, } - var wantRes *ScrapeConfigsResponse + var wantRes *argus.ScrapeConfigsResponse if !tt.getFails { - wantRes = &ScrapeConfigsResponse{ + wantRes = &argus.ScrapeConfigsResponse{ Data: &tt.jobs, } } else { @@ -357,7 +358,7 @@ func TestDeleteScrapeConfigWaitHandler(t *testing.T) { if wantRes == nil && gotRes != nil { t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) } - if wantRes != nil && !cmp.Equal(gotRes, wantRes, cmpopts.IgnoreUnexported(NullableString{})) { + if wantRes != nil && !cmp.Equal(gotRes, wantRes, cmpopts.IgnoreUnexported(argus.NullableString{})) { t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) } }) diff --git a/services/dns/wait.go b/services/dns/wait/wait.go similarity index 97% rename from services/dns/wait.go rename to services/dns/wait/wait.go index 17bc40b63..bcefb1aba 100644 --- a/services/dns/wait.go +++ b/services/dns/wait/wait.go @@ -1,10 +1,11 @@ -package dns +package wait import ( "context" "fmt" "github.com/stackitcloud/stackit-sdk-go/core/wait" + "github.com/stackitcloud/stackit-sdk-go/services/dns" ) const ( @@ -18,8 +19,8 @@ const ( // Interfaces needed for tests type APIClientInterface interface { - GetZoneExecute(ctx context.Context, projectId, zoneId string) (*ZoneResponse, error) - GetRecordSetExecute(ctx context.Context, projectId, zoneId, rrSetId string) (*RecordSetResponse, error) + GetZoneExecute(ctx context.Context, projectId, zoneId string) (*dns.ZoneResponse, error) + GetRecordSetExecute(ctx context.Context, projectId, zoneId, rrSetId string) (*dns.RecordSetResponse, error) } // CreateZoneWaitHandler will wait for creation diff --git a/services/dns/wait_test.go b/services/dns/wait/wait_test.go similarity index 92% rename from services/dns/wait_test.go rename to services/dns/wait/wait_test.go index d33b4517d..2fe840ca3 100644 --- a/services/dns/wait_test.go +++ b/services/dns/wait/wait_test.go @@ -1,4 +1,4 @@ -package dns +package wait import ( "context" @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp" oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/utils" + "github.com/stackitcloud/stackit-sdk-go/services/dns" ) type apiClientMocked struct { @@ -15,30 +16,30 @@ type apiClientMocked struct { resourceState string } -func (a *apiClientMocked) GetZoneExecute(_ context.Context, _, _ string) (*ZoneResponse, error) { +func (a *apiClientMocked) GetZoneExecute(_ context.Context, _, _ string) (*dns.ZoneResponse, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, } } - return &ZoneResponse{ - Zone: &Zone{ + return &dns.ZoneResponse{ + Zone: &dns.Zone{ State: &a.resourceState, Id: utils.Ptr("zid"), }, }, nil } -func (a *apiClientMocked) GetRecordSetExecute(_ context.Context, _, _, _ string) (*RecordSetResponse, error) { +func (a *apiClientMocked) GetRecordSetExecute(_ context.Context, _, _, _ string) (*dns.RecordSetResponse, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, } } - return &RecordSetResponse{ - Rrset: &RecordSet{ + return &dns.RecordSetResponse{ + Rrset: &dns.RecordSet{ State: &a.resourceState, Id: utils.Ptr("rid"), }, @@ -84,10 +85,10 @@ func TestCreateZoneWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *ZoneResponse + var wantRes *dns.ZoneResponse if !tt.getFails { - wantRes = &ZoneResponse{ - Zone: &Zone{ + wantRes = &dns.ZoneResponse{ + Zone: &dns.Zone{ State: &tt.resourceState, Id: utils.Ptr("zid"), }, @@ -152,10 +153,10 @@ func TestUpdateZoneWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *ZoneResponse + var wantRes *dns.ZoneResponse if !tt.getFails { - wantRes = &ZoneResponse{ - Zone: &Zone{ + wantRes = &dns.ZoneResponse{ + Zone: &dns.Zone{ State: &tt.resourceState, Id: utils.Ptr("zid"), }, @@ -220,10 +221,10 @@ func TestDeleteZoneWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *ZoneResponse + var wantRes *dns.ZoneResponse if !tt.getFails { - wantRes = &ZoneResponse{ - Zone: &Zone{ + wantRes = &dns.ZoneResponse{ + Zone: &dns.Zone{ State: &tt.resourceState, Id: utils.Ptr("zid"), }, @@ -288,10 +289,10 @@ func TestCreateRecordSetWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *RecordSetResponse + var wantRes *dns.RecordSetResponse if !tt.getFails { - wantRes = &RecordSetResponse{ - Rrset: &RecordSet{ + wantRes = &dns.RecordSetResponse{ + Rrset: &dns.RecordSet{ State: &tt.resourceState, Id: utils.Ptr("rid"), }, @@ -356,10 +357,10 @@ func TestUpdateRecordSetWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *RecordSetResponse + var wantRes *dns.RecordSetResponse if !tt.getFails { - wantRes = &RecordSetResponse{ - Rrset: &RecordSet{ + wantRes = &dns.RecordSetResponse{ + Rrset: &dns.RecordSet{ State: &tt.resourceState, Id: utils.Ptr("rid"), }, @@ -424,10 +425,10 @@ func TestDeleteRecordSetWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *RecordSetResponse + var wantRes *dns.RecordSetResponse if !tt.getFails { - wantRes = &RecordSetResponse{ - Rrset: &RecordSet{ + wantRes = &dns.RecordSetResponse{ + Rrset: &dns.RecordSet{ State: &tt.resourceState, Id: utils.Ptr("rid"), }, diff --git a/services/loadbalancer/wait.go b/services/loadbalancer/wait/wait.go similarity index 94% rename from services/loadbalancer/wait.go rename to services/loadbalancer/wait/wait.go index 480a3f19c..c53b4727c 100644 --- a/services/loadbalancer/wait.go +++ b/services/loadbalancer/wait/wait.go @@ -1,4 +1,4 @@ -package loadbalancer +package wait import ( "context" @@ -7,6 +7,7 @@ import ( oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/wait" + "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer" ) // Load balancer instance status @@ -30,8 +31,8 @@ const ( // Interface needed for tests type APIClientInterface interface { - GetLoadBalancerExecute(ctx context.Context, projectId, name string) (*LoadBalancer, error) - GetStatusExecute(ctx context.Context, projectId string) (*StatusResponse, error) + GetLoadBalancerExecute(ctx context.Context, projectId, name string) (*loadbalancer.LoadBalancer, error) + GetStatusExecute(ctx context.Context, projectId string) (*loadbalancer.StatusResponse, error) } // CreateInstanceWaitHandler will wait for creation diff --git a/services/loadbalancer/wait_test.go b/services/loadbalancer/wait/wait_test.go similarity index 93% rename from services/loadbalancer/wait_test.go rename to services/loadbalancer/wait/wait_test.go index 6228a6f9c..8c6ed2749 100644 --- a/services/loadbalancer/wait_test.go +++ b/services/loadbalancer/wait/wait_test.go @@ -1,4 +1,4 @@ -package loadbalancer +package wait import ( "context" @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" + "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer" ) // Used for testing instance operations @@ -19,7 +20,7 @@ type apiClientMocked struct { functionalityStatusGetFails bool } -func (a *apiClientMocked) GetLoadBalancerExecute(_ context.Context, _, _ string) (*LoadBalancer, error) { +func (a *apiClientMocked) GetLoadBalancerExecute(_ context.Context, _, _ string) (*loadbalancer.LoadBalancer, error) { if a.instanceGetFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, @@ -32,19 +33,19 @@ func (a *apiClientMocked) GetLoadBalancerExecute(_ context.Context, _, _ string) } } - return &LoadBalancer{ + return &loadbalancer.LoadBalancer{ Name: &a.instanceName, Status: &a.instanceStatus, }, nil } -func (a *apiClientMocked) GetStatusExecute(_ context.Context, _ string) (*StatusResponse, error) { +func (a *apiClientMocked) GetStatusExecute(_ context.Context, _ string) (*loadbalancer.StatusResponse, error) { if a.functionalityStatusGetFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, } } - return &StatusResponse{ + return &loadbalancer.StatusResponse{ Status: &a.functionalityStatus, }, nil } @@ -96,9 +97,9 @@ func TestCreateInstanceWaitHandler(t *testing.T) { instanceGetFails: tt.instanceGetFails, } - var wantRes *LoadBalancer + var wantRes *loadbalancer.LoadBalancer if (tt.instanceStatus == InstanceStatusReady) && !tt.instanceGetFails { - wantRes = &LoadBalancer{ + wantRes = &loadbalancer.LoadBalancer{ Name: &instanceName, Status: &tt.instanceStatus, } @@ -209,9 +210,9 @@ func TestEnableLoadBalancingWaitHandler(t *testing.T) { functionalityStatusGetFails: tt.functionalityStatusGetFails, } - var wantRes *StatusResponse + var wantRes *loadbalancer.StatusResponse if (tt.functionalityStatus == FunctionalityStatusReady) && !tt.functionalityStatusGetFails { - wantRes = &StatusResponse{ + wantRes = &loadbalancer.StatusResponse{ Status: &tt.functionalityStatus, } } diff --git a/services/logme/wait.go b/services/logme/wait/wait.go similarity index 97% rename from services/logme/wait.go rename to services/logme/wait/wait.go index d871ef342..1a298ec46 100644 --- a/services/logme/wait.go +++ b/services/logme/wait/wait.go @@ -1,4 +1,4 @@ -package logme +package wait import ( "context" @@ -8,6 +8,7 @@ import ( oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/wait" + "github.com/stackitcloud/stackit-sdk-go/services/logme" ) const ( @@ -20,12 +21,12 @@ const ( // Interface needed for tests type APIClientInstanceInterface interface { - GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*Instance, error) + GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*logme.Instance, error) } // Interface needed for tests type APIClientCredentialsInterface interface { - GetCredentialsExecute(ctx context.Context, projectId, instanceId, credentialsId string) (*CredentialsResponse, error) + GetCredentialsExecute(ctx context.Context, projectId, instanceId, credentialsId string) (*logme.CredentialsResponse, error) } // CreateInstanceWaitHandler will wait for creation diff --git a/services/rabbitmq/wait_test.go b/services/logme/wait/wait_test.go similarity index 93% rename from services/rabbitmq/wait_test.go rename to services/logme/wait/wait_test.go index 6670907ba..243dc4eba 100644 --- a/services/rabbitmq/wait_test.go +++ b/services/logme/wait/wait_test.go @@ -1,4 +1,4 @@ -package rabbitmq +package wait import ( "context" @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp" oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/utils" + "github.com/stackitcloud/stackit-sdk-go/services/logme" ) // Used for testing instance operations @@ -26,7 +27,7 @@ var ( instanceTypeDelete = InstanceTypeDelete ) -func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ string) (*Instance, error) { +func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ string) (*logme.Instance, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, @@ -34,9 +35,9 @@ func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ str } if *a.resourceOperation == InstanceTypeDelete && a.resourceState == InstanceStateSuccess { if a.deletionSucceedsWithErrors { - return &Instance{ + return &logme.Instance{ InstanceId: &a.resourceId, - LastOperation: &LastOperation{ + LastOperation: &logme.LastOperation{ Description: &a.resourceDescription, Type: a.resourceOperation, State: &a.resourceState, @@ -48,9 +49,9 @@ func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ str } } - return &Instance{ + return &logme.Instance{ InstanceId: &a.resourceId, - LastOperation: &LastOperation{ + LastOperation: &logme.LastOperation{ Description: &a.resourceDescription, Type: a.resourceOperation, State: &a.resourceState, @@ -66,7 +67,7 @@ type apiClientCredentialsMocked struct { deletionSucceeds bool } -func (a *apiClientCredentialsMocked) GetCredentialsExecute(_ context.Context, _, _, _ string) (*CredentialsResponse, error) { +func (a *apiClientCredentialsMocked) GetCredentialsExecute(_ context.Context, _, _, _ string) (*logme.CredentialsResponse, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, @@ -79,7 +80,7 @@ func (a *apiClientCredentialsMocked) GetCredentialsExecute(_ context.Context, _, } } - return &CredentialsResponse{ + return &logme.CredentialsResponse{ Id: &a.resourceId, }, nil } @@ -126,11 +127,11 @@ func TestCreateInstanceWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *Instance + var wantRes *logme.Instance if !tt.getFails { - wantRes = &Instance{ + wantRes = &logme.Instance{ InstanceId: &instanceId, - LastOperation: &LastOperation{ + LastOperation: &logme.LastOperation{ Type: &instanceTypeCreate, State: &tt.resourceState, Description: utils.Ptr(""), @@ -198,11 +199,11 @@ func TestUpdateInstanceWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *Instance + var wantRes *logme.Instance if !tt.getFails { - wantRes = &Instance{ + wantRes = &logme.Instance{ InstanceId: &instanceId, - LastOperation: &LastOperation{ + LastOperation: &logme.LastOperation{ Type: &instanceTypeUpdate, State: &tt.resourceState, Description: utils.Ptr(""), @@ -327,9 +328,9 @@ func TestCreateCredentialsWaitHandler(t *testing.T) { operationSucceeds: tt.operationSucceeds, } - var wantRes *CredentialsResponse + var wantRes *logme.CredentialsResponse if !tt.getFails && tt.operationSucceeds { - wantRes = &CredentialsResponse{ + wantRes = &logme.CredentialsResponse{ Id: &credentialsId, } } else if !tt.getFails && !tt.operationSucceeds { @@ -390,9 +391,9 @@ func TestDeleteCredentialsWaitHandler(t *testing.T) { deletionSucceeds: tt.deletionSucceeds, } - var wantRes *CredentialsResponse + var wantRes *logme.CredentialsResponse if !tt.getFails && !tt.deletionSucceeds { - wantRes = &CredentialsResponse{ + wantRes = &logme.CredentialsResponse{ Id: &credentialsId, } } else if !tt.getFails && tt.deletionSucceeds { diff --git a/services/postgresql/wait.go b/services/mariadb/wait/wait.go similarity index 97% rename from services/postgresql/wait.go rename to services/mariadb/wait/wait.go index b99ec5398..a1667edb1 100644 --- a/services/postgresql/wait.go +++ b/services/mariadb/wait/wait.go @@ -1,4 +1,4 @@ -package postgresql +package wait import ( "context" @@ -8,6 +8,7 @@ import ( oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/wait" + "github.com/stackitcloud/stackit-sdk-go/services/mariadb" ) const ( @@ -20,12 +21,12 @@ const ( // Interface needed for tests type APIClientInstanceInterface interface { - GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*Instance, error) + GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*mariadb.Instance, error) } // Interface needed for tests type APIClientCredentialsInterface interface { - GetCredentialsExecute(ctx context.Context, projectId, instanceId, credentialsId string) (*CredentialsResponse, error) + GetCredentialsExecute(ctx context.Context, projectId, instanceId, credentialsId string) (*mariadb.CredentialsResponse, error) } // CreateInstanceWaitHandler will wait for creation diff --git a/services/opensearch/wait_test.go b/services/mariadb/wait/wait_test.go similarity index 93% rename from services/opensearch/wait_test.go rename to services/mariadb/wait/wait_test.go index 3526d17d7..7c1cd59c0 100644 --- a/services/opensearch/wait_test.go +++ b/services/mariadb/wait/wait_test.go @@ -1,4 +1,4 @@ -package opensearch +package wait import ( "context" @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp" oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/utils" + "github.com/stackitcloud/stackit-sdk-go/services/mariadb" ) // Used for testing instance operations @@ -26,7 +27,7 @@ var ( instanceTypeDelete = InstanceTypeDelete ) -func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ string) (*Instance, error) { +func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ string) (*mariadb.Instance, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, @@ -34,9 +35,9 @@ func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ str } if *a.resourceOperation == InstanceTypeDelete && a.resourceState == InstanceStateSuccess { if a.deletionSucceedsWithErrors { - return &Instance{ + return &mariadb.Instance{ InstanceId: &a.resourceId, - LastOperation: &LastOperation{ + LastOperation: &mariadb.LastOperation{ Description: &a.resourceDescription, Type: a.resourceOperation, State: &a.resourceState, @@ -48,9 +49,9 @@ func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ str } } - return &Instance{ + return &mariadb.Instance{ InstanceId: &a.resourceId, - LastOperation: &LastOperation{ + LastOperation: &mariadb.LastOperation{ Description: &a.resourceDescription, Type: a.resourceOperation, State: &a.resourceState, @@ -66,7 +67,7 @@ type apiClientCredentialsMocked struct { deletionSucceeds bool } -func (a *apiClientCredentialsMocked) GetCredentialsExecute(_ context.Context, _, _, _ string) (*CredentialsResponse, error) { +func (a *apiClientCredentialsMocked) GetCredentialsExecute(_ context.Context, _, _, _ string) (*mariadb.CredentialsResponse, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, @@ -79,7 +80,7 @@ func (a *apiClientCredentialsMocked) GetCredentialsExecute(_ context.Context, _, } } - return &CredentialsResponse{ + return &mariadb.CredentialsResponse{ Id: &a.resourceId, }, nil } @@ -126,11 +127,11 @@ func TestCreateInstanceWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *Instance + var wantRes *mariadb.Instance if !tt.getFails { - wantRes = &Instance{ + wantRes = &mariadb.Instance{ InstanceId: &instanceId, - LastOperation: &LastOperation{ + LastOperation: &mariadb.LastOperation{ Type: &instanceTypeCreate, State: &tt.resourceState, Description: utils.Ptr(""), @@ -198,11 +199,11 @@ func TestUpdateInstanceWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *Instance + var wantRes *mariadb.Instance if !tt.getFails { - wantRes = &Instance{ + wantRes = &mariadb.Instance{ InstanceId: &instanceId, - LastOperation: &LastOperation{ + LastOperation: &mariadb.LastOperation{ Type: &instanceTypeUpdate, State: &tt.resourceState, Description: utils.Ptr(""), @@ -327,9 +328,9 @@ func TestCreateCredentialsWaitHandler(t *testing.T) { operationSucceeds: tt.operationSucceeds, } - var wantRes *CredentialsResponse + var wantRes *mariadb.CredentialsResponse if !tt.getFails && tt.operationSucceeds { - wantRes = &CredentialsResponse{ + wantRes = &mariadb.CredentialsResponse{ Id: &credentialsId, } } else if !tt.getFails && !tt.operationSucceeds { @@ -390,9 +391,9 @@ func TestDeleteCredentialsWaitHandler(t *testing.T) { deletionSucceeds: tt.deletionSucceeds, } - var wantRes *CredentialsResponse + var wantRes *mariadb.CredentialsResponse if !tt.getFails && !tt.deletionSucceeds { - wantRes = &CredentialsResponse{ + wantRes = &mariadb.CredentialsResponse{ Id: &credentialsId, } } else if !tt.getFails && tt.deletionSucceeds { diff --git a/services/mongodbflex/wait.go b/services/mongodbflex/wait/wait.go similarity index 96% rename from services/mongodbflex/wait.go rename to services/mongodbflex/wait/wait.go index 646ff2fe0..364015bf3 100644 --- a/services/mongodbflex/wait.go +++ b/services/mongodbflex/wait/wait.go @@ -1,4 +1,4 @@ -package mongodbflex +package wait import ( "context" @@ -8,6 +8,7 @@ import ( oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/wait" + "github.com/stackitcloud/stackit-sdk-go/services/mongodbflex" ) const ( @@ -20,7 +21,7 @@ const ( // Interface needed for tests type APIClientInstanceInterface interface { - GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*GetInstanceResponse, error) + GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*mongodbflex.GetInstanceResponse, error) } // CreateInstanceWaitHandler will wait for creation diff --git a/services/mongodbflex/wait_test.go b/services/mongodbflex/wait/wait_test.go similarity index 91% rename from services/mongodbflex/wait_test.go rename to services/mongodbflex/wait/wait_test.go index a14616f54..2b24dd0f8 100644 --- a/services/mongodbflex/wait_test.go +++ b/services/mongodbflex/wait/wait_test.go @@ -1,4 +1,4 @@ -package mongodbflex +package wait import ( "context" @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" + "github.com/stackitcloud/stackit-sdk-go/services/mongodbflex" ) // Used for testing instance operations @@ -17,7 +18,7 @@ type apiClientInstanceMocked struct { instanceGetFails bool } -func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ string) (*GetInstanceResponse, error) { +func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ string) (*mongodbflex.GetInstanceResponse, error) { if a.instanceGetFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, @@ -30,8 +31,8 @@ func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ str } } - return &GetInstanceResponse{ - Item: &InstanceSingleInstance{ + return &mongodbflex.GetInstanceResponse{ + Item: &mongodbflex.InstanceSingleInstance{ Id: &a.instanceId, Status: &a.instanceState, }, @@ -86,10 +87,10 @@ func TestCreateInstanceWaitHandler(t *testing.T) { instanceGetFails: tt.instanceGetFails, } - var wantRes *GetInstanceResponse + var wantRes *mongodbflex.GetInstanceResponse if (tt.instanceState == InstanceStateSuccess) && !tt.instanceGetFails { - wantRes = &GetInstanceResponse{ - Item: &InstanceSingleInstance{ + wantRes = &mongodbflex.GetInstanceResponse{ + Item: &mongodbflex.InstanceSingleInstance{ Id: &instanceId, Status: &tt.instanceState, }, @@ -160,10 +161,10 @@ func TestUpdateInstanceWaitHandler(t *testing.T) { instanceGetFails: tt.instanceGetFails, } - var wantRes *GetInstanceResponse + var wantRes *mongodbflex.GetInstanceResponse if !tt.instanceGetFails { - wantRes = &GetInstanceResponse{ - Item: &InstanceSingleInstance{ + wantRes = &mongodbflex.GetInstanceResponse{ + Item: &mongodbflex.InstanceSingleInstance{ Id: &instanceId, Status: &tt.instanceState, }, diff --git a/services/objectstorage/wait.go b/services/objectstorage/wait/wait.go similarity index 91% rename from services/objectstorage/wait.go rename to services/objectstorage/wait/wait.go index 0680fcfb2..2043b1d30 100644 --- a/services/objectstorage/wait.go +++ b/services/objectstorage/wait/wait.go @@ -1,4 +1,4 @@ -package objectstorage +package wait import ( "context" @@ -7,11 +7,12 @@ import ( oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/wait" + "github.com/stackitcloud/stackit-sdk-go/services/objectstorage" ) // Interface needed for tests type APIClientBucketInterface interface { - GetBucketExecute(ctx context.Context, projectId string, bucketName string) (*GetBucketResponse, error) + GetBucketExecute(ctx context.Context, projectId string, bucketName string) (*objectstorage.GetBucketResponse, error) } // CreateBucketWaitHandler will wait for creation diff --git a/services/objectstorage/wait_test.go b/services/objectstorage/wait/wait_test.go similarity index 89% rename from services/objectstorage/wait_test.go rename to services/objectstorage/wait/wait_test.go index 5655fe2dc..c64f5487f 100644 --- a/services/objectstorage/wait_test.go +++ b/services/objectstorage/wait/wait_test.go @@ -1,4 +1,4 @@ -package objectstorage +package wait import ( "context" @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" + "github.com/stackitcloud/stackit-sdk-go/services/objectstorage" ) // Used for testing bucket operations @@ -15,7 +16,7 @@ type apiClientBucketMocked struct { bucketGetFails bool } -func (a *apiClientBucketMocked) GetBucketExecute(_ context.Context, _, _ string) (*GetBucketResponse, error) { +func (a *apiClientBucketMocked) GetBucketExecute(_ context.Context, _, _ string) (*objectstorage.GetBucketResponse, error) { if a.bucketGetFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, @@ -28,7 +29,7 @@ func (a *apiClientBucketMocked) GetBucketExecute(_ context.Context, _, _ string) } } - return &GetBucketResponse{}, nil + return &objectstorage.GetBucketResponse{}, nil } func TestCreateBucketWaitHandler(t *testing.T) { @@ -54,9 +55,9 @@ func TestCreateBucketWaitHandler(t *testing.T) { bucketGetFails: tt.bucketGetFails, } - var wantRes *GetBucketResponse + var wantRes *objectstorage.GetBucketResponse if !tt.bucketGetFails { - wantRes = &GetBucketResponse{} + wantRes = &objectstorage.GetBucketResponse{} } handler := CreateBucketWaitHandler(context.Background(), apiClient, "", "") diff --git a/services/opensearch/wait/wait.go b/services/opensearch/wait/wait.go new file mode 100644 index 000000000..91dd57861 --- /dev/null +++ b/services/opensearch/wait/wait.go @@ -0,0 +1,140 @@ +package wait + +import ( + "context" + "fmt" + "net/http" + "strings" + + oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" + "github.com/stackitcloud/stackit-sdk-go/core/wait" + "github.com/stackitcloud/stackit-sdk-go/services/opensearch" +) + +const ( + InstanceStateSuccess = "succeeded" + InstanceStateFailed = "failed" + InstanceTypeCreate = "create" + InstanceTypeUpdate = "update" + InstanceTypeDelete = "delete" +) + +// Interface needed for tests +type APIClientInstanceInterface interface { + GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*opensearch.Instance, error) +} + +// Interface needed for tests +type APIClientCredentialsInterface interface { + GetCredentialsExecute(ctx context.Context, projectId, instanceId, credentialsId string) (*opensearch.CredentialsResponse, error) +} + +// CreateInstanceWaitHandler will wait for creation +func CreateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.Handler { + return wait.New(func() (res interface{}, done bool, err error) { + s, err := a.GetInstanceExecute(ctx, projectId, instanceId) + if err != nil { + return nil, false, err + } + if s.InstanceId == nil || s.LastOperation == nil || s.LastOperation.Type == nil || s.LastOperation.State == nil { + return s, false, fmt.Errorf("create failed for instance with id %s. The response is not valid: the instance id, the last operation type or the state are missing", instanceId) + } + if *s.InstanceId == instanceId && *s.LastOperation.Type == InstanceTypeCreate && *s.LastOperation.State == InstanceStateSuccess { + return s, true, nil + } + if *s.InstanceId == instanceId && *s.LastOperation.Type == InstanceTypeCreate && *s.LastOperation.State == InstanceStateFailed { + return s, true, fmt.Errorf("create failed for instance with id %s", instanceId) + } + return s, false, nil + }) +} + +// UpdateInstanceWaitHandler will wait for update +func UpdateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.Handler { + return wait.New(func() (res interface{}, done bool, err error) { + s, err := a.GetInstanceExecute(ctx, projectId, instanceId) + if err != nil { + return nil, false, err + } + if s.InstanceId == nil || s.LastOperation == nil || s.LastOperation.Type == nil || s.LastOperation.State == nil { + return s, false, fmt.Errorf("update failed for instance with id %s. The response is not valid: the instance id, the last operation type or the state are missing", instanceId) + } + if *s.InstanceId == instanceId && *s.LastOperation.Type == InstanceTypeUpdate && *s.LastOperation.State == InstanceStateSuccess { + return s, true, nil + } + if *s.InstanceId == instanceId && *s.LastOperation.Type == InstanceTypeUpdate && *s.LastOperation.State == InstanceStateFailed { + return s, true, fmt.Errorf("create failed for instance with id %s", instanceId) + } + return s, false, nil + }) +} + +// DeleteInstanceWaitHandler will wait for delete +func DeleteInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.Handler { + return wait.New(func() (res interface{}, done bool, err error) { + s, err := a.GetInstanceExecute(ctx, projectId, instanceId) + if err == nil { + if s.LastOperation == nil || s.LastOperation.Type == nil || s.LastOperation.State == nil || s.LastOperation.Description == nil { + return s, false, fmt.Errorf("delete failed for instance with id %s. The response is not valid: The last operation type, description or the state are missing", instanceId) + } + if *s.LastOperation.Type != InstanceTypeDelete { + return nil, false, nil + } + if *s.LastOperation.State == InstanceStateSuccess { + if strings.Contains(*s.LastOperation.Description, "DeleteFailed") || strings.Contains(*s.LastOperation.Description, "failed") { + return s, true, fmt.Errorf("instance was deleted successfully but has errors: %s", *s.LastOperation.Description) + } + return s, true, nil + } + return s, false, nil + } + oapiErr, ok := err.(*oapiError.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped + if !ok { + return nil, false, fmt.Errorf("could not convert error to oapiError.GenericOpenAPIError") + } + if oapiErr.StatusCode != http.StatusGone { + return nil, false, err + } + return nil, true, nil + }) +} + +// CreateCredentialsWaitHandler will wait for creation +func CreateCredentialsWaitHandler(ctx context.Context, a APIClientCredentialsInterface, projectId, instanceId, credentialsId string) *wait.Handler { + return wait.New(func() (res interface{}, done bool, err error) { + s, err := a.GetCredentialsExecute(ctx, projectId, instanceId, credentialsId) + if err != nil { + oapiErr, ok := err.(*oapiError.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped + if !ok { + return nil, false, fmt.Errorf("could not convert error to oapiError.GenericOpenAPIError") + } + // If the request returns 404, the credentials have not been created yet + if oapiErr.StatusCode == http.StatusNotFound { + return nil, false, nil + } + return nil, false, err + } + if *s.Id == credentialsId { + return s, true, nil + } + return s, false, nil + }) +} + +// DeleteCredentialsWaitHandler will wait for deletion +func DeleteCredentialsWaitHandler(ctx context.Context, a APIClientCredentialsInterface, projectId, instanceId, credentialsId string) *wait.Handler { + return wait.New(func() (res interface{}, done bool, err error) { + s, err := a.GetCredentialsExecute(ctx, projectId, instanceId, credentialsId) + if err != nil { + oapiErr, ok := err.(*oapiError.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped + if !ok { + return nil, false, fmt.Errorf("could not convert error to oapiError.GenericOpenAPIError") + } + if oapiErr.StatusCode != http.StatusNotFound && oapiErr.StatusCode != http.StatusGone { + return nil, false, err + } + return nil, true, nil + } + return s, false, nil + }) +} diff --git a/services/opensearch/wait/wait_test.go b/services/opensearch/wait/wait_test.go new file mode 100644 index 000000000..41acd434b --- /dev/null +++ b/services/opensearch/wait/wait_test.go @@ -0,0 +1,418 @@ +package wait + +import ( + "context" + "testing" + "time" + + "github.com/google/go-cmp/cmp" + oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" + "github.com/stackitcloud/stackit-sdk-go/core/utils" + "github.com/stackitcloud/stackit-sdk-go/services/opensearch" +) + +// Used for testing instance operations +type apiClientInstanceMocked struct { + getFails bool + deletionSucceedsWithErrors bool + resourceId string + resourceOperation *string + resourceState string + resourceDescription string +} + +var ( + instanceTypeCreate = InstanceTypeCreate + instanceTypeUpdate = InstanceTypeUpdate + instanceTypeDelete = InstanceTypeDelete +) + +func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ string) (*opensearch.Instance, error) { + if a.getFails { + return nil, &oapiError.GenericOpenAPIError{ + StatusCode: 500, + } + } + if *a.resourceOperation == InstanceTypeDelete && a.resourceState == InstanceStateSuccess { + if a.deletionSucceedsWithErrors { + return &opensearch.Instance{ + InstanceId: &a.resourceId, + LastOperation: &opensearch.LastOperation{ + Description: &a.resourceDescription, + Type: a.resourceOperation, + State: &a.resourceState, + }, + }, nil + } + return nil, &oapiError.GenericOpenAPIError{ + StatusCode: 410, + } + } + + return &opensearch.Instance{ + InstanceId: &a.resourceId, + LastOperation: &opensearch.LastOperation{ + Description: &a.resourceDescription, + Type: a.resourceOperation, + State: &a.resourceState, + }, + }, nil +} + +// Used for testing credentials operations +type apiClientCredentialsMocked struct { + getFails bool + resourceId string + operationSucceeds bool + deletionSucceeds bool +} + +func (a *apiClientCredentialsMocked) GetCredentialsExecute(_ context.Context, _, _, _ string) (*opensearch.CredentialsResponse, error) { + if a.getFails { + return nil, &oapiError.GenericOpenAPIError{ + StatusCode: 500, + } + } + + if !a.operationSucceeds || a.deletionSucceeds { + return nil, &oapiError.GenericOpenAPIError{ + StatusCode: 404, + } + } + + return &opensearch.CredentialsResponse{ + Id: &a.resourceId, + }, nil +} + +func TestCreateInstanceWaitHandler(t *testing.T) { + tests := []struct { + desc string + getFails bool + resourceState string + wantErr bool + }{ + { + desc: "create_succeeded", + getFails: false, + resourceState: InstanceStateSuccess, + wantErr: false, + }, + { + desc: "create_failed", + getFails: false, + resourceState: InstanceStateFailed, + wantErr: true, + }, + { + desc: "get_fails", + getFails: true, + wantErr: true, + }, + { + desc: "timeout", + getFails: false, + resourceState: "ANOTHER STATE", + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + instanceId := "foo-bar" + + apiClient := &apiClientInstanceMocked{ + getFails: tt.getFails, + resourceId: instanceId, + resourceOperation: &instanceTypeCreate, + resourceState: tt.resourceState, + } + + var wantRes *opensearch.Instance + if !tt.getFails { + wantRes = &opensearch.Instance{ + InstanceId: &instanceId, + LastOperation: &opensearch.LastOperation{ + Type: &instanceTypeCreate, + State: &tt.resourceState, + Description: utils.Ptr(""), + }, + } + } + + handler := CreateInstanceWaitHandler(context.Background(), apiClient, "pid", instanceId) + + gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) + + if (err != nil) != tt.wantErr { + t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) + } + if wantRes == nil && gotRes != nil { + t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) + } + diff := cmp.Diff(gotRes, wantRes) + if wantRes != nil && diff != "" { + t.Fatalf("handler gotRes = %+v\n want %+v\n diff = %s", gotRes, wantRes, diff) + } + }) + } +} + +func TestUpdateInstanceWaitHandler(t *testing.T) { + tests := []struct { + desc string + getFails bool + resourceState string + wantErr bool + }{ + { + desc: "update_succeeded", + getFails: false, + resourceState: InstanceStateSuccess, + wantErr: false, + }, + { + desc: "update_failed", + getFails: false, + resourceState: InstanceStateFailed, + wantErr: true, + }, + { + desc: "get_fails", + getFails: true, + wantErr: true, + }, + { + desc: "timeout", + getFails: false, + resourceState: "ANOTHER STATE", + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + instanceId := "foo-bar" + + apiClient := &apiClientInstanceMocked{ + getFails: tt.getFails, + resourceId: instanceId, + resourceOperation: &instanceTypeUpdate, + resourceState: tt.resourceState, + } + + var wantRes *opensearch.Instance + if !tt.getFails { + wantRes = &opensearch.Instance{ + InstanceId: &instanceId, + LastOperation: &opensearch.LastOperation{ + Type: &instanceTypeUpdate, + State: &tt.resourceState, + Description: utils.Ptr(""), + }, + } + } + + handler := UpdateInstanceWaitHandler(context.Background(), apiClient, "", instanceId) + + gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) + + if (err != nil) != tt.wantErr { + t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) + } + if wantRes == nil && gotRes != nil { + t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) + } + if wantRes != nil && !cmp.Equal(gotRes, wantRes) { + t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) + } + }) + } +} + +func TestDeleteInstanceWaitHandler(t *testing.T) { + tests := []struct { + desc string + getFails bool + deleteSucceeedsWithErrors bool + resourceState string + resourceDescription string + wantErr bool + }{ + { + desc: "delete_succeeded", + getFails: false, + deleteSucceeedsWithErrors: false, + resourceState: InstanceStateSuccess, + wantErr: false, + }, + { + desc: "delete_failed", + getFails: false, + deleteSucceeedsWithErrors: false, + resourceState: InstanceStateFailed, + wantErr: true, + }, + { + desc: "delete_succeeds_with_errors", + getFails: false, + resourceState: InstanceStateSuccess, + deleteSucceeedsWithErrors: true, + resourceDescription: "Deleting resource: cf failed with error: DeleteFailed", + wantErr: true, + }, + { + desc: "get_fails", + deleteSucceeedsWithErrors: false, + getFails: true, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + instanceId := "foo-bar" + + apiClient := &apiClientInstanceMocked{ + getFails: tt.getFails, + deletionSucceedsWithErrors: tt.deleteSucceeedsWithErrors, + resourceId: instanceId, + resourceOperation: &instanceTypeDelete, + resourceDescription: tt.resourceDescription, + resourceState: tt.resourceState, + } + + handler := DeleteInstanceWaitHandler(context.Background(), apiClient, "", instanceId) + + gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) + + if (err != nil) != tt.wantErr { + t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) + } + if err == nil && gotRes != nil { + t.Fatalf("handler gotRes = %v, want %v", gotRes, nil) + } + }) + } +} + +func TestCreateCredentialsWaitHandler(t *testing.T) { + tests := []struct { + desc string + getFails bool + operationSucceeds bool + wantErr bool + }{ + { + desc: "create_succeeded", + getFails: false, + operationSucceeds: true, + wantErr: false, + }, + { + desc: "create_failed", + getFails: false, + operationSucceeds: false, + wantErr: true, + }, + { + desc: "get_fails", + getFails: true, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + credentialsId := "foo-bar" + + apiClient := &apiClientCredentialsMocked{ + getFails: tt.getFails, + resourceId: credentialsId, + operationSucceeds: tt.operationSucceeds, + } + + var wantRes *opensearch.CredentialsResponse + if !tt.getFails && tt.operationSucceeds { + wantRes = &opensearch.CredentialsResponse{ + Id: &credentialsId, + } + } else if !tt.getFails && !tt.operationSucceeds { + wantRes = nil + } + + handler := CreateCredentialsWaitHandler(context.Background(), apiClient, "", "", credentialsId) + + gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) + + if (err != nil) != tt.wantErr { + t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) + } + if wantRes == nil && gotRes != nil { + t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) + } + if wantRes != nil && !cmp.Equal(gotRes, wantRes) { + t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) + } + }) + } +} + +func TestDeleteCredentialsWaitHandler(t *testing.T) { + tests := []struct { + desc string + getFails bool + deletionSucceeds bool + wantErr bool + }{ + { + desc: "delete_succeeded", + getFails: false, + deletionSucceeds: true, + wantErr: false, + }, + { + desc: "delete_failed", + getFails: false, + deletionSucceeds: false, + wantErr: true, + }, + { + desc: "get_fails", + getFails: true, + deletionSucceeds: false, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.desc, func(t *testing.T) { + credentialsId := "foo-bar" + + apiClient := &apiClientCredentialsMocked{ + getFails: tt.getFails, + resourceId: credentialsId, + operationSucceeds: true, + deletionSucceeds: tt.deletionSucceeds, + } + + var wantRes *opensearch.CredentialsResponse + if !tt.getFails && !tt.deletionSucceeds { + wantRes = &opensearch.CredentialsResponse{ + Id: &credentialsId, + } + } else if !tt.getFails && tt.deletionSucceeds { + wantRes = nil + } + + handler := DeleteCredentialsWaitHandler(context.Background(), apiClient, "", "", credentialsId) + + gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) + + if (err != nil) != tt.wantErr { + t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) + } + if wantRes == nil && gotRes != nil { + t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) + } + if wantRes != nil && !cmp.Equal(gotRes, wantRes) { + t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) + } + }) + } +} diff --git a/services/postgresflex/wait.go b/services/postgresflex/wait/wait.go similarity index 94% rename from services/postgresflex/wait.go rename to services/postgresflex/wait/wait.go index 2655edbdf..33ec68dcf 100644 --- a/services/postgresflex/wait.go +++ b/services/postgresflex/wait/wait.go @@ -1,4 +1,4 @@ -package postgresflex +package wait import ( "context" @@ -6,6 +6,7 @@ import ( oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/wait" + "github.com/stackitcloud/stackit-sdk-go/services/postgresflex" ) const ( @@ -17,19 +18,19 @@ const ( // Interface needed for tests type APIClientInstanceInterface interface { - GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*InstanceResponse, error) - GetUsersExecute(ctx context.Context, projectId string, instanceId string) (*UsersResponse, error) + GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*postgresflex.InstanceResponse, error) + GetUsersExecute(ctx context.Context, projectId string, instanceId string) (*postgresflex.UsersResponse, error) } // Interface needed for tests type APIClientUserInterface interface { - GetUserExecute(ctx context.Context, projectId, instanceId, userId string) (*UserResponse, error) + GetUserExecute(ctx context.Context, projectId, instanceId, userId string) (*postgresflex.UserResponse, error) } // CreateInstanceWaitHandler will wait for creation func CreateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.Handler { instanceCreated := false - var instanceGetResponse *InstanceResponse + var instanceGetResponse *postgresflex.InstanceResponse return wait.New(func() (res interface{}, done bool, err error) { if !instanceCreated { diff --git a/services/postgresflex/wait_test.go b/services/postgresflex/wait/wait_test.go similarity index 90% rename from services/postgresflex/wait_test.go rename to services/postgresflex/wait/wait_test.go index 52b3456ea..a950fb9f4 100644 --- a/services/postgresflex/wait_test.go +++ b/services/postgresflex/wait/wait_test.go @@ -1,4 +1,4 @@ -package postgresflex +package wait import ( "context" @@ -7,6 +7,7 @@ import ( "github.com/google/go-cmp/cmp" oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" + "github.com/stackitcloud/stackit-sdk-go/services/postgresflex" ) // Used for testing instance operations @@ -18,7 +19,7 @@ type apiClientInstanceMocked struct { usersGetErrorStatus int } -func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ string) (*InstanceResponse, error) { +func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ string) (*postgresflex.InstanceResponse, error) { if a.instanceGetFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, @@ -31,15 +32,15 @@ func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ str } } - return &InstanceResponse{ - Item: &InstanceSingleInstance{ + return &postgresflex.InstanceResponse{ + Item: &postgresflex.InstanceSingleInstance{ Id: &a.instanceId, Status: &a.instanceState, }, }, nil } -func (a *apiClientInstanceMocked) GetUsersExecute(_ context.Context, _, _ string) (*UsersResponse, error) { +func (a *apiClientInstanceMocked) GetUsersExecute(_ context.Context, _, _ string) (*postgresflex.UsersResponse, error) { if a.usersGetErrorStatus != 0 { return nil, &oapiError.GenericOpenAPIError{ StatusCode: a.usersGetErrorStatus, @@ -47,9 +48,9 @@ func (a *apiClientInstanceMocked) GetUsersExecute(_ context.Context, _, _ string } aux := int32(0) - return &UsersResponse{ + return &postgresflex.UsersResponse{ Count: &aux, - Items: &[]InstanceListUser{}, + Items: &[]postgresflex.InstanceListUser{}, }, nil } @@ -60,7 +61,7 @@ type apiClientUserMocked struct { isUserDeleted bool } -func (a *apiClientUserMocked) GetUserExecute(_ context.Context, _, _, _ string) (*UserResponse, error) { +func (a *apiClientUserMocked) GetUserExecute(_ context.Context, _, _, _ string) (*postgresflex.UserResponse, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, @@ -73,8 +74,8 @@ func (a *apiClientUserMocked) GetUserExecute(_ context.Context, _, _, _ string) } } - return &UserResponse{ - Item: &UserResponseUser{ + return &postgresflex.UserResponse{ + Item: &postgresflex.UserResponseUser{ Id: &a.userId, }, }, nil @@ -143,10 +144,10 @@ func TestCreateInstanceWaitHandler(t *testing.T) { usersGetErrorStatus: tt.usersGetErrorStatus, } - var wantRes *InstanceResponse + var wantRes *postgresflex.InstanceResponse if (tt.instanceState == InstanceStateSuccess) && !tt.instanceGetFails && (tt.usersGetErrorStatus == 0) { - wantRes = &InstanceResponse{ - Item: &InstanceSingleInstance{ + wantRes = &postgresflex.InstanceResponse{ + Item: &postgresflex.InstanceSingleInstance{ Id: &instanceId, Status: &tt.instanceState, }, @@ -217,10 +218,10 @@ func TestUpdateInstanceWaitHandler(t *testing.T) { instanceGetFails: tt.instanceGetFails, } - var wantRes *InstanceResponse + var wantRes *postgresflex.InstanceResponse if !tt.instanceGetFails { - wantRes = &InstanceResponse{ - Item: &InstanceSingleInstance{ + wantRes = &postgresflex.InstanceResponse{ + Item: &postgresflex.InstanceSingleInstance{ Id: &instanceId, Status: &tt.instanceState, }, diff --git a/services/postgresql/wait/wait.go b/services/postgresql/wait/wait.go new file mode 100644 index 000000000..60a615bb9 --- /dev/null +++ b/services/postgresql/wait/wait.go @@ -0,0 +1,140 @@ +package wait + +import ( + "context" + "fmt" + "net/http" + "strings" + + oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" + "github.com/stackitcloud/stackit-sdk-go/core/wait" + "github.com/stackitcloud/stackit-sdk-go/services/postgresql" +) + +const ( + InstanceStateSuccess = "succeeded" + InstanceStateFailed = "failed" + InstanceTypeCreate = "create" + InstanceTypeUpdate = "update" + InstanceTypeDelete = "delete" +) + +// Interface needed for tests +type APIClientInstanceInterface interface { + GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*postgresql.Instance, error) +} + +// Interface needed for tests +type APIClientCredentialsInterface interface { + GetCredentialsExecute(ctx context.Context, projectId, instanceId, credentialsId string) (*postgresql.CredentialsResponse, error) +} + +// CreateInstanceWaitHandler will wait for creation +func CreateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.Handler { + return wait.New(func() (res interface{}, done bool, err error) { + s, err := a.GetInstanceExecute(ctx, projectId, instanceId) + if err != nil { + return nil, false, err + } + if s.InstanceId == nil || s.LastOperation == nil || s.LastOperation.Type == nil || s.LastOperation.State == nil { + return s, false, fmt.Errorf("create failed for instance with id %s. The response is not valid: the instance id, the last operation type or the state are missing", instanceId) + } + if *s.InstanceId == instanceId && *s.LastOperation.Type == InstanceTypeCreate && *s.LastOperation.State == InstanceStateSuccess { + return s, true, nil + } + if *s.InstanceId == instanceId && *s.LastOperation.Type == InstanceTypeCreate && *s.LastOperation.State == InstanceStateFailed { + return s, true, fmt.Errorf("create failed for instance with id %s", instanceId) + } + return s, false, nil + }) +} + +// UpdateInstanceWaitHandler will wait for update +func UpdateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.Handler { + return wait.New(func() (res interface{}, done bool, err error) { + s, err := a.GetInstanceExecute(ctx, projectId, instanceId) + if err != nil { + return nil, false, err + } + if s.InstanceId == nil || s.LastOperation == nil || s.LastOperation.Type == nil || s.LastOperation.State == nil { + return s, false, fmt.Errorf("update failed for instance with id %s. The response is not valid: the instance id, the last operation type or the state are missing", instanceId) + } + if *s.InstanceId == instanceId && *s.LastOperation.Type == InstanceTypeUpdate && *s.LastOperation.State == InstanceStateSuccess { + return s, true, nil + } + if *s.InstanceId == instanceId && *s.LastOperation.Type == InstanceTypeUpdate && *s.LastOperation.State == InstanceStateFailed { + return s, true, fmt.Errorf("create failed for instance with id %s", instanceId) + } + return s, false, nil + }) +} + +// DeleteInstanceWaitHandler will wait for delete +func DeleteInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.Handler { + return wait.New(func() (res interface{}, done bool, err error) { + s, err := a.GetInstanceExecute(ctx, projectId, instanceId) + if err == nil { + if s.LastOperation == nil || s.LastOperation.Type == nil || s.LastOperation.State == nil || s.LastOperation.Description == nil { + return s, false, fmt.Errorf("delete failed for instance with id %s. The response is not valid: The last operation type, description or the state are missing", instanceId) + } + if *s.LastOperation.Type != InstanceTypeDelete { + return nil, false, nil + } + if *s.LastOperation.State == InstanceStateSuccess { + if strings.Contains(*s.LastOperation.Description, "DeleteFailed") || strings.Contains(*s.LastOperation.Description, "failed") { + return s, true, fmt.Errorf("instance was deleted successfully but has errors: %s", *s.LastOperation.Description) + } + return s, true, nil + } + return s, false, nil + } + oapiErr, ok := err.(*oapiError.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped + if !ok { + return nil, false, fmt.Errorf("could not convert error to oapiError.GenericOpenAPIError") + } + if oapiErr.StatusCode != http.StatusGone { + return nil, false, err + } + return nil, true, nil + }) +} + +// CreateCredentialsWaitHandler will wait for creation +func CreateCredentialsWaitHandler(ctx context.Context, a APIClientCredentialsInterface, projectId, instanceId, credentialsId string) *wait.Handler { + return wait.New(func() (res interface{}, done bool, err error) { + s, err := a.GetCredentialsExecute(ctx, projectId, instanceId, credentialsId) + if err != nil { + oapiErr, ok := err.(*oapiError.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped + if !ok { + return nil, false, fmt.Errorf("could not convert error to oapiError.GenericOpenAPIError") + } + // If the request returns 404, the credentials have not been created yet + if oapiErr.StatusCode == http.StatusNotFound { + return nil, false, nil + } + return nil, false, err + } + if *s.Id == credentialsId { + return s, true, nil + } + return s, false, nil + }) +} + +// DeleteCredentialsWaitHandler will wait for deletion +func DeleteCredentialsWaitHandler(ctx context.Context, a APIClientCredentialsInterface, projectId, instanceId, credentialsId string) *wait.Handler { + return wait.New(func() (res interface{}, done bool, err error) { + s, err := a.GetCredentialsExecute(ctx, projectId, instanceId, credentialsId) + if err != nil { + oapiErr, ok := err.(*oapiError.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped + if !ok { + return nil, false, fmt.Errorf("could not convert error to oapiError.GenericOpenAPIError") + } + if oapiErr.StatusCode != http.StatusNotFound && oapiErr.StatusCode != http.StatusGone { + return nil, false, err + } + return nil, true, nil + } + return s, false, nil + }) +} diff --git a/services/postgresql/wait_test.go b/services/postgresql/wait/wait_test.go similarity index 92% rename from services/postgresql/wait_test.go rename to services/postgresql/wait/wait_test.go index 8e3a5f6a7..b536e1bdb 100644 --- a/services/postgresql/wait_test.go +++ b/services/postgresql/wait/wait_test.go @@ -1,4 +1,4 @@ -package postgresql +package wait import ( "context" @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp" oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/utils" + "github.com/stackitcloud/stackit-sdk-go/services/postgresql" ) // Used for testing instance operations @@ -20,7 +21,7 @@ type apiClientInstanceMocked struct { resourceDescription *string } -func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ string) (*Instance, error) { +func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ string) (*postgresql.Instance, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, @@ -28,9 +29,9 @@ func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ str } if a.resourceOperation == InstanceTypeDelete && a.resourceState == InstanceStateSuccess { if a.deletionSucceedsWithErrors { - return &Instance{ + return &postgresql.Instance{ InstanceId: &a.resourceId, - LastOperation: &LastOperation{ + LastOperation: &postgresql.LastOperation{ Description: a.resourceDescription, Type: &a.resourceOperation, State: &a.resourceState, @@ -42,9 +43,9 @@ func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ str } } - return &Instance{ + return &postgresql.Instance{ InstanceId: &a.resourceId, - LastOperation: &LastOperation{ + LastOperation: &postgresql.LastOperation{ Description: a.resourceDescription, Type: &a.resourceOperation, State: &a.resourceState, @@ -60,7 +61,7 @@ type apiClientCredentialsMocked struct { deletionSucceeds bool } -func (a *apiClientCredentialsMocked) GetCredentialsExecute(_ context.Context, _, _, _ string) (*CredentialsResponse, error) { +func (a *apiClientCredentialsMocked) GetCredentialsExecute(_ context.Context, _, _, _ string) (*postgresql.CredentialsResponse, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, @@ -73,7 +74,7 @@ func (a *apiClientCredentialsMocked) GetCredentialsExecute(_ context.Context, _, } } - return &CredentialsResponse{ + return &postgresql.CredentialsResponse{ Id: &a.resourceId, }, nil } @@ -120,11 +121,11 @@ func TestCreateInstanceWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *Instance + var wantRes *postgresql.Instance if !tt.getFails { - wantRes = &Instance{ + wantRes = &postgresql.Instance{ InstanceId: &instanceId, - LastOperation: &LastOperation{ + LastOperation: &postgresql.LastOperation{ Type: &createType, State: &tt.resourceState, Description: nil, @@ -192,11 +193,11 @@ func TestUpdateInstanceWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *Instance + var wantRes *postgresql.Instance if !tt.getFails { - wantRes = &Instance{ + wantRes = &postgresql.Instance{ InstanceId: &instanceId, - LastOperation: &LastOperation{ + LastOperation: &postgresql.LastOperation{ Type: &updateType, State: &tt.resourceState, }, @@ -320,9 +321,9 @@ func TestCreateCredentialsWaitHandler(t *testing.T) { operationSucceeds: tt.operationSucceeds, } - var wantRes *CredentialsResponse + var wantRes *postgresql.CredentialsResponse if !tt.getFails && tt.operationSucceeds { - wantRes = &CredentialsResponse{ + wantRes = &postgresql.CredentialsResponse{ Id: &credentialsId, } } else if !tt.getFails && !tt.operationSucceeds { @@ -383,9 +384,9 @@ func TestDeleteCredentialsWaitHandler(t *testing.T) { deletionSucceeds: tt.deletionSucceeds, } - var wantRes *CredentialsResponse + var wantRes *postgresql.CredentialsResponse if !tt.getFails && !tt.deletionSucceeds { - wantRes = &CredentialsResponse{ + wantRes = &postgresql.CredentialsResponse{ Id: &credentialsId, } } else if !tt.getFails && tt.deletionSucceeds { diff --git a/services/rabbitmq/wait.go b/services/rabbitmq/wait.go deleted file mode 100644 index ac772eaaf..000000000 --- a/services/rabbitmq/wait.go +++ /dev/null @@ -1,139 +0,0 @@ -package rabbitmq - -import ( - "context" - "fmt" - "net/http" - "strings" - - oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" - "github.com/stackitcloud/stackit-sdk-go/core/wait" -) - -const ( - InstanceStateSuccess = "succeeded" - InstanceStateFailed = "failed" - InstanceTypeCreate = "create" - InstanceTypeUpdate = "update" - InstanceTypeDelete = "delete" -) - -// Interface needed for tests -type APIClientInstanceInterface interface { - GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*Instance, error) -} - -// Interface needed for tests -type APIClientCredentialsInterface interface { - GetCredentialsExecute(ctx context.Context, projectId, instanceId, credentialsId string) (*CredentialsResponse, error) -} - -// CreateInstanceWaitHandler will wait for creation -func CreateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.Handler { - return wait.New(func() (res interface{}, done bool, err error) { - s, err := a.GetInstanceExecute(ctx, projectId, instanceId) - if err != nil { - return nil, false, err - } - if s.InstanceId == nil || s.LastOperation == nil || s.LastOperation.Type == nil || s.LastOperation.State == nil { - return s, false, fmt.Errorf("create failed for instance with id %s. The response is not valid: the instance id, the last operation type or the state are missing", instanceId) - } - if *s.InstanceId == instanceId && *s.LastOperation.Type == InstanceTypeCreate && *s.LastOperation.State == InstanceStateSuccess { - return s, true, nil - } - if *s.InstanceId == instanceId && *s.LastOperation.Type == InstanceTypeCreate && *s.LastOperation.State == InstanceStateFailed { - return s, true, fmt.Errorf("create failed for instance with id %s", instanceId) - } - return s, false, nil - }) -} - -// UpdateInstanceWaitHandler will wait for update -func UpdateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.Handler { - return wait.New(func() (res interface{}, done bool, err error) { - s, err := a.GetInstanceExecute(ctx, projectId, instanceId) - if err != nil { - return nil, false, err - } - if s.InstanceId == nil || s.LastOperation == nil || s.LastOperation.Type == nil || s.LastOperation.State == nil { - return s, false, fmt.Errorf("update failed for instance with id %s. The response is not valid: the instance id, the last operation type or the state are missing", instanceId) - } - if *s.InstanceId == instanceId && *s.LastOperation.Type == InstanceTypeUpdate && *s.LastOperation.State == InstanceStateSuccess { - return s, true, nil - } - if *s.InstanceId == instanceId && *s.LastOperation.Type == InstanceTypeUpdate && *s.LastOperation.State == InstanceStateFailed { - return s, true, fmt.Errorf("create failed for instance with id %s", instanceId) - } - return s, false, nil - }) -} - -// DeleteInstanceWaitHandler will wait for delete -func DeleteInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.Handler { - return wait.New(func() (res interface{}, done bool, err error) { - s, err := a.GetInstanceExecute(ctx, projectId, instanceId) - if err == nil { - if s.LastOperation == nil || s.LastOperation.Type == nil || s.LastOperation.State == nil || s.LastOperation.Description == nil { - return s, false, fmt.Errorf("delete failed for instance with id %s. The response is not valid: The last operation type, description or the state are missing", instanceId) - } - if *s.LastOperation.Type != InstanceTypeDelete { - return nil, false, nil - } - if *s.LastOperation.State == InstanceStateSuccess { - if strings.Contains(*s.LastOperation.Description, "DeleteFailed") || strings.Contains(*s.LastOperation.Description, "failed") { - return s, true, fmt.Errorf("instance was deleted successfully but has errors: %s", *s.LastOperation.Description) - } - return s, true, nil - } - return s, false, nil - } - oapiErr, ok := err.(*oapiError.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped - if !ok { - return nil, false, fmt.Errorf("could not convert error to oapiError.GenericOpenAPIError") - } - if oapiErr.StatusCode != http.StatusGone { - return nil, false, err - } - return nil, true, nil - }) -} - -// CreateCredentialsWaitHandler will wait for creation -func CreateCredentialsWaitHandler(ctx context.Context, a APIClientCredentialsInterface, projectId, instanceId, credentialsId string) *wait.Handler { - return wait.New(func() (res interface{}, done bool, err error) { - s, err := a.GetCredentialsExecute(ctx, projectId, instanceId, credentialsId) - if err != nil { - oapiErr, ok := err.(*oapiError.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped - if !ok { - return nil, false, fmt.Errorf("could not convert error to oapiError.GenericOpenAPIError") - } - // If the request returns 404, the credentials have not been created yet - if oapiErr.StatusCode == http.StatusNotFound { - return nil, false, nil - } - return nil, false, err - } - if *s.Id == credentialsId { - return s, true, nil - } - return s, false, nil - }) -} - -// DeleteCredentialsWaitHandler will wait for deletion -func DeleteCredentialsWaitHandler(ctx context.Context, a APIClientCredentialsInterface, projectId, instanceId, credentialsId string) *wait.Handler { - return wait.New(func() (res interface{}, done bool, err error) { - s, err := a.GetCredentialsExecute(ctx, projectId, instanceId, credentialsId) - if err != nil { - oapiErr, ok := err.(*oapiError.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped - if !ok { - return nil, false, fmt.Errorf("could not convert error to oapiError.GenericOpenAPIError") - } - if oapiErr.StatusCode != http.StatusNotFound && oapiErr.StatusCode != http.StatusGone { - return nil, false, err - } - return nil, true, nil - } - return s, false, nil - }) -} diff --git a/services/opensearch/wait.go b/services/rabbitmq/wait/wait.go similarity index 97% rename from services/opensearch/wait.go rename to services/rabbitmq/wait/wait.go index 7f86e1ac1..afa277651 100644 --- a/services/opensearch/wait.go +++ b/services/rabbitmq/wait/wait.go @@ -1,4 +1,4 @@ -package opensearch +package wait import ( "context" @@ -8,6 +8,7 @@ import ( oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/wait" + "github.com/stackitcloud/stackit-sdk-go/services/rabbitmq" ) const ( @@ -20,12 +21,12 @@ const ( // Interface needed for tests type APIClientInstanceInterface interface { - GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*Instance, error) + GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*rabbitmq.Instance, error) } // Interface needed for tests type APIClientCredentialsInterface interface { - GetCredentialsExecute(ctx context.Context, projectId, instanceId, credentialsId string) (*CredentialsResponse, error) + GetCredentialsExecute(ctx context.Context, projectId, instanceId, credentialsId string) (*rabbitmq.CredentialsResponse, error) } // CreateInstanceWaitHandler will wait for creation diff --git a/services/mariadb/wait_test.go b/services/rabbitmq/wait/wait_test.go similarity index 92% rename from services/mariadb/wait_test.go rename to services/rabbitmq/wait/wait_test.go index 42516d21d..806eefd18 100644 --- a/services/mariadb/wait_test.go +++ b/services/rabbitmq/wait/wait_test.go @@ -1,4 +1,4 @@ -package mariadb +package wait import ( "context" @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp" oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/utils" + "github.com/stackitcloud/stackit-sdk-go/services/rabbitmq" ) // Used for testing instance operations @@ -26,7 +27,7 @@ var ( instanceTypeDelete = InstanceTypeDelete ) -func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ string) (*Instance, error) { +func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ string) (*rabbitmq.Instance, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, @@ -34,9 +35,9 @@ func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ str } if *a.resourceOperation == InstanceTypeDelete && a.resourceState == InstanceStateSuccess { if a.deletionSucceedsWithErrors { - return &Instance{ + return &rabbitmq.Instance{ InstanceId: &a.resourceId, - LastOperation: &LastOperation{ + LastOperation: &rabbitmq.LastOperation{ Description: &a.resourceDescription, Type: a.resourceOperation, State: &a.resourceState, @@ -48,9 +49,9 @@ func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ str } } - return &Instance{ + return &rabbitmq.Instance{ InstanceId: &a.resourceId, - LastOperation: &LastOperation{ + LastOperation: &rabbitmq.LastOperation{ Description: &a.resourceDescription, Type: a.resourceOperation, State: &a.resourceState, @@ -66,7 +67,7 @@ type apiClientCredentialsMocked struct { deletionSucceeds bool } -func (a *apiClientCredentialsMocked) GetCredentialsExecute(_ context.Context, _, _, _ string) (*CredentialsResponse, error) { +func (a *apiClientCredentialsMocked) GetCredentialsExecute(_ context.Context, _, _, _ string) (*rabbitmq.CredentialsResponse, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, @@ -79,7 +80,7 @@ func (a *apiClientCredentialsMocked) GetCredentialsExecute(_ context.Context, _, } } - return &CredentialsResponse{ + return &rabbitmq.CredentialsResponse{ Id: &a.resourceId, }, nil } @@ -126,11 +127,11 @@ func TestCreateInstanceWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *Instance + var wantRes *rabbitmq.Instance if !tt.getFails { - wantRes = &Instance{ + wantRes = &rabbitmq.Instance{ InstanceId: &instanceId, - LastOperation: &LastOperation{ + LastOperation: &rabbitmq.LastOperation{ Type: &instanceTypeCreate, State: &tt.resourceState, Description: utils.Ptr(""), @@ -198,11 +199,11 @@ func TestUpdateInstanceWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *Instance + var wantRes *rabbitmq.Instance if !tt.getFails { - wantRes = &Instance{ + wantRes = &rabbitmq.Instance{ InstanceId: &instanceId, - LastOperation: &LastOperation{ + LastOperation: &rabbitmq.LastOperation{ Type: &instanceTypeUpdate, State: &tt.resourceState, Description: utils.Ptr(""), @@ -327,9 +328,9 @@ func TestCreateCredentialsWaitHandler(t *testing.T) { operationSucceeds: tt.operationSucceeds, } - var wantRes *CredentialsResponse + var wantRes *rabbitmq.CredentialsResponse if !tt.getFails && tt.operationSucceeds { - wantRes = &CredentialsResponse{ + wantRes = &rabbitmq.CredentialsResponse{ Id: &credentialsId, } } else if !tt.getFails && !tt.operationSucceeds { @@ -390,9 +391,9 @@ func TestDeleteCredentialsWaitHandler(t *testing.T) { deletionSucceeds: tt.deletionSucceeds, } - var wantRes *CredentialsResponse + var wantRes *rabbitmq.CredentialsResponse if !tt.getFails && !tt.deletionSucceeds { - wantRes = &CredentialsResponse{ + wantRes = &rabbitmq.CredentialsResponse{ Id: &credentialsId, } } else if !tt.getFails && tt.deletionSucceeds { diff --git a/services/redis/wait.go b/services/redis/wait.go deleted file mode 100644 index 4f5d79400..000000000 --- a/services/redis/wait.go +++ /dev/null @@ -1,139 +0,0 @@ -package redis - -import ( - "context" - "fmt" - "net/http" - "strings" - - oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" - "github.com/stackitcloud/stackit-sdk-go/core/wait" -) - -const ( - InstanceStateSuccess = "succeeded" - InstanceStateFailed = "failed" - InstanceTypeCreate = "create" - InstanceTypeUpdate = "update" - InstanceTypeDelete = "delete" -) - -// Interface needed for tests -type APIClientInstanceInterface interface { - GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*Instance, error) -} - -// Interface needed for tests -type APIClientCredentialsInterface interface { - GetCredentialsExecute(ctx context.Context, projectId, instanceId, credentialsId string) (*CredentialsResponse, error) -} - -// CreateInstanceWaitHandler will wait for creation -func CreateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.Handler { - return wait.New(func() (res interface{}, done bool, err error) { - s, err := a.GetInstanceExecute(ctx, projectId, instanceId) - if err != nil { - return nil, false, err - } - if s.InstanceId == nil || s.LastOperation == nil || s.LastOperation.Type == nil || s.LastOperation.State == nil { - return s, false, fmt.Errorf("create failed for instance with id %s. The response is not valid: the instance id, the last operation type or the state are missing", instanceId) - } - if *s.InstanceId == instanceId && *s.LastOperation.Type == InstanceTypeCreate && *s.LastOperation.State == InstanceStateSuccess { - return s, true, nil - } - if *s.InstanceId == instanceId && *s.LastOperation.Type == InstanceTypeCreate && *s.LastOperation.State == InstanceStateFailed { - return s, true, fmt.Errorf("create failed for instance with id %s", instanceId) - } - return s, false, nil - }) -} - -// UpdateInstanceWaitHandler will wait for update -func UpdateInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.Handler { - return wait.New(func() (res interface{}, done bool, err error) { - s, err := a.GetInstanceExecute(ctx, projectId, instanceId) - if err != nil { - return nil, false, err - } - if s.InstanceId == nil || s.LastOperation == nil || s.LastOperation.Type == nil || s.LastOperation.State == nil { - return s, false, fmt.Errorf("update failed for instance with id %s. The response is not valid: the instance id, the last operation type or the state are missing", instanceId) - } - if *s.InstanceId == instanceId && *s.LastOperation.Type == InstanceTypeUpdate && *s.LastOperation.State == InstanceStateSuccess { - return s, true, nil - } - if *s.InstanceId == instanceId && *s.LastOperation.Type == InstanceTypeUpdate && *s.LastOperation.State == InstanceStateFailed { - return s, true, fmt.Errorf("create failed for instance with id %s", instanceId) - } - return s, false, nil - }) -} - -// DeleteInstanceWaitHandler will wait for delete -func DeleteInstanceWaitHandler(ctx context.Context, a APIClientInstanceInterface, projectId, instanceId string) *wait.Handler { - return wait.New(func() (res interface{}, done bool, err error) { - s, err := a.GetInstanceExecute(ctx, projectId, instanceId) - if err == nil { - if s.LastOperation == nil || s.LastOperation.Type == nil || s.LastOperation.State == nil || s.LastOperation.Description == nil { - return s, false, fmt.Errorf("delete failed for instance with id %s. The response is not valid: The last operation type, description or the state are missing", instanceId) - } - if *s.LastOperation.Type != InstanceTypeDelete { - return nil, false, nil - } - if *s.LastOperation.State == InstanceStateSuccess { - if strings.Contains(*s.LastOperation.Description, "DeleteFailed") || strings.Contains(*s.LastOperation.Description, "failed") { - return s, true, fmt.Errorf("instance was deleted successfully but has errors: %s", *s.LastOperation.Description) - } - return s, true, nil - } - return s, false, nil - } - oapiErr, ok := err.(*oapiError.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped - if !ok { - return nil, false, fmt.Errorf("could not convert error to oapiError.GenericOpenAPIError") - } - if oapiErr.StatusCode != http.StatusGone { - return nil, false, err - } - return nil, true, nil - }) -} - -// CreateCredentialsWaitHandler will wait for creation -func CreateCredentialsWaitHandler(ctx context.Context, a APIClientCredentialsInterface, projectId, instanceId, credentialsId string) *wait.Handler { - return wait.New(func() (res interface{}, done bool, err error) { - s, err := a.GetCredentialsExecute(ctx, projectId, instanceId, credentialsId) - if err != nil { - oapiErr, ok := err.(*oapiError.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped - if !ok { - return nil, false, fmt.Errorf("could not convert error to oapiError.GenericOpenAPIError") - } - // If the request returns 404, the credentials have not been created yet - if oapiErr.StatusCode == http.StatusNotFound { - return nil, false, nil - } - return nil, false, err - } - if *s.Id == credentialsId { - return s, true, nil - } - return s, false, nil - }) -} - -// DeleteCredentialsWaitHandler will wait for deletion -func DeleteCredentialsWaitHandler(ctx context.Context, a APIClientCredentialsInterface, projectId, instanceId, credentialsId string) *wait.Handler { - return wait.New(func() (res interface{}, done bool, err error) { - s, err := a.GetCredentialsExecute(ctx, projectId, instanceId, credentialsId) - if err != nil { - oapiErr, ok := err.(*oapiError.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped - if !ok { - return nil, false, fmt.Errorf("could not convert error to oapiError.GenericOpenAPIError") - } - if oapiErr.StatusCode != http.StatusNotFound && oapiErr.StatusCode != http.StatusGone { - return nil, false, err - } - return nil, true, nil - } - return s, false, nil - }) -} diff --git a/services/mariadb/wait.go b/services/redis/wait/wait.go similarity index 97% rename from services/mariadb/wait.go rename to services/redis/wait/wait.go index 5c93fd9b5..dc549bf53 100644 --- a/services/mariadb/wait.go +++ b/services/redis/wait/wait.go @@ -1,4 +1,4 @@ -package mariadb +package wait import ( "context" @@ -8,6 +8,7 @@ import ( oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/wait" + "github.com/stackitcloud/stackit-sdk-go/services/redis" ) const ( @@ -20,12 +21,12 @@ const ( // Interface needed for tests type APIClientInstanceInterface interface { - GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*Instance, error) + GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*redis.Instance, error) } // Interface needed for tests type APIClientCredentialsInterface interface { - GetCredentialsExecute(ctx context.Context, projectId, instanceId, credentialsId string) (*CredentialsResponse, error) + GetCredentialsExecute(ctx context.Context, projectId, instanceId, credentialsId string) (*redis.CredentialsResponse, error) } // CreateInstanceWaitHandler will wait for creation diff --git a/services/logme/wait_test.go b/services/redis/wait/wait_test.go similarity index 93% rename from services/logme/wait_test.go rename to services/redis/wait/wait_test.go index a43b3d823..beffb6ccd 100644 --- a/services/logme/wait_test.go +++ b/services/redis/wait/wait_test.go @@ -1,4 +1,4 @@ -package logme +package wait import ( "context" @@ -8,6 +8,7 @@ import ( "github.com/google/go-cmp/cmp" oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/utils" + "github.com/stackitcloud/stackit-sdk-go/services/redis" ) // Used for testing instance operations @@ -26,7 +27,7 @@ var ( instanceTypeDelete = InstanceTypeDelete ) -func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ string) (*Instance, error) { +func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ string) (*redis.Instance, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, @@ -34,9 +35,9 @@ func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ str } if *a.resourceOperation == InstanceTypeDelete && a.resourceState == InstanceStateSuccess { if a.deletionSucceedsWithErrors { - return &Instance{ + return &redis.Instance{ InstanceId: &a.resourceId, - LastOperation: &LastOperation{ + LastOperation: &redis.LastOperation{ Description: &a.resourceDescription, Type: a.resourceOperation, State: &a.resourceState, @@ -48,9 +49,9 @@ func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ str } } - return &Instance{ + return &redis.Instance{ InstanceId: &a.resourceId, - LastOperation: &LastOperation{ + LastOperation: &redis.LastOperation{ Description: &a.resourceDescription, Type: a.resourceOperation, State: &a.resourceState, @@ -66,7 +67,7 @@ type apiClientCredentialsMocked struct { deletionSucceeds bool } -func (a *apiClientCredentialsMocked) GetCredentialsExecute(_ context.Context, _, _, _ string) (*CredentialsResponse, error) { +func (a *apiClientCredentialsMocked) GetCredentialsExecute(_ context.Context, _, _, _ string) (*redis.CredentialsResponse, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: 500, @@ -79,7 +80,7 @@ func (a *apiClientCredentialsMocked) GetCredentialsExecute(_ context.Context, _, } } - return &CredentialsResponse{ + return &redis.CredentialsResponse{ Id: &a.resourceId, }, nil } @@ -126,11 +127,11 @@ func TestCreateInstanceWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *Instance + var wantRes *redis.Instance if !tt.getFails { - wantRes = &Instance{ + wantRes = &redis.Instance{ InstanceId: &instanceId, - LastOperation: &LastOperation{ + LastOperation: &redis.LastOperation{ Type: &instanceTypeCreate, State: &tt.resourceState, Description: utils.Ptr(""), @@ -198,11 +199,11 @@ func TestUpdateInstanceWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *Instance + var wantRes *redis.Instance if !tt.getFails { - wantRes = &Instance{ + wantRes = &redis.Instance{ InstanceId: &instanceId, - LastOperation: &LastOperation{ + LastOperation: &redis.LastOperation{ Type: &instanceTypeUpdate, State: &tt.resourceState, Description: utils.Ptr(""), @@ -327,9 +328,9 @@ func TestCreateCredentialsWaitHandler(t *testing.T) { operationSucceeds: tt.operationSucceeds, } - var wantRes *CredentialsResponse + var wantRes *redis.CredentialsResponse if !tt.getFails && tt.operationSucceeds { - wantRes = &CredentialsResponse{ + wantRes = &redis.CredentialsResponse{ Id: &credentialsId, } } else if !tt.getFails && !tt.operationSucceeds { @@ -390,9 +391,9 @@ func TestDeleteCredentialsWaitHandler(t *testing.T) { deletionSucceeds: tt.deletionSucceeds, } - var wantRes *CredentialsResponse + var wantRes *redis.CredentialsResponse if !tt.getFails && !tt.deletionSucceeds { - wantRes = &CredentialsResponse{ + wantRes = &redis.CredentialsResponse{ Id: &credentialsId, } } else if !tt.getFails && tt.deletionSucceeds { diff --git a/services/redis/wait_test.go b/services/redis/wait_test.go deleted file mode 100644 index db51c91d6..000000000 --- a/services/redis/wait_test.go +++ /dev/null @@ -1,417 +0,0 @@ -package redis - -import ( - "context" - "testing" - "time" - - "github.com/google/go-cmp/cmp" - oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" - "github.com/stackitcloud/stackit-sdk-go/core/utils" -) - -// Used for testing instance operations -type apiClientInstanceMocked struct { - getFails bool - deletionSucceedsWithErrors bool - resourceId string - resourceOperation *string - resourceState string - resourceDescription string -} - -var ( - instanceTypeCreate = InstanceTypeCreate - instanceTypeUpdate = InstanceTypeUpdate - instanceTypeDelete = InstanceTypeDelete -) - -func (a *apiClientInstanceMocked) GetInstanceExecute(_ context.Context, _, _ string) (*Instance, error) { - if a.getFails { - return nil, &oapiError.GenericOpenAPIError{ - StatusCode: 500, - } - } - if *a.resourceOperation == InstanceTypeDelete && a.resourceState == InstanceStateSuccess { - if a.deletionSucceedsWithErrors { - return &Instance{ - InstanceId: &a.resourceId, - LastOperation: &LastOperation{ - Description: &a.resourceDescription, - Type: a.resourceOperation, - State: &a.resourceState, - }, - }, nil - } - return nil, &oapiError.GenericOpenAPIError{ - StatusCode: 410, - } - } - - return &Instance{ - InstanceId: &a.resourceId, - LastOperation: &LastOperation{ - Description: &a.resourceDescription, - Type: a.resourceOperation, - State: &a.resourceState, - }, - }, nil -} - -// Used for testing credentials operations -type apiClientCredentialsMocked struct { - getFails bool - resourceId string - operationSucceeds bool - deletionSucceeds bool -} - -func (a *apiClientCredentialsMocked) GetCredentialsExecute(_ context.Context, _, _, _ string) (*CredentialsResponse, error) { - if a.getFails { - return nil, &oapiError.GenericOpenAPIError{ - StatusCode: 500, - } - } - - if !a.operationSucceeds || a.deletionSucceeds { - return nil, &oapiError.GenericOpenAPIError{ - StatusCode: 404, - } - } - - return &CredentialsResponse{ - Id: &a.resourceId, - }, nil -} - -func TestCreateInstanceWaitHandler(t *testing.T) { - tests := []struct { - desc string - getFails bool - resourceState string - wantErr bool - }{ - { - desc: "create_succeeded", - getFails: false, - resourceState: InstanceStateSuccess, - wantErr: false, - }, - { - desc: "create_failed", - getFails: false, - resourceState: InstanceStateFailed, - wantErr: true, - }, - { - desc: "get_fails", - getFails: true, - wantErr: true, - }, - { - desc: "timeout", - getFails: false, - resourceState: "ANOTHER STATE", - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.desc, func(t *testing.T) { - instanceId := "foo-bar" - - apiClient := &apiClientInstanceMocked{ - getFails: tt.getFails, - resourceId: instanceId, - resourceOperation: &instanceTypeCreate, - resourceState: tt.resourceState, - } - - var wantRes *Instance - if !tt.getFails { - wantRes = &Instance{ - InstanceId: &instanceId, - LastOperation: &LastOperation{ - Type: &instanceTypeCreate, - State: &tt.resourceState, - Description: utils.Ptr(""), - }, - } - } - - handler := CreateInstanceWaitHandler(context.Background(), apiClient, "pid", instanceId) - - gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) - - if (err != nil) != tt.wantErr { - t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) - } - if wantRes == nil && gotRes != nil { - t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) - } - diff := cmp.Diff(gotRes, wantRes) - if wantRes != nil && diff != "" { - t.Fatalf("handler gotRes = %+v\n want %+v\n diff = %s", gotRes, wantRes, diff) - } - }) - } -} - -func TestUpdateInstanceWaitHandler(t *testing.T) { - tests := []struct { - desc string - getFails bool - resourceState string - wantErr bool - }{ - { - desc: "update_succeeded", - getFails: false, - resourceState: InstanceStateSuccess, - wantErr: false, - }, - { - desc: "update_failed", - getFails: false, - resourceState: InstanceStateFailed, - wantErr: true, - }, - { - desc: "get_fails", - getFails: true, - wantErr: true, - }, - { - desc: "timeout", - getFails: false, - resourceState: "ANOTHER STATE", - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.desc, func(t *testing.T) { - instanceId := "foo-bar" - - apiClient := &apiClientInstanceMocked{ - getFails: tt.getFails, - resourceId: instanceId, - resourceOperation: &instanceTypeUpdate, - resourceState: tt.resourceState, - } - - var wantRes *Instance - if !tt.getFails { - wantRes = &Instance{ - InstanceId: &instanceId, - LastOperation: &LastOperation{ - Type: &instanceTypeUpdate, - State: &tt.resourceState, - Description: utils.Ptr(""), - }, - } - } - - handler := UpdateInstanceWaitHandler(context.Background(), apiClient, "", instanceId) - - gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) - - if (err != nil) != tt.wantErr { - t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) - } - if wantRes == nil && gotRes != nil { - t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) - } - if wantRes != nil && !cmp.Equal(gotRes, wantRes) { - t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) - } - }) - } -} - -func TestDeleteInstanceWaitHandler(t *testing.T) { - tests := []struct { - desc string - getFails bool - deleteSucceeedsWithErrors bool - resourceState string - resourceDescription string - wantErr bool - }{ - { - desc: "delete_succeeded", - getFails: false, - deleteSucceeedsWithErrors: false, - resourceState: InstanceStateSuccess, - wantErr: false, - }, - { - desc: "delete_failed", - getFails: false, - deleteSucceeedsWithErrors: false, - resourceState: InstanceStateFailed, - wantErr: true, - }, - { - desc: "delete_succeeds_with_errors", - getFails: false, - resourceState: InstanceStateSuccess, - deleteSucceeedsWithErrors: true, - resourceDescription: "Deleting resource: cf failed with error: DeleteFailed", - wantErr: true, - }, - { - desc: "get_fails", - deleteSucceeedsWithErrors: false, - getFails: true, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.desc, func(t *testing.T) { - instanceId := "foo-bar" - - apiClient := &apiClientInstanceMocked{ - getFails: tt.getFails, - deletionSucceedsWithErrors: tt.deleteSucceeedsWithErrors, - resourceId: instanceId, - resourceOperation: &instanceTypeDelete, - resourceDescription: tt.resourceDescription, - resourceState: tt.resourceState, - } - - handler := DeleteInstanceWaitHandler(context.Background(), apiClient, "", instanceId) - - gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) - - if (err != nil) != tt.wantErr { - t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) - } - if err == nil && gotRes != nil { - t.Fatalf("handler gotRes = %v, want %v", gotRes, nil) - } - }) - } -} - -func TestCreateCredentialsWaitHandler(t *testing.T) { - tests := []struct { - desc string - getFails bool - operationSucceeds bool - wantErr bool - }{ - { - desc: "create_succeeded", - getFails: false, - operationSucceeds: true, - wantErr: false, - }, - { - desc: "create_failed", - getFails: false, - operationSucceeds: false, - wantErr: true, - }, - { - desc: "get_fails", - getFails: true, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.desc, func(t *testing.T) { - credentialsId := "foo-bar" - - apiClient := &apiClientCredentialsMocked{ - getFails: tt.getFails, - resourceId: credentialsId, - operationSucceeds: tt.operationSucceeds, - } - - var wantRes *CredentialsResponse - if !tt.getFails && tt.operationSucceeds { - wantRes = &CredentialsResponse{ - Id: &credentialsId, - } - } else if !tt.getFails && !tt.operationSucceeds { - wantRes = nil - } - - handler := CreateCredentialsWaitHandler(context.Background(), apiClient, "", "", credentialsId) - - gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) - - if (err != nil) != tt.wantErr { - t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) - } - if wantRes == nil && gotRes != nil { - t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) - } - if wantRes != nil && !cmp.Equal(gotRes, wantRes) { - t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) - } - }) - } -} - -func TestDeleteCredentialsWaitHandler(t *testing.T) { - tests := []struct { - desc string - getFails bool - deletionSucceeds bool - wantErr bool - }{ - { - desc: "delete_succeeded", - getFails: false, - deletionSucceeds: true, - wantErr: false, - }, - { - desc: "delete_failed", - getFails: false, - deletionSucceeds: false, - wantErr: true, - }, - { - desc: "get_fails", - getFails: true, - deletionSucceeds: false, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.desc, func(t *testing.T) { - credentialsId := "foo-bar" - - apiClient := &apiClientCredentialsMocked{ - getFails: tt.getFails, - resourceId: credentialsId, - operationSucceeds: true, - deletionSucceeds: tt.deletionSucceeds, - } - - var wantRes *CredentialsResponse - if !tt.getFails && !tt.deletionSucceeds { - wantRes = &CredentialsResponse{ - Id: &credentialsId, - } - } else if !tt.getFails && tt.deletionSucceeds { - wantRes = nil - } - - handler := DeleteCredentialsWaitHandler(context.Background(), apiClient, "", "", credentialsId) - - gotRes, err := handler.SetTimeout(10 * time.Millisecond).WaitWithContext(context.Background()) - - if (err != nil) != tt.wantErr { - t.Fatalf("handler error = %v, wantErr %v", err, tt.wantErr) - } - if wantRes == nil && gotRes != nil { - t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) - } - if wantRes != nil && !cmp.Equal(gotRes, wantRes) { - t.Fatalf("handler gotRes = %v, want %v", gotRes, wantRes) - } - }) - } -} diff --git a/services/resourcemanager/wait.go b/services/resourcemanager/wait/wait.go similarity index 79% rename from services/resourcemanager/wait.go rename to services/resourcemanager/wait/wait.go index c5f272ff7..9b2fc46b3 100644 --- a/services/resourcemanager/wait.go +++ b/services/resourcemanager/wait/wait.go @@ -1,4 +1,4 @@ -package resourcemanager +package wait import ( "context" @@ -7,20 +7,21 @@ import ( oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/wait" + "github.com/stackitcloud/stackit-sdk-go/services/resourcemanager" ) const ( - ActiveState = LifecycleState("ACTIVE") - CreatingState = LifecycleState("CREATING") + ActiveState = resourcemanager.LifecycleState("ACTIVE") + CreatingState = resourcemanager.LifecycleState("CREATING") ) // Interfaces needed for tests type APIClientInterface interface { - GetProjectExecute(ctx context.Context, containerId string) (*ProjectResponseWithParents, error) + GetProjectExecute(ctx context.Context, containerId string) (*resourcemanager.ProjectResponseWithParents, error) } // CreateProjectWaitHandler will wait for creation -// returned interface is nil or *ProjectResponseWithParents +// returned interface is nil or *resourcemanager.ProjectResponseWithParents func CreateProjectWaitHandler(ctx context.Context, a APIClientInterface, containerId string) *wait.Handler { return wait.New(func() (res interface{}, done bool, err error) { p, err := a.GetProjectExecute(ctx, containerId) @@ -41,7 +42,7 @@ func CreateProjectWaitHandler(ctx context.Context, a APIClientInterface, contain } // DeleteProjectWaitHandler will wait for delete -// returned interface is nil or *ProjectResponseWithParents +// returned interface is nil or *resourcemanager.ProjectResponseWithParents func DeleteProjectWaitHandler(ctx context.Context, a APIClientInterface, containerId string) *wait.Handler { return wait.New(func() (res interface{}, done bool, err error) { p, err := a.GetProjectExecute(ctx, containerId) diff --git a/services/resourcemanager/wait_test.go b/services/resourcemanager/wait/wait_test.go similarity index 82% rename from services/resourcemanager/wait_test.go rename to services/resourcemanager/wait/wait_test.go index 641f10741..632688924 100644 --- a/services/resourcemanager/wait_test.go +++ b/services/resourcemanager/wait/wait_test.go @@ -1,4 +1,4 @@ -package resourcemanager +package wait import ( "context" @@ -9,15 +9,16 @@ import ( "github.com/google/go-cmp/cmp" oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/utils" + "github.com/stackitcloud/stackit-sdk-go/services/resourcemanager" ) type apiClientMocked struct { getFails bool getNotFound bool - projectState LifecycleState + projectState resourcemanager.LifecycleState } -func (a *apiClientMocked) GetProjectExecute(_ context.Context, _ string) (*ProjectResponseWithParents, error) { +func (a *apiClientMocked) GetProjectExecute(_ context.Context, _ string) (*resourcemanager.ProjectResponseWithParents, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: http.StatusInternalServerError, @@ -30,7 +31,7 @@ func (a *apiClientMocked) GetProjectExecute(_ context.Context, _ string) (*Proje } } - return &ProjectResponseWithParents{ + return &resourcemanager.ProjectResponseWithParents{ LifecycleState: &a.projectState, ContainerId: utils.Ptr("cid"), }, nil @@ -40,7 +41,7 @@ func TestCreateProjectWaitHandler(t *testing.T) { tests := []struct { desc string getFails bool - projectState LifecycleState + projectState resourcemanager.LifecycleState wantErr bool }{ { @@ -58,13 +59,13 @@ func TestCreateProjectWaitHandler(t *testing.T) { { desc: "get_fails", getFails: true, - projectState: LifecycleState(""), + projectState: resourcemanager.LifecycleState(""), wantErr: true, }, { desc: "unknown_state", getFails: false, - projectState: LifecycleState("ANOTHER STATE"), + projectState: resourcemanager.LifecycleState("ANOTHER STATE"), wantErr: true, }, } @@ -75,9 +76,9 @@ func TestCreateProjectWaitHandler(t *testing.T) { projectState: tt.projectState, } - var wantRes *ProjectResponseWithParents + var wantRes *resourcemanager.ProjectResponseWithParents if !tt.getFails { - wantRes = &ProjectResponseWithParents{ + wantRes = &resourcemanager.ProjectResponseWithParents{ LifecycleState: &tt.projectState, ContainerId: utils.Ptr("cid"), } @@ -107,14 +108,14 @@ func TestDeleteProjectWaitHandler(t *testing.T) { desc string getFails bool getNotFound bool - projectState LifecycleState + projectState resourcemanager.LifecycleState wantErr bool }{ { desc: "delete_succeeded", getFails: false, getNotFound: true, - projectState: LifecycleState(""), + projectState: resourcemanager.LifecycleState(""), wantErr: false, }, { @@ -138,9 +139,9 @@ func TestDeleteProjectWaitHandler(t *testing.T) { projectState: tt.projectState, } - var wantRes *ProjectResponseWithParents + var wantRes *resourcemanager.ProjectResponseWithParents if !tt.getFails && !tt.getNotFound { - wantRes = &ProjectResponseWithParents{ + wantRes = &resourcemanager.ProjectResponseWithParents{ LifecycleState: &tt.projectState, ContainerId: utils.Ptr("cid"), } diff --git a/services/ske/wait.go b/services/ske/wait/wait.go similarity index 91% rename from services/ske/wait.go rename to services/ske/wait/wait.go index ecf30c413..5843c7a65 100644 --- a/services/ske/wait.go +++ b/services/ske/wait/wait.go @@ -1,4 +1,4 @@ -package ske +package wait import ( "context" @@ -7,6 +7,7 @@ import ( oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/wait" + "github.com/stackitcloud/stackit-sdk-go/services/ske" ) const ( @@ -20,16 +21,16 @@ const ( ) type APIClientProjectInterface interface { - GetProjectExecute(ctx context.Context, projectId string) (*ProjectResponse, error) + GetProjectExecute(ctx context.Context, projectId string) (*ske.ProjectResponse, error) } type APIClientClusterInterface interface { - GetClusterExecute(ctx context.Context, projectId, name string) (*ClusterResponse, error) - GetClustersExecute(ctx context.Context, projectId string) (*ClustersResponse, error) + GetClusterExecute(ctx context.Context, projectId, name string) (*ske.ClusterResponse, error) + GetClustersExecute(ctx context.Context, projectId string) (*ske.ClustersResponse, error) } type APIClientCredentialsInterface interface { - GetCredentialsExecute(ctx context.Context, projectId, instanceId, credentialsId string) (*CredentialsResponse, error) + GetCredentialsExecute(ctx context.Context, projectId, instanceId, credentialsId string) (*ske.CredentialsResponse, error) } // CreateOrUpdateClusterWaitHandler will wait for creation diff --git a/services/ske/wait_test.go b/services/ske/wait/wait_test.go similarity index 85% rename from services/ske/wait_test.go rename to services/ske/wait/wait_test.go index 13c802b60..8f5663f43 100644 --- a/services/ske/wait_test.go +++ b/services/ske/wait/wait_test.go @@ -1,4 +1,4 @@ -package ske +package wait import ( "context" @@ -9,6 +9,7 @@ import ( "github.com/google/go-cmp/cmp" oapiError "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/utils" + "github.com/stackitcloud/stackit-sdk-go/services/ske" ) // Used for testing cluster operations @@ -19,46 +20,46 @@ type apiClientClusterMocked struct { invalidArgusInstance bool } -func (a *apiClientClusterMocked) GetClusterExecute(_ context.Context, _, _ string) (*ClusterResponse, error) { +func (a *apiClientClusterMocked) GetClusterExecute(_ context.Context, _, _ string) (*ske.ClusterResponse, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: http.StatusInternalServerError, } } - rs := ClusterStatusState(a.resourceState) + rs := ske.ClusterStatusState(a.resourceState) if a.invalidArgusInstance { - return &ClusterResponse{ + return &ske.ClusterResponse{ Name: utils.Ptr("cluster"), - Status: &ClusterStatus{ + Status: &ske.ClusterStatus{ Aggregated: &rs, - Error: &RuntimeError{ + Error: &ske.RuntimeError{ Code: utils.Ptr(string(InvalidArgusInstanceErrorCode)), Message: utils.Ptr("invalid argus instance"), }, }, }, nil } - return &ClusterResponse{ + return &ske.ClusterResponse{ Name: utils.Ptr("cluster"), - Status: &ClusterStatus{ + Status: &ske.ClusterStatus{ Aggregated: &rs, }, }, nil } -func (a *apiClientClusterMocked) GetClustersExecute(_ context.Context, _ string) (*ClustersResponse, error) { +func (a *apiClientClusterMocked) GetClustersExecute(_ context.Context, _ string) (*ske.ClustersResponse, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: http.StatusInternalServerError, } } - rs := ClusterStatusState(a.resourceState) - return &ClustersResponse{ - Items: &[]ClusterResponse{ + rs := ske.ClusterStatusState(a.resourceState) + return &ske.ClustersResponse{ + Items: &[]ske.ClusterResponse{ { Name: utils.Ptr("cluster"), - Status: &ClusterStatus{ + Status: &ske.ClusterStatus{ Aggregated: &rs, }, }, @@ -73,7 +74,7 @@ type apiClientProjectMocked struct { resourceState string } -func (a *apiClientProjectMocked) GetProjectExecute(_ context.Context, _ string) (*ProjectResponse, error) { +func (a *apiClientProjectMocked) GetProjectExecute(_ context.Context, _ string) (*ske.ProjectResponse, error) { if a.getFails { return nil, &oapiError.GenericOpenAPIError{ StatusCode: http.StatusInternalServerError, @@ -84,8 +85,8 @@ func (a *apiClientProjectMocked) GetProjectExecute(_ context.Context, _ string) StatusCode: http.StatusNotFound, } } - rs := ProjectState(a.resourceState) - return &ProjectResponse{ + rs := ske.ProjectState(a.resourceState) + return &ske.ProjectResponse{ ProjectId: utils.Ptr("pid"), State: &rs, }, nil @@ -145,12 +146,12 @@ func TestCreateOrUpdateClusterWaitHandler(t *testing.T) { resourceState: tt.resourceState, invalidArgusInstance: tt.invalidArgusInstance, } - var wantRes *ClusterResponse - rs := ClusterStatusState(tt.resourceState) + var wantRes *ske.ClusterResponse + rs := ske.ClusterStatusState(tt.resourceState) if !tt.getFails { - wantRes = &ClusterResponse{ + wantRes = &ske.ClusterResponse{ Name: &name, - Status: &ClusterStatus{ + Status: &ske.ClusterStatus{ Aggregated: &rs, }, } @@ -159,7 +160,7 @@ func TestCreateOrUpdateClusterWaitHandler(t *testing.T) { } if tt.invalidArgusInstance { - wantRes.Status.Error = &RuntimeError{ + wantRes.Status.Error = &ske.RuntimeError{ Code: utils.Ptr(string(InvalidArgusInstanceErrorCode)), Message: utils.Ptr("invalid argus instance"), } @@ -218,10 +219,10 @@ func TestCreateProjectWaitHandler(t *testing.T) { getFails: tt.getFails, resourceState: tt.resourceState, } - var wantRes *ProjectResponse - rs := ProjectState(tt.resourceState) + var wantRes *ske.ProjectResponse + rs := ske.ProjectState(tt.resourceState) if !tt.getFails { - wantRes = &ProjectResponse{ + wantRes = &ske.ProjectResponse{ ProjectId: utils.Ptr("pid"), State: &rs, } @@ -278,10 +279,10 @@ func TestDeleteProjectWaitHandler(t *testing.T) { resourceState: tt.resourceState, } - var wantRes *ProjectResponse + var wantRes *ske.ProjectResponse if !tt.getFails && !tt.getNotFound { - rs := ProjectState(tt.resourceState) - wantRes = &ProjectResponse{ + rs := ske.ProjectState(tt.resourceState) + wantRes = &ske.ProjectResponse{ ProjectId: utils.Ptr("pid"), State: &rs, }