Skip to content

Commit 13df2f0

Browse files
synthetics/parameter: Remove duplicate code when rereading in create/update
Signed-off-by: Andrew Gunnerson <andrew.gunnerson@elastic.co>
1 parent 0f147fb commit 13df2f0

File tree

3 files changed

+36
-55
lines changed

3 files changed

+36
-55
lines changed

internal/kibana/synthetics/parameter/create.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,8 @@ func (r *Resource) Create(ctx context.Context, request resource.CreateRequest, r
2929
return
3030
}
3131

32-
resourceId := result.Id
33-
34-
// We can't trust the response from the POST request. At least with Kibana
35-
// 9.0.0, it responds without the `value` field set.
36-
result, err = kibanaClient.KibanaSynthetics.Parameter.Get(ctx, resourceId)
37-
if err != nil {
38-
response.Diagnostics.AddError(fmt.Sprintf("Failed to get parameter after creation `%s`", resourceId), err.Error())
39-
return
40-
}
41-
42-
plan = toModelV0(*result)
43-
44-
diags = response.State.Set(ctx, plan)
45-
response.Diagnostics.Append(diags...)
46-
if response.Diagnostics.HasError() {
47-
return
48-
}
32+
// We can't trust the response from the POST request, so read the parameter
33+
// again. At least with Kibana 9.0.0, the POST request responds without the
34+
// `value` field set.
35+
r.readState(ctx, kibanaClient, toModelV0(*result), &response.State, &response.Diagnostics)
4936
}

internal/kibana/synthetics/parameter/read.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,20 @@ import (
55
"errors"
66
"fmt"
77

8+
"github.com/disaster37/go-kibana-rest/v8"
89
"github.com/disaster37/go-kibana-rest/v8/kbapi"
910
"github.com/elastic/terraform-provider-elasticstack/internal/kibana/synthetics"
11+
"github.com/hashicorp/terraform-plugin-framework/diag"
1012
"github.com/hashicorp/terraform-plugin-framework/resource"
13+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
1114
)
1215

13-
func (r *Resource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) {
14-
kibanaClient := synthetics.GetKibanaClient(r, response.Diagnostics)
15-
if kibanaClient == nil {
16-
return
17-
}
18-
19-
var state tfModelV0
20-
diags := request.State.Get(ctx, &state)
21-
response.Diagnostics.Append(diags...)
22-
if response.Diagnostics.HasError() {
23-
return
24-
}
25-
26-
resourceId := state.ID.ValueString()
16+
func (r *Resource) readState(ctx context.Context, kibanaClient *kibana.Client, model tfModelV0, state *tfsdk.State, diagnostics *diag.Diagnostics) {
17+
resourceId := model.ID.ValueString()
2718

2819
compositeId, dg := tryReadCompositeId(resourceId)
29-
response.Diagnostics.Append(dg...)
30-
if response.Diagnostics.HasError() {
20+
diagnostics.Append(dg...)
21+
if diagnostics.HasError() {
3122
return
3223
}
3324

@@ -39,20 +30,36 @@ func (r *Resource) Read(ctx context.Context, request resource.ReadRequest, respo
3930
if err != nil {
4031
var apiError *kbapi.APIError
4132
if errors.As(err, &apiError) && apiError.Code == 404 {
42-
response.State.RemoveResource(ctx)
33+
state.RemoveResource(ctx)
4334
return
4435
}
4536

46-
response.Diagnostics.AddError(fmt.Sprintf("Failed to get parameter `%s`", resourceId), err.Error())
37+
diagnostics.AddError(fmt.Sprintf("Failed to get parameter `%s`", resourceId), err.Error())
4738
return
4839
}
4940

50-
state = toModelV0(*result)
41+
model = toModelV0(*result)
5142

5243
// Set refreshed state
53-
diags = response.State.Set(ctx, &state)
44+
diags := state.Set(ctx, &model)
45+
diagnostics.Append(diags...)
46+
if diagnostics.HasError() {
47+
return
48+
}
49+
}
50+
51+
func (r *Resource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) {
52+
kibanaClient := synthetics.GetKibanaClient(r, response.Diagnostics)
53+
if kibanaClient == nil {
54+
return
55+
}
56+
57+
var state tfModelV0
58+
diags := request.State.Get(ctx, &state)
5459
response.Diagnostics.Append(diags...)
5560
if response.Diagnostics.HasError() {
5661
return
5762
}
63+
64+
r.readState(ctx, kibanaClient, state, &response.State, &response.Diagnostics)
5865
}

internal/kibana/synthetics/parameter/update.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,14 @@ func (r *Resource) Update(ctx context.Context, request resource.UpdateRequest, r
3535

3636
input := state.toParameterConfig(true)
3737

38-
_, err := kibanaClient.KibanaSynthetics.Parameter.Update(ctx, resourceId, input)
38+
result, err := kibanaClient.KibanaSynthetics.Parameter.Update(ctx, resourceId, input)
3939
if err != nil {
4040
response.Diagnostics.AddError(fmt.Sprintf("Failed to update parameter `%s`", resourceId), err.Error())
4141
return
4242
}
4343

44-
// We can't trust the response from the PUT request. At least with Kibana
45-
// 9.0.0, it responds with the new values for every field, except `value`,
46-
// which contains the old value.
47-
result, err := kibanaClient.KibanaSynthetics.Parameter.Get(ctx, resourceId)
48-
if err != nil {
49-
response.Diagnostics.AddError(fmt.Sprintf("Failed to get parameter after update `%s`", resourceId), err.Error())
50-
return
51-
}
52-
53-
state = toModelV0(*result)
54-
55-
// Set refreshed state
56-
diags = response.State.Set(ctx, &state)
57-
response.Diagnostics.Append(diags...)
58-
if response.Diagnostics.HasError() {
59-
return
60-
}
44+
// We can't trust the response from the PUT request, so read the parameter
45+
// again. At least with Kibana 9.0.0, the PUT request responds with the new
46+
// values for every field, except `value`, which contains the old value.
47+
r.readState(ctx, kibanaClient, toModelV0(*result), &response.State, &response.Diagnostics)
6148
}

0 commit comments

Comments
 (0)