github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/resource_aws_elasticsearch_domain_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	elasticsearch "github.com/aws/aws-sdk-go/service/elasticsearchservice"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSElasticSearchDomain_basic(t *testing.T) {
    16  	var domain elasticsearch.ElasticsearchDomainStatus
    17  	ri := acctest.RandInt()
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckESDomainDestroy,
    23  		Steps: []resource.TestStep{
    24  			{
    25  				Config: testAccESDomainConfig(ri),
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain),
    28  					resource.TestCheckResourceAttr(
    29  						"aws_elasticsearch_domain.example", "elasticsearch_version", "1.5"),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func TestAccAWSElasticSearchDomain_importBasic(t *testing.T) {
    37  	resourceName := "aws_elasticsearch_domain.example"
    38  	ri := acctest.RandInt()
    39  	resourceId := fmt.Sprintf("tf-test-%d", ri)
    40  
    41  	resource.Test(t, resource.TestCase{
    42  		PreCheck:     func() { testAccPreCheck(t) },
    43  		Providers:    testAccProviders,
    44  		CheckDestroy: testAccCheckAWSRedshiftClusterDestroy,
    45  		Steps: []resource.TestStep{
    46  			{
    47  				Config: testAccESDomainConfig(ri),
    48  			},
    49  			{
    50  				ResourceName:      resourceName,
    51  				ImportState:       true,
    52  				ImportStateVerify: true,
    53  				ImportStateId:     resourceId,
    54  			},
    55  		},
    56  	})
    57  }
    58  
    59  func TestAccAWSElasticSearchDomain_v23(t *testing.T) {
    60  	var domain elasticsearch.ElasticsearchDomainStatus
    61  	ri := acctest.RandInt()
    62  
    63  	resource.Test(t, resource.TestCase{
    64  		PreCheck:     func() { testAccPreCheck(t) },
    65  		Providers:    testAccProviders,
    66  		CheckDestroy: testAccCheckESDomainDestroy,
    67  		Steps: []resource.TestStep{
    68  			{
    69  				Config: testAccESDomainConfigV23(ri),
    70  				Check: resource.ComposeTestCheckFunc(
    71  					testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain),
    72  					resource.TestCheckResourceAttr(
    73  						"aws_elasticsearch_domain.example", "elasticsearch_version", "2.3"),
    74  				),
    75  			},
    76  		},
    77  	})
    78  }
    79  
    80  func TestAccAWSElasticSearchDomain_complex(t *testing.T) {
    81  	var domain elasticsearch.ElasticsearchDomainStatus
    82  	ri := acctest.RandInt()
    83  
    84  	resource.Test(t, resource.TestCase{
    85  		PreCheck:     func() { testAccPreCheck(t) },
    86  		Providers:    testAccProviders,
    87  		CheckDestroy: testAccCheckESDomainDestroy,
    88  		Steps: []resource.TestStep{
    89  			{
    90  				Config: testAccESDomainConfig_complex(ri),
    91  				Check: resource.ComposeTestCheckFunc(
    92  					testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain),
    93  				),
    94  			},
    95  		},
    96  	})
    97  }
    98  
    99  func TestAccAWSElasticSearchDomain_tags(t *testing.T) {
   100  	var domain elasticsearch.ElasticsearchDomainStatus
   101  	var td elasticsearch.ListTagsOutput
   102  	ri := acctest.RandInt()
   103  
   104  	resource.Test(t, resource.TestCase{
   105  		PreCheck:     func() { testAccPreCheck(t) },
   106  		Providers:    testAccProviders,
   107  		CheckDestroy: testAccCheckAWSELBDestroy,
   108  		Steps: []resource.TestStep{
   109  			{
   110  				Config: testAccESDomainConfig(ri),
   111  				Check: resource.ComposeTestCheckFunc(
   112  					testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain),
   113  				),
   114  			},
   115  
   116  			{
   117  				Config: testAccESDomainConfig_TagUpdate(ri),
   118  				Check: resource.ComposeTestCheckFunc(
   119  					testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain),
   120  					testAccLoadESTags(&domain, &td),
   121  					testAccCheckElasticsearchServiceTags(&td.TagList, "foo", "bar"),
   122  					testAccCheckElasticsearchServiceTags(&td.TagList, "new", "type"),
   123  				),
   124  			},
   125  		},
   126  	})
   127  }
   128  
   129  func testAccLoadESTags(conf *elasticsearch.ElasticsearchDomainStatus, td *elasticsearch.ListTagsOutput) resource.TestCheckFunc {
   130  	return func(s *terraform.State) error {
   131  		conn := testAccProvider.Meta().(*AWSClient).esconn
   132  
   133  		describe, err := conn.ListTags(&elasticsearch.ListTagsInput{
   134  			ARN: conf.ARN,
   135  		})
   136  
   137  		if err != nil {
   138  			return err
   139  		}
   140  		if len(describe.TagList) > 0 {
   141  			*td = *describe
   142  		}
   143  		return nil
   144  	}
   145  }
   146  
   147  func testAccCheckESDomainExists(n string, domain *elasticsearch.ElasticsearchDomainStatus) resource.TestCheckFunc {
   148  	return func(s *terraform.State) error {
   149  		rs, ok := s.RootModule().Resources[n]
   150  		if !ok {
   151  			return fmt.Errorf("Not found: %s", n)
   152  		}
   153  
   154  		if rs.Primary.ID == "" {
   155  			return fmt.Errorf("No ES Domain ID is set")
   156  		}
   157  
   158  		conn := testAccProvider.Meta().(*AWSClient).esconn
   159  		opts := &elasticsearch.DescribeElasticsearchDomainInput{
   160  			DomainName: aws.String(rs.Primary.Attributes["domain_name"]),
   161  		}
   162  
   163  		resp, err := conn.DescribeElasticsearchDomain(opts)
   164  		if err != nil {
   165  			return fmt.Errorf("Error describing domain: %s", err.Error())
   166  		}
   167  
   168  		*domain = *resp.DomainStatus
   169  
   170  		return nil
   171  	}
   172  }
   173  
   174  func testAccCheckESDomainDestroy(s *terraform.State) error {
   175  	for _, rs := range s.RootModule().Resources {
   176  		if rs.Type != "aws_elasticsearch_domain" {
   177  			continue
   178  		}
   179  
   180  		conn := testAccProvider.Meta().(*AWSClient).esconn
   181  		opts := &elasticsearch.DescribeElasticsearchDomainInput{
   182  			DomainName: aws.String(rs.Primary.Attributes["domain_name"]),
   183  		}
   184  
   185  		_, err := conn.DescribeElasticsearchDomain(opts)
   186  		// Verify the error is what we want
   187  		if err != nil {
   188  			if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "ResourceNotFoundException" {
   189  				continue
   190  			}
   191  			return err
   192  		}
   193  	}
   194  	return nil
   195  }
   196  
   197  func testAccESDomainConfig(randInt int) string {
   198  	return fmt.Sprintf(`
   199  resource "aws_elasticsearch_domain" "example" {
   200    domain_name = "tf-test-%d"
   201    ebs_options {
   202      ebs_enabled = true
   203      volume_size = 10
   204    }
   205  }
   206  `, randInt)
   207  }
   208  
   209  func testAccESDomainConfig_TagUpdate(randInt int) string {
   210  	return fmt.Sprintf(`
   211  resource "aws_elasticsearch_domain" "example" {
   212    domain_name = "tf-test-%d"
   213    ebs_options {
   214      ebs_enabled = true
   215      volume_size = 10
   216    }
   217  
   218    tags {
   219      foo = "bar"
   220      new = "type"
   221    }
   222  }
   223  `, randInt)
   224  }
   225  
   226  func testAccESDomainConfig_complex(randInt int) string {
   227  	return fmt.Sprintf(`
   228  resource "aws_elasticsearch_domain" "example" {
   229    domain_name = "tf-test-%d"
   230  
   231    advanced_options {
   232      "indices.fielddata.cache.size" = 80
   233    }
   234  
   235    ebs_options {
   236      ebs_enabled = false
   237    }
   238  
   239    cluster_config {
   240      instance_count = 2
   241      zone_awareness_enabled = true
   242      instance_type = "r3.large.elasticsearch"
   243    }
   244  
   245    snapshot_options {
   246      automated_snapshot_start_hour = 23
   247    }
   248  
   249    tags {
   250      bar = "complex"
   251    }
   252  }
   253  `, randInt)
   254  }
   255  
   256  func testAccESDomainConfigV23(randInt int) string {
   257  	return fmt.Sprintf(`
   258  resource "aws_elasticsearch_domain" "example" {
   259    domain_name = "tf-test-%d"
   260    ebs_options {
   261      ebs_enabled = true
   262      volume_size = 10
   263    }
   264    elasticsearch_version = "2.3"
   265  }
   266  `, randInt)
   267  }