github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  			resource.TestStep{
    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_v23(t *testing.T) {
    37  	var domain elasticsearch.ElasticsearchDomainStatus
    38  	ri := acctest.RandInt()
    39  
    40  	resource.Test(t, resource.TestCase{
    41  		PreCheck:     func() { testAccPreCheck(t) },
    42  		Providers:    testAccProviders,
    43  		CheckDestroy: testAccCheckESDomainDestroy,
    44  		Steps: []resource.TestStep{
    45  			resource.TestStep{
    46  				Config: testAccESDomainConfigV23(ri),
    47  				Check: resource.ComposeTestCheckFunc(
    48  					testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain),
    49  					resource.TestCheckResourceAttr(
    50  						"aws_elasticsearch_domain.example", "elasticsearch_version", "2.3"),
    51  				),
    52  			},
    53  		},
    54  	})
    55  }
    56  
    57  func TestAccAWSElasticSearchDomain_complex(t *testing.T) {
    58  	var domain elasticsearch.ElasticsearchDomainStatus
    59  	ri := acctest.RandInt()
    60  
    61  	resource.Test(t, resource.TestCase{
    62  		PreCheck:     func() { testAccPreCheck(t) },
    63  		Providers:    testAccProviders,
    64  		CheckDestroy: testAccCheckESDomainDestroy,
    65  		Steps: []resource.TestStep{
    66  			resource.TestStep{
    67  				Config: testAccESDomainConfig_complex(ri),
    68  				Check: resource.ComposeTestCheckFunc(
    69  					testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain),
    70  				),
    71  			},
    72  		},
    73  	})
    74  }
    75  
    76  func TestAccAWSElasticSearch_tags(t *testing.T) {
    77  	var domain elasticsearch.ElasticsearchDomainStatus
    78  	var td elasticsearch.ListTagsOutput
    79  	ri := acctest.RandInt()
    80  
    81  	resource.Test(t, resource.TestCase{
    82  		PreCheck:     func() { testAccPreCheck(t) },
    83  		Providers:    testAccProviders,
    84  		CheckDestroy: testAccCheckAWSELBDestroy,
    85  		Steps: []resource.TestStep{
    86  			resource.TestStep{
    87  				Config: testAccESDomainConfig(ri),
    88  				Check: resource.ComposeTestCheckFunc(
    89  					testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain),
    90  				),
    91  			},
    92  
    93  			resource.TestStep{
    94  				Config: testAccESDomainConfig_TagUpdate(ri),
    95  				Check: resource.ComposeTestCheckFunc(
    96  					testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain),
    97  					testAccLoadESTags(&domain, &td),
    98  					testAccCheckElasticsearchServiceTags(&td.TagList, "foo", "bar"),
    99  					testAccCheckElasticsearchServiceTags(&td.TagList, "new", "type"),
   100  				),
   101  			},
   102  		},
   103  	})
   104  }
   105  
   106  func testAccLoadESTags(conf *elasticsearch.ElasticsearchDomainStatus, td *elasticsearch.ListTagsOutput) resource.TestCheckFunc {
   107  	return func(s *terraform.State) error {
   108  		conn := testAccProvider.Meta().(*AWSClient).esconn
   109  
   110  		describe, err := conn.ListTags(&elasticsearch.ListTagsInput{
   111  			ARN: conf.ARN,
   112  		})
   113  
   114  		if err != nil {
   115  			return err
   116  		}
   117  		if len(describe.TagList) > 0 {
   118  			*td = *describe
   119  		}
   120  		return nil
   121  	}
   122  }
   123  
   124  func testAccCheckESDomainExists(n string, domain *elasticsearch.ElasticsearchDomainStatus) resource.TestCheckFunc {
   125  	return func(s *terraform.State) error {
   126  		rs, ok := s.RootModule().Resources[n]
   127  		if !ok {
   128  			return fmt.Errorf("Not found: %s", n)
   129  		}
   130  
   131  		if rs.Primary.ID == "" {
   132  			return fmt.Errorf("No ES Domain ID is set")
   133  		}
   134  
   135  		conn := testAccProvider.Meta().(*AWSClient).esconn
   136  		opts := &elasticsearch.DescribeElasticsearchDomainInput{
   137  			DomainName: aws.String(rs.Primary.Attributes["domain_name"]),
   138  		}
   139  
   140  		resp, err := conn.DescribeElasticsearchDomain(opts)
   141  		if err != nil {
   142  			return fmt.Errorf("Error describing domain: %s", err.Error())
   143  		}
   144  
   145  		*domain = *resp.DomainStatus
   146  
   147  		return nil
   148  	}
   149  }
   150  
   151  func testAccCheckESDomainDestroy(s *terraform.State) error {
   152  	for _, rs := range s.RootModule().Resources {
   153  		if rs.Type != "aws_elasticsearch_domain" {
   154  			continue
   155  		}
   156  
   157  		conn := testAccProvider.Meta().(*AWSClient).esconn
   158  		opts := &elasticsearch.DescribeElasticsearchDomainInput{
   159  			DomainName: aws.String(rs.Primary.Attributes["domain_name"]),
   160  		}
   161  
   162  		_, err := conn.DescribeElasticsearchDomain(opts)
   163  		// Verify the error is what we want
   164  		if err != nil {
   165  			if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "ResourceNotFoundException" {
   166  				continue
   167  			}
   168  			return err
   169  		}
   170  	}
   171  	return nil
   172  }
   173  
   174  func testAccESDomainConfig(randInt int) string {
   175  	return fmt.Sprintf(`
   176  resource "aws_elasticsearch_domain" "example" {
   177    domain_name = "tf-test-%d"
   178  }
   179  `, randInt)
   180  }
   181  
   182  func testAccESDomainConfig_TagUpdate(randInt int) string {
   183  	return fmt.Sprintf(`
   184  resource "aws_elasticsearch_domain" "example" {
   185    domain_name = "tf-test-%d"
   186  
   187    tags {
   188      foo = "bar"
   189      new = "type"
   190    }
   191  }
   192  `, randInt)
   193  }
   194  
   195  func testAccESDomainConfig_complex(randInt int) string {
   196  	return fmt.Sprintf(`
   197  resource "aws_elasticsearch_domain" "example" {
   198    domain_name = "tf-test-%d"
   199  
   200    advanced_options {
   201      "indices.fielddata.cache.size" = 80
   202    }
   203  
   204    ebs_options {
   205      ebs_enabled = false
   206    }
   207  
   208    cluster_config {
   209      instance_count = 2
   210      zone_awareness_enabled = true
   211    }
   212  
   213    snapshot_options {
   214      automated_snapshot_start_hour = 23
   215    }
   216  
   217    tags {
   218      bar = "complex"
   219    }
   220  }
   221  `, randInt)
   222  }
   223  
   224  func testAccESDomainConfigV23(randInt int) string {
   225  	return fmt.Sprintf(`
   226  resource "aws_elasticsearch_domain" "example" {
   227    domain_name = "tf-test-%d"
   228    elasticsearch_version = "2.3"
   229  }
   230  `, randInt)
   231  }