|
| 1 | +package wait |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" |
| 11 | + "github.com/stackitcloud/stackit-sdk-go/core/wait" |
| 12 | + "github.com/stackitcloud/stackit-sdk-go/services/cdn" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + DistributionStateCreating = "CREATING" |
| 17 | + DistributionStateActive = "ACTIVE" |
| 18 | + DistributionStateUpdating = "UPDATING" |
| 19 | + DistributionStateDeleting = "DELETING" |
| 20 | + DistributionStateError = "ERROR" |
| 21 | +) |
| 22 | + |
| 23 | +// Interfaces needed for tests |
| 24 | +type APIClientInterface interface { |
| 25 | + GetDistributionExecute(ctx context.Context, projectId string, distributionId string) (*cdn.GetDistributionResponse, error) |
| 26 | +} |
| 27 | + |
| 28 | +func CreateDistributionPoolWaitHandler(ctx context.Context, api APIClientInterface, projectId, distributionId string) *wait.AsyncActionHandler[cdn.GetDistributionResponse] { |
| 29 | + handler := wait.New(func() (waitFinished bool, distribution *cdn.GetDistributionResponse, err error) { |
| 30 | + distribution, err = api.GetDistributionExecute(ctx, projectId, distributionId) |
| 31 | + if err != nil { |
| 32 | + return false, distribution, err |
| 33 | + } |
| 34 | + if distribution == nil || |
| 35 | + distribution.Distribution == nil || |
| 36 | + distribution.Distribution.Id == nil || |
| 37 | + distribution.Distribution.Status == nil { |
| 38 | + return false, distribution, fmt.Errorf("create failed for distribution with id %s, the response is not valid (state missing)", distributionId) |
| 39 | + } |
| 40 | + if *distribution.Distribution.Id == distributionId { |
| 41 | + switch *distribution.Distribution.Status { |
| 42 | + case DistributionStateActive: |
| 43 | + return true, distribution, err |
| 44 | + default: |
| 45 | + return false, distribution, err |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return false, nil, nil |
| 50 | + }) |
| 51 | + |
| 52 | + handler.SetTimeout(10 * time.Minute) |
| 53 | + return handler |
| 54 | +} |
| 55 | + |
| 56 | +func UpdateDistributionWaitHandler(ctx context.Context, api APIClientInterface, projectId, distributionId string) *wait.AsyncActionHandler[cdn.GetDistributionResponse] { |
| 57 | + handler := wait.New(func() (waitFinished bool, distribution *cdn.GetDistributionResponse, err error) { |
| 58 | + distribution, err = api.GetDistributionExecute(ctx, projectId, distributionId) |
| 59 | + if err != nil { |
| 60 | + return false, distribution, err |
| 61 | + } |
| 62 | + if distribution == nil || |
| 63 | + distribution.Distribution == nil || |
| 64 | + distribution.Distribution.Id == nil || |
| 65 | + distribution.Distribution.Status == nil { |
| 66 | + return false, distribution, fmt.Errorf("update failed for distribution with id %s, the response is not valid (state missing)", distributionId) |
| 67 | + } |
| 68 | + if *distribution.Distribution.Id == distributionId { |
| 69 | + switch *distribution.Distribution.Status { |
| 70 | + case DistributionStateActive: |
| 71 | + return true, distribution, err |
| 72 | + default: |
| 73 | + return false, distribution, err |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return false, nil, nil |
| 78 | + }) |
| 79 | + |
| 80 | + handler.SetTimeout(10 * time.Minute) |
| 81 | + return handler |
| 82 | +} |
| 83 | + |
| 84 | +func DeleteDistributionWaitHandler(ctx context.Context, api APIClientInterface, projectId, distributionId string) *wait.AsyncActionHandler[cdn.GetDistributionResponse] { |
| 85 | + handler := wait.New(func() (waitFinished bool, distribution *cdn.GetDistributionResponse, err error) { |
| 86 | + distribution, err = api.GetDistributionExecute(ctx, projectId, distributionId) |
| 87 | + if err != nil { |
| 88 | + var oapiError *oapierror.GenericOpenAPIError |
| 89 | + if errors.As(err, &oapiError) { |
| 90 | + if statusCode := oapiError.StatusCode; statusCode == http.StatusNotFound || statusCode == http.StatusGone { |
| 91 | + return true, distribution, nil |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + return false, nil, nil |
| 96 | + }) |
| 97 | + |
| 98 | + handler.SetTimeout(10 * time.Minute) |
| 99 | + return handler |
| 100 | +} |
0 commit comments