Skip to content

Commit 5c90036

Browse files
Copilottobio
andcommitted
Complete alias resource implementation with linting and documentation
Co-authored-by: tobio <444668+tobio@users.noreply.github.com>
1 parent 8bb4289 commit 5c90036

File tree

10 files changed

+56
-176
lines changed

10 files changed

+56
-176
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
---
3+
# generated by https://github.com/hashicorp/terraform-plugin-docs
4+
page_title: "elasticstack_elasticsearch_alias Resource - terraform-provider-elasticstack"
5+
subcategory: "Elasticsearch"
6+
description: |-
7+
Manages an Elasticsearch alias. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html
8+
---
9+
10+
# elasticstack_elasticsearch_alias (Resource)
11+
12+
Manages an Elasticsearch alias. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html
13+
14+
15+
16+
<!-- schema generated by tfplugindocs -->
17+
## Schema
18+
19+
### Required
20+
21+
- `indices` (Set of String) A set of indices to which the alias should point.
22+
- `name` (String) The alias name.
23+
24+
### Optional
25+
26+
- `filter` (String) Query used to limit documents the alias can access.
27+
- `index_routing` (String) Value used to route indexing operations to a specific shard. If specified, this overwrites the `routing` value for indexing operations.
28+
- `is_hidden` (Boolean) If true, the alias is hidden.
29+
- `is_write_index` (Boolean) If true, the index is the write index for the alias.
30+
- `routing` (String) Value used to route indexing and search operations to a specific shard.
31+
- `search_routing` (String) Value used to route search operations to a specific shard. If specified, this overwrites the routing value for search operations.
32+
33+
### Read-Only
34+
35+
- `id` (String) Generated ID of the alias resource.

internal/clients/elasticsearch/index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ func PutAlias(ctx context.Context, apiClient *clients.ApiClient, aliasName strin
640640

641641
// Only include non-empty optional fields in the add action
642642
addActionDetails := addAction["add"].(map[string]interface{})
643-
643+
644644
if alias.Filter != nil {
645645
addActionDetails["filter"] = alias.Filter
646646
}

internal/elasticsearch/index/alias/acc_test.go

Lines changed: 4 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestAccResourceAlias(t *testing.T) {
1919
indexName2 := sdkacctest.RandStringFromCharSet(22, sdkacctest.CharSetAlpha)
2020

2121
resource.Test(t, resource.TestCase{
22-
PreCheck: func() {
22+
PreCheck: func() {
2323
acctest.PreCheck(t)
2424
// Create indices directly via curl to avoid terraform index resource conflicts
2525
createTestIndex(t, indexName)
@@ -154,114 +154,6 @@ resource "elasticstack_elasticsearch_alias" "test_alias" {
154154
`, aliasName, indexName)
155155
}
156156

157-
func testAccResourceAliasCreate(aliasName, indexName string) string {
158-
return fmt.Sprintf(`
159-
provider "elasticstack" {
160-
elasticsearch {}
161-
}
162-
163-
# Create index with mappings to avoid alias management
164-
resource "elasticstack_elasticsearch_index" "test_index" {
165-
name = "%s"
166-
deletion_protection = false
167-
168-
mappings = jsonencode({
169-
properties = {
170-
title = {
171-
type = "text"
172-
}
173-
}
174-
})
175-
}
176-
177-
resource "elasticstack_elasticsearch_alias" "test_alias" {
178-
name = "%s"
179-
indices = [elasticstack_elasticsearch_index.test_index.name]
180-
181-
depends_on = [elasticstack_elasticsearch_index.test_index]
182-
}
183-
`, indexName, aliasName)
184-
}
185-
186-
func testAccResourceAliasUpdate(aliasName, indexName, indexName2 string) string {
187-
return fmt.Sprintf(`
188-
provider "elasticstack" {
189-
elasticsearch {}
190-
}
191-
192-
resource "elasticstack_elasticsearch_index" "test_index" {
193-
name = "%s"
194-
deletion_protection = false
195-
196-
mappings = jsonencode({
197-
properties = {
198-
title = {
199-
type = "text"
200-
}
201-
}
202-
})
203-
}
204-
205-
resource "elasticstack_elasticsearch_index" "test_index2" {
206-
name = "%s"
207-
deletion_protection = false
208-
209-
mappings = jsonencode({
210-
properties = {
211-
title = {
212-
type = "text"
213-
}
214-
}
215-
})
216-
}
217-
218-
resource "elasticstack_elasticsearch_alias" "test_alias" {
219-
name = "%s"
220-
indices = [elasticstack_elasticsearch_index.test_index.name, elasticstack_elasticsearch_index.test_index2.name]
221-
is_write_index = true
222-
routing = "test-routing"
223-
224-
depends_on = [elasticstack_elasticsearch_index.test_index, elasticstack_elasticsearch_index.test_index2]
225-
}
226-
`, indexName, indexName2, aliasName)
227-
}
228-
229-
func testAccResourceAliasWithFilter(aliasName, indexName string) string {
230-
return fmt.Sprintf(`
231-
provider "elasticstack" {
232-
elasticsearch {}
233-
}
234-
235-
resource "elasticstack_elasticsearch_index" "test_index" {
236-
name = "%s"
237-
deletion_protection = false
238-
239-
mappings = jsonencode({
240-
properties = {
241-
title = {
242-
type = "text"
243-
}
244-
status = {
245-
type = "keyword"
246-
}
247-
}
248-
})
249-
}
250-
251-
resource "elasticstack_elasticsearch_alias" "test_alias" {
252-
name = "%s"
253-
indices = [elasticstack_elasticsearch_index.test_index.name]
254-
filter = jsonencode({
255-
term = {
256-
status = "published"
257-
}
258-
})
259-
260-
depends_on = [elasticstack_elasticsearch_index.test_index]
261-
}
262-
`, indexName, aliasName)
263-
}
264-
265157
func testAccResourceAliasDataStreamCreate(aliasName, dsName string) string {
266158
return fmt.Sprintf(`
267159
provider "elasticstack" {
@@ -298,7 +190,7 @@ func checkResourceAliasDestroy(s *terraform.State) error {
298190
if rs.Type != "elasticstack_elasticsearch_alias" {
299191
continue
300192
}
301-
193+
302194
// Handle the case where ID might not be in the expected format
303195
aliasName := rs.Primary.ID
304196
if compId, err := clients.CompositeIdFromStr(rs.Primary.ID); err == nil {
@@ -309,7 +201,7 @@ func checkResourceAliasDestroy(s *terraform.State) error {
309201
if err != nil {
310202
return err
311203
}
312-
204+
313205
res, err := esClient.Indices.GetAlias(
314206
esClient.Indices.GetAlias.WithName(aliasName),
315207
)
@@ -323,4 +215,4 @@ func checkResourceAliasDestroy(s *terraform.State) error {
323215
}
324216
}
325217
return nil
326-
}
218+
}

internal/elasticsearch/index/alias/create.go

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -42,60 +42,13 @@ func (r *aliasResource) Create(ctx context.Context, req resource.CreateRequest,
4242
resp.Diagnostics.Append(resp.State.Set(ctx, finalModel)...)
4343
}
4444

45-
func readAlias(ctx context.Context, client *clients.ApiClient, aliasName string) (*tfModel, diag.Diagnostics) {
46-
indices, diags := elasticsearch.GetAlias(ctx, client, aliasName)
47-
if diags.HasError() {
48-
return nil, diags
49-
}
50-
51-
if indices == nil || len(indices) == 0 {
52-
return nil, diag.Diagnostics{
53-
diag.NewErrorDiagnostic(
54-
"Alias not found after creation",
55-
"The alias was not found after creation, which indicates an error in the Elasticsearch API response.",
56-
),
57-
}
58-
}
59-
60-
// Extract indices and alias data from the response
61-
var indexNames []string
62-
var aliasData *models.IndexAlias
63-
64-
for indexName, index := range indices {
65-
if alias, exists := index.Aliases[aliasName]; exists {
66-
indexNames = append(indexNames, indexName)
67-
if aliasData == nil {
68-
// Use the first alias definition we find (they should all be the same)
69-
aliasData = &alias
70-
}
71-
}
72-
}
73-
74-
if aliasData == nil {
75-
return nil, diag.Diagnostics{
76-
diag.NewErrorDiagnostic(
77-
"Alias data not found after creation",
78-
"The alias data was not found after creation, which indicates an error in the Elasticsearch API response.",
79-
),
80-
}
81-
}
82-
83-
finalModel := &tfModel{}
84-
diags = finalModel.populateFromAPI(ctx, aliasName, *aliasData, indexNames)
85-
if diags.HasError() {
86-
return nil, diags
87-
}
88-
89-
return finalModel, nil
90-
}
91-
9245
func readAliasWithPlan(ctx context.Context, client *clients.ApiClient, aliasName string, planModel *tfModel) (*tfModel, diag.Diagnostics) {
9346
indices, diags := elasticsearch.GetAlias(ctx, client, aliasName)
9447
if diags.HasError() {
9548
return nil, diags
9649
}
9750

98-
if indices == nil || len(indices) == 0 {
51+
if len(indices) == 0 {
9952
return nil, diag.Diagnostics{
10053
diag.NewErrorDiagnostic(
10154
"Alias not found after creation",
@@ -107,7 +60,7 @@ func readAliasWithPlan(ctx context.Context, client *clients.ApiClient, aliasName
10760
// Extract indices and alias data from the response
10861
var indexNames []string
10962
var aliasData *models.IndexAlias
110-
63+
11164
for indexName, index := range indices {
11265
if alias, exists := index.Aliases[aliasName]; exists {
11366
indexNames = append(indexNames, indexName)
@@ -134,4 +87,4 @@ func readAliasWithPlan(ctx context.Context, client *clients.ApiClient, aliasName
13487
}
13588

13689
return finalModel, nil
137-
}
90+
}

internal/elasticsearch/index/alias/delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func (r *aliasResource) Delete(ctx context.Context, req resource.DeleteRequest,
1616
}
1717

1818
aliasName := stateModel.Name.ValueString()
19-
19+
2020
// Get the current indices from state
2121
var indices []string
2222
resp.Diagnostics.Append(stateModel.Indices.ElementsAs(ctx, &indices, false)...)
@@ -29,4 +29,4 @@ func (r *aliasResource) Delete(ctx context.Context, req resource.DeleteRequest,
2929
if resp.Diagnostics.HasError() {
3030
return
3131
}
32-
}
32+
}

internal/elasticsearch/index/alias/models.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ func (model *tfModel) populateFromAPI(ctx context.Context, aliasName string, ali
3939
} else {
4040
model.IndexRouting = types.StringNull()
4141
}
42-
42+
4343
model.IsHidden = types.BoolValue(aliasData.IsHidden)
4444
model.IsWriteIndex = types.BoolValue(aliasData.IsWriteIndex)
45-
45+
4646
if aliasData.Routing != "" {
4747
model.Routing = types.StringValue(aliasData.Routing)
4848
} else {
4949
model.Routing = types.StringNull()
5050
}
51-
51+
5252
if aliasData.SearchRouting != "" {
5353
model.SearchRouting = types.StringValue(aliasData.SearchRouting)
5454
} else {
@@ -93,4 +93,4 @@ func (model *tfModel) toAPIModel() (models.IndexAlias, []string, diag.Diagnostic
9393
}
9494

9595
return apiModel, indices, nil
96-
}
96+
}

internal/elasticsearch/index/alias/read.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ func (r *aliasResource) Read(ctx context.Context, req resource.ReadRequest, resp
2626
}
2727

2828
// If no indices returned, the alias doesn't exist
29-
if indices == nil || len(indices) == 0 {
29+
if len(indices) == 0 {
3030
resp.State.RemoveResource(ctx)
3131
return
3232
}
3333

3434
// Extract indices and alias data from the response
3535
var indexNames []string
3636
var aliasData *models.IndexAlias
37-
37+
3838
for indexName, index := range indices {
3939
if alias, exists := index.Aliases[aliasName]; exists {
4040
indexNames = append(indexNames, indexName)
@@ -57,4 +57,4 @@ func (r *aliasResource) Read(ctx context.Context, req resource.ReadRequest, resp
5757
}
5858

5959
resp.Diagnostics.Append(resp.State.Set(ctx, stateModel)...)
60-
}
60+
}

internal/elasticsearch/index/alias/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ func (r *aliasResource) Configure(_ context.Context, req resource.ConfigureReque
2323
client, diags := clients.ConvertProviderData(req.ProviderData)
2424
resp.Diagnostics.Append(diags...)
2525
r.client = client
26-
}
26+
}

internal/elasticsearch/index/alias/schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,4 @@ func (r *aliasResource) Schema(ctx context.Context, req resource.SchemaRequest,
7272
},
7373
},
7474
}
75-
}
75+
}

internal/elasticsearch/index/alias/update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (r *aliasResource) Update(ctx context.Context, req resource.UpdateRequest,
2222
}
2323

2424
aliasName := planModel.Name.ValueString()
25-
25+
2626
// Get the current indices from state for removal
2727
var currentIndices []string
2828
resp.Diagnostics.Append(stateModel.Indices.ElementsAs(ctx, &currentIndices, false)...)
@@ -59,4 +59,4 @@ func (r *aliasResource) Update(ctx context.Context, req resource.UpdateRequest,
5959
}
6060

6161
resp.Diagnostics.Append(resp.State.Set(ctx, finalModel)...)
62-
}
62+
}

0 commit comments

Comments
 (0)