Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support import of the index settings #55

Merged
merged 5 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed
- Update only changed index settings ([#52](https://github.com/elastic/terraform-provider-elasticstack/issues/52))
- Enable import of index settings ([#53](https://github.com/elastic/terraform-provider-elasticstack/issues/53))

## [0.2.0] - 2022-01-27
### Added
Expand Down
7 changes: 7 additions & 0 deletions docs/resources/elasticsearch_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,15 @@ Required:

## Import

**NOTE:** While importing index resource, keep in mind, that some of the default index settings will be imported into the TF state too.
You can later adjust the index configuration to account for those imported settings.

Some of the default settings, which could be imported are: `index.number_of_replicas`, `index.number_of_shards` and `index.routing.allocation.include._tier_preference`.

Import is supported using the following syntax:

```shell
# NOTE: while importing index resource, keep in mind, that some of the default index settings will be imported into the TF state too
# You can later adjust the index configuration to account for those imported settings
terraform import elasticstack_elasticsearch_index.my_index <cluster_uuid>/<index_name>
```
2 changes: 2 additions & 0 deletions examples/resources/elasticstack_elasticsearch_index/import.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# NOTE: while importing index resource, keep in mind, that some of the default index settings will be imported into the TF state too
# You can later adjust the index configuration to account for those imported settings
terraform import elasticstack_elasticsearch_index.my_index <cluster_uuid>/<index_name>

54 changes: 51 additions & 3 deletions internal/elasticsearch/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package index
import (
"context"
"encoding/json"
"fmt"
"log"
"reflect"
"regexp"
Expand Down Expand Up @@ -147,7 +148,55 @@ If specified, this mapping can include: field names, field data types (https://w
DeleteContext: resourceIndexDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: func(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
// default settings populated by Elasticsearch, which we do not support and should ignore
var ignoredDefaults = map[string]struct{}{
"index.creation_date": struct{}{},
"index.provided_name": struct{}{},
"index.uuid": struct{}{},
"index.version.created": struct{}{},
}

// first populate what we can with Read
diags := resourceIndexRead(ctx, d, m)
if diags.HasError() {
return nil, fmt.Errorf("Unable to import requested index")
}

client, err := clients.NewApiClient(d, m)
if err != nil {
return nil, err
}
compId, diags := clients.CompositeIdFromStr(d.Id())
if diags.HasError() {
return nil, fmt.Errorf("Failed to parse provided ID")
}
indexName := compId.ResourceId
index, diags := client.GetElasticsearchIndex(indexName)
if diags.HasError() {
return nil, fmt.Errorf("Failed to get an ES Index")
}
// check the settings and import those as well
if index.Settings != nil {
settings := make(map[string]interface{})
result := make([]interface{}, 0)
for k, v := range index.Settings {
if _, ok := ignoredDefaults[k]; ok {
continue
}
setting := make(map[string]interface{})
setting["name"] = k
setting["value"] = v
result = append(result, setting)
}
settings["setting"] = result

if err := d.Set("settings", []interface{}{settings}); err != nil {
return nil, err
}
}
return []*schema.ResourceData{d}, nil
},
},

CustomizeDiff: customdiff.ForceNewIfChange("mappings", func(ctx context.Context, old, new, meta interface{}) bool {
Expand Down Expand Up @@ -402,8 +451,7 @@ func resourceIndexRead(ctx context.Context, d *schema.ResourceData, meta interfa
}
}
if index.Settings != nil {
// normalize settings before saving raw settings
s, err := json.Marshal(utils.NormalizeIndexSettings(index.Settings))
s, err := json.Marshal(index.Settings)
if err != nil {
return diag.FromErr(err)
}
Expand Down
5 changes: 5 additions & 0 deletions templates/resources/elasticsearch_index.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ Creates or updates an index. This resource can define settings, mappings and ali

## Import

**NOTE:** While importing index resource, keep in mind, that some of the default index settings will be imported into the TF state too.
You can later adjust the index configuration to account for those imported settings.

Some of the default settings, which could be imported are: `index.number_of_replicas`, `index.number_of_shards` and `index.routing.allocation.include._tier_preference`.

Import is supported using the following syntax:

{{ codefile "shell" "examples/resources/elasticstack_elasticsearch_index/import.sh" }}