|
| 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 | + cdn "github.com/stackitcloud/stackit-sdk-go/services/cdn/v1api" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + DISTRIBUTIONSTATUS_CREATING = "CREATING" |
| 17 | + DISTRIBUTIONSTATUS_ACTIVE = "ACTIVE" |
| 18 | + DISTRIBUTIONSTATUS_UPDATING = "UPDATING" |
| 19 | + DISTRIBUTIONSTATUS_DELETING = "DELETING" |
| 20 | + DISTRIBUTIONSTATUS_ERROR = "ERROR" |
| 21 | +) |
| 22 | + |
| 23 | +func CreateDistributionPoolWaitHandler(ctx context.Context, api cdn.DefaultAPI, projectId, distributionId string) *wait.AsyncActionHandler[cdn.GetDistributionResponse] { |
| 24 | + handler := wait.New(func() (waitFinished bool, distribution *cdn.GetDistributionResponse, err error) { |
| 25 | + distribution, err = api.GetDistribution(ctx, projectId, distributionId).Execute() |
| 26 | + if err != nil { |
| 27 | + return false, distribution, err |
| 28 | + } |
| 29 | + if distribution == nil { |
| 30 | + return false, distribution, fmt.Errorf("create failed for distribution with id %s, the response is nil", distributionId) |
| 31 | + } |
| 32 | + if distribution.Distribution.Id == distributionId { |
| 33 | + switch distribution.Distribution.Status { |
| 34 | + case DISTRIBUTIONSTATUS_ACTIVE: |
| 35 | + return true, distribution, nil |
| 36 | + case DISTRIBUTIONSTATUS_CREATING, DISTRIBUTIONSTATUS_UPDATING: |
| 37 | + return false, nil, nil |
| 38 | + case DISTRIBUTIONSTATUS_DELETING: |
| 39 | + return true, nil, fmt.Errorf("creating CDN distribution failed") |
| 40 | + case DISTRIBUTIONSTATUS_ERROR: |
| 41 | + return true, nil, fmt.Errorf("creating CDN distribution failed") |
| 42 | + default: |
| 43 | + return true, nil, fmt.Errorf("CDNDistributionWaitHandler: unexpected status %s", distribution.Distribution.Status) |
| 44 | + } |
| 45 | + } |
| 46 | + return false, nil, nil |
| 47 | + }) |
| 48 | + handler.SetTimeout(10 * time.Minute) |
| 49 | + return handler |
| 50 | +} |
| 51 | + |
| 52 | +func UpdateDistributionWaitHandler(ctx context.Context, api cdn.DefaultAPI, projectId, distributionId string) *wait.AsyncActionHandler[cdn.GetDistributionResponse] { |
| 53 | + handler := wait.New(func() (waitFinished bool, distribution *cdn.GetDistributionResponse, err error) { |
| 54 | + distribution, err = api.GetDistribution(ctx, projectId, distributionId).Execute() |
| 55 | + if err != nil { |
| 56 | + return false, distribution, err |
| 57 | + } |
| 58 | + if distribution == nil { |
| 59 | + return false, distribution, fmt.Errorf("update failed for distribution with id %s, the response is nil", distributionId) |
| 60 | + } |
| 61 | + if distribution.Distribution.Id == distributionId { |
| 62 | + switch distribution.Distribution.Status { |
| 63 | + case DISTRIBUTIONSTATUS_ACTIVE: |
| 64 | + return true, distribution, err |
| 65 | + case DISTRIBUTIONSTATUS_UPDATING: |
| 66 | + return false, nil, nil |
| 67 | + case DISTRIBUTIONSTATUS_DELETING: |
| 68 | + return true, nil, fmt.Errorf("updating CDN distribution failed") |
| 69 | + case DISTRIBUTIONSTATUS_ERROR: |
| 70 | + return true, nil, fmt.Errorf("updating CDN distribution failed") |
| 71 | + default: |
| 72 | + return true, nil, fmt.Errorf("UpdateDistributionWaitHandler: unexpected status %s", distribution.Distribution.Status) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return false, nil, nil |
| 77 | + }) |
| 78 | + |
| 79 | + handler.SetTimeout(10 * time.Minute) |
| 80 | + return handler |
| 81 | +} |
| 82 | + |
| 83 | +func DeleteDistributionWaitHandler(ctx context.Context, api cdn.DefaultAPI, projectId, distributionId string) *wait.AsyncActionHandler[cdn.GetDistributionResponse] { |
| 84 | + handler := wait.New(func() (waitFinished bool, distribution *cdn.GetDistributionResponse, err error) { |
| 85 | + _, err = api.GetDistribution(ctx, projectId, distributionId).Execute() |
| 86 | + |
| 87 | + // the distribution is still gettable, e.g. not deleted |
| 88 | + if err == nil { |
| 89 | + return false, nil, nil |
| 90 | + } |
| 91 | + var oapiError *oapierror.GenericOpenAPIError |
| 92 | + if errors.As(err, &oapiError) { |
| 93 | + if statusCode := oapiError.StatusCode; statusCode == http.StatusNotFound || statusCode == http.StatusGone { |
| 94 | + return true, nil, nil |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return false, nil, err |
| 99 | + }) |
| 100 | + handler.SetTimeout(10 * time.Minute) |
| 101 | + return handler |
| 102 | +} |
| 103 | + |
| 104 | +func CreateCDNCustomDomainWaitHandler(ctx context.Context, a cdn.DefaultAPI, projectId, distributionId, domain string) *wait.AsyncActionHandler[cdn.CustomDomain] { |
| 105 | + handler := wait.New(func() (waitFinished bool, response *cdn.CustomDomain, err error) { |
| 106 | + resp, err := a.GetCustomDomain(ctx, projectId, distributionId, domain).Execute() |
| 107 | + if err != nil { |
| 108 | + return false, nil, err |
| 109 | + } |
| 110 | + if resp == nil { |
| 111 | + return false, nil, errors.New("CDNDistributionWaitHandler: response is nil") |
| 112 | + } |
| 113 | + |
| 114 | + switch resp.CustomDomain.Status { |
| 115 | + case cdn.DOMAINSTATUS_ACTIVE: |
| 116 | + return true, &resp.CustomDomain, nil |
| 117 | + case cdn.DOMAINSTATUS_CREATING, cdn.DOMAINSTATUS_UPDATING: |
| 118 | + return false, nil, nil |
| 119 | + case cdn.DOMAINSTATUS_DELETING: |
| 120 | + return true, nil, fmt.Errorf("creating CDN custom domain failed") |
| 121 | + case cdn.DOMAINSTATUS_ERROR: |
| 122 | + return true, nil, fmt.Errorf("creating CDN custom domain failed") |
| 123 | + default: |
| 124 | + return true, nil, fmt.Errorf("CDNCustomDomainWaitHandler: unexpected status %s", resp.CustomDomain.Status) |
| 125 | + } |
| 126 | + }) |
| 127 | + handler.SetTimeout(10 * time.Minute) |
| 128 | + return handler |
| 129 | +} |
| 130 | + |
| 131 | +func DeleteCDNCustomDomainWaitHandler(ctx context.Context, a cdn.DefaultAPI, projectId, distributionId, domain string) *wait.AsyncActionHandler[cdn.CustomDomain] { |
| 132 | + handler := wait.New(func() (waitFinished bool, response *cdn.CustomDomain, err error) { |
| 133 | + _, err = a.GetCustomDomain(ctx, projectId, distributionId, domain).Execute() |
| 134 | + |
| 135 | + // the custom domain is still gettable, e.g. not deleted |
| 136 | + if err == nil { |
| 137 | + return false, nil, nil |
| 138 | + } |
| 139 | + var oapiError *oapierror.GenericOpenAPIError |
| 140 | + if errors.As(err, &oapiError) { |
| 141 | + if statusCode := oapiError.StatusCode; statusCode == http.StatusNotFound || statusCode == http.StatusGone { |
| 142 | + return true, nil, nil |
| 143 | + } |
| 144 | + } |
| 145 | + return false, nil, err |
| 146 | + }) |
| 147 | + handler.SetTimeout(10 * time.Minute) |
| 148 | + return handler |
| 149 | +} |
0 commit comments