diff --git a/CHANGELOG.md b/CHANGELOG.md index 13e639fe5..a0d0cbb0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -109,6 +109,8 @@ - **Dependencies:** Bump STACKIT SDK core module from `v0.24.1` to `v0.25.0` - [v0.12.2](services/git/CHANGELOG.md#v0122) - **Dependencies:** Bump STACKIT SDK core module from `v0.25.0` to `v0.26.0` + - [v0.12.3](services/git/CHANGELOG.md#v0123) + - `v1betaapi`: **Improvement**: Use new `WaiterHandler` struct in the Git WaitHandler - `iaas`: - [v1.9.1](services/iaas/CHANGELOG.md#v191) - **Dependencies:** Bump STACKIT SDK core module from `v0.24.0` to `v0.24.1` diff --git a/services/git/CHANGELOG.md b/services/git/CHANGELOG.md index d4a094535..41f53a6b2 100644 --- a/services/git/CHANGELOG.md +++ b/services/git/CHANGELOG.md @@ -1,3 +1,6 @@ +## v0.12.3 +- `v1betaapi`: **Improvement**: Use new `WaiterHandler` struct in the Git WaitHandler + ## v0.12.2 - **Dependencies:** Bump STACKIT SDK core module from `v0.25.0` to `v0.26.0` diff --git a/services/git/VERSION b/services/git/VERSION index c750bd7d5..1af39b364 100644 --- a/services/git/VERSION +++ b/services/git/VERSION @@ -1 +1 @@ -v0.12.2 \ No newline at end of file +v0.12.3 \ No newline at end of file diff --git a/services/git/v1betaapi/wait/wait.go b/services/git/v1betaapi/wait/wait.go index 7fc504363..14319396e 100644 --- a/services/git/v1betaapi/wait/wait.go +++ b/services/git/v1betaapi/wait/wait.go @@ -3,11 +3,8 @@ package wait import ( "context" "errors" - "fmt" - "net/http" "time" - "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/core/wait" git "github.com/stackitcloud/stackit-sdk-go/services/git/v1betaapi" ) @@ -21,39 +18,36 @@ const ( INSTANCESTATE_ERROR = "Error" ) -func CreateGitInstanceWaitHandler(ctx context.Context, a git.DefaultAPI, projectId, instanceId string) *wait.AsyncActionHandler[git.Instance] { - handler := wait.New(func() (waitFinished bool, response *git.Instance, err error) { - instance, err := a.GetInstance(ctx, projectId, instanceId).Execute() - if err != nil { - return false, nil, err - } - if instance.Id == instanceId && instance.State == INSTANCESTATE_READY { - return true, instance, nil - } - if instance.Id == instanceId && instance.State == INSTANCESTATE_ERROR { - return true, instance, fmt.Errorf("create failed for Instance with id %s", instanceId) - } - return false, nil, nil - }) +func CreateGitInstanceWaitHandler(ctx context.Context, client git.DefaultAPI, projectId, instanceId string) *wait.AsyncActionHandler[git.Instance] { + waitConfig := wait.WaiterHelper[git.Instance, string]{ + FetchInstance: client.GetInstance(ctx, projectId, instanceId).Execute, + GetState: func(instance *git.Instance) (string, error) { + if instance == nil { + return "", errors.New("empty response") + } + return instance.State, nil + }, + ActiveState: []string{INSTANCESTATE_READY}, + ErrorState: []string{INSTANCESTATE_ERROR}, + } + handler := wait.New(waitConfig.Wait()) handler.SetTimeout(10 * time.Minute) return handler } -func DeleteGitInstanceWaitHandler(ctx context.Context, a git.DefaultAPI, projectId, instanceId string) *wait.AsyncActionHandler[git.Instance] { - handler := wait.New(func() (waitFinished bool, response *git.Instance, err error) { - _, err = a.GetInstance(ctx, projectId, instanceId).Execute() - // the instances is still gettable, e.g. not deleted, when the errors is null - if err == nil { - return false, nil, nil - } - var oapiError *oapierror.GenericOpenAPIError - if errors.As(err, &oapiError) { - if statusCode := oapiError.StatusCode; statusCode == http.StatusNotFound { - return true, nil, nil +func DeleteGitInstanceWaitHandler(ctx context.Context, client git.DefaultAPI, projectId, instanceId string) *wait.AsyncActionHandler[git.Instance] { + waitConfig := wait.WaiterHelper[git.Instance, string]{ + FetchInstance: client.GetInstance(ctx, projectId, instanceId).Execute, + GetState: func(instance *git.Instance) (string, error) { + if instance == nil { + return "", errors.New("empty response") } - } - return false, nil, err - }) + return instance.State, nil + }, + ActiveState: []string{}, + ErrorState: []string{INSTANCESTATE_ERROR}, + } + handler := wait.New(waitConfig.Wait()) handler.SetTimeout(10 * time.Minute) return handler } diff --git a/services/git/v1betaapi/wait/wait_test.go b/services/git/v1betaapi/wait/wait_test.go index ca040ad55..ddfe4ae4d 100644 --- a/services/git/v1betaapi/wait/wait_test.go +++ b/services/git/v1betaapi/wait/wait_test.go @@ -115,15 +115,25 @@ func TestCreateGitInstanceWaitHandler(t *testing.T) { projectId: PROJECT_ID, instanceId: INSTANCE_ID, returnInstance: true, + getGitResponse: nil, + }, + { + desc: "Creation of an instance with a wrong state on the response", + getFails: false, + wantErr: true, + wantResp: false, + projectId: PROJECT_ID, + instanceId: INSTANCE_ID, + returnInstance: true, getGitResponse: &git.Instance{ Created: time.Now(), + Id: INSTANCE_ID, Name: "instance-test", - State: INSTANCESTATE_ERROR, + State: "wrong-state", Url: "https://testing.git.onstackit.cloud", Version: "v1.6.0", }, }, - { desc: "Creation of an instance without state on the response", getFails: false, @@ -189,10 +199,9 @@ func TestDeleteGitInstanceWaitHandler(t *testing.T) { getFails: true, }, { - desc: "Instance deletion failed returning existing instance", - wantErr: true, - getFails: false, - + desc: "Instance deletion failed returning existing instance", + wantErr: true, + getFails: false, wantReturnedInstance: false, returnInstance: true, getGitResponse: &git.Instance{