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