diff --git a/CHANGELOG.md b/CHANGELOG.md index e5043f8b3..ddac5b7bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/resources/elasticsearch_index.md b/docs/resources/elasticsearch_index.md index 5da816ed9..f15b057fd 100644 --- a/docs/resources/elasticsearch_index.md +++ b/docs/resources/elasticsearch_index.md @@ -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 / ``` diff --git a/examples/resources/elasticstack_elasticsearch_index/import.sh b/examples/resources/elasticstack_elasticsearch_index/import.sh index 827b254d0..2d42c47c0 100644 --- a/examples/resources/elasticstack_elasticsearch_index/import.sh +++ b/examples/resources/elasticstack_elasticsearch_index/import.sh @@ -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 / diff --git a/internal/elasticsearch/index/index.go b/internal/elasticsearch/index/index.go index f110610b4..583be2b1f 100644 --- a/internal/elasticsearch/index/index.go +++ b/internal/elasticsearch/index/index.go @@ -3,6 +3,7 @@ package index import ( "context" "encoding/json" + "fmt" "log" "reflect" "regexp" @@ -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 { @@ -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) } diff --git a/templates/resources/elasticsearch_index.md.tmpl b/templates/resources/elasticsearch_index.md.tmpl index c612e1322..c82cb37cc 100644 --- a/templates/resources/elasticsearch_index.md.tmpl +++ b/templates/resources/elasticsearch_index.md.tmpl @@ -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" }}