From b8eda2d5f104232772c48d0f8feeb5e5b9516e67 Mon Sep 17 00:00:00 2001 From: Victor Cabezas Date: Wed, 9 Dec 2020 15:49:47 +0100 Subject: [PATCH 01/12] Add resource_elasticsearch_template_index.go and update ES7 client to latest version --- .travis.yml | 2 +- es/provider.go | 1 + es/resource_elasticsearch_template_index.go | 150 ++++++++++++++++++++ es/util.go | 9 +- go.mod | 2 +- go.sum | 12 ++ 6 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 es/resource_elasticsearch_template_index.go diff --git a/.travis.yml b/.travis.yml index 8faf612f..71a01e29 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ env: jobs: - ES_VERSION=5.6.16 ES_OSS_IMAGE=elasticsearch:${ES_VERSION} ES_IMAGE=docker.elastic.co/elasticsearch/elasticsearch:${ES_VERSION} ES_COMMAND="elasticsearch -Epath.repo=/tmp" - ES_VERSION=6.8.9 ES_OSS_IMAGE=docker.elastic.co/elasticsearch/elasticsearch-oss:${ES_VERSION} ES_IMAGE=docker.elastic.co/elasticsearch/elasticsearch:${ES_VERSION} ES_OPENDISTRO_IMAGE=amazon/opendistro-for-elasticsearch:0.9.0 - - ES_VERSION=7.6.1 ES_OSS_IMAGE=docker.elastic.co/elasticsearch/elasticsearch-oss:${ES_VERSION} ES_IMAGE=docker.elastic.co/elasticsearch/elasticsearch:${ES_VERSION} ES_OPENDISTRO_IMAGE=amazon/opendistro-for-elasticsearch:1.6.0 + - ES_VERSION=7.9.3 ES_OSS_IMAGE=docker.elastic.co/elasticsearch/elasticsearch-oss:${ES_VERSION} ES_IMAGE=docker.elastic.co/elasticsearch/elasticsearch:${ES_VERSION} ES_OPENDISTRO_IMAGE=amazon/opendistro-for-elasticsearch:1.6.0 addons: ssh_known_hosts: github.com apt: diff --git a/es/provider.go b/es/provider.go index c321a57b..fc8e350b 100644 --- a/es/provider.go +++ b/es/provider.go @@ -147,6 +147,7 @@ func Provider() terraform.ResourceProvider { "elasticsearch_index": resourceElasticsearchIndex(), "elasticsearch_index_lifecycle_policy": resourceElasticsearchDeprecatedIndexLifecyclePolicy(), "elasticsearch_index_template": resourceElasticsearchIndexTemplate(), + "elasticsearch_template_index": resourceElasticsearchTemplateIndex(), "elasticsearch_ingest_pipeline": resourceElasticsearchIngestPipeline(), "elasticsearch_kibana_object": resourceElasticsearchKibanaObject(), "elasticsearch_monitor": resourceElasticsearchDeprecatedMonitor(), diff --git a/es/resource_elasticsearch_template_index.go b/es/resource_elasticsearch_template_index.go new file mode 100644 index 00000000..f30b9b02 --- /dev/null +++ b/es/resource_elasticsearch_template_index.go @@ -0,0 +1,150 @@ +package es + +import ( + "context" + "encoding/json" + "errors" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" + elastic7 "github.com/olivere/elastic/v7" + elastic5 "gopkg.in/olivere/elastic.v5" + elastic6 "gopkg.in/olivere/elastic.v6" +) + +func resourceElasticsearchTemplateIndex() *schema.Resource { + return &schema.Resource{ + Create: resourceElasticsearchTemplateIndexCreate, + Read: resourceElasticsearchTemplateIndexRead, + Update: resourceElasticsearchTemplateIndexUpdate, + Delete: resourceElasticsearchTemplateIndexDelete, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + ForceNew: true, + Required: true, + }, + "body": { + Type: schema.TypeString, + Required: true, + DiffSuppressFunc: diffSuppressIndexTemplate, + ValidateFunc: validation.StringIsJSON, + }, + }, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + } +} + +func resourceElasticsearchTemplateIndexCreate(d *schema.ResourceData, meta interface{}) error { + err := resourceElasticsearchPutTemplateIndex(d, meta, true) + if err != nil { + return err + } + d.SetId(d.Get("name").(string)) + return nil +} + +func resourceElasticsearchTemplateIndexRead(d *schema.ResourceData, meta interface{}) error { + id := d.Id() + + var result string + var err error + switch client := meta.(type) { + case *elastic7.Client: + version, err := elastic7GetVersion(client) + if err == nil { + if version < "7.8.0" { + err = errors.New("index_template endpoint only available from ElasticSearch >= 7.8") + } else { + result, err = elastic7GetIndexTemplate(client, id) + } + } + default: + err = errors.New("index_template endpoint only available from ElasticSearch >= 7.8") + } + if err != nil { + if elastic7.IsNotFound(err) || elastic6.IsNotFound(err) || elastic5.IsNotFound(err) { + log.Printf("[WARN] Index template (%s) not found, removing from state", id) + d.SetId("") + return nil + } + + return err + } + + ds := &resourceDataSetter{d: d} + ds.set("name", d.Id()) + ds.set("body", result) + return ds.err +} + +func elastic7GetIndexTemplate(client *elastic7.Client, id string) (string, error) { + res, err := client.IndexGetIndexTemplate(id).Do(context.TODO()) + if err != nil { + return "", err + } + + t := res.IndexTemplates[0] + tj, err := json.Marshal(t) + if err != nil { + return "", err + } + return string(tj), nil +} + +func resourceElasticsearchTemplateIndexUpdate(d *schema.ResourceData, meta interface{}) error { + return resourceElasticsearchPutTemplateIndex(d, meta, false) +} + +func resourceElasticsearchTemplateIndexDelete(d *schema.ResourceData, meta interface{}) error { + id := d.Id() + + var err error + switch client := meta.(type) { + case *elastic7.Client: + version, err := elastic7GetVersion(client) + if err == nil { + if version < "7.8.0" { + err = errors.New("index_template endpoint only available from ElasticSearch >= 7.8") + } else { + err = elastic7DeleteIndexTemplate(client, id) + } + } + default: + err = errors.New("index_template endpoint only available from ElasticSearch >= 7.8") + } + + if err != nil { + return err + } + d.SetId("") + return nil +} + +func elastic7DeleteIndexTemplate(client *elastic7.Client, id string) error { + _, err := client.IndexDeleteIndexTemplate(id).Do(context.TODO()) + return err +} + +func resourceElasticsearchPutTemplateIndex(d *schema.ResourceData, meta interface{}, create bool) error { + name := d.Get("name").(string) + body := d.Get("body").(string) + + var err error + switch client := meta.(type) { + case *elastic7.Client: + err = elastic7PutIndexTemplate(client, name, body, create) + default: + err = errors.New("index_template endpoint only available from ElasticSearch >= 7.8") + } + + return err +} + +func elastic7PutIndexTemplate(client *elastic7.Client, name string, body string, create bool) error { + _, err := client.IndexPutIndexTemplate(name).BodyString(body).Create(create).Do(context.TODO()) + return err +} diff --git a/es/util.go b/es/util.go index a18e150d..4661fe93 100644 --- a/es/util.go +++ b/es/util.go @@ -5,10 +5,12 @@ import ( "context" "encoding/json" "fmt" - "github.com/hashicorp/terraform-plugin-sdk/helper/hashcode" + "reflect" "sort" "strings" + "github.com/hashicorp/terraform-plugin-sdk/helper/hashcode" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" elastic7 "github.com/olivere/elastic/v7" elastic5 "gopkg.in/olivere/elastic.v5" @@ -478,3 +480,8 @@ func tenantPermissionsHash(v interface{}) int { return hashcode.String(buf.String()) } + +func elastic7GetVersion(client *elastic7.Client) (string, error) { + urls := reflect.ValueOf(client).Elem().FieldByName("urls") + return client.ElasticsearchVersion(urls.Index(0).String()) +} diff --git a/go.mod b/go.mod index 105bc681..22bf0e7c 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/deoxxa/aws_signing_client v0.0.0-20161109131055-c20ee106809e github.com/hashicorp/terraform-plugin-sdk v1.12.0 github.com/olivere/elastic v6.2.26+incompatible - github.com/olivere/elastic/v7 v7.0.19 + github.com/olivere/elastic/v7 v7.0.22 gopkg.in/olivere/elastic.v5 v5.0.85 gopkg.in/olivere/elastic.v6 v6.2.35 ) diff --git a/go.sum b/go.sum index 1b95a456..a147ad32 100644 --- a/go.sum +++ b/go.sum @@ -30,6 +30,7 @@ github.com/aws/aws-sdk-go v1.29.11 h1:f1QJRPu30p0i1lzKhkSSaZFudFGCra2HKgdE442nN6 github.com/aws/aws-sdk-go v1.29.11/go.mod h1:1KvfttTE3SPKMpo8g2c6jL3ZKfXtFvKscTgahTma5Xg= github.com/aws/aws-sdk-go v1.33.5 h1:p2fr1ryvNTU6avUWLI+/H7FGv0TBIjzVM5WDgXBBv4U= github.com/aws/aws-sdk-go v1.33.5/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0= +github.com/aws/aws-sdk-go v1.35.20/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k= github.com/aws/aws-sdk-go v1.35.33 h1:8qPRZqCRok5i7VNN51k/Ky7CuyoXMdSs4mUfKyCqvPw= github.com/aws/aws-sdk-go v1.35.33/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d h1:xDfNPAt8lFiC1UJrqV3uuy861HCTo708pDMbjHHdCas= @@ -80,6 +81,8 @@ github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= @@ -140,6 +143,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= +github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/keybase/go-crypto v0.0.0-20161004153544-93f5b35093ba/go.mod h1:ghbZscTyKdM07+Fw3KSi0hcJm+AlEUWj8QLlPtijN/M= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= @@ -154,6 +159,8 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0 github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/mailru/easyjson v0.7.1 h1:mdxE1MF9o53iCb2Ghj1VfWvh7ZOwHpnVG/xwXrV90U8= github.com/mailru/easyjson v0.7.1/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs= +github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA= +github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg= @@ -192,6 +199,8 @@ github.com/olivere/elastic/v7 v7.0.12 h1:91kj/UMKWQt8VAHBm5BDHpVmzdfPCmICaUFy2oH github.com/olivere/elastic/v7 v7.0.12/go.mod h1:14rWX28Pnh3qCKYRVnSGXWLf9MbLonYS/4FDCY3LAPo= github.com/olivere/elastic/v7 v7.0.19 h1:w4F6JpqOISadhYf/n0NR1cNj73xHqh4pzPwD1Gkidts= github.com/olivere/elastic/v7 v7.0.19/go.mod h1:4Jqt5xvjqpjCqgnTcHwl3j8TLs8mvoOK8NYgo/qEOu4= +github.com/olivere/elastic/v7 v7.0.22 h1:esBA6JJwvYgfms0EVlH7Z+9J4oQ/WUADF2y/nCNDw7s= +github.com/olivere/elastic/v7 v7.0.22/go.mod h1:VDexNy9NjmtAkrjNoI7tImv7FR4tf5zUA3ickqu5Pc8= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= @@ -210,6 +219,7 @@ github.com/smartystreets/assertions v1.1.1/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYl github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9/go.mod h1:SnhjPscd9TpLiy1LpzGSKh3bXCfxxXuqd9xmQJy3slM= github.com/smartystreets/gunit v1.1.3/go.mod h1:EH5qMBab2UclzXUcpR8b93eHsIlp9u+pDQIRp5DZNzQ= github.com/smartystreets/gunit v1.3.4/go.mod h1:ZjM1ozSIMJlAz/ay4SG8PeKF00ckUp+zMHZXV9/bvak= +github.com/smartystreets/gunit v1.4.2/go.mod h1:ZjM1ozSIMJlAz/ay4SG8PeKF00ckUp+zMHZXV9/bvak= github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc= github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= @@ -241,6 +251,8 @@ go.opencensus.io v0.22.3 h1:8sGtKOrtQqkN1bp2AtX+misvLIlOmsEsNd+9NIcPEm8= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4 h1:LYy1Hy3MJdrCdMwwzxA/dRok4ejH+RwNGbuoD9fCjto= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= +go.opencensus.io v0.22.5 h1:dntmOdLpSpHlVqbW5Eay97DelsZHe+55D+xC6i0dDS0= +go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= From 2d092dd1bf6a6162efa3db0059b4d31d5ef6fc07 Mon Sep 17 00:00:00 2001 From: Victor Cabezas Date: Wed, 9 Dec 2020 17:17:49 +0100 Subject: [PATCH 02/12] Remove phases.delete.actions.delete.delete_searchable_snapshot when true from IL Policy --- es/util.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/es/util.go b/es/util.go index 4661fe93..75367fd0 100644 --- a/es/util.go +++ b/es/util.go @@ -138,6 +138,11 @@ func normalizedIndexLifecyclePolicy(policy map[string]interface{}) map[string]in f := flattenMap(policy) for k, v := range f { f[k] = fmt.Sprintf("%v", v) + // Supress phases.delete.actions.delete.delete_searchable_snapshot = true that is included by default + // starting in 7.8 + if k == "phases.delete.actions.delete.delete_searchable_snapshot" && fmt.Sprintf("%v", v) == "true" { + delete(f, k) + } } return f From f7e1398c281068b52fbf37b307a99d4707785b24 Mon Sep 17 00:00:00 2001 From: Victor Cabezas Date: Wed, 9 Dec 2020 17:36:44 +0100 Subject: [PATCH 03/12] Fix ineffassign errors --- es/resource_elasticsearch_template_index.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/es/resource_elasticsearch_template_index.go b/es/resource_elasticsearch_template_index.go index f30b9b02..a6fa8fc2 100644 --- a/es/resource_elasticsearch_template_index.go +++ b/es/resource_elasticsearch_template_index.go @@ -3,7 +3,7 @@ package es import ( "context" "encoding/json" - "errors" + "fmt" "log" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" @@ -50,20 +50,20 @@ func resourceElasticsearchTemplateIndexCreate(d *schema.ResourceData, meta inter func resourceElasticsearchTemplateIndexRead(d *schema.ResourceData, meta interface{}) error { id := d.Id() - var result string + var result, version string var err error switch client := meta.(type) { case *elastic7.Client: - version, err := elastic7GetVersion(client) + version, err = elastic7GetVersion(client) if err == nil { if version < "7.8.0" { - err = errors.New("index_template endpoint only available from ElasticSearch >= 7.8") + err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", version) } else { result, err = elastic7GetIndexTemplate(client, id) } } default: - err = errors.New("index_template endpoint only available from ElasticSearch >= 7.8") + err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", version) } if err != nil { if elastic7.IsNotFound(err) || elastic6.IsNotFound(err) || elastic5.IsNotFound(err) { @@ -102,19 +102,20 @@ func resourceElasticsearchTemplateIndexUpdate(d *schema.ResourceData, meta inter func resourceElasticsearchTemplateIndexDelete(d *schema.ResourceData, meta interface{}) error { id := d.Id() + var version string var err error switch client := meta.(type) { case *elastic7.Client: - version, err := elastic7GetVersion(client) + version, err = elastic7GetVersion(client) if err == nil { if version < "7.8.0" { - err = errors.New("index_template endpoint only available from ElasticSearch >= 7.8") + err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", version) } else { err = elastic7DeleteIndexTemplate(client, id) } } default: - err = errors.New("index_template endpoint only available from ElasticSearch >= 7.8") + err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", version) } if err != nil { @@ -138,7 +139,7 @@ func resourceElasticsearchPutTemplateIndex(d *schema.ResourceData, meta interfac case *elastic7.Client: err = elastic7PutIndexTemplate(client, name, body, create) default: - err = errors.New("index_template endpoint only available from ElasticSearch >= 7.8") + err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", version) } return err From 008b401ccee361dc92bf913a194e8e48e03b0b37 Mon Sep 17 00:00:00 2001 From: Victor Cabezas Date: Wed, 9 Dec 2020 19:17:26 +0100 Subject: [PATCH 04/12] Add testing for new resource, adjust some error messages and add new diff supress function --- es/diff_suppress_funcs.go | 24 +++ es/resource_elasticsearch_template_index.go | 12 +- ...ource_elasticsearch_template_index_test.go | 172 ++++++++++++++++++ es/util.go | 17 ++ 4 files changed, 220 insertions(+), 5 deletions(-) create mode 100644 es/resource_elasticsearch_template_index_test.go diff --git a/es/diff_suppress_funcs.go b/es/diff_suppress_funcs.go index ba78332f..f57b0c9e 100644 --- a/es/diff_suppress_funcs.go +++ b/es/diff_suppress_funcs.go @@ -27,6 +27,30 @@ func diffSuppressIndexTemplate(k, old, new string, d *schema.ResourceData) bool return reflect.DeepEqual(oo, no) } +/* +diffSuppressTemplateIndex compares an index_template (ES >= 7.8) Index template definition +For legacy index templates (ES < 7.8) or /_template endpoint on ES >= 7.8 see diffSuppressIndexTemplate. +*/ +func diffSuppressTemplateIndex(k, old, new string, d *schema.ResourceData) bool { + var oo, no interface{} + if err := json.Unmarshal([]byte(old), &oo); err != nil { + return false + } + if err := json.Unmarshal([]byte(new), &no); err != nil { + return false + } + + if om, ok := oo.(map[string]interface{}); ok { + normalizeTemplateIndex(om) + } + + if nm, ok := no.(map[string]interface{}); ok { + normalizeTemplateIndex(nm) + } + + return reflect.DeepEqual(oo, no) +} + func diffSuppressDestination(k, old, new string, d *schema.ResourceData) bool { var oo, no interface{} if err := json.Unmarshal([]byte(old), &oo); err != nil { diff --git a/es/resource_elasticsearch_template_index.go b/es/resource_elasticsearch_template_index.go index a6fa8fc2..d670b01f 100644 --- a/es/resource_elasticsearch_template_index.go +++ b/es/resource_elasticsearch_template_index.go @@ -28,7 +28,7 @@ func resourceElasticsearchTemplateIndex() *schema.Resource { "body": { Type: schema.TypeString, Required: true, - DiffSuppressFunc: diffSuppressIndexTemplate, + DiffSuppressFunc: diffSuppressTemplateIndex, ValidateFunc: validation.StringIsJSON, }, }, @@ -63,7 +63,7 @@ func resourceElasticsearchTemplateIndexRead(d *schema.ResourceData, meta interfa } } default: - err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", version) + err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version < 7.0.0") } if err != nil { if elastic7.IsNotFound(err) || elastic6.IsNotFound(err) || elastic5.IsNotFound(err) { @@ -87,7 +87,9 @@ func elastic7GetIndexTemplate(client *elastic7.Client, id string) (string, error return "", err } - t := res.IndexTemplates[0] + // No more than 1 element is expected, if the index template is not found, previous call should + // return a 404 error + t := res.IndexTemplates[0].IndexTemplate tj, err := json.Marshal(t) if err != nil { return "", err @@ -115,7 +117,7 @@ func resourceElasticsearchTemplateIndexDelete(d *schema.ResourceData, meta inter } } default: - err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", version) + err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version < 7.0.0") } if err != nil { @@ -139,7 +141,7 @@ func resourceElasticsearchPutTemplateIndex(d *schema.ResourceData, meta interfac case *elastic7.Client: err = elastic7PutIndexTemplate(client, name, body, create) default: - err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", version) + err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version < 7.0.0") } return err diff --git a/es/resource_elasticsearch_template_index_test.go b/es/resource_elasticsearch_template_index_test.go new file mode 100644 index 00000000..b0ca8696 --- /dev/null +++ b/es/resource_elasticsearch_template_index_test.go @@ -0,0 +1,172 @@ +package es + +import ( + "context" + "errors" + "fmt" + "testing" + + elastic7 "github.com/olivere/elastic/v7" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +func TestAccElasticsearchTemplateIndex(t *testing.T) { + provider := Provider().(*schema.Provider) + err := provider.Configure(&terraform.ResourceConfig{}) + if err != nil { + t.Skipf("err: %s", err) + } + meta := provider.Meta() + var allowed bool + switch meta.(type) { + case *elastic7.Client: + allowed = true + default: + allowed = false + } + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + if !allowed { + t.Skip("/_index_template endpoint only supported on ES >= 7.8") + } + }, + Providers: testAccProviders, + CheckDestroy: testCheckElasticsearchTemplateIndexDestroy, + Steps: []resource.TestStep{ + { + Config: testAccElasticsearchTemplateIndex, + Check: resource.ComposeTestCheckFunc( + testCheckElasticsearchTemplateIndexExists("elasticsearch_template_index.test"), + ), + }, + }, + }) +} + +func TestAccElasticsearchTemplateIndex_importBasic(t *testing.T) { + provider := Provider().(*schema.Provider) + err := provider.Configure(&terraform.ResourceConfig{}) + if err != nil { + t.Skipf("err: %s", err) + } + meta := provider.Meta() + var allowed bool + switch meta.(type) { + case *elastic7.Client: + allowed = true + default: + allowed = false + } + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + if !allowed { + t.Skip("/_index_template endpoint only supported on ES >= 7.8") + } + }, + Providers: testAccProviders, + CheckDestroy: testCheckElasticsearchTemplateIndexDestroy, + Steps: []resource.TestStep{ + { + Config: testAccElasticsearchTemplateIndex, + }, + { + ResourceName: "elasticsearch_template_index.test", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testCheckElasticsearchTemplateIndexExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + if rs.Primary.ID == "" { + return fmt.Errorf("No index template ID is set") + } + + meta := testAccProvider.Meta() + + var err error + switch client := meta.(type) { + case *elastic7.Client: + _, err = client.IndexGetIndexTemplate(rs.Primary.ID).Do(context.TODO()) + default: + err = errors.New("/_index_template endpoint only supported on ES >= 7.8") + } + + if err != nil { + return err + } + + return nil + } +} + +func testCheckElasticsearchTemplateIndexDestroy(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "elasticsearch_template_index" { + continue + } + + meta := testAccProvider.Meta() + + var err error + switch client := meta.(type) { + case *elastic7.Client: + _, err = client.IndexGetTemplate(rs.Primary.ID).Do(context.TODO()) + default: + err = errors.New("/_index_template endpoint only supported on ES >= 7.8") + } + + if err != nil { + return nil // should be not found error + } + + return fmt.Errorf("Index template %q still exists", rs.Primary.ID) + } + + return nil +} + +var testAccElasticsearchTemplateIndex = ` +resource "elasticsearch_template_index" "test" { + name = "terraform-test" + body = <= 7.8) Index template definition. +For legacy index templates (ES < 7.8) or /_template endpoint on ES >= 7.8 see normalizeIndexTemplate. +*/ +func normalizeTemplateIndex(tpl map[string]interface{}) { + delete(tpl, "version") + if innerTpl, ok := tpl["template"]; ok { + if innerTplMap, ok := innerTpl.(map[string]interface{}); ok { + if settings, ok := innerTplMap["settings"]; ok { + if settingsMap, ok := settings.(map[string]interface{}); ok { + innerTplMap["settings"] = normalizedIndexSettings(settingsMap) + } + } + } + } +} + func normalizedIndexSettings(settings map[string]interface{}) map[string]interface{} { f := flattenMap(settings) for k, v := range f { From 968cd8ca0a6497dbd8374e573e2f32cab5a85cfe Mon Sep 17 00:00:00 2001 From: Victor Cabezas Date: Wed, 9 Dec 2020 19:21:29 +0100 Subject: [PATCH 05/12] Update go.sum --- go.sum | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/go.sum b/go.sum index a147ad32..8e20931c 100644 --- a/go.sum +++ b/go.sum @@ -28,8 +28,6 @@ github.com/aws/aws-sdk-go v1.15.78/go.mod h1:E3/ieXAlvM0XWO57iftYVDLLvQ824smPP3A github.com/aws/aws-sdk-go v1.25.3/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo= github.com/aws/aws-sdk-go v1.29.11 h1:f1QJRPu30p0i1lzKhkSSaZFudFGCra2HKgdE442nN6c= github.com/aws/aws-sdk-go v1.29.11/go.mod h1:1KvfttTE3SPKMpo8g2c6jL3ZKfXtFvKscTgahTma5Xg= -github.com/aws/aws-sdk-go v1.33.5 h1:p2fr1ryvNTU6avUWLI+/H7FGv0TBIjzVM5WDgXBBv4U= -github.com/aws/aws-sdk-go v1.33.5/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0= github.com/aws/aws-sdk-go v1.35.20/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k= github.com/aws/aws-sdk-go v1.35.33 h1:8qPRZqCRok5i7VNN51k/Ky7CuyoXMdSs4mUfKyCqvPw= github.com/aws/aws-sdk-go v1.35.33/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro= @@ -79,8 +77,6 @@ github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w= -github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no= @@ -137,8 +133,6 @@ github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKe github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= -github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc= -github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= @@ -197,8 +191,6 @@ github.com/olivere/elastic v6.2.26+incompatible h1:3PjUHKyt8xKwbFQpRC5cgtEY7Qz6e github.com/olivere/elastic v6.2.26+incompatible/go.mod h1:J+q1zQJTgAz9woqsbVRqGeB5G1iqDKVBWLNSYW8yfJ8= github.com/olivere/elastic/v7 v7.0.12 h1:91kj/UMKWQt8VAHBm5BDHpVmzdfPCmICaUFy2oH4LkQ= github.com/olivere/elastic/v7 v7.0.12/go.mod h1:14rWX28Pnh3qCKYRVnSGXWLf9MbLonYS/4FDCY3LAPo= -github.com/olivere/elastic/v7 v7.0.19 h1:w4F6JpqOISadhYf/n0NR1cNj73xHqh4pzPwD1Gkidts= -github.com/olivere/elastic/v7 v7.0.19/go.mod h1:4Jqt5xvjqpjCqgnTcHwl3j8TLs8mvoOK8NYgo/qEOu4= github.com/olivere/elastic/v7 v7.0.22 h1:esBA6JJwvYgfms0EVlH7Z+9J4oQ/WUADF2y/nCNDw7s= github.com/olivere/elastic/v7 v7.0.22/go.mod h1:VDexNy9NjmtAkrjNoI7tImv7FR4tf5zUA3ickqu5Pc8= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= @@ -218,7 +210,6 @@ github.com/smartystreets/assertions v1.0.1/go.mod h1:kHHU4qYBaI3q23Pp3VPrmWhuIUr github.com/smartystreets/assertions v1.1.1/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= github.com/smartystreets/go-aws-auth v0.0.0-20180515143844-0c1422d1fdb9/go.mod h1:SnhjPscd9TpLiy1LpzGSKh3bXCfxxXuqd9xmQJy3slM= github.com/smartystreets/gunit v1.1.3/go.mod h1:EH5qMBab2UclzXUcpR8b93eHsIlp9u+pDQIRp5DZNzQ= -github.com/smartystreets/gunit v1.3.4/go.mod h1:ZjM1ozSIMJlAz/ay4SG8PeKF00ckUp+zMHZXV9/bvak= github.com/smartystreets/gunit v1.4.2/go.mod h1:ZjM1ozSIMJlAz/ay4SG8PeKF00ckUp+zMHZXV9/bvak= github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc= github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= @@ -249,8 +240,6 @@ go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.3 h1:8sGtKOrtQqkN1bp2AtX+misvLIlOmsEsNd+9NIcPEm8= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= -go.opencensus.io v0.22.4 h1:LYy1Hy3MJdrCdMwwzxA/dRok4ejH+RwNGbuoD9fCjto= -go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.5 h1:dntmOdLpSpHlVqbW5Eay97DelsZHe+55D+xC6i0dDS0= go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= From d01ee8288dd2689781baeb4cf4d73a9050a16b39 Mon Sep 17 00:00:00 2001 From: Victor Cabezas Date: Fri, 11 Dec 2020 18:53:48 +0100 Subject: [PATCH 06/12] Rename resource and functions from template index to composable index template --- es/diff_suppress_funcs.go | 8 +++--- es/provider.go | 2 +- ...lasticsearch_composable_index_template.go} | 26 +++++++++---------- ...csearch_composable_index_template_test.go} | 26 +++++++++---------- es/util.go | 4 +-- 5 files changed, 33 insertions(+), 33 deletions(-) rename es/{resource_elasticsearch_template_index.go => resource_elasticsearch_composable_index_template.go} (75%) rename es/{resource_elasticsearch_template_index_test.go => resource_elasticsearch_composable_index_template_test.go} (76%) diff --git a/es/diff_suppress_funcs.go b/es/diff_suppress_funcs.go index f57b0c9e..b29c8b5f 100644 --- a/es/diff_suppress_funcs.go +++ b/es/diff_suppress_funcs.go @@ -28,10 +28,10 @@ func diffSuppressIndexTemplate(k, old, new string, d *schema.ResourceData) bool } /* -diffSuppressTemplateIndex compares an index_template (ES >= 7.8) Index template definition +diffSuppressComposableIndexTemplate compares an index_template (ES >= 7.8) Index template definition For legacy index templates (ES < 7.8) or /_template endpoint on ES >= 7.8 see diffSuppressIndexTemplate. */ -func diffSuppressTemplateIndex(k, old, new string, d *schema.ResourceData) bool { +func diffSuppressComposableIndexTemplate(k, old, new string, d *schema.ResourceData) bool { var oo, no interface{} if err := json.Unmarshal([]byte(old), &oo); err != nil { return false @@ -41,11 +41,11 @@ func diffSuppressTemplateIndex(k, old, new string, d *schema.ResourceData) bool } if om, ok := oo.(map[string]interface{}); ok { - normalizeTemplateIndex(om) + normalizeComposableIndexTemplate(om) } if nm, ok := no.(map[string]interface{}); ok { - normalizeTemplateIndex(nm) + normalizeComposableIndexTemplate(nm) } return reflect.DeepEqual(oo, no) diff --git a/es/provider.go b/es/provider.go index fc8e350b..6b33cce8 100644 --- a/es/provider.go +++ b/es/provider.go @@ -147,7 +147,7 @@ func Provider() terraform.ResourceProvider { "elasticsearch_index": resourceElasticsearchIndex(), "elasticsearch_index_lifecycle_policy": resourceElasticsearchDeprecatedIndexLifecyclePolicy(), "elasticsearch_index_template": resourceElasticsearchIndexTemplate(), - "elasticsearch_template_index": resourceElasticsearchTemplateIndex(), + "elasticsearch_composable_index_template": resourceElasticsearchComposableIndexTemplate(), "elasticsearch_ingest_pipeline": resourceElasticsearchIngestPipeline(), "elasticsearch_kibana_object": resourceElasticsearchKibanaObject(), "elasticsearch_monitor": resourceElasticsearchDeprecatedMonitor(), diff --git a/es/resource_elasticsearch_template_index.go b/es/resource_elasticsearch_composable_index_template.go similarity index 75% rename from es/resource_elasticsearch_template_index.go rename to es/resource_elasticsearch_composable_index_template.go index d670b01f..533c1188 100644 --- a/es/resource_elasticsearch_template_index.go +++ b/es/resource_elasticsearch_composable_index_template.go @@ -13,12 +13,12 @@ import ( elastic6 "gopkg.in/olivere/elastic.v6" ) -func resourceElasticsearchTemplateIndex() *schema.Resource { +func resourceElasticsearchComposableIndexTemplate() *schema.Resource { return &schema.Resource{ - Create: resourceElasticsearchTemplateIndexCreate, - Read: resourceElasticsearchTemplateIndexRead, - Update: resourceElasticsearchTemplateIndexUpdate, - Delete: resourceElasticsearchTemplateIndexDelete, + Create: resourceElasticsearchComposableIndexTemplateCreate, + Read: resourceElasticsearchComposableIndexTemplateRead, + Update: resourceElasticsearchComposableIndexTemplateUpdate, + Delete: resourceElasticsearchComposableIndexTemplateDelete, Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -28,7 +28,7 @@ func resourceElasticsearchTemplateIndex() *schema.Resource { "body": { Type: schema.TypeString, Required: true, - DiffSuppressFunc: diffSuppressTemplateIndex, + DiffSuppressFunc: diffSuppressComposableIndexTemplate, ValidateFunc: validation.StringIsJSON, }, }, @@ -38,8 +38,8 @@ func resourceElasticsearchTemplateIndex() *schema.Resource { } } -func resourceElasticsearchTemplateIndexCreate(d *schema.ResourceData, meta interface{}) error { - err := resourceElasticsearchPutTemplateIndex(d, meta, true) +func resourceElasticsearchComposableIndexTemplateCreate(d *schema.ResourceData, meta interface{}) error { + err := resourceElasticsearchPutComposableIndexTemplate(d, meta, true) if err != nil { return err } @@ -47,7 +47,7 @@ func resourceElasticsearchTemplateIndexCreate(d *schema.ResourceData, meta inter return nil } -func resourceElasticsearchTemplateIndexRead(d *schema.ResourceData, meta interface{}) error { +func resourceElasticsearchComposableIndexTemplateRead(d *schema.ResourceData, meta interface{}) error { id := d.Id() var result, version string @@ -97,11 +97,11 @@ func elastic7GetIndexTemplate(client *elastic7.Client, id string) (string, error return string(tj), nil } -func resourceElasticsearchTemplateIndexUpdate(d *schema.ResourceData, meta interface{}) error { - return resourceElasticsearchPutTemplateIndex(d, meta, false) +func resourceElasticsearchComposableIndexTemplateUpdate(d *schema.ResourceData, meta interface{}) error { + return resourceElasticsearchPutComposableIndexTemplate(d, meta, false) } -func resourceElasticsearchTemplateIndexDelete(d *schema.ResourceData, meta interface{}) error { +func resourceElasticsearchComposableIndexTemplateDelete(d *schema.ResourceData, meta interface{}) error { id := d.Id() var version string @@ -132,7 +132,7 @@ func elastic7DeleteIndexTemplate(client *elastic7.Client, id string) error { return err } -func resourceElasticsearchPutTemplateIndex(d *schema.ResourceData, meta interface{}, create bool) error { +func resourceElasticsearchPutComposableIndexTemplate(d *schema.ResourceData, meta interface{}, create bool) error { name := d.Get("name").(string) body := d.Get("body").(string) diff --git a/es/resource_elasticsearch_template_index_test.go b/es/resource_elasticsearch_composable_index_template_test.go similarity index 76% rename from es/resource_elasticsearch_template_index_test.go rename to es/resource_elasticsearch_composable_index_template_test.go index b0ca8696..ee7e0e06 100644 --- a/es/resource_elasticsearch_template_index_test.go +++ b/es/resource_elasticsearch_composable_index_template_test.go @@ -13,7 +13,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/terraform" ) -func TestAccElasticsearchTemplateIndex(t *testing.T) { +func TestAccElasticsearchComposableIndexTemplate(t *testing.T) { provider := Provider().(*schema.Provider) err := provider.Configure(&terraform.ResourceConfig{}) if err != nil { @@ -35,19 +35,19 @@ func TestAccElasticsearchTemplateIndex(t *testing.T) { } }, Providers: testAccProviders, - CheckDestroy: testCheckElasticsearchTemplateIndexDestroy, + CheckDestroy: testCheckElasticsearchComposableIndexTemplateDestroy, Steps: []resource.TestStep{ { - Config: testAccElasticsearchTemplateIndex, + Config: testAccElasticsearchComposableIndexTemplate, Check: resource.ComposeTestCheckFunc( - testCheckElasticsearchTemplateIndexExists("elasticsearch_template_index.test"), + testCheckElasticsearchComposableIndexTemplateExists("elasticsearch_composable_index_template.test"), ), }, }, }) } -func TestAccElasticsearchTemplateIndex_importBasic(t *testing.T) { +func TestAccElasticsearchComposableIndexTemplate_importBasic(t *testing.T) { provider := Provider().(*schema.Provider) err := provider.Configure(&terraform.ResourceConfig{}) if err != nil { @@ -69,13 +69,13 @@ func TestAccElasticsearchTemplateIndex_importBasic(t *testing.T) { } }, Providers: testAccProviders, - CheckDestroy: testCheckElasticsearchTemplateIndexDestroy, + CheckDestroy: testCheckElasticsearchComposableIndexTemplateDestroy, Steps: []resource.TestStep{ { - Config: testAccElasticsearchTemplateIndex, + Config: testAccElasticsearchComposableIndexTemplate, }, { - ResourceName: "elasticsearch_template_index.test", + ResourceName: "elasticsearch_composable_index_template.test", ImportState: true, ImportStateVerify: true, }, @@ -83,7 +83,7 @@ func TestAccElasticsearchTemplateIndex_importBasic(t *testing.T) { }) } -func testCheckElasticsearchTemplateIndexExists(name string) resource.TestCheckFunc { +func testCheckElasticsearchComposableIndexTemplateExists(name string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[name] if !ok { @@ -111,9 +111,9 @@ func testCheckElasticsearchTemplateIndexExists(name string) resource.TestCheckFu } } -func testCheckElasticsearchTemplateIndexDestroy(s *terraform.State) error { +func testCheckElasticsearchComposableIndexTemplateDestroy(s *terraform.State) error { for _, rs := range s.RootModule().Resources { - if rs.Type != "elasticsearch_template_index" { + if rs.Type != "elasticsearch_composable_index_template" { continue } @@ -137,8 +137,8 @@ func testCheckElasticsearchTemplateIndexDestroy(s *terraform.State) error { return nil } -var testAccElasticsearchTemplateIndex = ` -resource "elasticsearch_template_index" "test" { +var testAccElasticsearchComposableIndexTemplate = ` +resource "elasticsearch_composable_index_template" "test" { name = "terraform-test" body = <= 7.8) Index template definition. +normalizeComposableIndexTemplate normalizes an index_template (ES >= 7.8) Index template definition. For legacy index templates (ES < 7.8) or /_template endpoint on ES >= 7.8 see normalizeIndexTemplate. */ -func normalizeTemplateIndex(tpl map[string]interface{}) { +func normalizeComposableIndexTemplate(tpl map[string]interface{}) { delete(tpl, "version") if innerTpl, ok := tpl["template"]; ok { if innerTplMap, ok := innerTpl.(map[string]interface{}); ok { From fcfd4c1f900c4d200d4272e6419ee975e4f50209 Mon Sep 17 00:00:00 2001 From: Victor Cabezas Date: Fri, 11 Dec 2020 18:53:57 +0100 Subject: [PATCH 07/12] Add documentation page --- docs/resources/composable_index_template.md | 63 +++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 docs/resources/composable_index_template.md diff --git a/docs/resources/composable_index_template.md b/docs/resources/composable_index_template.md new file mode 100644 index 00000000..8669f2b1 --- /dev/null +++ b/docs/resources/composable_index_template.md @@ -0,0 +1,63 @@ +--- +layout: "elasticsearch" +page_title: "Elasticsearch: elasticsearch_composable_index_template" +subcategory: "Elasticsearch Opensource" +description: |- + Provides an Elasticsearch Composable index template resource. +--- + +# elasticsearch_composable_index_template + +Provides an Elasticsearch Composable index template resource. This resource uses the `/_index_template` +endpoint of Elasticsearch API that is available since version 7.8. Use `elasticsearch_index_template` if +you are using older versions of Elasticsearch or if you want to keep using legacy Index Templates in Elasticsearch 7.8+. + +## Example Usage + +```tf +# Create an index template +resource "elasticsearch_composable_index_template" "template_1" { + name = "template_1" + body = < Date: Mon, 14 Dec 2020 23:19:11 -0500 Subject: [PATCH 08/12] cleanup, add changelog --- CHANGELOG.md | 2 +- docs/resources/composable_index_template.md | 6 +++--- es/resource_elasticsearch_composable_index_template.go | 2 +- es/resource_elasticsearch_composable_index_template_test.go | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66d213fe..64e141ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ - Bump aws client to v1.35.33. ### Added -- +- Composable Index Template resource, available in ESv7.8+ ## [1.5.0] - 2020-10-26 ### Changed diff --git a/docs/resources/composable_index_template.md b/docs/resources/composable_index_template.md index 8669f2b1..013bbf75 100644 --- a/docs/resources/composable_index_template.md +++ b/docs/resources/composable_index_template.md @@ -23,9 +23,9 @@ resource "elasticsearch_composable_index_template" "template_1" { "index_patterns": ["te*", "bar*"], "template": { "settings": { - "index": { - "number_of_shards": 1 - } + "index": { + "number_of_shards": 1 + } }, "mappings": { "properties": { diff --git a/es/resource_elasticsearch_composable_index_template.go b/es/resource_elasticsearch_composable_index_template.go index 533c1188..63091121 100644 --- a/es/resource_elasticsearch_composable_index_template.go +++ b/es/resource_elasticsearch_composable_index_template.go @@ -66,7 +66,7 @@ func resourceElasticsearchComposableIndexTemplateRead(d *schema.ResourceData, me err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version < 7.0.0") } if err != nil { - if elastic7.IsNotFound(err) || elastic6.IsNotFound(err) || elastic5.IsNotFound(err) { + if elastic7.IsNotFound(err) { log.Printf("[WARN] Index template (%s) not found, removing from state", id) d.SetId("") return nil diff --git a/es/resource_elasticsearch_composable_index_template_test.go b/es/resource_elasticsearch_composable_index_template_test.go index ee7e0e06..0f1ea030 100644 --- a/es/resource_elasticsearch_composable_index_template_test.go +++ b/es/resource_elasticsearch_composable_index_template_test.go @@ -145,9 +145,9 @@ resource "elasticsearch_composable_index_template" "test" { "index_patterns": ["te*", "bar*"], "template": { "settings": { - "index": { - "number_of_shards": 1 - } + "index": { + "number_of_shards": 1 + } }, "mappings": { "properties": { From a539380f04d12740ca3d7160515e9663f0b52733 Mon Sep 17 00:00:00 2001 From: Phillip Baker Date: Mon, 14 Dec 2020 23:20:56 -0500 Subject: [PATCH 09/12] back out phases.delete.actions.delete.delete_searchable_snapshot diff supress for now --- es/util.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/es/util.go b/es/util.go index e0dcdeeb..023fa3d8 100644 --- a/es/util.go +++ b/es/util.go @@ -155,11 +155,6 @@ func normalizedIndexLifecyclePolicy(policy map[string]interface{}) map[string]in f := flattenMap(policy) for k, v := range f { f[k] = fmt.Sprintf("%v", v) - // Supress phases.delete.actions.delete.delete_searchable_snapshot = true that is included by default - // starting in 7.8 - if k == "phases.delete.actions.delete.delete_searchable_snapshot" && fmt.Sprintf("%v", v) == "true" { - delete(f, k) - } } return f From 77a7fea64c61de2064e7591a2aec0c7aba9ff11a Mon Sep 17 00:00:00 2001 From: Phillip Baker Date: Mon, 14 Dec 2020 23:25:03 -0500 Subject: [PATCH 10/12] fix linting --- es/resource_elasticsearch_composable_index_template.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/es/resource_elasticsearch_composable_index_template.go b/es/resource_elasticsearch_composable_index_template.go index 63091121..f0488471 100644 --- a/es/resource_elasticsearch_composable_index_template.go +++ b/es/resource_elasticsearch_composable_index_template.go @@ -9,8 +9,6 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/helper/validation" elastic7 "github.com/olivere/elastic/v7" - elastic5 "gopkg.in/olivere/elastic.v5" - elastic6 "gopkg.in/olivere/elastic.v6" ) func resourceElasticsearchComposableIndexTemplate() *schema.Resource { From fd03815ecacf40756f0883165862fa01291e7818 Mon Sep 17 00:00:00 2001 From: Phillip Baker Date: Mon, 14 Dec 2020 23:36:06 -0500 Subject: [PATCH 11/12] update tests for default value --- ...esource_elasticsearch_xpack_index_lifecycle_policy_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/es/resource_elasticsearch_xpack_index_lifecycle_policy_test.go b/es/resource_elasticsearch_xpack_index_lifecycle_policy_test.go index ce2b6d98..96e8e9ea 100644 --- a/es/resource_elasticsearch_xpack_index_lifecycle_policy_test.go +++ b/es/resource_elasticsearch_xpack_index_lifecycle_policy_test.go @@ -163,7 +163,9 @@ resource "elasticsearch_xpack_index_lifecycle_policy" "test" { "delete": { "min_age": "30d", "actions": { - "delete": {} + "delete": { + "delete_searchable_snapshot": true + } } } } From c530beefbd7a18b3e0a92e5e197bc3b447a239c7 Mon Sep 17 00:00:00 2001 From: Phillip Baker Date: Mon, 14 Dec 2020 23:47:22 -0500 Subject: [PATCH 12/12] split es6 from es7 fixtures --- ...earch_xpack_index_lifecycle_policy_test.go | 44 +++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/es/resource_elasticsearch_xpack_index_lifecycle_policy_test.go b/es/resource_elasticsearch_xpack_index_lifecycle_policy_test.go index 96e8e9ea..0cfed896 100644 --- a/es/resource_elasticsearch_xpack_index_lifecycle_policy_test.go +++ b/es/resource_elasticsearch_xpack_index_lifecycle_policy_test.go @@ -22,12 +22,17 @@ func TestAccElasticsearchXpackIndexLifecyclePolicy(t *testing.T) { t.Skipf("err: %s", err) } meta := provider.Meta() + var config string var allowed bool switch meta.(type) { case *elastic5.Client: allowed = false + case *elastic6.Client: + allowed = true + config = testAccElasticsearch6XpackIndexLifecyclePolicy default: allowed = true + config = testAccElasticsearch7XpackIndexLifecyclePolicy } resource.Test(t, resource.TestCase{ @@ -41,7 +46,7 @@ func TestAccElasticsearchXpackIndexLifecyclePolicy(t *testing.T) { CheckDestroy: testCheckElasticsearchXpackIndexLifecyclePolicyDestroy, Steps: []resource.TestStep{ { - Config: testAccElasticsearchXpackIndexLifecyclePolicy, + Config: config, Check: resource.ComposeTestCheckFunc( testCheckElasticsearchXpackIndexLifecyclePolicyExists("elasticsearch_xpack_index_lifecycle_policy.test"), ), @@ -57,12 +62,17 @@ func TestAccElasticsearchXpackIndexLifecyclePolicy_importBasic(t *testing.T) { t.Skipf("err: %s", err) } meta := provider.Meta() + var config string var allowed bool switch meta.(type) { case *elastic5.Client: allowed = false + case *elastic6.Client: + allowed = true + config = testAccElasticsearch6XpackIndexLifecyclePolicy default: allowed = true + config = testAccElasticsearch7XpackIndexLifecyclePolicy } resource.Test(t, resource.TestCase{ @@ -76,7 +86,7 @@ func TestAccElasticsearchXpackIndexLifecyclePolicy_importBasic(t *testing.T) { CheckDestroy: testCheckElasticsearchXpackIndexLifecyclePolicyDestroy, Steps: []resource.TestStep{ { - Config: testAccElasticsearchXpackIndexLifecyclePolicy, + Config: config, }, { ResourceName: "elasticsearch_xpack_index_lifecycle_policy.test", @@ -145,7 +155,35 @@ func testCheckElasticsearchXpackIndexLifecyclePolicyDestroy(s *terraform.State) return nil } -var testAccElasticsearchXpackIndexLifecyclePolicy = ` +var testAccElasticsearch6XpackIndexLifecyclePolicy = ` +resource "elasticsearch_xpack_index_lifecycle_policy" "test" { + name = "terraform-test" + body = <