github.com/subuk/terraform@v0.6.14-0.20160317140351-de1567c2e732/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/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSBeanstalkConfigurationTemplate_basic(t *testing.T) { 15 var config elasticbeanstalk.ConfigurationSettingsDescription 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckBeanstalkConfigurationTemplateDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccBeanstalkConfigurationTemplateConfig, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckBeanstalkConfigurationTemplateExists("aws_elastic_beanstalk_configuration_template.tf_template", &config), 26 ), 27 }, 28 }, 29 }) 30 } 31 32 func testAccCheckBeanstalkConfigurationTemplateDestroy(s *terraform.State) error { 33 conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn 34 35 for _, rs := range s.RootModule().Resources { 36 if rs.Type != "aws_elastic_beanstalk_configuration_template" { 37 continue 38 } 39 40 // Try to find the Configuration Template 41 opts := elasticbeanstalk.DescribeConfigurationSettingsInput{ 42 TemplateName: aws.String(rs.Primary.ID), 43 ApplicationName: aws.String(rs.Primary.Attributes["application"]), 44 } 45 resp, err := conn.DescribeConfigurationSettings(&opts) 46 if err == nil { 47 if len(resp.ConfigurationSettings) > 0 { 48 return fmt.Errorf("Elastic Beanstalk Application still exists.") 49 } 50 51 return nil 52 } 53 54 // Verify the error is what we want 55 ec2err, ok := err.(awserr.Error) 56 if !ok { 57 return err 58 } 59 60 switch { 61 case ec2err.Code() == "InvalidBeanstalkConfigurationTemplateID.NotFound": 62 return nil 63 // This error can be returned when the beanstalk application no longer exists. 64 case ec2err.Code() == "InvalidParameterValue": 65 return nil 66 default: 67 return err 68 } 69 } 70 71 return nil 72 } 73 74 func testAccCheckBeanstalkConfigurationTemplateExists(n string, config *elasticbeanstalk.ConfigurationSettingsDescription) resource.TestCheckFunc { 75 return func(s *terraform.State) error { 76 conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn 77 rs, ok := s.RootModule().Resources[n] 78 if !ok { 79 return fmt.Errorf("Not found: %s", n) 80 } 81 82 if rs.Primary.ID == "" { 83 return fmt.Errorf("Elastic Beanstalk config ID is not set") 84 } 85 86 opts := elasticbeanstalk.DescribeConfigurationSettingsInput{ 87 TemplateName: aws.String(rs.Primary.ID), 88 ApplicationName: aws.String(rs.Primary.Attributes["application"]), 89 } 90 resp, err := conn.DescribeConfigurationSettings(&opts) 91 if err != nil { 92 return err 93 } 94 if len(resp.ConfigurationSettings) == 0 { 95 return fmt.Errorf("Elastic Beanstalk Configurations not found.") 96 } 97 98 *config = *resp.ConfigurationSettings[0] 99 100 return nil 101 } 102 } 103 104 const testAccBeanstalkConfigurationTemplateConfig = ` 105 resource "aws_elastic_beanstalk_application" "tftest" { 106 name = "tf-test-name" 107 description = "tf-test-desc" 108 } 109 110 #resource "aws_elastic_beanstalk_environment" "tfenvtest" { 111 # name = "tf-test-name" 112 # application = "${aws_elastic_beanstalk_application.tftest.name}" 113 # solution_stack_name = "64bit Amazon Linux 2015.03 v2.0.3 running Go 1.4" 114 #} 115 116 resource "aws_elastic_beanstalk_configuration_template" "tf_template" { 117 name = "tf-test-template-config" 118 application = "${aws_elastic_beanstalk_application.tftest.name}" 119 solution_stack_name = "64bit Amazon Linux 2015.09 v2.0.8 running Go 1.4" 120 } 121 `