Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/aws: Elastic Beanstalk Tier Attribute #5209

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ func resourceAwsElasticBeanstalkEnvironment() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tier": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
switch value {
case
"Worker",
"WebServer":
return
}
errors = append(errors, fmt.Errorf("%s is not a valid tier. Valid options are WebServer or Worker", value))
return
},
ForceNew: true,
},
"setting": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -94,6 +110,7 @@ func resourceAwsElasticBeanstalkEnvironmentCreate(d *schema.ResourceData, meta i
// Get values from config
name := d.Get("name").(string)
cname := d.Get("cname").(string)
tier := d.Get("tier").(string)
app := d.Get("application").(string)
desc := d.Get("description").(string)
settings := d.Get("setting").(*schema.Set)
Expand All @@ -118,6 +135,22 @@ func resourceAwsElasticBeanstalkEnvironmentCreate(d *schema.ResourceData, meta i
createOpts.CNAMEPrefix = aws.String(cname)
}

if tier != "" {
var tierType string

switch tier {
case "WebServer":
tierType = "Standard"
case "Worker":
tierType = "SQS/HTTP"
}
environmentTier := elasticbeanstalk.EnvironmentTier{
Name: aws.String(tier),
Type: aws.String(tierType),
}
createOpts.Tier = &environmentTier
}

if solutionStack != "" {
createOpts.SolutionStackName = aws.String(solutionStack)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ func TestAccAWSBeanstalkEnv_basic(t *testing.T) {
})
}

func TestAccAWSBeanstalkEnv_tier(t *testing.T) {
var app elasticbeanstalk.EnvironmentDescription

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckBeanstalkEnvDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccBeanstalkWorkerEnvConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckBeanstalkEnvTier("aws_elastic_beanstalk_environment.tfenvtest", &app),
),
},
},
})
}

func testAccCheckBeanstalkEnvDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn

Expand Down Expand Up @@ -81,27 +99,63 @@ func testAccCheckBeanstalkEnvExists(n string, app *elasticbeanstalk.EnvironmentD
return fmt.Errorf("Elastic Beanstalk ENV is not set")
}

conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn
describeBeanstalkEnvOpts := &elasticbeanstalk.DescribeEnvironmentsInput{
EnvironmentIds: []*string{aws.String(rs.Primary.ID)},
env, err := describeBeanstalkEnv(testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn, aws.String(rs.Primary.ID))
if err != nil {
return err
}

log.Printf("[DEBUG] Elastic Beanstalk Environment TEST describe opts: %s", describeBeanstalkEnvOpts)
*app = *env

resp, err := conn.DescribeEnvironments(describeBeanstalkEnvOpts)
return nil
}
}

func testAccCheckBeanstalkEnvTier(n string, app *elasticbeanstalk.EnvironmentDescription) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("Elastic Beanstalk ENV is not set")
}

env, err := describeBeanstalkEnv(testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn, aws.String(rs.Primary.ID))
if err != nil {
return err
}
if len(resp.Environments) == 0 {
return fmt.Errorf("Elastic Beanstalk ENV not found.")
if *env.Tier.Name != "Worker" {
return fmt.Errorf("Beanstalk Environment tier is %s, expected Worker", *env.Tier.Name)
}

*app = *resp.Environments[0]
*app = *env

return nil
}
}

func describeBeanstalkEnv(conn *elasticbeanstalk.ElasticBeanstalk,
envID *string) (*elasticbeanstalk.EnvironmentDescription, error) {
describeBeanstalkEnvOpts := &elasticbeanstalk.DescribeEnvironmentsInput{
EnvironmentIds: []*string{envID},
}

log.Printf("[DEBUG] Elastic Beanstalk Environment TEST describe opts: %s", describeBeanstalkEnvOpts)

resp, err := conn.DescribeEnvironments(describeBeanstalkEnvOpts)
if err != nil {
return &elasticbeanstalk.EnvironmentDescription{}, err
}
if len(resp.Environments) == 0 {
return &elasticbeanstalk.EnvironmentDescription{}, fmt.Errorf("Elastic Beanstalk ENV not found.")
}
if len(resp.Environments) > 1 {
return &elasticbeanstalk.EnvironmentDescription{}, fmt.Errorf("Found %d environments, expected 1.", len(resp.Environments))
}
return resp.Environments[0], nil
}

const testAccBeanstalkEnvConfig = `
resource "aws_elastic_beanstalk_application" "tftest" {
name = "tf-test-name"
Expand All @@ -115,3 +169,17 @@ resource "aws_elastic_beanstalk_environment" "tfenvtest" {
#solution_stack_name =
}
`

const testAccBeanstalkWorkerEnvConfig = `
resource "aws_elastic_beanstalk_application" "tftest" {
name = "tf-test-name"
description = "tf-test-desc"
}

resource "aws_elastic_beanstalk_environment" "tfenvtest" {
name = "tf-test-name"
application = "${aws_elastic_beanstalk_application.tftest.name}"
tier = "Worker"
solution_stack_name = "64bit Amazon Linux 2015.09 v2.0.4 running Go 1.4"
}
`
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ The following arguments are supported:
* `application` – (Required) Name of the application that contains the version
to be deployed
* `description` - (Optional) Short description of the Environment
* `tier` - (Optional) Elastic Beanstalk Environment tier. Valid values are `Worker`
or `WebServer`. If tier is left blank `WebServer` will be used.
* `setting` – (Optional) Option settings to configure the new Environment. These
override specific values that are set as defaults. The format is detailed
below in [Option Settings](#option-settings)
Expand Down Expand Up @@ -68,6 +70,7 @@ The following attributes are exported:

* `name`
* `description`
* `tier` - the environment tier specified.
* `application` – the application specified
* `setting` – Settings specifically set for this Environment
* `all_settings` – List of all option settings configured in the Environment. These
Expand Down