Skip to content

Commit aaea807

Browse files
Copilottobio
andauthored
Fix data view color field format params not computed in Terraform state (#1414)
* Initial plan * Add Computed flag to params attribute and acceptance test for color field format Co-authored-by: tobio <444668+tobio@users.noreply.github.com> * Fix acceptance test to ignore override attribute in import Co-authored-by: tobio <444668+tobio@users.noreply.github.com> * Restructure data view tests to use ConfigDirectory and ConfigVariables Co-authored-by: tobio <444668+tobio@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tobio <444668+tobio@users.noreply.github.com>
1 parent 796f491 commit aaea807

File tree

7 files changed

+251
-95
lines changed

7 files changed

+251
-95
lines changed
Lines changed: 68 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package data_view_test
22

33
import (
4-
"fmt"
54
"testing"
65

76
"github.com/elastic/terraform-provider-elasticstack/internal/acctest"
87
"github.com/elastic/terraform-provider-elasticstack/internal/versionutils"
98
"github.com/hashicorp/go-version"
9+
"github.com/hashicorp/terraform-plugin-testing/config"
1010
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
1111
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1212
)
@@ -18,19 +18,26 @@ func TestAccResourceDataView(t *testing.T) {
1818
indexName := "my-index-" + sdkacctest.RandStringFromCharSet(4, sdkacctest.CharSetAlphaNum)
1919

2020
resource.Test(t, resource.TestCase{
21-
PreCheck: func() { acctest.PreCheck(t) },
22-
ProtoV6ProviderFactories: acctest.Providers,
21+
PreCheck: func() { acctest.PreCheck(t) },
2322
Steps: []resource.TestStep{
2423
{
25-
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minDataViewAPISupport),
26-
Config: testAccResourceDataViewPre8_8DV(indexName),
24+
ProtoV6ProviderFactories: acctest.Providers,
25+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minDataViewAPISupport),
26+
ConfigDirectory: acctest.NamedTestCaseDirectory("pre_8_8"),
27+
ConfigVariables: config.Variables{
28+
"index_name": config.StringVariable(indexName),
29+
},
2730
Check: resource.ComposeTestCheckFunc(
2831
resource.TestCheckResourceAttrSet("elasticstack_kibana_data_view.dv", "id"),
2932
),
3033
},
3134
{
32-
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minFullDataviewSupport),
33-
Config: testAccResourceDataViewBasicDV(indexName),
35+
ProtoV6ProviderFactories: acctest.Providers,
36+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minFullDataviewSupport),
37+
ConfigDirectory: acctest.NamedTestCaseDirectory("basic"),
38+
ConfigVariables: config.Variables{
39+
"index_name": config.StringVariable(indexName),
40+
},
3441
Check: resource.ComposeTestCheckFunc(
3542
resource.TestCheckResourceAttrSet("elasticstack_kibana_data_view.dv", "id"),
3643
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.dv", "override", "true"),
@@ -43,8 +50,12 @@ func TestAccResourceDataView(t *testing.T) {
4350
),
4451
},
4552
{
46-
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minFullDataviewSupport),
47-
Config: testAccResourceDataViewBasicDVUpdated(indexName),
53+
ProtoV6ProviderFactories: acctest.Providers,
54+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minFullDataviewSupport),
55+
ConfigDirectory: acctest.NamedTestCaseDirectory("basic_updated"),
56+
ConfigVariables: config.Variables{
57+
"index_name": config.StringVariable(indexName),
58+
},
4859
Check: resource.ComposeTestCheckFunc(
4960
resource.TestCheckResourceAttrSet("elasticstack_kibana_data_view.dv", "id"),
5061
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.dv", "override", "false"),
@@ -55,8 +66,12 @@ func TestAccResourceDataView(t *testing.T) {
5566
),
5667
},
5768
{
58-
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minFullDataviewSupport),
59-
Config: testAccResourceDataViewBasicDVUpdated(indexName),
69+
ProtoV6ProviderFactories: acctest.Providers,
70+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minFullDataviewSupport),
71+
ConfigDirectory: acctest.NamedTestCaseDirectory("basic_updated"),
72+
ConfigVariables: config.Variables{
73+
"index_name": config.StringVariable(indexName),
74+
},
6075
ImportState: true,
6176
ImportStateVerify: true,
6277
ResourceName: "elasticstack_kibana_data_view.dv",
@@ -65,89 +80,47 @@ func TestAccResourceDataView(t *testing.T) {
6580
})
6681
}
6782

68-
func testAccResourceDataViewPre8_8DV(indexName string) string {
69-
return fmt.Sprintf(`
70-
provider "elasticstack" {
71-
elasticsearch {}
72-
kibana {}
73-
}
74-
75-
resource "elasticstack_elasticsearch_index" "my_index" {
76-
name = "%s"
77-
deletion_protection = false
78-
}
79-
80-
resource "elasticstack_kibana_data_view" "dv" {
81-
data_view = {
82-
title = "%s*"
83-
}
84-
}`, indexName, indexName)
85-
}
86-
87-
func testAccResourceDataViewBasicDV(indexName string) string {
88-
return fmt.Sprintf(`
89-
provider "elasticstack" {
90-
elasticsearch {}
91-
kibana {}
92-
}
93-
94-
resource "elasticstack_elasticsearch_index" "my_index" {
95-
name = "%s"
96-
deletion_protection = false
97-
}
98-
99-
resource "elasticstack_kibana_data_view" "dv" {
100-
override = true
101-
data_view = {
102-
title = "%s*"
103-
name = "%s"
104-
time_field_name = "@timestamp"
105-
source_filters = ["event_time", "machine.ram"]
106-
allow_no_index = true
107-
namespaces = ["default", "foo", "bar"]
108-
field_formats = {
109-
event_time = {
110-
id = "date_nanos"
111-
}
112-
"machine.ram" = {
113-
id = "number"
114-
params = {
115-
pattern = "0,0.[000] b"
116-
}
117-
}
118-
}
119-
runtime_field_map = {
120-
runtime_shape_name = {
121-
type = "keyword"
122-
script_source = "emit(doc['shape_name'].value)"
123-
}
124-
}
125-
field_attrs = {
126-
ingest_failure = { custom_label = "error.ingest_failure", count = 6 },
127-
}
128-
}
129-
}`, indexName, indexName, indexName)
130-
}
131-
132-
func testAccResourceDataViewBasicDVUpdated(indexName string) string {
133-
return fmt.Sprintf(`
134-
provider "elasticstack" {
135-
elasticsearch {}
136-
kibana {}
137-
}
83+
func TestAccResourceDataViewColorFieldFormat(t *testing.T) {
84+
indexName := "my-color-index-" + sdkacctest.RandStringFromCharSet(4, sdkacctest.CharSetAlphaNum)
13885

139-
resource "elasticstack_elasticsearch_index" "my_index" {
140-
name = "%s"
141-
deletion_protection = false
142-
}
143-
144-
resource "elasticstack_kibana_data_view" "dv" {
145-
override = false
146-
data_view = {
147-
title = "%s*"
148-
name = "%s"
149-
time_field_name = "@timestamp"
150-
allow_no_index = true
151-
}
152-
}`, indexName, indexName, indexName)
86+
resource.Test(t, resource.TestCase{
87+
PreCheck: func() { acctest.PreCheck(t) },
88+
Steps: []resource.TestStep{
89+
{
90+
ProtoV6ProviderFactories: acctest.Providers,
91+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minFullDataviewSupport),
92+
ConfigDirectory: acctest.NamedTestCaseDirectory("create"),
93+
ConfigVariables: config.Variables{
94+
"index_name": config.StringVariable(indexName),
95+
},
96+
Check: resource.ComposeTestCheckFunc(
97+
resource.TestCheckResourceAttrSet("elasticstack_kibana_data_view.color_dv", "id"),
98+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.color_dv", "data_view.field_formats.status.id", "color"),
99+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.color_dv", "data_view.field_formats.status.params.field_type", "string"),
100+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.color_dv", "data_view.field_formats.status.params.colors.#", "2"),
101+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.color_dv", "data_view.field_formats.status.params.colors.0.range", "-Infinity:Infinity"),
102+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.color_dv", "data_view.field_formats.status.params.colors.0.regex", "Completed"),
103+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.color_dv", "data_view.field_formats.status.params.colors.0.text", "#000000"),
104+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.color_dv", "data_view.field_formats.status.params.colors.0.background", "#54B399"),
105+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.color_dv", "data_view.field_formats.status.params.colors.1.regex", "Error"),
106+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.color_dv", "data_view.field_formats.status.params.colors.1.text", "#FFFFFF"),
107+
resource.TestCheckResourceAttr("elasticstack_kibana_data_view.color_dv", "data_view.field_formats.status.params.colors.1.background", "#BD271E"),
108+
),
109+
},
110+
{
111+
ProtoV6ProviderFactories: acctest.Providers,
112+
SkipFunc: versionutils.CheckIfVersionIsUnsupported(minFullDataviewSupport),
113+
ConfigDirectory: acctest.NamedTestCaseDirectory("import"),
114+
ConfigVariables: config.Variables{
115+
"index_name": config.StringVariable(indexName),
116+
},
117+
ImportState: true,
118+
ImportStateVerifyIgnore: []string{
119+
"override",
120+
},
121+
ImportStateVerify: true,
122+
ResourceName: "elasticstack_kibana_data_view.color_dv",
123+
},
124+
},
125+
})
153126
}

internal/kibana/data_view/schema.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func getSchema() schema.Schema {
134134
},
135135
"params": schema.SingleNestedAttribute{
136136
Optional: true,
137+
Computed: true,
137138
Attributes: map[string]schema.Attribute{
138139
"pattern": schema.StringAttribute{
139140
Optional: true,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
variable "index_name" {
2+
description = "The index name"
3+
type = string
4+
}
5+
6+
provider "elasticstack" {
7+
elasticsearch {}
8+
kibana {}
9+
}
10+
11+
resource "elasticstack_elasticsearch_index" "my_index" {
12+
name = var.index_name
13+
deletion_protection = false
14+
}
15+
16+
resource "elasticstack_kibana_data_view" "dv" {
17+
override = true
18+
data_view = {
19+
title = "${var.index_name}*"
20+
name = var.index_name
21+
time_field_name = "@timestamp"
22+
source_filters = ["event_time", "machine.ram"]
23+
allow_no_index = true
24+
namespaces = ["default", "foo", "bar"]
25+
field_formats = {
26+
event_time = {
27+
id = "date_nanos"
28+
}
29+
"machine.ram" = {
30+
id = "number"
31+
params = {
32+
pattern = "0,0.[000] b"
33+
}
34+
}
35+
}
36+
runtime_field_map = {
37+
runtime_shape_name = {
38+
type = "keyword"
39+
script_source = "emit(doc['shape_name'].value)"
40+
}
41+
}
42+
field_attrs = {
43+
ingest_failure = { custom_label = "error.ingest_failure", count = 6 }
44+
}
45+
}
46+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
variable "index_name" {
2+
description = "The index name"
3+
type = string
4+
}
5+
6+
provider "elasticstack" {
7+
elasticsearch {}
8+
kibana {}
9+
}
10+
11+
resource "elasticstack_elasticsearch_index" "my_index" {
12+
name = var.index_name
13+
deletion_protection = false
14+
}
15+
16+
resource "elasticstack_kibana_data_view" "dv" {
17+
override = false
18+
data_view = {
19+
title = "${var.index_name}*"
20+
name = var.index_name
21+
time_field_name = "@timestamp"
22+
allow_no_index = true
23+
}
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
variable "index_name" {
2+
description = "The index name"
3+
type = string
4+
}
5+
6+
provider "elasticstack" {
7+
elasticsearch {}
8+
kibana {}
9+
}
10+
11+
resource "elasticstack_elasticsearch_index" "my_index" {
12+
name = var.index_name
13+
deletion_protection = false
14+
}
15+
16+
resource "elasticstack_kibana_data_view" "dv" {
17+
data_view = {
18+
title = "${var.index_name}*"
19+
}
20+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
variable "index_name" {
2+
description = "The index name"
3+
type = string
4+
}
5+
6+
provider "elasticstack" {
7+
elasticsearch {}
8+
kibana {}
9+
}
10+
11+
resource "elasticstack_elasticsearch_index" "my_color_index" {
12+
name = var.index_name
13+
deletion_protection = false
14+
}
15+
16+
resource "elasticstack_kibana_data_view" "color_dv" {
17+
override = true
18+
data_view = {
19+
title = "${var.index_name}*"
20+
name = var.index_name
21+
time_field_name = "@timestamp"
22+
allow_no_index = true
23+
field_formats = {
24+
status = {
25+
id = "color"
26+
params = {
27+
field_type = "string"
28+
colors = [
29+
{
30+
range = "-Infinity:Infinity"
31+
regex = "Completed"
32+
text = "#000000"
33+
background = "#54B399"
34+
},
35+
{
36+
range = "-Infinity:Infinity"
37+
regex = "Error"
38+
text = "#FFFFFF"
39+
background = "#BD271E"
40+
}
41+
]
42+
}
43+
}
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)