github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_elastic_beanstalk_environment_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"reflect"
     7  	"regexp"
     8  	"sort"
     9  	"testing"
    10  
    11  	"github.com/aws/aws-sdk-go/aws"
    12  	"github.com/aws/aws-sdk-go/aws/awserr"
    13  	"github.com/aws/aws-sdk-go/service/elasticbeanstalk"
    14  	"github.com/hashicorp/terraform/helper/acctest"
    15  	"github.com/hashicorp/terraform/helper/resource"
    16  	"github.com/hashicorp/terraform/terraform"
    17  )
    18  
    19  func TestAccAWSBeanstalkEnv_basic(t *testing.T) {
    20  	var app elasticbeanstalk.EnvironmentDescription
    21  	rInt := acctest.RandInt()
    22  
    23  	resource.Test(t, resource.TestCase{
    24  		PreCheck:     func() { testAccPreCheck(t) },
    25  		Providers:    testAccProviders,
    26  		CheckDestroy: testAccCheckBeanstalkEnvDestroy,
    27  		Steps: []resource.TestStep{
    28  			{
    29  				Config: testAccBeanstalkEnvConfig(rInt),
    30  				Check: resource.ComposeTestCheckFunc(
    31  					testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tfenvtest", &app),
    32  				),
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func TestAccAWSBeanstalkEnv_tier(t *testing.T) {
    39  	var app elasticbeanstalk.EnvironmentDescription
    40  	beanstalkQueuesNameRegexp := regexp.MustCompile("https://sqs.+?awseb[^,]+")
    41  	rInt := acctest.RandInt()
    42  
    43  	resource.Test(t, resource.TestCase{
    44  		PreCheck:     func() { testAccPreCheck(t) },
    45  		Providers:    testAccProviders,
    46  		CheckDestroy: testAccCheckBeanstalkEnvDestroy,
    47  		Steps: []resource.TestStep{
    48  			{
    49  				Config: testAccBeanstalkWorkerEnvConfig(rInt),
    50  				Check: resource.ComposeTestCheckFunc(
    51  					testAccCheckBeanstalkEnvTier("aws_elastic_beanstalk_environment.tfenvtest", &app),
    52  					resource.TestMatchResourceAttr(
    53  						"aws_elastic_beanstalk_environment.tfenvtest", "queues.0", beanstalkQueuesNameRegexp),
    54  				),
    55  			},
    56  		},
    57  	})
    58  }
    59  
    60  func TestAccAWSBeanstalkEnv_outputs(t *testing.T) {
    61  	var app elasticbeanstalk.EnvironmentDescription
    62  	rInt := acctest.RandInt()
    63  	beanstalkAsgNameRegexp := regexp.MustCompile("awseb.+?AutoScalingGroup[^,]+")
    64  	beanstalkElbNameRegexp := regexp.MustCompile("awseb.+?EBLoa[^,]+")
    65  	beanstalkInstancesNameRegexp := regexp.MustCompile("i-([0-9a-fA-F]{8}|[0-9a-fA-F]{17})")
    66  	beanstalkLcNameRegexp := regexp.MustCompile("awseb.+?AutoScalingLaunch[^,]+")
    67  
    68  	resource.Test(t, resource.TestCase{
    69  		PreCheck:     func() { testAccPreCheck(t) },
    70  		Providers:    testAccProviders,
    71  		CheckDestroy: testAccCheckBeanstalkEnvDestroy,
    72  		Steps: []resource.TestStep{
    73  			{
    74  				Config: testAccBeanstalkEnvConfig(rInt),
    75  				Check: resource.ComposeTestCheckFunc(
    76  					testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tfenvtest", &app),
    77  					resource.TestMatchResourceAttr(
    78  						"aws_elastic_beanstalk_environment.tfenvtest", "autoscaling_groups.0", beanstalkAsgNameRegexp),
    79  					resource.TestMatchResourceAttr(
    80  						"aws_elastic_beanstalk_environment.tfenvtest", "load_balancers.0", beanstalkElbNameRegexp),
    81  					resource.TestMatchResourceAttr(
    82  						"aws_elastic_beanstalk_environment.tfenvtest", "instances.0", beanstalkInstancesNameRegexp),
    83  					resource.TestMatchResourceAttr(
    84  						"aws_elastic_beanstalk_environment.tfenvtest", "launch_configurations.0", beanstalkLcNameRegexp),
    85  				),
    86  			},
    87  		},
    88  	})
    89  }
    90  
    91  func TestAccAWSBeanstalkEnv_cname_prefix(t *testing.T) {
    92  	var app elasticbeanstalk.EnvironmentDescription
    93  	cnamePrefix := acctest.RandString(8)
    94  	rInt := acctest.RandInt()
    95  	beanstalkCnameRegexp := regexp.MustCompile("^" + cnamePrefix + ".+?elasticbeanstalk.com$")
    96  
    97  	resource.Test(t, resource.TestCase{
    98  		PreCheck:     func() { testAccPreCheck(t) },
    99  		Providers:    testAccProviders,
   100  		CheckDestroy: testAccCheckBeanstalkEnvDestroy,
   101  		Steps: []resource.TestStep{
   102  			{
   103  				Config: testAccBeanstalkEnvCnamePrefixConfig(cnamePrefix, rInt),
   104  				Check: resource.ComposeTestCheckFunc(
   105  					testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tfenvtest", &app),
   106  					resource.TestMatchResourceAttr(
   107  						"aws_elastic_beanstalk_environment.tfenvtest", "cname", beanstalkCnameRegexp),
   108  				),
   109  			},
   110  		},
   111  	})
   112  }
   113  
   114  func TestAccAWSBeanstalkEnv_config(t *testing.T) {
   115  	var app elasticbeanstalk.EnvironmentDescription
   116  	rInt := acctest.RandInt()
   117  
   118  	resource.Test(t, resource.TestCase{
   119  		PreCheck:     func() { testAccPreCheck(t) },
   120  		Providers:    testAccProviders,
   121  		CheckDestroy: testAccCheckBeanstalkEnvDestroy,
   122  		Steps: []resource.TestStep{
   123  			{
   124  				Config: testAccBeanstalkConfigTemplate(rInt),
   125  				Check: resource.ComposeTestCheckFunc(
   126  					testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tftest", &app),
   127  					testAccCheckBeanstalkEnvConfigValue("aws_elastic_beanstalk_environment.tftest", "1"),
   128  				),
   129  			},
   130  
   131  			{
   132  				Config: testAccBeanstalkConfigTemplateUpdate(rInt),
   133  				Check: resource.ComposeTestCheckFunc(
   134  					testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tftest", &app),
   135  					testAccCheckBeanstalkEnvConfigValue("aws_elastic_beanstalk_environment.tftest", "2"),
   136  				),
   137  			},
   138  
   139  			{
   140  				Config: testAccBeanstalkConfigTemplateUpdate(rInt),
   141  				Check: resource.ComposeTestCheckFunc(
   142  					testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tftest", &app),
   143  					testAccCheckBeanstalkEnvConfigValue("aws_elastic_beanstalk_environment.tftest", "3"),
   144  				),
   145  			},
   146  		},
   147  	})
   148  }
   149  
   150  func TestAccAWSBeanstalkEnv_resource(t *testing.T) {
   151  	var app elasticbeanstalk.EnvironmentDescription
   152  	rInt := acctest.RandInt()
   153  
   154  	resource.Test(t, resource.TestCase{
   155  		PreCheck:     func() { testAccPreCheck(t) },
   156  		Providers:    testAccProviders,
   157  		CheckDestroy: testAccCheckBeanstalkEnvDestroy,
   158  		Steps: []resource.TestStep{
   159  			{
   160  				Config: testAccBeanstalkResourceOptionSetting(rInt),
   161  				Check: resource.ComposeTestCheckFunc(
   162  					testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tfenvtest", &app),
   163  				),
   164  			},
   165  		},
   166  	})
   167  }
   168  
   169  func TestAccAWSBeanstalkEnv_vpc(t *testing.T) {
   170  	var app elasticbeanstalk.EnvironmentDescription
   171  	rInt := acctest.RandInt()
   172  
   173  	resource.Test(t, resource.TestCase{
   174  		PreCheck: func() {
   175  			testAccPreCheck(t)
   176  		},
   177  		Providers:    testAccProviders,
   178  		CheckDestroy: testAccCheckBeanstalkEnvDestroy,
   179  		Steps: []resource.TestStep{
   180  			{
   181  				Config: testAccBeanstalkEnv_VPC(acctest.RandString(5), rInt),
   182  				Check: resource.ComposeTestCheckFunc(
   183  					testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.default", &app),
   184  				),
   185  			},
   186  		},
   187  	})
   188  }
   189  
   190  func TestAccAWSBeanstalkEnv_template_change(t *testing.T) {
   191  	var app elasticbeanstalk.EnvironmentDescription
   192  
   193  	rInt := acctest.RandInt()
   194  
   195  	resource.Test(t, resource.TestCase{
   196  		PreCheck: func() {
   197  			testAccPreCheck(t)
   198  		},
   199  		Providers:    testAccProviders,
   200  		CheckDestroy: testAccCheckBeanstalkEnvDestroy,
   201  		Steps: []resource.TestStep{
   202  			{
   203  				Config: testAccBeanstalkEnv_TemplateChange_stack(rInt),
   204  				Check: resource.ComposeTestCheckFunc(
   205  					testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.environment", &app),
   206  				),
   207  			},
   208  			{
   209  				Config: testAccBeanstalkEnv_TemplateChange_temp(rInt),
   210  				Check: resource.ComposeTestCheckFunc(
   211  					testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.environment", &app),
   212  				),
   213  			},
   214  			{
   215  				Config: testAccBeanstalkEnv_TemplateChange_stack(rInt),
   216  				Check: resource.ComposeTestCheckFunc(
   217  					testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.environment", &app),
   218  				),
   219  			},
   220  		},
   221  	})
   222  }
   223  
   224  func TestAccAWSBeanstalkEnv_basic_settings_update(t *testing.T) {
   225  	var app elasticbeanstalk.EnvironmentDescription
   226  
   227  	rInt := acctest.RandInt()
   228  
   229  	resource.Test(t, resource.TestCase{
   230  		PreCheck:     func() { testAccPreCheck(t) },
   231  		Providers:    testAccProviders,
   232  		CheckDestroy: testAccCheckBeanstalkEnvDestroy,
   233  		Steps: []resource.TestStep{
   234  			{
   235  				Config: testAccBeanstalkEnvConfig_empty_settings(rInt),
   236  				Check: resource.ComposeTestCheckFunc(
   237  					testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tfenvtest", &app),
   238  					testAccVerifyBeanstalkConfig(&app, []string{}),
   239  				),
   240  			},
   241  			{
   242  				Config: testAccBeanstalkEnvConfig_settings(rInt),
   243  				Check: resource.ComposeTestCheckFunc(
   244  					testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tfenvtest", &app),
   245  					testAccVerifyBeanstalkConfig(&app, []string{"ENV_STATIC", "ENV_UPDATE"}),
   246  				),
   247  			},
   248  			{
   249  				Config: testAccBeanstalkEnvConfig_settings_update(rInt),
   250  				Check: resource.ComposeTestCheckFunc(
   251  					testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tfenvtest", &app),
   252  					testAccVerifyBeanstalkConfig(&app, []string{"ENV_STATIC", "ENV_UPDATE"}),
   253  				),
   254  			},
   255  			{
   256  				Config: testAccBeanstalkEnvConfig_empty_settings(rInt),
   257  				Check: resource.ComposeTestCheckFunc(
   258  					testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tfenvtest", &app),
   259  					testAccVerifyBeanstalkConfig(&app, []string{}),
   260  				),
   261  			},
   262  		},
   263  	})
   264  }
   265  
   266  func TestAccAWSBeanstalkEnv_version_label(t *testing.T) {
   267  	var app elasticbeanstalk.EnvironmentDescription
   268  
   269  	resource.Test(t, resource.TestCase{
   270  		PreCheck:     func() { testAccPreCheck(t) },
   271  		Providers:    testAccProviders,
   272  		CheckDestroy: testAccCheckBeanstalkEnvDestroy,
   273  		Steps: []resource.TestStep{
   274  			resource.TestStep{
   275  				Config: testAccBeanstalkEnvApplicationVersionConfig(acctest.RandInt()),
   276  				Check: resource.ComposeTestCheckFunc(
   277  					testAccCheckBeanstalkApplicationVersionDeployed("aws_elastic_beanstalk_environment.default", &app),
   278  				),
   279  			},
   280  			resource.TestStep{
   281  				Config: testAccBeanstalkEnvApplicationVersionConfigUpdate(acctest.RandInt()),
   282  				Check: resource.ComposeTestCheckFunc(
   283  					testAccCheckBeanstalkApplicationVersionDeployed("aws_elastic_beanstalk_environment.default", &app),
   284  				),
   285  			},
   286  		},
   287  	})
   288  }
   289  
   290  func testAccVerifyBeanstalkConfig(env *elasticbeanstalk.EnvironmentDescription, expected []string) resource.TestCheckFunc {
   291  	return func(s *terraform.State) error {
   292  		if env == nil {
   293  			return fmt.Errorf("Nil environment in testAccVerifyBeanstalkConfig")
   294  		}
   295  		conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn
   296  
   297  		resp, err := conn.DescribeConfigurationSettings(&elasticbeanstalk.DescribeConfigurationSettingsInput{
   298  			ApplicationName: env.ApplicationName,
   299  			EnvironmentName: env.EnvironmentName,
   300  		})
   301  
   302  		if err != nil {
   303  			return fmt.Errorf("Error describing config settings in testAccVerifyBeanstalkConfig: %s", err)
   304  		}
   305  
   306  		// should only be 1 environment
   307  		if len(resp.ConfigurationSettings) != 1 {
   308  			return fmt.Errorf("Expected only 1 set of Configuration Settings in testAccVerifyBeanstalkConfig, got (%d)", len(resp.ConfigurationSettings))
   309  		}
   310  
   311  		cs := resp.ConfigurationSettings[0]
   312  
   313  		var foundEnvs []string
   314  		testStrings := []string{"ENV_STATIC", "ENV_UPDATE"}
   315  		for _, os := range cs.OptionSettings {
   316  			for _, k := range testStrings {
   317  				if *os.OptionName == k {
   318  					foundEnvs = append(foundEnvs, k)
   319  				}
   320  			}
   321  		}
   322  
   323  		// if expected is zero, then we should not have found any of the predefined
   324  		// env vars
   325  		if len(expected) == 0 {
   326  			if len(foundEnvs) > 0 {
   327  				return fmt.Errorf("Found configs we should not have: %#v", foundEnvs)
   328  			}
   329  			return nil
   330  		}
   331  
   332  		sort.Strings(testStrings)
   333  		sort.Strings(expected)
   334  		if !reflect.DeepEqual(testStrings, expected) {
   335  			return fmt.Errorf("Error matching strings, expected:\n\n%#v\n\ngot:\n\n%#v\n", testStrings, foundEnvs)
   336  		}
   337  
   338  		return nil
   339  	}
   340  }
   341  
   342  func testAccCheckBeanstalkEnvDestroy(s *terraform.State) error {
   343  	conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn
   344  
   345  	for _, rs := range s.RootModule().Resources {
   346  		if rs.Type != "aws_elastic_beanstalk_environment" {
   347  			continue
   348  		}
   349  
   350  		// Try to find the environment
   351  		describeBeanstalkEnvOpts := &elasticbeanstalk.DescribeEnvironmentsInput{
   352  			EnvironmentIds: []*string{aws.String(rs.Primary.ID)},
   353  		}
   354  		resp, err := conn.DescribeEnvironments(describeBeanstalkEnvOpts)
   355  		if err == nil {
   356  			switch {
   357  			case len(resp.Environments) > 1:
   358  				return fmt.Errorf("Error %d environments match, expected 1", len(resp.Environments))
   359  			case len(resp.Environments) == 1:
   360  				if *resp.Environments[0].Status == "Terminated" {
   361  					return nil
   362  				}
   363  				return fmt.Errorf("Elastic Beanstalk ENV still exists.")
   364  			default:
   365  				return nil
   366  			}
   367  		}
   368  
   369  		// Verify the error is what we want
   370  		ec2err, ok := err.(awserr.Error)
   371  		if !ok {
   372  			return err
   373  		}
   374  		if ec2err.Code() != "InvalidBeanstalkEnvID.NotFound" {
   375  			return err
   376  		}
   377  	}
   378  
   379  	return nil
   380  }
   381  
   382  func testAccCheckBeanstalkEnvExists(n string, app *elasticbeanstalk.EnvironmentDescription) resource.TestCheckFunc {
   383  	return func(s *terraform.State) error {
   384  		rs, ok := s.RootModule().Resources[n]
   385  		if !ok {
   386  			return fmt.Errorf("Not found: %s", n)
   387  		}
   388  
   389  		if rs.Primary.ID == "" {
   390  			return fmt.Errorf("Elastic Beanstalk ENV is not set")
   391  		}
   392  
   393  		env, err := describeBeanstalkEnv(testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn, aws.String(rs.Primary.ID))
   394  		if err != nil {
   395  			return err
   396  		}
   397  
   398  		*app = *env
   399  
   400  		return nil
   401  	}
   402  }
   403  
   404  func testAccCheckBeanstalkEnvTier(n string, app *elasticbeanstalk.EnvironmentDescription) resource.TestCheckFunc {
   405  	return func(s *terraform.State) error {
   406  		rs, ok := s.RootModule().Resources[n]
   407  		if !ok {
   408  			return fmt.Errorf("Not found: %s", n)
   409  		}
   410  
   411  		if rs.Primary.ID == "" {
   412  			return fmt.Errorf("Elastic Beanstalk ENV is not set")
   413  		}
   414  
   415  		env, err := describeBeanstalkEnv(testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn, aws.String(rs.Primary.ID))
   416  		if err != nil {
   417  			return err
   418  		}
   419  		if *env.Tier.Name != "Worker" {
   420  			return fmt.Errorf("Beanstalk Environment tier is %s, expected Worker", *env.Tier.Name)
   421  		}
   422  
   423  		*app = *env
   424  
   425  		return nil
   426  	}
   427  }
   428  
   429  func testAccCheckBeanstalkEnvConfigValue(n string, expectedValue string) resource.TestCheckFunc {
   430  	return func(s *terraform.State) error {
   431  		conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn
   432  
   433  		rs, ok := s.RootModule().Resources[n]
   434  		if !ok {
   435  			return fmt.Errorf("Not found: %s", n)
   436  		}
   437  
   438  		if rs.Primary.ID == "" {
   439  			return fmt.Errorf("Elastic Beanstalk ENV is not set")
   440  		}
   441  
   442  		resp, err := conn.DescribeConfigurationOptions(&elasticbeanstalk.DescribeConfigurationOptionsInput{
   443  			ApplicationName: aws.String(rs.Primary.Attributes["application"]),
   444  			EnvironmentName: aws.String(rs.Primary.Attributes["name"]),
   445  			Options: []*elasticbeanstalk.OptionSpecification{
   446  				{
   447  					Namespace:  aws.String("aws:elasticbeanstalk:application:environment"),
   448  					OptionName: aws.String("TEMPLATE"),
   449  				},
   450  			},
   451  		})
   452  		if err != nil {
   453  			return err
   454  		}
   455  
   456  		if len(resp.Options) != 1 {
   457  			return fmt.Errorf("Found %d options, expected 1.", len(resp.Options))
   458  		}
   459  
   460  		log.Printf("[DEBUG] %d Elastic Beanstalk Option values returned.", len(resp.Options[0].ValueOptions))
   461  
   462  		for _, value := range resp.Options[0].ValueOptions {
   463  			if *value != expectedValue {
   464  				return fmt.Errorf("Option setting value: %s. Expected %s", *value, expectedValue)
   465  			}
   466  		}
   467  
   468  		return nil
   469  	}
   470  }
   471  
   472  func testAccCheckBeanstalkApplicationVersionDeployed(n string, app *elasticbeanstalk.EnvironmentDescription) resource.TestCheckFunc {
   473  	return func(s *terraform.State) error {
   474  		rs, ok := s.RootModule().Resources[n]
   475  		if !ok {
   476  			return fmt.Errorf("Not found: %s", n)
   477  		}
   478  
   479  		if rs.Primary.ID == "" {
   480  			return fmt.Errorf("Elastic Beanstalk ENV is not set")
   481  		}
   482  
   483  		env, err := describeBeanstalkEnv(testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn, aws.String(rs.Primary.ID))
   484  		if err != nil {
   485  			return err
   486  		}
   487  
   488  		if *env.VersionLabel != rs.Primary.Attributes["version_label"] {
   489  			return fmt.Errorf("Elastic Beanstalk version deployed %s. Expected %s", *env.VersionLabel, rs.Primary.Attributes["version_label"])
   490  		}
   491  
   492  		*app = *env
   493  
   494  		return nil
   495  	}
   496  }
   497  
   498  func describeBeanstalkEnv(conn *elasticbeanstalk.ElasticBeanstalk,
   499  	envID *string) (*elasticbeanstalk.EnvironmentDescription, error) {
   500  	describeBeanstalkEnvOpts := &elasticbeanstalk.DescribeEnvironmentsInput{
   501  		EnvironmentIds: []*string{envID},
   502  	}
   503  
   504  	log.Printf("[DEBUG] Elastic Beanstalk Environment TEST describe opts: %s", describeBeanstalkEnvOpts)
   505  
   506  	resp, err := conn.DescribeEnvironments(describeBeanstalkEnvOpts)
   507  	if err != nil {
   508  		return &elasticbeanstalk.EnvironmentDescription{}, err
   509  	}
   510  	if len(resp.Environments) == 0 {
   511  		return &elasticbeanstalk.EnvironmentDescription{}, fmt.Errorf("Elastic Beanstalk ENV not found.")
   512  	}
   513  	if len(resp.Environments) > 1 {
   514  		return &elasticbeanstalk.EnvironmentDescription{}, fmt.Errorf("Found %d environments, expected 1.", len(resp.Environments))
   515  	}
   516  	return resp.Environments[0], nil
   517  }
   518  
   519  func testAccBeanstalkEnvConfig(rInt int) string {
   520  	return fmt.Sprintf(`
   521   resource "aws_elastic_beanstalk_application" "tftest" {
   522  	 name = "tf-test-name-%d"
   523  	 description = "tf-test-desc"
   524   }
   525  
   526   resource "aws_elastic_beanstalk_environment" "tfenvtest" {
   527  	 name = "tf-test-name-%d"
   528  	 application = "${aws_elastic_beanstalk_application.tftest.name}"
   529  	 solution_stack_name = "64bit Amazon Linux running Python"
   530  	 depends_on = ["aws_elastic_beanstalk_application.tftest"]
   531   }
   532   `, rInt, rInt)
   533  }
   534  
   535  func testAccBeanstalkEnvConfig_empty_settings(r int) string {
   536  	return fmt.Sprintf(`
   537  resource "aws_elastic_beanstalk_application" "tftest" {
   538    name = "tf-test-name-%d"
   539    description = "tf-test-desc"
   540  }
   541  
   542  resource "aws_elastic_beanstalk_environment" "tfenvtest" {
   543    name = "tf-test-name-%d"
   544    application = "${aws_elastic_beanstalk_application.tftest.name}"
   545    solution_stack_name = "64bit Amazon Linux running Python"
   546  
   547          wait_for_ready_timeout = "15m"
   548  }`, r, r)
   549  }
   550  
   551  func testAccBeanstalkEnvConfig_settings(r int) string {
   552  	return fmt.Sprintf(`
   553  resource "aws_elastic_beanstalk_application" "tftest" {
   554    name = "tf-test-name-%d"
   555    description = "tf-test-desc"
   556  }
   557  
   558  resource "aws_elastic_beanstalk_environment" "tfenvtest" {
   559    name                = "tf-test-name-%d"
   560    application         = "${aws_elastic_beanstalk_application.tftest.name}"
   561    solution_stack_name = "64bit Amazon Linux running Python"
   562  
   563          wait_for_ready_timeout = "15m"
   564  
   565    setting {
   566      namespace = "aws:elasticbeanstalk:application:environment"
   567      name      = "ENV_STATIC"
   568      value     = "true"
   569    }
   570  
   571    setting {
   572      namespace = "aws:elasticbeanstalk:application:environment"
   573      name      = "ENV_UPDATE"
   574      value     = "true"
   575    }
   576  
   577    setting {
   578      namespace = "aws:elasticbeanstalk:application:environment"
   579      name      = "ENV_REMOVE"
   580      value     = "true"
   581    }
   582  
   583    setting {
   584      namespace = "aws:autoscaling:scheduledaction"
   585      resource  = "ScheduledAction01"
   586      name      = "MinSize"
   587      value     = 2
   588    }
   589  
   590    setting {
   591      namespace = "aws:autoscaling:scheduledaction"
   592      resource  = "ScheduledAction01"
   593      name      = "MaxSize"
   594      value     = 3
   595    }
   596  
   597    setting {
   598      namespace = "aws:autoscaling:scheduledaction"
   599      resource  = "ScheduledAction01"
   600      name      = "StartTime"
   601      value     = "2016-07-28T04:07:02Z"
   602    }
   603  }`, r, r)
   604  }
   605  
   606  func testAccBeanstalkEnvConfig_settings_update(r int) string {
   607  	return fmt.Sprintf(`
   608  resource "aws_elastic_beanstalk_application" "tftest" {
   609    name = "tf-test-name-%d"
   610    description = "tf-test-desc"
   611  }
   612  
   613  resource "aws_elastic_beanstalk_environment" "tfenvtest" {
   614    name                = "tf-test-name-%d"
   615    application         = "${aws_elastic_beanstalk_application.tftest.name}"
   616    solution_stack_name = "64bit Amazon Linux running Python"
   617  
   618          wait_for_ready_timeout = "15m"
   619  
   620    setting {
   621      namespace = "aws:elasticbeanstalk:application:environment"
   622      name      = "ENV_STATIC"
   623      value     = "true"
   624    }
   625  
   626    setting {
   627      namespace = "aws:elasticbeanstalk:application:environment"
   628      name      = "ENV_UPDATE"
   629      value     = "false"
   630    }
   631  
   632    setting {
   633      namespace = "aws:elasticbeanstalk:application:environment"
   634      name      = "ENV_ADD"
   635      value     = "true"
   636    }
   637  
   638    setting {
   639      namespace = "aws:autoscaling:scheduledaction"
   640      resource  = "ScheduledAction01"
   641      name      = "MinSize"
   642      value     = 2
   643    }
   644  
   645    setting {
   646      namespace = "aws:autoscaling:scheduledaction"
   647      resource  = "ScheduledAction01"
   648      name      = "MaxSize"
   649      value     = 3
   650    }
   651  
   652    setting {
   653      namespace = "aws:autoscaling:scheduledaction"
   654      resource  = "ScheduledAction01"
   655      name      = "StartTime"
   656      value     = "2016-07-28T04:07:02Z"
   657    }
   658  }`, r, r)
   659  }
   660  
   661  func testAccBeanstalkWorkerEnvConfig(rInt int) string {
   662  	return fmt.Sprintf(`
   663   resource "aws_iam_instance_profile" "tftest" {
   664  	 name = "tftest_profile-%d"
   665  	 roles = ["${aws_iam_role.tftest.name}"]
   666   }
   667  
   668   resource "aws_iam_role" "tftest" {
   669  	 name = "tftest_role"
   670  	 path = "/"
   671  	 assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Principal\":{\"Service\":\"ec2.amazonaws.com\"},\"Effect\":\"Allow\",\"Sid\":\"\"}]}"
   672   }
   673  
   674   resource "aws_iam_role_policy" "tftest" {
   675  	 name = "tftest_policy"
   676  	 role = "${aws_iam_role.tftest.id}"
   677  	 policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"QueueAccess\",\"Action\":[\"sqs:ChangeMessageVisibility\",\"sqs:DeleteMessage\",\"sqs:ReceiveMessage\"],\"Effect\":\"Allow\",\"Resource\":\"*\"}]}"
   678   }
   679  
   680   resource "aws_elastic_beanstalk_application" "tftest" {
   681  	 name = "tf-test-name-%d"
   682  	 description = "tf-test-desc"
   683   }
   684  
   685   resource "aws_elastic_beanstalk_environment" "tfenvtest" {
   686  	 name = "tf-test-name-%d"
   687  	 application = "${aws_elastic_beanstalk_application.tftest.name}"
   688  	 tier = "Worker"
   689  	 solution_stack_name = "64bit Amazon Linux running Python"
   690  
   691  	 setting {
   692  		 namespace = "aws:autoscaling:launchconfiguration"
   693  		 name      = "IamInstanceProfile"
   694  		 value     = "${aws_iam_instance_profile.tftest.name}"
   695  	 }
   696   }`, rInt, rInt, rInt)
   697  }
   698  
   699  func testAccBeanstalkEnvCnamePrefixConfig(randString string, rInt int) string {
   700  	return fmt.Sprintf(`
   701  resource "aws_elastic_beanstalk_application" "tftest" {
   702  name = "tf-test-name-%d"
   703  description = "tf-test-desc"
   704  }
   705  
   706  resource "aws_elastic_beanstalk_environment" "tfenvtest" {
   707  name = "tf-test-name-%d"
   708  application = "${aws_elastic_beanstalk_application.tftest.name}"
   709  cname_prefix = "%s"
   710  solution_stack_name = "64bit Amazon Linux running Python"
   711  }
   712  `, rInt, rInt, randString)
   713  }
   714  
   715  func testAccBeanstalkConfigTemplate(rInt int) string {
   716  	return fmt.Sprintf(`
   717  	resource "aws_elastic_beanstalk_application" "tftest" {
   718  		name = "tf-test-name-%d"
   719  		description = "tf-test-desc"
   720  	}
   721  
   722  	resource "aws_elastic_beanstalk_environment" "tftest" {
   723  		name = "tf-test-name-%d"
   724  		application = "${aws_elastic_beanstalk_application.tftest.name}"
   725  		template_name = "${aws_elastic_beanstalk_configuration_template.tftest.name}"
   726  	}
   727  
   728  	resource "aws_elastic_beanstalk_configuration_template" "tftest" {
   729  		name        = "tf-test-original"
   730  		application = "${aws_elastic_beanstalk_application.tftest.name}"
   731  		solution_stack_name = "64bit Amazon Linux running Python"
   732  
   733  		setting {
   734  			namespace = "aws:elasticbeanstalk:application:environment"
   735  			name      = "TEMPLATE"
   736  			value     = "1"
   737  	 }
   738  	}
   739  	`, rInt, rInt)
   740  }
   741  
   742  func testAccBeanstalkConfigTemplateUpdate(rInt int) string {
   743  	return fmt.Sprintf(`
   744  	resource "aws_elastic_beanstalk_application" "tftest" {
   745  		name = "tf-test-name-%d"
   746  		description = "tf-test-desc"
   747  	}
   748  
   749  	resource "aws_elastic_beanstalk_environment" "tftest" {
   750  		name = "tf-test-name-%d"
   751  		application = "${aws_elastic_beanstalk_application.tftest.name}"
   752  		template_name = "${aws_elastic_beanstalk_configuration_template.tftest.name}"
   753  	}
   754  
   755  	resource "aws_elastic_beanstalk_configuration_template" "tftest" {
   756  		name        = "tf-test-updated"
   757  		application = "${aws_elastic_beanstalk_application.tftest.name}"
   758  		solution_stack_name = "64bit Amazon Linux running Python"
   759  
   760  		setting {
   761  			namespace = "aws:elasticbeanstalk:application:environment"
   762  			name      = "TEMPLATE"
   763  			value     = "2"
   764  		}
   765  	}
   766  	`, rInt, rInt)
   767  }
   768  
   769  func testAccBeanstalkResourceOptionSetting(rInt int) string {
   770  	return fmt.Sprintf(`
   771  resource "aws_elastic_beanstalk_application" "tftest" {
   772    name = "tf-test-name-%d"
   773    description = "tf-test-desc"
   774  }
   775  
   776  resource "aws_elastic_beanstalk_environment" "tfenvtest" {
   777    name = "tf-test-name-%d"
   778    application = "${aws_elastic_beanstalk_application.tftest.name}"
   779    solution_stack_name = "64bit Amazon Linux running Python"
   780  
   781    setting {
   782      namespace = "aws:autoscaling:scheduledaction"
   783      resource = "ScheduledAction01"
   784      name = "MinSize"
   785      value = "2"
   786    }
   787  
   788    setting {
   789      namespace = "aws:autoscaling:scheduledaction"
   790      resource = "ScheduledAction01"
   791      name = "MaxSize"
   792      value = "6"
   793    }
   794  
   795    setting {
   796      namespace = "aws:autoscaling:scheduledaction"
   797      resource = "ScheduledAction01"
   798      name = "Recurrence"
   799      value = "0 8 * * *"
   800    }
   801  }`, rInt, rInt)
   802  }
   803  
   804  func testAccBeanstalkEnv_VPC(name string, rInt int) string {
   805  	return fmt.Sprintf(`
   806  resource "aws_vpc" "tf_b_test" {
   807    cidr_block = "10.0.0.0/16"
   808  }
   809  
   810  resource "aws_internet_gateway" "tf_b_test" {
   811    vpc_id = "${aws_vpc.tf_b_test.id}"
   812  }
   813  
   814  resource "aws_route" "r" {
   815    route_table_id = "${aws_vpc.tf_b_test.main_route_table_id}"
   816    destination_cidr_block = "0.0.0.0/0"
   817    gateway_id = "${aws_internet_gateway.tf_b_test.id}"
   818  }
   819  
   820  resource "aws_subnet" "main" {
   821    vpc_id     = "${aws_vpc.tf_b_test.id}"
   822    cidr_block = "10.0.0.0/24"
   823  }
   824  
   825  resource "aws_security_group" "default" {
   826    name = "tf-b-test-%s"
   827    vpc_id = "${aws_vpc.tf_b_test.id}"
   828  }
   829  
   830  resource "aws_elastic_beanstalk_application" "default" {
   831    name = "tf-test-name-%d"
   832    description = "tf-test-desc"
   833  }
   834  
   835  resource "aws_elastic_beanstalk_environment" "default" {
   836    name = "tf-test-name-%d"
   837    application = "${aws_elastic_beanstalk_application.default.name}"
   838    solution_stack_name = "64bit Amazon Linux running Python"
   839  
   840    setting {
   841      namespace = "aws:ec2:vpc"
   842      name      = "VPCId"
   843      value     = "${aws_vpc.tf_b_test.id}"
   844    }
   845  
   846    setting {
   847      namespace = "aws:ec2:vpc"
   848      name      = "Subnets"
   849      value     = "${aws_subnet.main.id}"
   850    }
   851  
   852    setting {
   853      namespace = "aws:ec2:vpc"
   854      name      = "AssociatePublicIpAddress"
   855      value     = "true"
   856    }
   857  
   858    setting {
   859      namespace = "aws:autoscaling:launchconfiguration"
   860      name      = "SecurityGroups"
   861      value     = "${aws_security_group.default.id}"
   862    }
   863  }
   864  `, name, rInt, rInt)
   865  }
   866  
   867  func testAccBeanstalkEnv_TemplateChange_stack(r int) string {
   868  	return fmt.Sprintf(`
   869  provider "aws" {
   870    region = "us-east-1"
   871  }
   872  
   873  resource "aws_elastic_beanstalk_application" "app" {
   874    name        = "beanstalk-app-%d"
   875    description = ""
   876  }
   877  
   878  resource "aws_elastic_beanstalk_environment" "environment" {
   879    name        = "beanstalk-env-%d"
   880    application = "${aws_elastic_beanstalk_application.app.name}"
   881  
   882    # Go 1.4
   883  
   884    solution_stack_name = "64bit Amazon Linux 2016.03 v2.1.0 running Go 1.4"
   885  }
   886  
   887  resource "aws_elastic_beanstalk_configuration_template" "template" {
   888    name        = "beanstalk-config-%d"
   889    application = "${aws_elastic_beanstalk_application.app.name}"
   890  
   891    # Go 1.5
   892    solution_stack_name = "64bit Amazon Linux 2016.03 v2.1.3 running Go 1.5"
   893  }
   894  `, r, r, r)
   895  }
   896  
   897  func testAccBeanstalkEnv_TemplateChange_temp(r int) string {
   898  	return fmt.Sprintf(`
   899  provider "aws" {
   900    region = "us-east-1"
   901  }
   902  
   903  resource "aws_elastic_beanstalk_application" "app" {
   904    name        = "beanstalk-app-%d"
   905    description = ""
   906  }
   907  
   908  resource "aws_elastic_beanstalk_environment" "environment" {
   909    name        = "beanstalk-env-%d"
   910    application = "${aws_elastic_beanstalk_application.app.name}"
   911  
   912    # Go 1.4
   913  
   914    template_name = "${aws_elastic_beanstalk_configuration_template.template.name}"
   915  }
   916  
   917  resource "aws_elastic_beanstalk_configuration_template" "template" {
   918    name        = "beanstalk-config-%d"
   919    application = "${aws_elastic_beanstalk_application.app.name}"
   920  
   921    # Go 1.5
   922    solution_stack_name = "64bit Amazon Linux 2016.03 v2.1.3 running Go 1.5"
   923  }
   924  `, r, r, r)
   925  }
   926  
   927  func testAccBeanstalkEnvApplicationVersionConfig(randInt int) string {
   928  	return fmt.Sprintf(`
   929  resource "aws_s3_bucket" "default" {
   930    bucket = "tftest.applicationversion.buckets-%d"
   931  }
   932  
   933  resource "aws_s3_bucket_object" "default" {
   934    bucket = "${aws_s3_bucket.default.id}"
   935    key = "python-v1.zip"
   936    source = "test-fixtures/python-v1.zip"
   937  }
   938  
   939  resource "aws_elastic_beanstalk_application" "default" {
   940    name = "tf-test-name-%d"
   941    description = "tf-test-desc"
   942  }
   943  
   944  resource "aws_elastic_beanstalk_application_version" "default" {
   945    application = "tf-test-name-%d"
   946    name = "tf-test-version-label"
   947    bucket = "${aws_s3_bucket.default.id}"
   948    key = "${aws_s3_bucket_object.default.id}"
   949  }
   950  
   951  resource "aws_elastic_beanstalk_environment" "default" {
   952    name = "tf-test-name-%d"
   953    application = "${aws_elastic_beanstalk_application.default.name}"
   954    version_label = "${aws_elastic_beanstalk_application_version.default.name}"
   955    solution_stack_name = "64bit Amazon Linux running Python"
   956  }
   957  `, randInt, randInt, randInt, randInt)
   958  }
   959  
   960  func testAccBeanstalkEnvApplicationVersionConfigUpdate(randInt int) string {
   961  	return fmt.Sprintf(`
   962  resource "aws_s3_bucket" "default" {
   963    bucket = "tftest.applicationversion.buckets-%d"
   964  }
   965  
   966  resource "aws_s3_bucket_object" "default" {
   967    bucket = "${aws_s3_bucket.default.id}"
   968    key = "python-v2.zip"
   969    source = "test-fixtures/python-v1.zip"
   970  }
   971  
   972  resource "aws_elastic_beanstalk_application" "default" {
   973    name = "tf-test-name-%d"
   974    description = "tf-test-desc"
   975  }
   976  
   977  resource "aws_elastic_beanstalk_application_version" "default" {
   978    application = "tf-test-name-%d"
   979    name = "tf-test-version-label-v2"
   980    bucket = "${aws_s3_bucket.default.id}"
   981    key = "${aws_s3_bucket_object.default.id}"
   982  }
   983  
   984  resource "aws_elastic_beanstalk_environment" "default" {
   985    name = "tf-test-name-%d"
   986    application = "${aws_elastic_beanstalk_application.default.name}"
   987    version_label = "${aws_elastic_beanstalk_application_version.default.name}"
   988    solution_stack_name = "64bit Amazon Linux running Python"
   989  }
   990  `, randInt, randInt, randInt, randInt)
   991  }