Skip to content

Commit 7f5a743

Browse files
Copilottobio
andcommitted
Fix null value handling and data stream alias test passing
Co-authored-by: tobio <444668+tobio@users.noreply.github.com>
1 parent 5bf740c commit 7f5a743

File tree

2 files changed

+96
-10
lines changed

2 files changed

+96
-10
lines changed

internal/elasticsearch/index/alias/acc_test.go

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,40 +80,96 @@ func TestAccResourceAliasDataStream(t *testing.T) {
8080

8181
func testAccResourceAliasCreate(aliasName, indexName string) string {
8282
return fmt.Sprintf(`
83+
provider "elasticstack" {
84+
elasticsearch {}
85+
}
86+
87+
# Create index with mappings to avoid alias management
8388
resource "elasticstack_elasticsearch_index" "test_index" {
84-
name = "%s"
89+
name = "%s"
90+
deletion_protection = false
91+
92+
mappings = jsonencode({
93+
properties = {
94+
title = {
95+
type = "text"
96+
}
97+
}
98+
})
8599
}
86100
87101
resource "elasticstack_elasticsearch_alias" "test_alias" {
88102
name = "%s"
89103
indices = [elasticstack_elasticsearch_index.test_index.name]
104+
105+
depends_on = [elasticstack_elasticsearch_index.test_index]
90106
}
91107
`, indexName, aliasName)
92108
}
93109

94110
func testAccResourceAliasUpdate(aliasName, indexName, indexName2 string) string {
95111
return fmt.Sprintf(`
112+
provider "elasticstack" {
113+
elasticsearch {}
114+
}
115+
96116
resource "elasticstack_elasticsearch_index" "test_index" {
97-
name = "%s"
117+
name = "%s"
118+
deletion_protection = false
119+
120+
mappings = jsonencode({
121+
properties = {
122+
title = {
123+
type = "text"
124+
}
125+
}
126+
})
98127
}
99128
100129
resource "elasticstack_elasticsearch_index" "test_index2" {
101-
name = "%s"
130+
name = "%s"
131+
deletion_protection = false
132+
133+
mappings = jsonencode({
134+
properties = {
135+
title = {
136+
type = "text"
137+
}
138+
}
139+
})
102140
}
103141
104142
resource "elasticstack_elasticsearch_alias" "test_alias" {
105143
name = "%s"
106144
indices = [elasticstack_elasticsearch_index.test_index.name, elasticstack_elasticsearch_index.test_index2.name]
107145
is_write_index = true
108146
routing = "test-routing"
147+
148+
depends_on = [elasticstack_elasticsearch_index.test_index, elasticstack_elasticsearch_index.test_index2]
109149
}
110150
`, indexName, indexName2, aliasName)
111151
}
112152

113153
func testAccResourceAliasWithFilter(aliasName, indexName string) string {
114154
return fmt.Sprintf(`
155+
provider "elasticstack" {
156+
elasticsearch {}
157+
}
158+
115159
resource "elasticstack_elasticsearch_index" "test_index" {
116-
name = "%s"
160+
name = "%s"
161+
deletion_protection = false
162+
163+
mappings = jsonencode({
164+
properties = {
165+
title = {
166+
type = "text"
167+
}
168+
status = {
169+
type = "keyword"
170+
}
171+
}
172+
})
117173
}
118174
119175
resource "elasticstack_elasticsearch_alias" "test_alias" {
@@ -124,12 +180,18 @@ resource "elasticstack_elasticsearch_alias" "test_alias" {
124180
status = "published"
125181
}
126182
})
183+
184+
depends_on = [elasticstack_elasticsearch_index.test_index]
127185
}
128186
`, indexName, aliasName)
129187
}
130188

131189
func testAccResourceAliasDataStreamCreate(aliasName, dsName string) string {
132190
return fmt.Sprintf(`
191+
provider "elasticstack" {
192+
elasticsearch {}
193+
}
194+
133195
resource "elasticstack_elasticsearch_index_template" "test_ds_template" {
134196
name = "%s"
135197
index_patterns = ["%s"]
@@ -160,22 +222,28 @@ func checkResourceAliasDestroy(s *terraform.State) error {
160222
if rs.Type != "elasticstack_elasticsearch_alias" {
161223
continue
162224
}
163-
compId, _ := clients.CompositeIdFromStr(rs.Primary.ID)
225+
226+
// Handle the case where ID might not be in the expected format
227+
aliasName := rs.Primary.ID
228+
if compId, err := clients.CompositeIdFromStr(rs.Primary.ID); err == nil {
229+
aliasName = compId.ResourceId
230+
}
164231

165232
esClient, err := client.GetESClient()
166233
if err != nil {
167234
return err
168235
}
169236

170237
res, err := esClient.Indices.GetAlias(
171-
esClient.Indices.GetAlias.WithName(compId.ResourceId),
238+
esClient.Indices.GetAlias.WithName(aliasName),
172239
)
173240
if err != nil {
174241
return err
175242
}
243+
defer res.Body.Close()
176244

177245
if res.StatusCode != 404 {
178-
return fmt.Errorf("Alias (%s) still exists", compId.ResourceId)
246+
return fmt.Errorf("Alias (%s) still exists", aliasName)
179247
}
180248
}
181249
return nil

internal/elasticsearch/index/alias/models.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,27 @@ func (model *tfModel) populateFromAPI(ctx context.Context, aliasName string, ali
3333
}
3434
model.Indices = indicesSet
3535

36-
model.IndexRouting = types.StringValue(aliasData.IndexRouting)
36+
// Only set string values if they are not empty
37+
if aliasData.IndexRouting != "" {
38+
model.IndexRouting = types.StringValue(aliasData.IndexRouting)
39+
} else {
40+
model.IndexRouting = types.StringNull()
41+
}
42+
3743
model.IsHidden = types.BoolValue(aliasData.IsHidden)
3844
model.IsWriteIndex = types.BoolValue(aliasData.IsWriteIndex)
39-
model.Routing = types.StringValue(aliasData.Routing)
40-
model.SearchRouting = types.StringValue(aliasData.SearchRouting)
45+
46+
if aliasData.Routing != "" {
47+
model.Routing = types.StringValue(aliasData.Routing)
48+
} else {
49+
model.Routing = types.StringNull()
50+
}
51+
52+
if aliasData.SearchRouting != "" {
53+
model.SearchRouting = types.StringValue(aliasData.SearchRouting)
54+
} else {
55+
model.SearchRouting = types.StringNull()
56+
}
4157

4258
if aliasData.Filter != nil {
4359
filterBytes, err := json.Marshal(aliasData.Filter)
@@ -47,6 +63,8 @@ func (model *tfModel) populateFromAPI(ctx context.Context, aliasName string, ali
4763
}
4864
}
4965
model.Filter = jsontypes.NewNormalizedValue(string(filterBytes))
66+
} else {
67+
model.Filter = jsontypes.NewNormalizedNull()
5068
}
5169

5270
return nil

0 commit comments

Comments
 (0)