github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 TestAccAWSElasticSearch_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  }
   202  `, randInt)
   203  }
   204  
   205  func testAccESDomainConfig_TagUpdate(randInt int) string {
   206  	return fmt.Sprintf(`
   207  resource "aws_elasticsearch_domain" "example" {
   208    domain_name = "tf-test-%d"
   209  
   210    tags {
   211      foo = "bar"
   212      new = "type"
   213    }
   214  }
   215  `, randInt)
   216  }
   217  
   218  func testAccESDomainConfig_complex(randInt int) string {
   219  	return fmt.Sprintf(`
   220  resource "aws_elasticsearch_domain" "example" {
   221    domain_name = "tf-test-%d"
   222  
   223    advanced_options {
   224      "indices.fielddata.cache.size" = 80
   225    }
   226  
   227    ebs_options {
   228      ebs_enabled = false
   229    }
   230  
   231    cluster_config {
   232      instance_count = 2
   233      zone_awareness_enabled = true
   234    }
   235  
   236    snapshot_options {
   237      automated_snapshot_start_hour = 23
   238    }
   239  
   240    tags {
   241      bar = "complex"
   242    }
   243  }
   244  `, randInt)
   245  }
   246  
   247  func testAccESDomainConfigV23(randInt int) string {
   248  	return fmt.Sprintf(`
   249  resource "aws_elasticsearch_domain" "example" {
   250    domain_name = "tf-test-%d"
   251    elasticsearch_version = "2.3"
   252  }
   253  `, randInt)
   254  }