Skip to content

Commit cb16b7e

Browse files
committed
fix(request_condition): save state before post-create calls
The Create function for okta_request_condition only persisted state after every API call succeeded. When the initial POST created the condition but a follow-up call failed (activation when the config sets status = "ACTIVE", or the priority PATCH), Create returned error diagnostics without ever calling resp.State.Set, so Terraform recorded nothing. The condition was left orphaned in Okta as an unmanaged INACTIVE draft, and the next apply created a duplicate. Persist the created condition to state immediately after the create POST succeeds, and refresh the saved state after successful activation, so a mid-Create failure marks the resource as tainted instead of orphaning it. State saved during Create is preserved by Terraform even when error diagnostics are returned. Fixes #2899
1 parent 3e47611 commit cb16b7e

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

okta/services/governance/resource_request_condition.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,23 @@ func (r *requestConditionResource) Create(ctx context.Context, req resource.Crea
211211
return
212212
}
213213

214+
// The condition now exists in Okta. Persist it to state immediately so
215+
// that a failure in any follow-up call below (activation, priority
216+
// update) leaves the resource tracked as tainted instead of orphaned
217+
// and unmanaged in Okta. State saved during Create is kept by Terraform
218+
// even when error diagnostics are returned. A copy of the plan model is
219+
// used so the planned status/priority in `data` stay available to the
220+
// follow-up logic below.
221+
createdState := data
222+
resp.Diagnostics.Append(applyRequestConditionToState(ctx, &createdState, requestConditionResp)...)
223+
if resp.Diagnostics.HasError() {
224+
return
225+
}
226+
resp.Diagnostics.Append(resp.State.Set(ctx, &createdState)...)
227+
if resp.Diagnostics.HasError() {
228+
return
229+
}
230+
214231
// Activate the condition if status is set to ACTIVE
215232
if !data.Status.IsNull() && data.Status.ValueString() == "ACTIVE" {
216233
requestConditionResp, _, err = r.OktaGovernanceClient.OktaGovernanceSDKClient().
@@ -224,6 +241,19 @@ func (r *requestConditionResource) Create(ctx context.Context, req resource.Crea
224241
)
225242
return
226243
}
244+
245+
// Refresh the saved state with the activation result so the recorded
246+
// status stays accurate (and Delete deactivates correctly) if the
247+
// priority update below fails.
248+
createdState = data
249+
resp.Diagnostics.Append(applyRequestConditionToState(ctx, &createdState, requestConditionResp)...)
250+
if resp.Diagnostics.HasError() {
251+
return
252+
}
253+
resp.Diagnostics.Append(resp.State.Set(ctx, &createdState)...)
254+
if resp.Diagnostics.HasError() {
255+
return
256+
}
227257
}
228258

229259
// The API may ignore the priority field on create and return a default

0 commit comments

Comments
 (0)