Skip to content

Commit 017ace6

Browse files
Copilotdimuon
andcommitted
Fix kibana_data_view forced replacement due to auto-calculated field_attrs count
Co-authored-by: dimuon <[email protected]>
1 parent b3d0daf commit 017ace6

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

internal/kibana/data_view/models_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,111 @@ func TestPopulateFromAPI(t *testing.T) {
202202
}, getDataViewAttrTypes(), path.Root("data_view"), &diags),
203203
},
204204
},
205+
{
206+
// Test handling of server-side count updates without existing field_attrs
207+
name: "server_side_count_updates_no_existing_attrs",
208+
existingModel: dataViewModel{
209+
ID: types.StringValue("default/test-id"),
210+
SpaceID: types.StringValue("default"),
211+
DataView: utils.ObjectValueFrom(ctx, &innerModel{
212+
ID: types.StringValue("test-id"),
213+
Title: types.StringValue("test-title"),
214+
FieldAttributes: types.MapNull(getFieldAttrElemType()),
215+
SourceFilters: types.ListNull(types.StringType),
216+
RuntimeFieldMap: types.MapNull(getRuntimeFieldMapElemType()),
217+
FieldFormats: types.MapNull(getFieldFormatElemType()),
218+
Namespaces: utils.ListValueFrom[string](ctx, nil, types.StringType, path.Root("data_view").AtName("namespaces"), &diags),
219+
}, getDataViewAttrTypes(), path.Root("data_view"), &diags),
220+
},
221+
response: kbapi.DataViewsDataViewResponseObject{
222+
DataView: &kbapi.DataViewsDataViewResponseObjectInner{
223+
Id: utils.Pointer("test-id"),
224+
Title: utils.Pointer("test-title"),
225+
FieldAttrs: &map[string]kbapi.DataViewsFieldattrs{
226+
"host.hostname": {
227+
Count: utils.Pointer(5),
228+
},
229+
"event.action": {
230+
Count: utils.Pointer(12),
231+
},
232+
},
233+
},
234+
},
235+
expectedModel: dataViewModel{
236+
ID: types.StringValue("default/test-id"),
237+
SpaceID: types.StringValue("default"),
238+
DataView: utils.ObjectValueFrom(ctx, &innerModel{
239+
ID: types.StringValue("test-id"),
240+
Title: types.StringValue("test-title"),
241+
FieldAttributes: utils.MapValueFrom(ctx, map[string]fieldAttrModel{
242+
"host.hostname": {
243+
CustomLabel: types.StringNull(),
244+
Count: types.Int64Value(5),
245+
},
246+
"event.action": {
247+
CustomLabel: types.StringNull(),
248+
Count: types.Int64Value(12),
249+
},
250+
}, getFieldAttrElemType(), path.Root("data_view").AtName("field_attrs"), &diags),
251+
SourceFilters: types.ListNull(types.StringType),
252+
RuntimeFieldMap: types.MapNull(getRuntimeFieldMapElemType()),
253+
FieldFormats: types.MapNull(getFieldFormatElemType()),
254+
Namespaces: utils.ListValueFrom[string](ctx, nil, types.StringType, path.Root("data_view").AtName("namespaces"), &diags),
255+
}, getDataViewAttrTypes(), path.Root("data_view"), &diags),
256+
},
257+
},
258+
{
259+
// Test handling of server-side count updates with existing field_attrs
260+
name: "server_side_count_updates_with_existing_attrs",
261+
existingModel: dataViewModel{
262+
ID: types.StringValue("default/test-id"),
263+
SpaceID: types.StringValue("default"),
264+
DataView: utils.ObjectValueFrom(ctx, &innerModel{
265+
ID: types.StringValue("test-id"),
266+
Title: types.StringValue("test-title"),
267+
FieldAttributes: utils.MapValueFrom(ctx, map[string]fieldAttrModel{
268+
"host.hostname": {
269+
CustomLabel: types.StringValue("Host Name"),
270+
Count: types.Int64Null(), // Null count in state
271+
},
272+
}, getFieldAttrElemType(), path.Root("data_view").AtName("field_attrs"), &diags),
273+
SourceFilters: types.ListNull(types.StringType),
274+
RuntimeFieldMap: types.MapNull(getRuntimeFieldMapElemType()),
275+
FieldFormats: types.MapNull(getFieldFormatElemType()),
276+
Namespaces: utils.ListValueFrom[string](ctx, nil, types.StringType, path.Root("data_view").AtName("namespaces"), &diags),
277+
}, getDataViewAttrTypes(), path.Root("data_view"), &diags),
278+
},
279+
response: kbapi.DataViewsDataViewResponseObject{
280+
DataView: &kbapi.DataViewsDataViewResponseObjectInner{
281+
Id: utils.Pointer("test-id"),
282+
Title: utils.Pointer("test-title"),
283+
FieldAttrs: &map[string]kbapi.DataViewsFieldattrs{
284+
"host.hostname": {
285+
CustomLabel: utils.Pointer("Host Name"),
286+
Count: utils.Pointer(15), // Server-side count
287+
},
288+
},
289+
},
290+
},
291+
expectedModel: dataViewModel{
292+
ID: types.StringValue("default/test-id"),
293+
SpaceID: types.StringValue("default"),
294+
DataView: utils.ObjectValueFrom(ctx, &innerModel{
295+
ID: types.StringValue("test-id"),
296+
Title: types.StringValue("test-title"),
297+
FieldAttributes: utils.MapValueFrom(ctx, map[string]fieldAttrModel{
298+
"host.hostname": {
299+
CustomLabel: types.StringValue("Host Name"),
300+
Count: types.Int64Value(15), // Should accept server-side value
301+
},
302+
}, getFieldAttrElemType(), path.Root("data_view").AtName("field_attrs"), &diags),
303+
SourceFilters: types.ListNull(types.StringType),
304+
RuntimeFieldMap: types.MapNull(getRuntimeFieldMapElemType()),
305+
FieldFormats: types.MapNull(getFieldFormatElemType()),
306+
Namespaces: utils.ListValueFrom[string](ctx, nil, types.StringType, path.Root("data_view").AtName("namespaces"), &diags),
307+
}, getDataViewAttrTypes(), path.Root("data_view"), &diags),
308+
},
309+
},
205310
}
206311

207312
require.Empty(t, diags)

internal/kibana/data_view/schema.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1010
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
1111
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
12+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
1213
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
1314
"github.com/hashicorp/terraform-plugin-framework/resource/schema/mapplanmodifier"
1415
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
@@ -99,6 +100,10 @@ func getSchema() schema.Schema {
99100
"count": schema.Int64Attribute{
100101
Description: "Popularity count for the field.",
101102
Optional: true,
103+
Computed: true,
104+
PlanModifiers: []planmodifier.Int64{
105+
int64planmodifier.UseStateForUnknown(),
106+
},
102107
},
103108
},
104109
},

0 commit comments

Comments
 (0)