Skip to content

Commit 8d1a2b8

Browse files
Copilotnick-benoit
andcommitted
Follow CODING_STANDARDS: read resource state after create/update operations
Per CODING_STANDARDS.md line 39, the final state should be derived from a read request following a mutative request. Updated both exception_list and exception_item resources to perform a read after create and update operations, using the read response to build the final state rather than the mutative response. This ensures state consistency and avoids dirty plans after apply. Co-authored-by: nick-benoit <[email protected]>
1 parent 21e6522 commit 8d1a2b8

File tree

4 files changed

+88
-8
lines changed

4 files changed

+88
-8
lines changed

internal/kibana/security/exception_item/create.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,28 @@ func (r *ExceptionItemResource) Create(ctx context.Context, req resource.CreateR
141141
return
142142
}
143143

144-
// Update state with create response
145-
diags = r.updateStateFromAPIResponse(ctx, &plan, createResp.JSON200)
144+
/*
145+
* In create/update paths we typically follow the write operation with a read, and then set the state from the read.
146+
* We want to avoid a dirty plan immediately after an apply.
147+
*/
148+
// Read back the created resource to get the final state
149+
readParams := &kbapi.ReadExceptionListItemParams{
150+
Id: (*kbapi.SecurityExceptionsAPIExceptionListItemId)(&createResp.JSON200.Id),
151+
}
152+
153+
readResp, diags := kibana_oapi.GetExceptionListItem(ctx, client, readParams)
154+
resp.Diagnostics.Append(diags...)
155+
if resp.Diagnostics.HasError() {
156+
return
157+
}
158+
159+
if readResp == nil || readResp.JSON200 == nil {
160+
resp.State.RemoveResource(ctx)
161+
return
162+
}
163+
164+
// Update state with read response
165+
diags = r.updateStateFromAPIResponse(ctx, &plan, readResp.JSON200)
146166
resp.Diagnostics.Append(diags...)
147167
if resp.Diagnostics.HasError() {
148168
return

internal/kibana/security/exception_item/update.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,28 @@ func (r *ExceptionItemResource) Update(ctx context.Context, req resource.UpdateR
132132
return
133133
}
134134

135-
// Update state with response
136-
diags = r.updateStateFromAPIResponse(ctx, &plan, updateResp.JSON200)
135+
/*
136+
* In create/update paths we typically follow the write operation with a read, and then set the state from the read.
137+
* We want to avoid a dirty plan immediately after an apply.
138+
*/
139+
// Read back the updated resource to get the final state
140+
readParams := &kbapi.ReadExceptionListItemParams{
141+
Id: (*kbapi.SecurityExceptionsAPIExceptionListItemId)(&updateResp.JSON200.Id),
142+
}
143+
144+
readResp, diags := kibana_oapi.GetExceptionListItem(ctx, client, readParams)
145+
resp.Diagnostics.Append(diags...)
146+
if resp.Diagnostics.HasError() {
147+
return
148+
}
149+
150+
if readResp == nil || readResp.JSON200 == nil {
151+
resp.State.RemoveResource(ctx)
152+
return
153+
}
154+
155+
// Update state with read response
156+
diags = r.updateStateFromAPIResponse(ctx, &plan, readResp.JSON200)
137157
resp.Diagnostics.Append(diags...)
138158
if resp.Diagnostics.HasError() {
139159
return

internal/kibana/security/exception_list/create.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,28 @@ func (r *ExceptionListResource) Create(ctx context.Context, req resource.CreateR
9494
return
9595
}
9696

97-
// Update state with create response
98-
diags = r.updateStateFromAPIResponse(ctx, &plan, createResp.JSON200)
97+
/*
98+
* In create/update paths we typically follow the write operation with a read, and then set the state from the read.
99+
* We want to avoid a dirty plan immediately after an apply.
100+
*/
101+
// Read back the created resource to get the final state
102+
readParams := &kbapi.ReadExceptionListParams{
103+
Id: (*kbapi.SecurityExceptionsAPIExceptionListId)(&createResp.JSON200.Id),
104+
}
105+
106+
readResp, diags := kibana_oapi.GetExceptionList(ctx, client, readParams)
107+
resp.Diagnostics.Append(diags...)
108+
if resp.Diagnostics.HasError() {
109+
return
110+
}
111+
112+
if readResp == nil || readResp.JSON200 == nil {
113+
resp.State.RemoveResource(ctx)
114+
return
115+
}
116+
117+
// Update state with read response
118+
diags = r.updateStateFromAPIResponse(ctx, &plan, readResp.JSON200)
99119
resp.Diagnostics.Append(diags...)
100120
if resp.Diagnostics.HasError() {
101121
return

internal/kibana/security/exception_list/update.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,28 @@ func (r *ExceptionListResource) Update(ctx context.Context, req resource.UpdateR
9595
return
9696
}
9797

98-
// Update state with response
99-
diags = r.updateStateFromAPIResponse(ctx, &plan, updateResp.JSON200)
98+
/*
99+
* In create/update paths we typically follow the write operation with a read, and then set the state from the read.
100+
* We want to avoid a dirty plan immediately after an apply.
101+
*/
102+
// Read back the updated resource to get the final state
103+
readParams := &kbapi.ReadExceptionListParams{
104+
Id: (*kbapi.SecurityExceptionsAPIExceptionListId)(&updateResp.JSON200.Id),
105+
}
106+
107+
readResp, diags := kibana_oapi.GetExceptionList(ctx, client, readParams)
108+
resp.Diagnostics.Append(diags...)
109+
if resp.Diagnostics.HasError() {
110+
return
111+
}
112+
113+
if readResp == nil || readResp.JSON200 == nil {
114+
resp.State.RemoveResource(ctx)
115+
return
116+
}
117+
118+
// Update state with read response
119+
diags = r.updateStateFromAPIResponse(ctx, &plan, readResp.JSON200)
100120
resp.Diagnostics.Append(diags...)
101121
if resp.Diagnostics.HasError() {
102122
return

0 commit comments

Comments
 (0)