|
7 | 7 | "strconv" |
8 | 8 | "strings" |
9 | 9 |
|
| 10 | + "github.com/google/go-cmp/cmp" |
10 | 11 | "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" |
11 | 12 |
|
12 | 13 | observabilityUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/observability/utils" |
@@ -1094,19 +1095,40 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques |
1094 | 1095 | core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Creating API payload: %v", err)) |
1095 | 1096 | return |
1096 | 1097 | } |
1097 | | - // Update existing instance |
1098 | | - _, err = r.client.UpdateInstance(ctx, instanceId, projectId).UpdateInstancePayload(*payload).Execute() |
1099 | | - if err != nil { |
1100 | | - core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Calling API: %v", err)) |
| 1098 | + var previousState Model |
| 1099 | + diags = req.State.Get(ctx, &previousState) |
| 1100 | + resp.Diagnostics.Append(diags...) |
| 1101 | + if resp.Diagnostics.HasError() { |
1101 | 1102 | return |
1102 | 1103 | } |
1103 | | - waitResp, err := wait.UpdateInstanceWaitHandler(ctx, r.client, instanceId, projectId).WaitWithContext(ctx) |
| 1104 | + previousStatePayload, err := toUpdatePayload(&previousState) |
1104 | 1105 | if err != nil { |
1105 | | - core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Instance update waiting: %v", err)) |
| 1106 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Creating previous state payload: %v", err)) |
1106 | 1107 | return |
1107 | 1108 | } |
| 1109 | + var instance *observability.GetInstanceResponse |
| 1110 | + // This check is required, because when values should be updated, that needs to be updated via a different endpoint, the waiter will run into a timeout |
| 1111 | + if !cmp.Equal(previousStatePayload, payload) { |
| 1112 | + // Update existing instance |
| 1113 | + _, err = r.client.UpdateInstance(ctx, instanceId, projectId).UpdateInstancePayload(*payload).Execute() |
| 1114 | + if err != nil { |
| 1115 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Calling API: %v", err)) |
| 1116 | + return |
| 1117 | + } |
| 1118 | + instance, err = wait.UpdateInstanceWaitHandler(ctx, r.client, instanceId, projectId).WaitWithContext(ctx) |
| 1119 | + if err != nil { |
| 1120 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Instance update waiting: %v", err)) |
| 1121 | + return |
| 1122 | + } |
| 1123 | + } else { |
| 1124 | + instance, err = r.client.GetInstanceExecute(ctx, instanceId, projectId) |
| 1125 | + if err != nil { |
| 1126 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Instance read: %v", err)) |
| 1127 | + return |
| 1128 | + } |
| 1129 | + } |
1108 | 1130 |
|
1109 | | - err = mapFields(ctx, waitResp, &model) |
| 1131 | + err = mapFields(ctx, instance, &model) |
1110 | 1132 | if err != nil { |
1111 | 1133 | core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Processing API payload: %v", err)) |
1112 | 1134 | return |
|
0 commit comments