Skip to content

Commit 823380c

Browse files
committed
Improve meta property handling / include test
1 parent 1847c2a commit 823380c

File tree

4 files changed

+83
-5
lines changed

4 files changed

+83
-5
lines changed

internal/kibana/security_list_item/acc_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,48 @@ func TestAccResourceSecurityListItem(t *testing.T) {
7272
})
7373
}
7474

75+
func TestAccResourceSecurityListItem_WithMeta(t *testing.T) {
76+
listID := "test-list-items-meta-" + uuid.New().String()
77+
resource.Test(t, resource.TestCase{
78+
PreCheck: func() {
79+
acctest.PreCheck(t)
80+
ensureListIndexExists(t)
81+
},
82+
ProtoV6ProviderFactories: acctest.Providers,
83+
Steps: []resource.TestStep{
84+
{ // Create with meta
85+
ConfigDirectory: acctest.NamedTestCaseDirectory("create"),
86+
ConfigVariables: config.Variables{
87+
"list_id": config.StringVariable(listID),
88+
"value": config.StringVariable("test-value-with-meta"),
89+
"meta": config.StringVariable(`{"category":"suspicious","severity":"high"}`),
90+
},
91+
Check: resource.ComposeTestCheckFunc(
92+
resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list_item.test", "id"),
93+
resource.TestCheckResourceAttr("elasticstack_kibana_security_list_item.test", "value", "test-value-with-meta"),
94+
resource.TestCheckResourceAttr("elasticstack_kibana_security_list_item.test", "meta", `{"category":"suspicious","severity":"high"}`),
95+
resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list_item.test", "created_at"),
96+
resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list_item.test", "created_by"),
97+
resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list_item.test", "updated_at"),
98+
resource.TestCheckResourceAttrSet("elasticstack_kibana_security_list_item.test", "updated_by"),
99+
),
100+
},
101+
{ // Update meta
102+
ConfigDirectory: acctest.NamedTestCaseDirectory("update"),
103+
ConfigVariables: config.Variables{
104+
"list_id": config.StringVariable(listID),
105+
"value": config.StringVariable("test-value-with-meta"),
106+
"meta": config.StringVariable(`{"category":"malicious","notes":"Updated metadata","severity":"critical"}`),
107+
},
108+
Check: resource.ComposeTestCheckFunc(
109+
resource.TestCheckResourceAttr("elasticstack_kibana_security_list_item.test", "value", "test-value-with-meta"),
110+
resource.TestCheckResourceAttr("elasticstack_kibana_security_list_item.test", "meta", `{"category":"malicious","notes":"Updated metadata","severity":"critical"}`),
111+
),
112+
},
113+
},
114+
})
115+
}
116+
75117
func TestAccResourceSecurityListItem_Space(t *testing.T) {
76118
spaceID := "test-space-" + uuid.New().String()
77119
listID := "test-list-" + uuid.New().String()

internal/kibana/security_list_item/models.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func (m *SecurityListItemModel) toAPICreateModel(ctx context.Context) (*kbapi.Cr
4242
// Set optional meta if specified
4343
if utils.IsKnown(m.Meta) {
4444
var meta kbapi.SecurityListsAPIListItemMetadata
45-
if err := json.Unmarshal([]byte(m.Meta.ValueString()), &meta); err != nil {
46-
diags.AddError("Failed to parse meta JSON", err.Error())
45+
diags.Append(m.Meta.Unmarshal(&meta)...)
46+
if diags.HasError() {
4747
return nil, diags
4848
}
4949
body.Meta = &meta
@@ -70,8 +70,8 @@ func (m *SecurityListItemModel) toAPIUpdateModel(ctx context.Context) (*kbapi.Up
7070
// Set optional meta if specified
7171
if utils.IsKnown(m.Meta) {
7272
var meta kbapi.SecurityListsAPIListItemMetadata
73-
if err := json.Unmarshal([]byte(m.Meta.ValueString()), &meta); err != nil {
74-
diags.AddError("Failed to parse meta JSON", err.Error())
73+
diags.Append(m.Meta.Unmarshal(&meta)...)
74+
if diags.HasError() {
7575
return nil, diags
7676
}
7777
body.Meta = &meta
@@ -103,7 +103,7 @@ func (m *SecurityListItemModel) fromAPIModel(ctx context.Context, apiItem *kbapi
103103
if apiItem.Meta != nil {
104104
metaJSON, err := json.Marshal(apiItem.Meta)
105105
if err != nil {
106-
diags.AddError("Failed to serialize meta", err.Error())
106+
diags.AddError("Failed to serialize meta field", err.Error())
107107
return diags
108108
}
109109
m.Meta = jsontypes.NewNormalizedValue(string(metaJSON))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
variable "list_id" {}
2+
variable "value" {}
3+
variable "meta" {}
4+
5+
# First create a security list to put items in
6+
resource "elasticstack_kibana_security_list" "test" {
7+
list_id = var.list_id
8+
name = "Test List for Items with Meta"
9+
description = "A test security list for items with metadata"
10+
type = "keyword"
11+
}
12+
13+
# Create a list item with meta
14+
resource "elasticstack_kibana_security_list_item" "test" {
15+
list_id = elasticstack_kibana_security_list.test.list_id
16+
value = var.value
17+
meta = var.meta
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
variable "list_id" {}
2+
variable "value" {}
3+
variable "meta" {}
4+
5+
# First create a security list to put items in
6+
resource "elasticstack_kibana_security_list" "test" {
7+
list_id = var.list_id
8+
name = "Test List for Items with Meta"
9+
description = "A test security list for items with metadata"
10+
type = "keyword"
11+
}
12+
13+
# Update list item with different meta
14+
resource "elasticstack_kibana_security_list_item" "test" {
15+
list_id = elasticstack_kibana_security_list.test.list_id
16+
value = var.value
17+
meta = var.meta
18+
}

0 commit comments

Comments
 (0)