|
| 1 | +package wait |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "net/http" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stackitcloud/stackit-sdk-go/core/wait" |
| 10 | + dremio "github.com/stackitcloud/stackit-sdk-go/services/dremio/v1alphaapi" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + DREMIOSTATE_ACTIVE = "active" |
| 15 | + DREMIOSTATE_ERROR = "error" |
| 16 | + |
| 17 | + DREMIOUSERSTATE_ACTIVE = "active" |
| 18 | + DREMIOUSERSTATE_ERROR = "error" |
| 19 | +) |
| 20 | + |
| 21 | +// CreateDremioWaitHandler will wait for the creation of a Dremio instance |
| 22 | +func CreateDremioWaitHandler(ctx context.Context, a dremio.DefaultAPI, projectId, regionId, dremioId string) *wait.AsyncActionHandler[dremio.DremioResponse] { |
| 23 | + waitConfig := wait.WaiterHelper[dremio.DremioResponse, string]{ |
| 24 | + FetchInstance: a.GetDremio(ctx, projectId, regionId, dremioId).Execute, |
| 25 | + GetState: func(dremio *dremio.DremioResponse) (string, error) { |
| 26 | + if dremio == nil { |
| 27 | + return "", errors.New("empty response") |
| 28 | + } |
| 29 | + return dremio.State, nil |
| 30 | + }, |
| 31 | + ActiveState: []string{DREMIOSTATE_ACTIVE}, |
| 32 | + ErrorState: []string{DREMIOSTATE_ERROR}, |
| 33 | + } |
| 34 | + |
| 35 | + handler := wait.New(waitConfig.Wait()) |
| 36 | + handler.SetTimeout(30 * time.Minute) |
| 37 | + return handler |
| 38 | +} |
| 39 | + |
| 40 | +// UpdateDremioWaitHandler will wait an update of a Dremio instance |
| 41 | +func UpdateDremioWaitHandler(ctx context.Context, a dremio.DefaultAPI, projectId, regionId, dremioId string) *wait.AsyncActionHandler[dremio.DremioResponse] { |
| 42 | + waitConfig := wait.WaiterHelper[dremio.DremioResponse, string]{ |
| 43 | + FetchInstance: a.GetDremio(ctx, projectId, regionId, dremioId).Execute, |
| 44 | + GetState: func(dremio *dremio.DremioResponse) (string, error) { |
| 45 | + if dremio == nil { |
| 46 | + return "", errors.New("empty response") |
| 47 | + } |
| 48 | + return dremio.State, nil |
| 49 | + }, |
| 50 | + ActiveState: []string{DREMIOSTATE_ACTIVE}, |
| 51 | + ErrorState: []string{DREMIOSTATE_ERROR}, |
| 52 | + } |
| 53 | + |
| 54 | + handler := wait.New(waitConfig.Wait()) |
| 55 | + handler.SetTimeout(30 * time.Minute) |
| 56 | + return handler |
| 57 | +} |
| 58 | + |
| 59 | +// DeleteDremioWaitHandler will wait for the deletion of a Dremio instance |
| 60 | +func DeleteDremioWaitHandler(ctx context.Context, a dremio.DefaultAPI, projectId, regionId, dremioId string) *wait.AsyncActionHandler[dremio.DremioResponse] { |
| 61 | + waitConfig := wait.WaiterHelper[dremio.DremioResponse, string]{ |
| 62 | + FetchInstance: a.GetDremio(ctx, projectId, regionId, dremioId).Execute, |
| 63 | + GetState: func(dremio *dremio.DremioResponse) (string, error) { |
| 64 | + if dremio == nil { |
| 65 | + return "", errors.New("empty response") |
| 66 | + } |
| 67 | + return dremio.State, nil |
| 68 | + }, |
| 69 | + ErrorState: []string{DREMIOSTATE_ERROR}, |
| 70 | + DeleteHttpErrorStatusCodes: []int{http.StatusNotFound}, |
| 71 | + } |
| 72 | + |
| 73 | + handler := wait.New(waitConfig.Wait()) |
| 74 | + handler.SetTimeout(30 * time.Minute) |
| 75 | + return handler |
| 76 | +} |
| 77 | + |
| 78 | +// CreateDremioUserWaitHandler will wait for the creation of a Dremio user |
| 79 | +func CreateDremioUserWaitHandler(ctx context.Context, a dremio.DefaultAPI, projectId, regionId, dremioId, dremioUserId string) *wait.AsyncActionHandler[dremio.DremioUserResponse] { |
| 80 | + waitConfig := wait.WaiterHelper[dremio.DremioUserResponse, string]{ |
| 81 | + FetchInstance: a.GetDremioUser(ctx, projectId, regionId, dremioId, dremioUserId).Execute, |
| 82 | + GetState: func(user *dremio.DremioUserResponse) (string, error) { |
| 83 | + if user == nil { |
| 84 | + return "", errors.New("empty response") |
| 85 | + } |
| 86 | + return user.State, nil |
| 87 | + }, |
| 88 | + ActiveState: []string{DREMIOUSERSTATE_ACTIVE}, |
| 89 | + ErrorState: []string{DREMIOUSERSTATE_ERROR}, |
| 90 | + } |
| 91 | + |
| 92 | + handler := wait.New(waitConfig.Wait()) |
| 93 | + handler.SetTimeout(10 * time.Minute) |
| 94 | + return handler |
| 95 | +} |
| 96 | + |
| 97 | +// UpdateDremioUserWaitHandler will wait for an update to a Dremio user |
| 98 | +func UpdateDremioUserWaitHandler(ctx context.Context, a dremio.DefaultAPI, projectId, regionId, dremioId, dremioUserId string) *wait.AsyncActionHandler[dremio.DremioUserResponse] { |
| 99 | + waitConfig := wait.WaiterHelper[dremio.DremioUserResponse, string]{ |
| 100 | + FetchInstance: a.GetDremioUser(ctx, projectId, regionId, dremioId, dremioUserId).Execute, |
| 101 | + GetState: func(user *dremio.DremioUserResponse) (string, error) { |
| 102 | + if user == nil { |
| 103 | + return "", errors.New("empty response") |
| 104 | + } |
| 105 | + return user.State, nil |
| 106 | + }, |
| 107 | + ActiveState: []string{DREMIOUSERSTATE_ACTIVE}, |
| 108 | + ErrorState: []string{DREMIOUSERSTATE_ERROR}, |
| 109 | + } |
| 110 | + |
| 111 | + handler := wait.New(waitConfig.Wait()) |
| 112 | + handler.SetTimeout(10 * time.Minute) |
| 113 | + return handler |
| 114 | +} |
| 115 | + |
| 116 | +// DeleteDremioUserWaitHandler will wait for a deletion of a Dremio user |
| 117 | +func DeleteDremioUserWaitHandler(ctx context.Context, a dremio.DefaultAPI, projectId, regionId, dremioId, dremioUserId string) *wait.AsyncActionHandler[dremio.DremioUserResponse] { |
| 118 | + waitConfig := wait.WaiterHelper[dremio.DremioUserResponse, string]{ |
| 119 | + FetchInstance: a.GetDremioUser(ctx, projectId, regionId, dremioId, dremioUserId).Execute, |
| 120 | + GetState: func(user *dremio.DremioUserResponse) (string, error) { |
| 121 | + if user == nil { |
| 122 | + return "", errors.New("empty response") |
| 123 | + } |
| 124 | + return user.State, nil |
| 125 | + }, |
| 126 | + ErrorState: []string{DREMIOUSERSTATE_ERROR}, |
| 127 | + DeleteHttpErrorStatusCodes: []int{http.StatusNotFound}, |
| 128 | + } |
| 129 | + |
| 130 | + handler := wait.New(waitConfig.Wait()) |
| 131 | + handler.SetTimeout(10 * time.Minute) |
| 132 | + return handler |
| 133 | +} |
0 commit comments