|
| 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 | + telemetryrouter "github.com/stackitcloud/stackit-sdk-go/services/telemetryrouter/v1betaapi" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + TELEMETRYROUTER_ACTIVE = "active" |
| 15 | + DESTINATION_ACTIVE = "active" |
| 16 | + ACCESSTOKEN_ACTIVE = "active" |
| 17 | +) |
| 18 | + |
| 19 | +// CreateTelemetryRouterWaitHandler will wait for TelemetryRouter creation |
| 20 | +func CreateTelemetryRouterWaitHandler(ctx context.Context, a telemetryrouter.DefaultAPI, projectId, regionId, instanceId string) *wait.AsyncActionHandler[telemetryrouter.TelemetryRouterResponse] { |
| 21 | + waitConfig := wait.WaiterHelper[telemetryrouter.TelemetryRouterResponse, string]{ |
| 22 | + FetchInstance: a.GetTelemetryRouter(ctx, projectId, regionId, instanceId).Execute, |
| 23 | + GetState: func(d *telemetryrouter.TelemetryRouterResponse) (string, error) { |
| 24 | + if d == nil { |
| 25 | + return "", errors.New("empty response") |
| 26 | + } |
| 27 | + return d.Status, nil |
| 28 | + }, |
| 29 | + ActiveState: []string{TELEMETRYROUTER_ACTIVE}, |
| 30 | + } |
| 31 | + |
| 32 | + handler := wait.New(waitConfig.Wait()) |
| 33 | + handler.SetTimeout(10 * time.Minute) |
| 34 | + return handler |
| 35 | +} |
| 36 | + |
| 37 | +// UpdateTelemetryRouterWaitHandler will wait for TelemetryRouter update |
| 38 | +func UpdateTelemetryRouterWaitHandler(ctx context.Context, a telemetryrouter.DefaultAPI, projectId, regionId, instanceId string) *wait.AsyncActionHandler[telemetryrouter.TelemetryRouterResponse] { |
| 39 | + waitConfig := wait.WaiterHelper[telemetryrouter.TelemetryRouterResponse, string]{ |
| 40 | + FetchInstance: a.GetTelemetryRouter(ctx, projectId, regionId, instanceId).Execute, |
| 41 | + GetState: func(d *telemetryrouter.TelemetryRouterResponse) (string, error) { |
| 42 | + if d == nil { |
| 43 | + return "", errors.New("empty response") |
| 44 | + } |
| 45 | + return d.Status, nil |
| 46 | + }, |
| 47 | + ActiveState: []string{TELEMETRYROUTER_ACTIVE}, |
| 48 | + } |
| 49 | + |
| 50 | + handler := wait.New(waitConfig.Wait()) |
| 51 | + handler.SetTimeout(10 * time.Minute) |
| 52 | + return handler |
| 53 | +} |
| 54 | + |
| 55 | +// DeleteTelemetryRouterWaitHandler will wait for TelemetryRouter deletion |
| 56 | +func DeleteTelemetryRouterWaitHandler(ctx context.Context, a telemetryrouter.DefaultAPI, projectId, regionId, instanceId string) *wait.AsyncActionHandler[telemetryrouter.TelemetryRouterResponse] { |
| 57 | + waitConfig := wait.WaiterHelper[telemetryrouter.TelemetryRouterResponse, string]{ |
| 58 | + FetchInstance: a.GetTelemetryRouter(ctx, projectId, regionId, instanceId).Execute, |
| 59 | + GetState: func(d *telemetryrouter.TelemetryRouterResponse) (string, error) { |
| 60 | + if d == nil { |
| 61 | + return "", errors.New("empty response") |
| 62 | + } |
| 63 | + return d.Status, nil |
| 64 | + }, |
| 65 | + DeleteHttpErrorStatusCodes: []int{http.StatusNotFound}, |
| 66 | + } |
| 67 | + |
| 68 | + handler := wait.New(waitConfig.Wait()) |
| 69 | + handler.SetTimeout(10 * time.Minute) |
| 70 | + return handler |
| 71 | +} |
| 72 | + |
| 73 | +// CreateDestinationWaitHandler will wait for Destination creation |
| 74 | +func CreateDestinationWaitHandler(ctx context.Context, a telemetryrouter.DefaultAPI, projectId, regionId, instanceId, destinationId string) *wait.AsyncActionHandler[telemetryrouter.DestinationResponse] { |
| 75 | + waitConfig := wait.WaiterHelper[telemetryrouter.DestinationResponse, string]{ |
| 76 | + FetchInstance: a.GetDestination(ctx, projectId, regionId, instanceId, destinationId).Execute, |
| 77 | + GetState: func(d *telemetryrouter.DestinationResponse) (string, error) { |
| 78 | + if d == nil { |
| 79 | + return "", errors.New("empty response") |
| 80 | + } |
| 81 | + return d.Status, nil |
| 82 | + }, |
| 83 | + ActiveState: []string{DESTINATION_ACTIVE}, |
| 84 | + } |
| 85 | + |
| 86 | + handler := wait.New(waitConfig.Wait()) |
| 87 | + handler.SetTimeout(10 * time.Minute) |
| 88 | + return handler |
| 89 | +} |
| 90 | + |
| 91 | +// UpdateDestinationWaitHandler will wait for Destination update |
| 92 | +func UpdateDestinationWaitHandler(ctx context.Context, a telemetryrouter.DefaultAPI, projectId, regionId, instanceId, destinationId string) *wait.AsyncActionHandler[telemetryrouter.DestinationResponse] { |
| 93 | + waitConfig := wait.WaiterHelper[telemetryrouter.DestinationResponse, string]{ |
| 94 | + FetchInstance: a.GetDestination(ctx, projectId, regionId, instanceId, destinationId).Execute, |
| 95 | + GetState: func(d *telemetryrouter.DestinationResponse) (string, error) { |
| 96 | + if d == nil { |
| 97 | + return "", errors.New("empty response") |
| 98 | + } |
| 99 | + return d.Status, nil |
| 100 | + }, |
| 101 | + ActiveState: []string{DESTINATION_ACTIVE}, |
| 102 | + } |
| 103 | + |
| 104 | + handler := wait.New(waitConfig.Wait()) |
| 105 | + handler.SetTimeout(10 * time.Minute) |
| 106 | + return handler |
| 107 | +} |
| 108 | + |
| 109 | +// DeleteDestinationWaitHandler will wait for Destination deletion |
| 110 | +func DeleteDestinationWaitHandler(ctx context.Context, a telemetryrouter.DefaultAPI, projectId, regionId, instanceId, destinationId string) *wait.AsyncActionHandler[telemetryrouter.DestinationResponse] { |
| 111 | + waitConfig := wait.WaiterHelper[telemetryrouter.DestinationResponse, string]{ |
| 112 | + FetchInstance: a.GetDestination(ctx, projectId, regionId, instanceId, destinationId).Execute, |
| 113 | + GetState: func(d *telemetryrouter.DestinationResponse) (string, error) { |
| 114 | + if d == nil { |
| 115 | + return "", errors.New("empty response") |
| 116 | + } |
| 117 | + return d.Status, nil |
| 118 | + }, |
| 119 | + DeleteHttpErrorStatusCodes: []int{http.StatusNotFound}, |
| 120 | + } |
| 121 | + |
| 122 | + handler := wait.New(waitConfig.Wait()) |
| 123 | + handler.SetTimeout(10 * time.Minute) |
| 124 | + return handler |
| 125 | +} |
| 126 | + |
| 127 | +// CreateAccessTokenWaitHandler will wait for AccessToken creation |
| 128 | +func CreateAccessTokenWaitHandler(ctx context.Context, a telemetryrouter.DefaultAPI, projectId, regionId, instanceId, accessTokenId string) *wait.AsyncActionHandler[telemetryrouter.GetAccessTokenResponse] { |
| 129 | + waitConfig := wait.WaiterHelper[telemetryrouter.GetAccessTokenResponse, string]{ |
| 130 | + FetchInstance: a.GetAccessToken(ctx, projectId, regionId, instanceId, accessTokenId).Execute, |
| 131 | + GetState: func(d *telemetryrouter.GetAccessTokenResponse) (string, error) { |
| 132 | + if d == nil { |
| 133 | + return "", errors.New("empty response") |
| 134 | + } |
| 135 | + return d.Status, nil |
| 136 | + }, |
| 137 | + ActiveState: []string{ACCESSTOKEN_ACTIVE}, |
| 138 | + } |
| 139 | + |
| 140 | + handler := wait.New(waitConfig.Wait()) |
| 141 | + handler.SetTimeout(10 * time.Minute) |
| 142 | + return handler |
| 143 | +} |
| 144 | + |
| 145 | +// UpdateAccessTokenWaitHandler will wait for AccessToken update |
| 146 | +func UpdateAccessTokenWaitHandler(ctx context.Context, a telemetryrouter.DefaultAPI, projectId, regionId, instanceId, accessTokenId string) *wait.AsyncActionHandler[telemetryrouter.GetAccessTokenResponse] { |
| 147 | + waitConfig := wait.WaiterHelper[telemetryrouter.GetAccessTokenResponse, string]{ |
| 148 | + FetchInstance: a.GetAccessToken(ctx, projectId, regionId, instanceId, accessTokenId).Execute, |
| 149 | + GetState: func(d *telemetryrouter.GetAccessTokenResponse) (string, error) { |
| 150 | + if d == nil { |
| 151 | + return "", errors.New("empty response") |
| 152 | + } |
| 153 | + return d.Status, nil |
| 154 | + }, |
| 155 | + ActiveState: []string{ACCESSTOKEN_ACTIVE}, |
| 156 | + } |
| 157 | + |
| 158 | + handler := wait.New(waitConfig.Wait()) |
| 159 | + handler.SetTimeout(10 * time.Minute) |
| 160 | + return handler |
| 161 | +} |
| 162 | + |
| 163 | +// DeleteAccessTokenWaitHandler will wait for AccessToken deletion |
| 164 | +func DeleteAccessTokenWaitHandler(ctx context.Context, a telemetryrouter.DefaultAPI, projectId, regionId, instanceId, accessTokenId string) *wait.AsyncActionHandler[telemetryrouter.GetAccessTokenResponse] { |
| 165 | + waitConfig := wait.WaiterHelper[telemetryrouter.GetAccessTokenResponse, string]{ |
| 166 | + FetchInstance: a.GetAccessToken(ctx, projectId, regionId, instanceId, accessTokenId).Execute, |
| 167 | + GetState: func(d *telemetryrouter.GetAccessTokenResponse) (string, error) { |
| 168 | + if d == nil { |
| 169 | + return "", errors.New("empty response") |
| 170 | + } |
| 171 | + return d.Status, nil |
| 172 | + }, |
| 173 | + DeleteHttpErrorStatusCodes: []int{http.StatusNotFound}, |
| 174 | + } |
| 175 | + |
| 176 | + handler := wait.New(waitConfig.Wait()) |
| 177 | + handler.SetTimeout(10 * time.Minute) |
| 178 | + return handler |
| 179 | +} |
0 commit comments