github.com/odise/terraform@v0.6.9-0.20160401223921-f7d1e0390da7/builtin/providers/aws/resource_aws_elastic_beanstalk_configuration_template_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  	"github.com/aws/aws-sdk-go/service/elasticbeanstalk"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSBeanstalkConfigurationTemplate_basic(t *testing.T) {
    16  	var config elasticbeanstalk.ConfigurationSettingsDescription
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckBeanstalkConfigurationTemplateDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccBeanstalkConfigurationTemplateConfig,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckBeanstalkConfigurationTemplateExists("aws_elastic_beanstalk_configuration_template.tf_template", &config),
    27  				),
    28  			},
    29  		},
    30  	})
    31  }
    32  
    33  func TestAccAWSBeanstalkConfigurationTemplate_VPC(t *testing.T) {
    34  	var config elasticbeanstalk.ConfigurationSettingsDescription
    35  
    36  	resource.Test(t, resource.TestCase{
    37  		PreCheck:     func() { testAccPreCheck(t) },
    38  		Providers:    testAccProviders,
    39  		CheckDestroy: testAccCheckBeanstalkConfigurationTemplateDestroy,
    40  		Steps: []resource.TestStep{
    41  			resource.TestStep{
    42  				Config: testAccBeanstalkConfigurationTemplateConfig_VPC(acctest.RandString(5)),
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccCheckBeanstalkConfigurationTemplateExists("aws_elastic_beanstalk_configuration_template.tf_template", &config),
    45  				),
    46  			},
    47  		},
    48  	})
    49  }
    50  
    51  func testAccCheckBeanstalkConfigurationTemplateDestroy(s *terraform.State) error {
    52  	conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn
    53  
    54  	for _, rs := range s.RootModule().Resources {
    55  		if rs.Type != "aws_elastic_beanstalk_configuration_template" {
    56  			continue
    57  		}
    58  
    59  		// Try to find the Configuration Template
    60  		opts := elasticbeanstalk.DescribeConfigurationSettingsInput{
    61  			TemplateName:    aws.String(rs.Primary.ID),
    62  			ApplicationName: aws.String(rs.Primary.Attributes["application"]),
    63  		}
    64  		resp, err := conn.DescribeConfigurationSettings(&opts)
    65  		if err == nil {
    66  			if len(resp.ConfigurationSettings) > 0 {
    67  				return fmt.Errorf("Elastic Beanstalk Application still exists.")
    68  			}
    69  
    70  			return nil
    71  		}
    72  
    73  		// Verify the error is what we want
    74  		ec2err, ok := err.(awserr.Error)
    75  		if !ok {
    76  			return err
    77  		}
    78  
    79  		switch {
    80  		case ec2err.Code() == "InvalidBeanstalkConfigurationTemplateID.NotFound":
    81  			return nil
    82  		// This error can be returned when the beanstalk application no longer exists.
    83  		case ec2err.Code() == "InvalidParameterValue":
    84  			return nil
    85  		default:
    86  			return err
    87  		}
    88  	}
    89  
    90  	return nil
    91  }
    92  
    93  func testAccCheckBeanstalkConfigurationTemplateExists(n string, config *elasticbeanstalk.ConfigurationSettingsDescription) resource.TestCheckFunc {
    94  	return func(s *terraform.State) error {
    95  		conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn
    96  		rs, ok := s.RootModule().Resources[n]
    97  		if !ok {
    98  			return fmt.Errorf("Not found: %s", n)
    99  		}
   100  
   101  		if rs.Primary.ID == "" {
   102  			return fmt.Errorf("Elastic Beanstalk config ID is not set")
   103  		}
   104  
   105  		opts := elasticbeanstalk.DescribeConfigurationSettingsInput{
   106  			TemplateName:    aws.String(rs.Primary.ID),
   107  			ApplicationName: aws.String(rs.Primary.Attributes["application"]),
   108  		}
   109  		resp, err := conn.DescribeConfigurationSettings(&opts)
   110  		if err != nil {
   111  			return err
   112  		}
   113  		if len(resp.ConfigurationSettings) == 0 {
   114  			return fmt.Errorf("Elastic Beanstalk Configurations not found.")
   115  		}
   116  
   117  		*config = *resp.ConfigurationSettings[0]
   118  
   119  		return nil
   120  	}
   121  }
   122  
   123  const testAccBeanstalkConfigurationTemplateConfig = `
   124  resource "aws_elastic_beanstalk_application" "tftest" {
   125    name = "tf-test-name"
   126    description = "tf-test-desc"
   127  }
   128  
   129  resource "aws_elastic_beanstalk_configuration_template" "tf_template" {
   130    name = "tf-test-template-config"
   131    application = "${aws_elastic_beanstalk_application.tftest.name}"
   132    solution_stack_name = "64bit Amazon Linux running Python"
   133  }
   134  `
   135  
   136  func testAccBeanstalkConfigurationTemplateConfig_VPC(name string) string {
   137  	return fmt.Sprintf(`
   138  resource "aws_vpc" "tf_b_test" {
   139    cidr_block = "10.0.0.0/16"
   140  
   141    tags {
   142      Name = "beanstalk_crash"
   143    }
   144  }
   145  
   146  resource "aws_subnet" "main" {
   147    vpc_id     = "${aws_vpc.tf_b_test.id}"
   148    cidr_block = "10.0.0.0/24"
   149  
   150    tags {
   151      Name = "subnet-count-test"
   152    }
   153  }
   154  
   155  resource "aws_elastic_beanstalk_application" "tftest" {
   156    name        = "tf-test-%s"
   157    description = "tf-test-desc"
   158  }
   159  
   160  resource "aws_elastic_beanstalk_configuration_template" "tf_template" {
   161    name        = "tf-test-%s"
   162    application = "${aws_elastic_beanstalk_application.tftest.name}"
   163  
   164    solution_stack_name = "64bit Amazon Linux running Python"
   165  
   166    setting {
   167      namespace = "aws:ec2:vpc"
   168      name      = "VPCId"
   169      value     = "${aws_vpc.tf_b_test.id}"
   170    }
   171  
   172    setting {
   173      namespace = "aws:ec2:vpc"
   174      name      = "Subnets"
   175      value     = "${aws_subnet.main.id}"
   176    }
   177  }
   178  `, name, name)
   179  }