github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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 TestAccAWSBeanstalkConfigurationTemplate_Setting(t *testing.T) {
    52  	var config elasticbeanstalk.ConfigurationSettingsDescription
    53  
    54  	resource.Test(t, resource.TestCase{
    55  		PreCheck:     func() { testAccPreCheck(t) },
    56  		Providers:    testAccProviders,
    57  		CheckDestroy: testAccCheckBeanstalkConfigurationTemplateDestroy,
    58  		Steps: []resource.TestStep{
    59  			resource.TestStep{
    60  				Config: testAccBeanstalkConfigurationTemplateConfig_Setting(acctest.RandString(5)),
    61  				Check: resource.ComposeTestCheckFunc(
    62  					testAccCheckBeanstalkConfigurationTemplateExists("aws_elastic_beanstalk_configuration_template.tf_template", &config),
    63  					resource.TestCheckResourceAttr(
    64  						"aws_elastic_beanstalk_configuration_template.tf_template", "setting.#", "1"),
    65  					resource.TestCheckResourceAttr(
    66  						"aws_elastic_beanstalk_configuration_template.tf_template", "setting.4112217815.value", "m1.small"),
    67  				),
    68  			},
    69  		},
    70  	})
    71  }
    72  
    73  func testAccCheckBeanstalkConfigurationTemplateDestroy(s *terraform.State) error {
    74  	conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn
    75  
    76  	for _, rs := range s.RootModule().Resources {
    77  		if rs.Type != "aws_elastic_beanstalk_configuration_template" {
    78  			continue
    79  		}
    80  
    81  		// Try to find the Configuration Template
    82  		opts := elasticbeanstalk.DescribeConfigurationSettingsInput{
    83  			TemplateName:    aws.String(rs.Primary.ID),
    84  			ApplicationName: aws.String(rs.Primary.Attributes["application"]),
    85  		}
    86  		resp, err := conn.DescribeConfigurationSettings(&opts)
    87  		if err == nil {
    88  			if len(resp.ConfigurationSettings) > 0 {
    89  				return fmt.Errorf("Elastic Beanstalk Application still exists.")
    90  			}
    91  
    92  			return nil
    93  		}
    94  
    95  		// Verify the error is what we want
    96  		ec2err, ok := err.(awserr.Error)
    97  		if !ok {
    98  			return err
    99  		}
   100  
   101  		switch {
   102  		case ec2err.Code() == "InvalidBeanstalkConfigurationTemplateID.NotFound":
   103  			return nil
   104  		// This error can be returned when the beanstalk application no longer exists.
   105  		case ec2err.Code() == "InvalidParameterValue":
   106  			return nil
   107  		default:
   108  			return err
   109  		}
   110  	}
   111  
   112  	return nil
   113  }
   114  
   115  func testAccCheckBeanstalkConfigurationTemplateExists(n string, config *elasticbeanstalk.ConfigurationSettingsDescription) resource.TestCheckFunc {
   116  	return func(s *terraform.State) error {
   117  		conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn
   118  		rs, ok := s.RootModule().Resources[n]
   119  		if !ok {
   120  			return fmt.Errorf("Not found: %s", n)
   121  		}
   122  
   123  		if rs.Primary.ID == "" {
   124  			return fmt.Errorf("Elastic Beanstalk config ID is not set")
   125  		}
   126  
   127  		opts := elasticbeanstalk.DescribeConfigurationSettingsInput{
   128  			TemplateName:    aws.String(rs.Primary.ID),
   129  			ApplicationName: aws.String(rs.Primary.Attributes["application"]),
   130  		}
   131  		resp, err := conn.DescribeConfigurationSettings(&opts)
   132  		if err != nil {
   133  			return err
   134  		}
   135  		if len(resp.ConfigurationSettings) == 0 {
   136  			return fmt.Errorf("Elastic Beanstalk Configurations not found.")
   137  		}
   138  
   139  		*config = *resp.ConfigurationSettings[0]
   140  
   141  		return nil
   142  	}
   143  }
   144  
   145  const testAccBeanstalkConfigurationTemplateConfig = `
   146  resource "aws_elastic_beanstalk_application" "tftest" {
   147    name = "tf-test-name"
   148    description = "tf-test-desc"
   149  }
   150  
   151  resource "aws_elastic_beanstalk_configuration_template" "tf_template" {
   152    name = "tf-test-template-config"
   153    application = "${aws_elastic_beanstalk_application.tftest.name}"
   154    solution_stack_name = "64bit Amazon Linux running Python"
   155  }
   156  `
   157  
   158  func testAccBeanstalkConfigurationTemplateConfig_VPC(name string) string {
   159  	return fmt.Sprintf(`
   160  resource "aws_vpc" "tf_b_test" {
   161    cidr_block = "10.0.0.0/16"
   162  
   163    tags {
   164      Name = "beanstalk_crash"
   165    }
   166  }
   167  
   168  resource "aws_subnet" "main" {
   169    vpc_id     = "${aws_vpc.tf_b_test.id}"
   170    cidr_block = "10.0.0.0/24"
   171  
   172    tags {
   173      Name = "subnet-count-test"
   174    }
   175  }
   176  
   177  resource "aws_elastic_beanstalk_application" "tftest" {
   178    name        = "tf-test-%s"
   179    description = "tf-test-desc"
   180  }
   181  
   182  resource "aws_elastic_beanstalk_configuration_template" "tf_template" {
   183    name        = "tf-test-%s"
   184    application = "${aws_elastic_beanstalk_application.tftest.name}"
   185  
   186    solution_stack_name = "64bit Amazon Linux running Python"
   187  
   188    setting {
   189      namespace = "aws:ec2:vpc"
   190      name      = "VPCId"
   191      value     = "${aws_vpc.tf_b_test.id}"
   192    }
   193  
   194    setting {
   195      namespace = "aws:ec2:vpc"
   196      name      = "Subnets"
   197      value     = "${aws_subnet.main.id}"
   198    }
   199  }
   200  `, name, name)
   201  }
   202  
   203  func testAccBeanstalkConfigurationTemplateConfig_Setting(name string) string {
   204  	return fmt.Sprintf(`
   205  resource "aws_elastic_beanstalk_application" "tftest" {
   206    name        = "tf-test-%s"
   207    description = "tf-test-desc"
   208  }
   209  
   210  resource "aws_elastic_beanstalk_configuration_template" "tf_template" {
   211    name        = "tf-test-%s"
   212    application = "${aws_elastic_beanstalk_application.tftest.name}"
   213  
   214    solution_stack_name = "64bit Amazon Linux running Python"
   215  
   216    setting {
   217      namespace = "aws:autoscaling:launchconfiguration"
   218      name      = "InstanceType"
   219      value     = "m1.small"
   220    }
   221  
   222  }
   223  `, name, name)
   224  }