github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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(acctest.RandString(5)), 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 func testAccBeanstalkConfigurationTemplateConfig(r string) string { 146 return fmt.Sprintf(` 147 resource "aws_elastic_beanstalk_application" "tftest" { 148 name = "tf-test-%s" 149 description = "tf-test-desc-%s" 150 } 151 152 resource "aws_elastic_beanstalk_configuration_template" "tf_template" { 153 name = "tf-test-template-config" 154 application = "${aws_elastic_beanstalk_application.tftest.name}" 155 solution_stack_name = "64bit Amazon Linux running Python" 156 }`, r, r) 157 } 158 159 func testAccBeanstalkConfigurationTemplateConfig_VPC(name string) string { 160 return fmt.Sprintf(` 161 resource "aws_vpc" "tf_b_test" { 162 cidr_block = "10.0.0.0/16" 163 164 tags { 165 Name = "beanstalk_crash" 166 } 167 } 168 169 resource "aws_subnet" "main" { 170 vpc_id = "${aws_vpc.tf_b_test.id}" 171 cidr_block = "10.0.0.0/24" 172 173 tags { 174 Name = "subnet-count-test" 175 } 176 } 177 178 resource "aws_elastic_beanstalk_application" "tftest" { 179 name = "tf-test-%s" 180 description = "tf-test-desc" 181 } 182 183 resource "aws_elastic_beanstalk_configuration_template" "tf_template" { 184 name = "tf-test-%s" 185 application = "${aws_elastic_beanstalk_application.tftest.name}" 186 187 solution_stack_name = "64bit Amazon Linux running Python" 188 189 setting { 190 namespace = "aws:ec2:vpc" 191 name = "VPCId" 192 value = "${aws_vpc.tf_b_test.id}" 193 } 194 195 setting { 196 namespace = "aws:ec2:vpc" 197 name = "Subnets" 198 value = "${aws_subnet.main.id}" 199 } 200 } 201 `, name, name) 202 } 203 204 func testAccBeanstalkConfigurationTemplateConfig_Setting(name string) string { 205 return fmt.Sprintf(` 206 resource "aws_elastic_beanstalk_application" "tftest" { 207 name = "tf-test-%s" 208 description = "tf-test-desc" 209 } 210 211 resource "aws_elastic_beanstalk_configuration_template" "tf_template" { 212 name = "tf-test-%s" 213 application = "${aws_elastic_beanstalk_application.tftest.name}" 214 215 solution_stack_name = "64bit Amazon Linux running Python" 216 217 setting { 218 namespace = "aws:autoscaling:launchconfiguration" 219 name = "InstanceType" 220 value = "m1.small" 221 } 222 223 } 224 `, name, name) 225 }