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 Environment update configuration template name changes #6342

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
85 changes: 30 additions & 55 deletions builtin/providers/aws/resource_aws_elastic_beanstalk_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,50 +256,18 @@ func resourceAwsElasticBeanstalkEnvironmentCreate(d *schema.ResourceData, meta i
func resourceAwsElasticBeanstalkEnvironmentUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).elasticbeanstalkconn

if d.HasChange("description") {
if err := resourceAwsElasticBeanstalkEnvironmentDescriptionUpdate(conn, d); err != nil {
return err
}
}

if d.HasChange("solution_stack_name") {
if err := resourceAwsElasticBeanstalkEnvironmentSolutionStackUpdate(conn, d); err != nil {
return err
}
}

if d.HasChange("setting") {
if err := resourceAwsElasticBeanstalkEnvironmentOptionSettingsUpdate(conn, d); err != nil {
return err
}
}

return resourceAwsElasticBeanstalkEnvironmentRead(d, meta)
}

func resourceAwsElasticBeanstalkEnvironmentDescriptionUpdate(conn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData) error {
name := d.Get("name").(string)
desc := d.Get("description").(string)
envId := d.Id()

log.Printf("[DEBUG] Elastic Beanstalk application: %s, update description: %s", name, desc)

_, err := conn.UpdateEnvironment(&elasticbeanstalk.UpdateEnvironmentInput{
updateOpts := elasticbeanstalk.UpdateEnvironmentInput{
EnvironmentId: aws.String(envId),
Description: aws.String(desc),
})

return err
}

func resourceAwsElasticBeanstalkEnvironmentOptionSettingsUpdate(conn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData) error {
name := d.Get("name").(string)
envId := d.Id()
}

log.Printf("[DEBUG] Elastic Beanstalk application: %s, update options", name)
if d.HasChange("description") {
updateOpts.Description = aws.String(d.Get("description").(string))
}

req := &elasticbeanstalk.UpdateEnvironmentInput{
EnvironmentId: aws.String(envId),
if d.HasChange("solution_stack_name") {
updateOpts.SolutionStackName = aws.String(d.Get("solution_stack_name").(string))
}

if d.HasChange("setting") {
Expand All @@ -314,29 +282,36 @@ func resourceAwsElasticBeanstalkEnvironmentOptionSettingsUpdate(conn *elasticbea
os := o.(*schema.Set)
ns := n.(*schema.Set)

req.OptionSettings = extractOptionSettings(ns.Difference(os))
updateOpts.OptionSettings = extractOptionSettings(ns.Difference(os))
}

if _, err := conn.UpdateEnvironment(req); err != nil {
return err
if d.HasChange("template_name") {
updateOpts.TemplateName = aws.String(d.Get("template_name").(string))
}

return nil
}

func resourceAwsElasticBeanstalkEnvironmentSolutionStackUpdate(conn *elasticbeanstalk.ElasticBeanstalk, d *schema.ResourceData) error {
name := d.Get("name").(string)
solutionStack := d.Get("solution_stack_name").(string)
envId := d.Id()
log.Printf("[DEBUG] Elastic Beanstalk Environment update opts: %s", updateOpts)
_, err := conn.UpdateEnvironment(&updateOpts)
if err != nil {
return err
}

log.Printf("[DEBUG] Elastic Beanstalk application: %s, update solution_stack_name: %s", name, solutionStack)
stateConf := &resource.StateChangeConf{
Pending: []string{"Launching", "Updating"},
Target: []string{"Ready"},
Refresh: environmentStateRefreshFunc(conn, d.Id()),
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}

_, err := conn.UpdateEnvironment(&elasticbeanstalk.UpdateEnvironmentInput{
EnvironmentId: aws.String(envId),
SolutionStackName: aws.String(solutionStack),
})
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf(
"Error waiting for Elastic Beanstalk Environment (%s) to become ready: %s",
d.Id(), err)
}

return err
return resourceAwsElasticBeanstalkEnvironmentRead(d, meta)
}

func resourceAwsElasticBeanstalkEnvironmentRead(d *schema.ResourceData, meta interface{}) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,41 @@ func TestAccAWSBeanstalkEnv_cname_prefix(t *testing.T) {
})
}

func TestAccAWSBeanstalkEnv_config(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: testAccBeanstalkConfigTemplate,
Check: resource.ComposeTestCheckFunc(
testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tftest", &app),
testAccCheckBeanstalkEnvConfigValue("aws_elastic_beanstalk_environment.tftest", "1"),
),
},

resource.TestStep{
Config: testAccBeanstalkConfigTemplateUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tftest", &app),
testAccCheckBeanstalkEnvConfigValue("aws_elastic_beanstalk_environment.tftest", "2"),
),
},

resource.TestStep{
Config: testAccBeanstalkConfigTemplateUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tftest", &app),
testAccCheckBeanstalkEnvConfigValue("aws_elastic_beanstalk_environment.tftest", "3"),
),
},
},
})
}

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

Expand Down Expand Up @@ -192,6 +227,49 @@ func testAccCheckBeanstalkEnvTier(n string, app *elasticbeanstalk.EnvironmentDes
}
}

func testAccCheckBeanstalkEnvConfigValue(n string, expectedValue string) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn

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")
}

resp, err := conn.DescribeConfigurationOptions(&elasticbeanstalk.DescribeConfigurationOptionsInput{
ApplicationName: aws.String(rs.Primary.Attributes["application"]),
EnvironmentName: aws.String(rs.Primary.Attributes["name"]),
Options: []*elasticbeanstalk.OptionSpecification{
{
Namespace: aws.String("aws:elasticbeanstalk:application:environment"),
OptionName: aws.String("TEMPLATE"),
},
},
})
if err != nil {
return err
}

if len(resp.Options) != 1 {
return fmt.Errorf("Found %d options, expected 1.", len(resp.Options))
}

log.Printf("[DEBUG] %d Elastic Beanstalk Option values returned.", len(resp.Options[0].ValueOptions))

for _, value := range resp.Options[0].ValueOptions {
if *value != expectedValue {
return fmt.Errorf("Option setting value: %s. Expected %s", value, expectedValue)
}
}

return nil
}
}

func describeBeanstalkEnv(conn *elasticbeanstalk.ElasticBeanstalk,
envID *string) (*elasticbeanstalk.EnvironmentDescription, error) {
describeBeanstalkEnvOpts := &elasticbeanstalk.DescribeEnvironmentsInput{
Expand Down Expand Up @@ -255,3 +333,84 @@ solution_stack_name = "64bit Amazon Linux running Python"
}
`, randString)
}

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

resource "aws_elastic_beanstalk_environment" "tftest" {
name = "tf-test-name"
application = "${aws_elastic_beanstalk_application.tftest.name}"
template_name = "${aws_elastic_beanstalk_configuration_template.tftest.name}"
}

resource "aws_elastic_beanstalk_configuration_template" "tftest" {
name = "tf-test-original"
application = "${aws_elastic_beanstalk_application.tftest.name}"
solution_stack_name = "64bit Amazon Linux running Python"

setting {
namespace = "aws:elasticbeanstalk:application:environment"
name = "TEMPLATE"
value = "1"
}
}
`

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

resource "aws_elastic_beanstalk_environment" "tftest" {
name = "tf-test-name"
application = "${aws_elastic_beanstalk_application.tftest.name}"
template_name = "${aws_elastic_beanstalk_configuration_template.tftest.name}"
}

resource "aws_elastic_beanstalk_configuration_template" "tftest" {
name = "tf-test-updated"
application = "${aws_elastic_beanstalk_application.tftest.name}"
solution_stack_name = "64bit Amazon Linux running Python"

setting {
namespace = "aws:elasticbeanstalk:application:environment"
name = "TEMPLATE"
value = "2"
}
}
`

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

resource "aws_elastic_beanstalk_environment" "tftest" {
name = "tf-test-name"
application = "${aws_elastic_beanstalk_application.tftest.name}"
template_name = "${aws_elastic_beanstalk_configuration_template.tftest.name}"

setting {
namespace = "aws:elasticbeanstalk:application:environment"
name = "TEMPLATE"
value = "3"
}
}

resource "aws_elastic_beanstalk_configuration_template" "tftest" {
name = "tf-test-updated"
application = "${aws_elastic_beanstalk_application.tftest.name}"
solution_stack_name = "64bit Amazon Linux running Python"

setting {
namespace = "aws:elasticbeanstalk:application:environment"
name = "TEMPLATE"
value = "2"
}
}
`