github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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  	elasticsearch "github.com/aws/aws-sdk-go/service/elasticsearchservice"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSElasticSearchDomain_basic(t *testing.T) {
    14  	var domain elasticsearch.ElasticsearchDomainStatus
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckESDomainDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccESDomainConfig_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func TestAccAWSElasticSearchDomain_complex(t *testing.T) {
    32  	var domain elasticsearch.ElasticsearchDomainStatus
    33  
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testAccCheckESDomainDestroy,
    38  		Steps: []resource.TestStep{
    39  			resource.TestStep{
    40  				Config: testAccESDomainConfig_complex,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckESDomainExists("aws_elasticsearch_domain.example", &domain),
    43  				),
    44  			},
    45  		},
    46  	})
    47  }
    48  
    49  func testAccCheckESDomainExists(n string, domain *elasticsearch.ElasticsearchDomainStatus) resource.TestCheckFunc {
    50  	return func(s *terraform.State) error {
    51  		rs, ok := s.RootModule().Resources[n]
    52  		if !ok {
    53  			return fmt.Errorf("Not found: %s", n)
    54  		}
    55  
    56  		if rs.Primary.ID == "" {
    57  			return fmt.Errorf("No ES Domain ID is set")
    58  		}
    59  
    60  		conn := testAccProvider.Meta().(*AWSClient).esconn
    61  		opts := &elasticsearch.DescribeElasticsearchDomainInput{
    62  			DomainName: aws.String(rs.Primary.Attributes["domain_name"]),
    63  		}
    64  
    65  		resp, err := conn.DescribeElasticsearchDomain(opts)
    66  		if err != nil {
    67  			return fmt.Errorf("Error describing domain: %s", err.Error())
    68  		}
    69  
    70  		*domain = *resp.DomainStatus
    71  
    72  		return nil
    73  	}
    74  }
    75  
    76  func testAccCheckESDomainDestroy(s *terraform.State) error {
    77  	for _, rs := range s.RootModule().Resources {
    78  		if rs.Type != "aws_elasticsearch_domain" {
    79  			continue
    80  		}
    81  
    82  		conn := testAccProvider.Meta().(*AWSClient).esconn
    83  		opts := &elasticsearch.DescribeElasticsearchDomainInput{
    84  			DomainName: aws.String(rs.Primary.Attributes["domain_name"]),
    85  		}
    86  
    87  		_, err := conn.DescribeElasticsearchDomain(opts)
    88  		if err != nil {
    89  			return fmt.Errorf("Error describing ES domains: %q", err.Error())
    90  		}
    91  	}
    92  	return nil
    93  }
    94  
    95  const testAccESDomainConfig_basic = `
    96  resource "aws_elasticsearch_domain" "example" {
    97  	domain_name = "tf-test-1"
    98  }
    99  `
   100  
   101  const testAccESDomainConfig_complex = `
   102  resource "aws_elasticsearch_domain" "example" {
   103  	domain_name = "tf-test-2"
   104  
   105  	advanced_options {
   106  		"indices.fielddata.cache.size" = 80
   107  	}
   108  
   109  	ebs_options {
   110  		ebs_enabled = false
   111  	}
   112  
   113  	cluster_config {
   114  		instance_count = 2
   115  		zone_awareness_enabled = true
   116  	}
   117  
   118  	snapshot_options {
   119  		automated_snapshot_start_hour = 23
   120  	}
   121  }
   122  `