Skip to content

Commit 2d15e2b

Browse files
authored
Merge pull request #1153 from ovh/dev/aamstutz/fix-loadbalancer-update
fix(cloud_project_loadbalancer): Wait for flavor to be updated before saving state
2 parents 926ac82 + 925c4bd commit 2d15e2b

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

ovh/resource_cloud_project_loadbalancer.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import (
55
"fmt"
66
"net/url"
77
"strings"
8+
"time"
89

910
"github.com/hashicorp/terraform-plugin-framework/attr"
1011
"github.com/hashicorp/terraform-plugin-framework/path"
1112
"github.com/hashicorp/terraform-plugin-framework/resource"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
1214
"github.com/ovh/terraform-provider-ovh/v2/ovh/types"
1315
)
1416

@@ -156,7 +158,7 @@ func (r *cloudProjectLoadbalancerResource) Read(ctx context.Context, req resourc
156158
}
157159

158160
func (r *cloudProjectLoadbalancerResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
159-
var data, planData, responseData CloudProjectRegionLoadbalancerModel
161+
var data, planData CloudProjectRegionLoadbalancerModel
160162

161163
// Read Terraform plan data into the model
162164
resp.Diagnostics.Append(req.Plan.Get(ctx, &planData)...)
@@ -180,13 +182,10 @@ func (r *cloudProjectLoadbalancerResource) Update(ctx context.Context, req resou
180182
return
181183
}
182184

183-
// Read updated resource
184-
endpoint = "/cloud/project/" + url.PathEscape(data.ServiceName.ValueString()) + "/region/" + url.PathEscape(data.RegionName.ValueString()) + "/loadbalancing/loadbalancer/" + url.PathEscape(data.Id.ValueString())
185-
if err := r.config.OVHClient.Get(endpoint, &responseData); err != nil {
186-
resp.Diagnostics.AddError(
187-
fmt.Sprintf("Error calling Get %s", endpoint),
188-
err.Error(),
189-
)
185+
// Wait for flavor to be updated (if changed) and read updated data
186+
responseData, err := r.waitForLoadBalancerToBeReady(ctx, endpoint, planData.FlavorId.ValueString(), defaultCloudOperationTimeout)
187+
if err != nil {
188+
resp.Diagnostics.AddError("error waiting for load balancer to be ready", err.Error())
190189
return
191190
}
192191

@@ -219,3 +218,25 @@ func (r *cloudProjectLoadbalancerResource) Delete(ctx context.Context, req resou
219218
)
220219
}
221220
}
221+
222+
func (r *cloudProjectLoadbalancerResource) waitForLoadBalancerToBeReady(ctx context.Context, path, expectedFlavor string, timeout time.Duration) (*CloudProjectRegionLoadbalancerModel, error) {
223+
var responseData CloudProjectRegionLoadbalancerModel
224+
225+
err := retry.RetryContext(ctx, timeout, func() *retry.RetryError {
226+
var resp CloudProjectRegionLoadbalancerModel
227+
if err := r.config.OVHClient.GetWithContext(ctx, path, &resp); err != nil {
228+
return retry.NonRetryableError(err)
229+
}
230+
231+
if resp.FlavorId.ValueString() != expectedFlavor {
232+
time.Sleep(5 * time.Second)
233+
return retry.RetryableError(fmt.Errorf("waiting for load balancer to have the expected flavor (current: %s, expected: %s)", resp.FlavorId.ValueString(), expectedFlavor))
234+
}
235+
236+
responseData = resp
237+
238+
return nil
239+
})
240+
241+
return &responseData, err
242+
}

0 commit comments

Comments
 (0)