@@ -10,6 +10,8 @@ import (
1010 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1111 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1212 "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
13+ . "github.com/onsi/gomega"
14+ . "github.com/onsi/gomega/gstruct"
1315
1416 elastic7 "github.com/olivere/elastic/v7"
1517 elastic6 "gopkg.in/olivere/elastic.v6"
@@ -148,6 +150,47 @@ resource "elasticsearch_index" "test_doctype" {
148150}
149151EOF
150152}
153+ `
154+ testAccElasticsearchBasicMapping = `
155+ resource "elasticsearch_index" "test_mapping" {
156+ name = "terraform-test"
157+ number_of_replicas = "1"
158+ include_type_name = false
159+ mappings = <<EOF
160+ {
161+ "properties": {
162+ "name": {
163+ "type": "text"
164+ },
165+ "age": {
166+ "type": "integer"
167+ }
168+ }
169+ }
170+ EOF
171+ }
172+ `
173+ testAccElasticsearchAddedMapping = `
174+ resource "elasticsearch_index" "test_mapping" {
175+ name = "terraform-test"
176+ number_of_replicas = "1"
177+ include_type_name = false
178+ mappings = <<EOF
179+ {
180+ "properties": {
181+ "name": {
182+ "type": "text"
183+ },
184+ "surname": {
185+ "type": "text"
186+ },
187+ "age": {
188+ "type": "integer"
189+ }
190+ }
191+ }
192+ EOF
193+ }
151194`
152195 testAccElasticsearchIndexUpdateForceDestroy = `
153196resource "elasticsearch_index" "test" {
@@ -440,6 +483,79 @@ func TestAccElasticsearchIndex_doctype(t *testing.T) {
440483 })
441484}
442485
486+ func TestAccElasticsearchIndex_mapping (t * testing.T ) {
487+ g := NewGomegaWithT (t )
488+ provider := Provider ()
489+ diags := provider .Configure (context .Background (), & terraform.ResourceConfig {})
490+ if diags .HasError () {
491+ t .Skipf ("err: %#v" , diags )
492+ }
493+
494+ var initialUUID string
495+ var err error
496+
497+ resource .Test (t , resource.TestCase {
498+ PreCheck : func () { testAccPreCheck (t ) },
499+ Providers : testAccProviders ,
500+ CheckDestroy : checkElasticsearchIndexDestroy ,
501+ Steps : []resource.TestStep {
502+ {
503+ Config : testAccElasticsearchBasicMapping ,
504+ Check : resource .ComposeTestCheckFunc (
505+ checkElasticsearchIndexMapping ("terraform-test" , func (s * terraform.State , mapping map [string ]interface {}) error {
506+ initialUUID , err = getElasticsearchIndexUUID ("elasticsearch_index.test_mapping" , s )
507+ if err != nil {
508+ return err
509+ }
510+ fmt .Println (initialUUID )
511+ g .Expect (mapping ).To (MatchAllKeys (Keys {
512+ "terraform-test" : MatchAllKeys (Keys {
513+ "mappings" : MatchAllKeys (Keys {
514+ "properties" : MatchAllKeys (Keys {
515+ "age" : MatchAllKeys (Keys {
516+ "type" : Equal ("integer" ),
517+ }),
518+ "name" : MatchAllKeys (Keys {
519+ "type" : Equal ("text" ),
520+ }),
521+ }),
522+ }),
523+ }),
524+ }))
525+ return nil
526+ }),
527+ ),
528+ },
529+ {
530+ Config : testAccElasticsearchAddedMapping ,
531+ Check : resource .ComposeTestCheckFunc (
532+ checkElasticsearchIndexNotRecreated ("elasticsearch_index.test_mapping" , & initialUUID ),
533+ checkElasticsearchIndexMapping ("terraform-test" , func (s * terraform.State , mapping map [string ]interface {}) error {
534+ g .Expect (mapping ).To (MatchAllKeys (Keys {
535+ "terraform-test" : MatchAllKeys (Keys {
536+ "mappings" : MatchAllKeys (Keys {
537+ "properties" : MatchAllKeys (Keys {
538+ "age" : MatchAllKeys (Keys {
539+ "type" : Equal ("integer" ),
540+ }),
541+ "name" : MatchAllKeys (Keys {
542+ "type" : Equal ("text" ),
543+ }),
544+ "surname" : MatchAllKeys (Keys {
545+ "type" : Equal ("text" ),
546+ }),
547+ }),
548+ }),
549+ }),
550+ }))
551+ return nil
552+ }),
553+ ),
554+ },
555+ },
556+ })
557+ }
558+
443559func TestAccElasticsearchIndex_rolloverAliasXpack (t * testing.T ) {
444560 resource .Test (t , resource.TestCase {
445561 PreCheck : func () {
@@ -518,33 +634,113 @@ func TestAccElasticsearchIndex_rolloverAliasOpendistro(t *testing.T) {
518634 })
519635}
520636
637+ func getElasticsearchIndex (name string , s * terraform.State ) (map [string ]interface {}, error ) {
638+ rs , ok := s .RootModule ().Resources [name ]
639+ if ! ok {
640+ return nil , fmt .Errorf ("not found: %s" , name )
641+ }
642+ if rs .Primary .ID == "" {
643+ return nil , fmt .Errorf ("index ID not set" )
644+ }
645+
646+ meta := testAccProvider .Meta ()
647+ var settings map [string ]interface {}
648+
649+ var err error
650+ esClient , err := getClient (meta .(* ProviderConf ))
651+ if err != nil {
652+ return nil , err
653+ }
654+ switch client := esClient .(type ) {
655+ case * elastic7.Client :
656+ resp , err := client .IndexGetSettings (rs .Primary .ID ).Do (context .TODO ())
657+ if err != nil {
658+ return nil , err
659+ }
660+ settings = resp [rs .Primary .ID ].Settings ["index" ].(map [string ]interface {})
661+
662+ case * elastic6.Client :
663+ resp , err := client .IndexGetSettings (rs .Primary .ID ).Do (context .TODO ())
664+ if err != nil {
665+ return nil , err
666+ }
667+ settings = resp [rs .Primary .ID ].Settings ["index" ].(map [string ]interface {})
668+
669+ default :
670+ return nil , errors .New ("Elasticsearch version not supported" )
671+ }
672+ return settings , nil
673+ }
674+
521675func checkElasticsearchIndexExists (name string ) resource.TestCheckFunc {
522676 return func (s * terraform.State ) error {
523- rs , ok := s .RootModule ().Resources [name ]
524- if ! ok {
525- return fmt .Errorf ("not found: %s" , name )
526- }
527- if rs .Primary .ID == "" {
528- return fmt .Errorf ("index ID not set" )
529- }
677+ _ , err := getElasticsearchIndex (name , s )
678+ return err
679+ }
680+ }
530681
682+ func getElasticsearchIndexUUID (name string , s * terraform.State ) (string , error ) {
683+ index , err := getElasticsearchIndex (name , s )
684+ if err != nil {
685+ return "" , fmt .Errorf ("failed to get index %s: %w" , name , err )
686+ }
687+ return index ["uuid" ].(string ), err
688+ }
689+
690+ func checkElasticsearchIndexRecreated (name string , initialIndexUUID * string ) resource.TestCheckFunc {
691+ return func (s * terraform.State ) error {
692+ newUUID , err := getElasticsearchIndexUUID (name , s )
693+ if err != nil {
694+ return fmt .Errorf ("failed to get index UUID for %s: %w" , name , err )
695+ }
696+ if newUUID == * initialIndexUUID {
697+ return fmt .Errorf ("index was not recreated where it was expected to: initial index UUID %s matches post-update index UUID %s" , * initialIndexUUID , newUUID )
698+ }
699+ return nil
700+ }
701+ }
702+
703+ func checkElasticsearchIndexNotRecreated (name string , initialIndexUUID * string ) resource.TestCheckFunc {
704+ return func (s * terraform.State ) error {
705+ newUUID , err := getElasticsearchIndexUUID (name , s )
706+ if err != nil {
707+ return fmt .Errorf ("failed to get index UUID for %s: %w" , name , err )
708+ }
709+ if newUUID != * initialIndexUUID {
710+ return fmt .Errorf ("index was recreated where it was not expected to: initial index UUID %s does not match post-update index UUID %s" , * initialIndexUUID , newUUID )
711+ }
712+ return nil
713+ }
714+ }
715+
716+ func checkElasticsearchIndexMapping (name string , mappingChecker func (s * terraform.State , mapping map [string ]interface {}) error ) resource.TestCheckFunc {
717+ return func (s * terraform.State ) error {
531718 meta := testAccProvider .Meta ()
532719
533720 var err error
534721 esClient , err := getClient (meta .(* ProviderConf ))
535722 if err != nil {
536723 return err
537724 }
725+ var mapping map [string ]interface {}
538726 switch client := esClient .(type ) {
539727 case * elastic7.Client :
540- _ , err = client .IndexGetSettings (rs .Primary .ID ).Do (context .TODO ())
728+ mapping , err = client .GetMapping ().Index (name ).Do (context .TODO ())
729+ if err != nil {
730+ return fmt .Errorf ("failed to find mapping for index %s: %w" , name , err )
731+ }
732+
541733 case * elastic6.Client :
542- _ , err = client .IndexGetSettings (rs .Primary .ID ).Do (context .TODO ())
734+ mapping , err = client .GetMapping ().Index (name ).Do (context .TODO ())
735+ if err != nil {
736+ return fmt .Errorf ("failed to find mapping for index %s: %w" , name , err )
737+ }
738+
543739 default :
544740 return errors .New ("Elasticsearch version not supported" )
545741 }
546742
547- return err
743+ return mappingChecker ( s , mapping )
548744 }
549745}
550746
0 commit comments