Skip to content

Commit bd62e01

Browse files
committed
call read after update/create
1 parent d623300 commit bd62e01

File tree

6 files changed

+44
-45
lines changed

6 files changed

+44
-45
lines changed

internal/clients/kibana_oapi/maintenance_window.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ func CreateMaintenanceWindow(ctx context.Context, client *Client, spaceID string
4141
}
4242

4343
// UpdateMaintenanceWindow updates an existing maintenance window.
44-
func UpdateMaintenanceWindow(ctx context.Context, client *Client, spaceID string, maintenanceWindowID string, req kbapi.PatchMaintenanceWindowIdJSONRequestBody) (*kbapi.PatchMaintenanceWindowIdResponse, diag.Diagnostics) {
44+
func UpdateMaintenanceWindow(ctx context.Context, client *Client, spaceID string, maintenanceWindowID string, req kbapi.PatchMaintenanceWindowIdJSONRequestBody) diag.Diagnostics {
4545
resp, err := client.API.PatchMaintenanceWindowIdWithResponse(ctx, spaceID, maintenanceWindowID, req)
4646
if err != nil {
47-
return nil, utils.FrameworkDiagFromError(err)
47+
return utils.FrameworkDiagFromError(err)
4848
}
4949

5050
switch resp.StatusCode() {
5151
case http.StatusOK:
52-
return resp, nil
52+
return nil
5353
default:
54-
return nil, reportUnknownError(resp.StatusCode(), resp.Body)
54+
return reportUnknownError(resp.StatusCode(), resp.Body)
5555
}
5656
}
5757

internal/kibana/maintenance_window/create.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,30 @@ func (r *MaintenanceWindowResource) Create(ctx context.Context, req resource.Cre
4545
}
4646

4747
spaceID := planMaintenanceWindow.SpaceID.ValueString()
48-
maintenanceWindowAPIResponse, diags := kibana_oapi.CreateMaintenanceWindow(ctx, client, spaceID, body)
48+
createMaintenanceWindowResponse, diags := kibana_oapi.CreateMaintenanceWindow(ctx, client, spaceID, body)
4949

5050
resp.Diagnostics.Append(diags...)
5151
if resp.Diagnostics.HasError() {
5252
return
5353
}
5454

55-
diags = planMaintenanceWindow.fromAPICreateResponse(ctx, maintenanceWindowAPIResponse)
55+
/*
56+
* In create/update paths we typically follow the write operation with a read, and then set the state from the read.
57+
* We want to avoid a dirty plan immediately after an apply.
58+
*/
59+
maintenanceWindowID := createMaintenanceWindowResponse.JSON200.Id
60+
readMaintenanceWindowResponse, diags := kibana_oapi.GetMaintenanceWindow(ctx, client, spaceID, maintenanceWindowID)
61+
resp.Diagnostics.Append(diags...)
62+
if resp.Diagnostics.HasError() {
63+
return
64+
}
65+
66+
if readMaintenanceWindowResponse == nil {
67+
resp.State.RemoveResource(ctx)
68+
return
69+
}
70+
71+
diags = planMaintenanceWindow.fromAPIReadResponse(ctx, readMaintenanceWindowResponse)
5672
resp.Diagnostics.Append(diags...)
5773
if resp.Diagnostics.HasError() {
5874
return

internal/kibana/maintenance_window/delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (r *MaintenanceWindowResource) Delete(ctx context.Context, req resource.Del
2222
return
2323
}
2424

25-
viewID, spaceID := stateModel.getMaintenanceWindowIDAndSpaceID()
26-
diags = kibana_oapi.DeleteMaintenanceWindow(ctx, client, spaceID, viewID)
25+
maintenanceWindowID, spaceID := stateModel.getMaintenanceWindowIDAndSpaceID()
26+
diags = kibana_oapi.DeleteMaintenanceWindow(ctx, client, spaceID, maintenanceWindowID)
2727
resp.Diagnostics.Append(diags...)
2828
}

internal/kibana/maintenance_window/models.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,6 @@ func (model MaintenanceWindowModel) toAPICreateRequest(ctx context.Context) (kba
6666
return body, diags
6767
}
6868

69-
func (model *MaintenanceWindowModel) fromAPICreateResponse(ctx context.Context, data *kbapi.PostMaintenanceWindowResponse) diag.Diagnostics {
70-
if data == nil {
71-
return nil
72-
}
73-
74-
var diags = diag.Diagnostics{}
75-
var response = &ResponseJson{}
76-
77-
if err := json.Unmarshal(data.Body, response); err != nil {
78-
diags.AddError(err.Error(), "cannot unmarshal PostMaintenanceWindowResponse")
79-
return diags
80-
}
81-
82-
return model._fromAPIResponse(ctx, *response)
83-
}
84-
8569
/* READ */
8670

8771
func (model *MaintenanceWindowModel) fromAPIReadResponse(ctx context.Context, data *kbapi.GetMaintenanceWindowIdResponse) diag.Diagnostics {
@@ -152,22 +136,6 @@ func (model MaintenanceWindowModel) toAPIUpdateRequest(ctx context.Context) (kba
152136
return body, diags
153137
}
154138

155-
func (model *MaintenanceWindowModel) fromAPIUpdateResponse(ctx context.Context, data *kbapi.PatchMaintenanceWindowIdResponse) diag.Diagnostics {
156-
if data == nil {
157-
return nil
158-
}
159-
160-
var diags = diag.Diagnostics{}
161-
var response = &ResponseJson{}
162-
163-
if err := json.Unmarshal(data.Body, response); err != nil {
164-
diags.AddError(err.Error(), "cannot unmarshal PatchMaintenanceWindowIdResponse")
165-
return diags
166-
}
167-
168-
return model._fromAPIResponse(ctx, *response)
169-
}
170-
171139
/* DELETE */
172140

173141
func (model MaintenanceWindowModel) getMaintenanceWindowIDAndSpaceID() (maintenanceWindowID string, spaceID string) {

internal/kibana/maintenance_window/read.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ func (r *MaintenanceWindowResource) Read(ctx context.Context, req resource.ReadR
3737
return
3838
}
3939

40-
viewID, spaceID := stateModel.getMaintenanceWindowIDAndSpaceID()
41-
maintenanceWindow, diags := kibana_oapi.GetMaintenanceWindow(ctx, client, spaceID, viewID)
40+
maintenanceWindowID, spaceID := stateModel.getMaintenanceWindowIDAndSpaceID()
41+
maintenanceWindow, diags := kibana_oapi.GetMaintenanceWindow(ctx, client, spaceID, maintenanceWindowID)
4242
resp.Diagnostics.Append(diags...)
4343
if resp.Diagnostics.HasError() {
4444
return

internal/kibana/maintenance_window/update.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,29 @@ func (r *MaintenanceWindowResource) Update(ctx context.Context, req resource.Upd
4343
return
4444
}
4545

46-
viewID, spaceID := planMaintenanceWindow.getMaintenanceWindowIDAndSpaceID()
47-
maintenanceWindow, diags := kibana_oapi.UpdateMaintenanceWindow(ctx, client, spaceID, viewID, body)
46+
maintenanceWindowID, spaceID := planMaintenanceWindow.getMaintenanceWindowIDAndSpaceID()
47+
diags = kibana_oapi.UpdateMaintenanceWindow(ctx, client, spaceID, maintenanceWindowID, body)
4848
resp.Diagnostics.Append(diags...)
4949
if resp.Diagnostics.HasError() {
5050
return
5151
}
5252

53-
diags = planMaintenanceWindow.fromAPIUpdateResponse(ctx, maintenanceWindow)
53+
/*
54+
* In create/update paths we typically follow the write operation with a read, and then set the state from the read.
55+
* We want to avoid a dirty plan immediately after an apply.
56+
*/
57+
readMaintenanceWindowResponse, diags := kibana_oapi.GetMaintenanceWindow(ctx, client, spaceID, maintenanceWindowID)
58+
resp.Diagnostics.Append(diags...)
59+
if resp.Diagnostics.HasError() {
60+
return
61+
}
62+
63+
if readMaintenanceWindowResponse == nil {
64+
resp.State.RemoveResource(ctx)
65+
return
66+
}
67+
68+
diags = planMaintenanceWindow.fromAPIReadResponse(ctx, readMaintenanceWindowResponse)
5469
resp.Diagnostics.Append(diags...)
5570
if resp.Diagnostics.HasError() {
5671
return

0 commit comments

Comments
 (0)