77 "net/http"
88 "time"
99
10- "github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1110 "github.com/stackitcloud/stackit-sdk-go/core/wait"
1211 logs "github.com/stackitcloud/stackit-sdk-go/services/logs/v1api"
1312)
@@ -17,39 +16,42 @@ const (
1716 instanceStatusDeleting = "deleting"
1817)
1918
19+ // CreateLogsInstanceWaitHandler will wait for logs instance creation
2020func CreateLogsInstanceWaitHandler (ctx context.Context , client logs.DefaultAPI , projectID , region , instanceID string ) * wait.AsyncActionHandler [logs.LogsInstance ] {
21- handler := wait .New (func () (waitFinished bool , response * logs.LogsInstance , err error ) {
22- instance , err := client .GetLogsInstance (ctx , projectID , region , instanceID ).Execute ()
23- if err != nil {
24- return false , nil , err
25- }
26- if instance .Id == instanceID && instance .Status == instanceStatusActive {
27- return true , instance , nil
28- }
29- if instance .Status == instanceStatusDeleting {
30- return true , nil , fmt .Errorf ("creating log instance failed, instance is being deleted" )
31- }
32- return false , nil , nil
33- })
21+ waitConfig := wait.WaiterHelper [logs.LogsInstance , string ]{
22+ FetchInstance : client .GetLogsInstance (ctx , projectID , region , instanceID ).Execute ,
23+ GetState : func (l * logs.LogsInstance ) (string , error ) {
24+ if l == nil {
25+ return "" , fmt .Errorf ("empty response" )
26+ }
27+ if l .Status == "" {
28+ return "" , fmt .Errorf ("instance status is empty" )
29+ }
30+ return l .Status , nil
31+ },
32+ ActiveState : []string {instanceStatusActive },
33+ ErrorState : []string {instanceStatusDeleting },
34+ }
35+
36+ handler := wait .New (waitConfig .Wait ())
3437 handler .SetTimeout (10 * time .Minute )
3538 return handler
3639}
3740
41+ // DeleteLogsInstanceWaitHandler will wait for logs instance deletion
3842func DeleteLogsInstanceWaitHandler (ctx context.Context , client logs.DefaultAPI , projectID , region , instanceID string ) * wait.AsyncActionHandler [logs.LogsInstance ] {
39- handler := wait .New (func () (waitFinished bool , response * logs.LogsInstance , err error ) {
40- _ , err = client .GetLogsInstance (ctx , projectID , region , instanceID ).Execute ()
41- // the instances is still gettable, e.g. not deleted, when the errors is null
42- if err == nil {
43- return false , nil , nil
44- }
45- var oapiError * oapierror.GenericOpenAPIError
46- if errors .As (err , & oapiError ) {
47- if statusCode := oapiError .StatusCode ; statusCode == http .StatusNotFound {
48- return true , nil , nil
43+ waitConfig := wait.WaiterHelper [logs.LogsInstance , string ]{
44+ FetchInstance : client .GetLogsInstance (ctx , projectID , region , instanceID ).Execute ,
45+ GetState : func (l * logs.LogsInstance ) (string , error ) {
46+ if l == nil {
47+ return "" , errors .New ("empty response" )
4948 }
50- }
51- return false , nil , err
52- })
49+ return l .Status , nil
50+ },
51+ DeleteHttpErrorStatusCodes : []int {http .StatusNotFound },
52+ }
53+
54+ handler := wait .New (waitConfig .Wait ())
5355 handler .SetTimeout (10 * time .Minute )
5456 return handler
5557}
0 commit comments