Skip to content

Commit 71e71bd

Browse files
author
Arthur Amstutz
committed
fix: Retry failover IP attach to cloud project
1 parent 4561b1d commit 71e71bd

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

ovh/resource_cloud_project_failover_ip_attach.go

+26-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package ovh
22

33
import (
4+
"context"
45
"fmt"
56
"log"
67
"net/url"
8+
"time"
79

10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
811
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/ovh/go-ovh/ovh"
913
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
1014
)
1115

@@ -182,19 +186,31 @@ func resourceCloudProjectFailoverIpAttachCreate(d *schema.ResourceData, meta int
182186
url.PathEscape(id),
183187
)
184188

185-
ip := &FailoverIp{}
186-
if err := config.OVHClient.Post(endpoint, opts, ip); err != nil {
187-
return fmt.Errorf("calling Post %s: %q", endpoint, err)
188-
}
189+
retry.RetryContext(context.Background(), 5*time.Minute, func() *retry.RetryError {
190+
ip := &FailoverIp{}
191+
if err := config.OVHClient.Post(endpoint, opts, ip); err != nil {
192+
// Retry 400 errors because it can mean that the instance IP
193+
// is not yet allocated internally.
194+
ovhError, isOvhApiError := err.(*ovh.APIError)
195+
if isOvhApiError && ovhError.Code == 400 {
196+
log.Printf("[INFO] container registry id %s on project %s deleted", id, serviceName)
197+
return retry.RetryableError(fmt.Errorf("error calling POST %s: %q", endpoint, err))
198+
} else {
199+
return retry.NonRetryableError(fmt.Errorf("failed to attach failover IP: %s", err))
200+
}
201+
}
189202

190-
for k, v := range ip.ToMap() {
191-
if k != "id" {
192-
err := d.Set(k, v)
193-
if err != nil {
194-
return err
203+
for k, v := range ip.ToMap() {
204+
if k != "id" {
205+
err := d.Set(k, v)
206+
if err != nil {
207+
return retry.NonRetryableError(err)
208+
}
195209
}
196210
}
197-
}
211+
212+
return nil
213+
})
198214

199215
for d.Get("status").(string) == "operationPending" {
200216
if err := resourceCloudProjectFailoverIpAttachRead(d, meta); err != nil {

0 commit comments

Comments
 (0)