Skip to content

Commit f081189

Browse files
committed
PR feedback
1 parent e166d75 commit f081189

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

internal/clients/kibana_oapi/data_views.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ func GetDefaultDataView(ctx context.Context, client *Client, spaceID string) (*s
9595
return nil, diagutil.FrameworkDiagFromError(err)
9696
}
9797

98+
// We don't check for a 404 here. The API doesn't document a 404 response for this endpoint.
99+
// In testing, there's no case where a 404 is returned:
100+
// - If no default data view is set, the API returns 200 with an empty string as the data view ID.
101+
// - If the space doesn't exist, the API still returns 200 with an empty string as the data view ID.
102+
// Therefore, we only handle the 200 response and treat any other status code as an error.
98103
switch resp.StatusCode() {
99104
case http.StatusOK:
100105
if resp.JSON200 != nil && resp.JSON200.DataViewId != nil && *resp.JSON200.DataViewId != "" {

internal/kibana/default_data_view/create.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/hashicorp/terraform-plugin-framework/diag"
99
"github.com/hashicorp/terraform-plugin-framework/resource"
1010
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
11-
"github.com/hashicorp/terraform-plugin-framework/types"
1211
)
1312

1413
func (r *DefaultDataViewResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
@@ -43,8 +42,11 @@ func (r *DefaultDataViewResource) setDefaultDataView(ctx context.Context, plan t
4342
return diags
4443
}
4544

46-
// Use the space_id as the resource ID
47-
model.ID = types.StringValue(spaceID)
45+
model, readDiags := r.read(ctx, client, model)
46+
diags.Append(readDiags...)
47+
if diags.HasError() {
48+
return diags
49+
}
4850

4951
diags = state.Set(ctx, model)
5052
return diags

internal/kibana/default_data_view/read.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55

66
"github.com/elastic/terraform-provider-elasticstack/internal/clients/kibana_oapi"
7+
"github.com/hashicorp/terraform-plugin-framework/diag"
78
"github.com/hashicorp/terraform-plugin-framework/resource"
89
"github.com/hashicorp/terraform-plugin-framework/types"
910
)
@@ -22,19 +23,28 @@ func (r *DefaultDataViewResource) Read(ctx context.Context, req resource.ReadReq
2223
return
2324
}
2425

25-
spaceID := state.SpaceID.ValueString()
26-
defaultDataViewID, diags := kibana_oapi.GetDefaultDataView(ctx, client, spaceID)
26+
state, diags = r.read(ctx, client, state)
2727
resp.Diagnostics.Append(diags...)
2828
if resp.Diagnostics.HasError() {
2929
return
3030
}
3131

32+
diags = resp.State.Set(ctx, state)
33+
resp.Diagnostics.Append(diags...)
34+
}
35+
36+
func (r *DefaultDataViewResource) read(ctx context.Context, client *kibana_oapi.Client, state defaultDataViewModel) (defaultDataViewModel, diag.Diagnostics) {
37+
spaceID := state.SpaceID.ValueString()
38+
defaultDataViewID, diags := kibana_oapi.GetDefaultDataView(ctx, client, spaceID)
39+
if diags.HasError() {
40+
return state, diags
41+
}
42+
3243
// Update state with current default data view
3344
state.DataViewID = types.StringPointerValue(defaultDataViewID)
3445

3546
// Use the space_id as the resource ID
3647
state.ID = types.StringValue(spaceID)
3748

38-
diags = resp.State.Set(ctx, state)
39-
resp.Diagnostics.Append(diags...)
49+
return state, nil
4050
}

0 commit comments

Comments
 (0)