@@ -3,11 +3,9 @@ package wait
33import (
44 "context"
55 "errors"
6- "fmt"
76 "net/http"
87 "time"
98
10- "github.com/stackitcloud/stackit-sdk-go/core/oapierror"
119 "github.com/stackitcloud/stackit-sdk-go/core/wait"
1210 git "github.com/stackitcloud/stackit-sdk-go/services/git/v1betaapi"
1311)
@@ -21,39 +19,37 @@ const (
2119 INSTANCESTATE_ERROR = "Error"
2220)
2321
24- func CreateGitInstanceWaitHandler (ctx context.Context , a git.DefaultAPI , projectId , instanceId string ) * wait.AsyncActionHandler [git.Instance ] {
25- handler := wait .New (func () (waitFinished bool , response * git.Instance , err error ) {
26- instance , err := a .GetInstance (ctx , projectId , instanceId ).Execute ()
27- if err != nil {
28- return false , nil , err
29- }
30- if instance .Id == instanceId && instance .State == INSTANCESTATE_READY {
31- return true , instance , nil
32- }
33- if instance .Id == instanceId && instance .State == INSTANCESTATE_ERROR {
34- return true , instance , fmt .Errorf ("create failed for Instance with id %s" , instanceId )
35- }
36- return false , nil , nil
37- })
22+ func CreateGitInstanceWaitHandler (ctx context.Context , client git.DefaultAPI , projectId , instanceId string ) * wait.AsyncActionHandler [git.Instance ] {
23+ waitConfig := wait.WaiterHelper [git.Instance , string ]{
24+ FetchInstance : client .GetInstance (ctx , projectId , instanceId ).Execute ,
25+ GetState : func (instance * git.Instance ) (string , error ) {
26+ if instance == nil {
27+ return "" , errors .New ("empty response" )
28+ }
29+ return instance .State , nil
30+ },
31+ ActiveState : []string {INSTANCESTATE_READY },
32+ ErrorState : []string {INSTANCESTATE_ERROR },
33+ }
34+ handler := wait .New (waitConfig .Wait ())
3835 handler .SetTimeout (10 * time .Minute )
3936 return handler
4037}
4138
42- func DeleteGitInstanceWaitHandler (ctx context.Context , a git.DefaultAPI , projectId , instanceId string ) * wait.AsyncActionHandler [git.Instance ] {
43- handler := wait .New (func () (waitFinished bool , response * git.Instance , err error ) {
44- _ , err = a .GetInstance (ctx , projectId , instanceId ).Execute ()
45- // the instances is still gettable, e.g. not deleted, when the errors is null
46- if err == nil {
47- return false , nil , nil
48- }
49- var oapiError * oapierror.GenericOpenAPIError
50- if errors .As (err , & oapiError ) {
51- if statusCode := oapiError .StatusCode ; statusCode == http .StatusNotFound {
52- return true , nil , nil
39+ func DeleteGitInstanceWaitHandler (ctx context.Context , client git.DefaultAPI , projectId , instanceId string ) * wait.AsyncActionHandler [git.Instance ] {
40+ waitConfig := wait.WaiterHelper [git.Instance , string ]{
41+ FetchInstance : client .GetInstance (ctx , projectId , instanceId ).Execute ,
42+ GetState : func (instance * git.Instance ) (string , error ) {
43+ if instance == nil {
44+ return "" , errors .New ("empty response" )
5345 }
54- }
55- return false , nil , err
56- })
46+ return instance .State , nil
47+ },
48+ ActiveState : []string {},
49+ ErrorState : []string {INSTANCESTATE_ERROR },
50+ DeleteHttpErrorStatusCodes : []int {http .StatusNotFound },
51+ }
52+ handler := wait .New (waitConfig .Wait ())
5753 handler .SetTimeout (10 * time .Minute )
5854 return handler
5955}
0 commit comments