From bb1d4ee886999b7ba5a842635b7a02bd79320ea2 Mon Sep 17 00:00:00 2001 From: David Harris Date: Tue, 29 Mar 2016 17:59:38 -0600 Subject: [PATCH] provider/aws: Add support for `cname_prefix` to `aws_elastic_beanstalk_environment`. --- ...ource_aws_elastic_beanstalk_environment.go | 34 ++++++++++++++-- ..._aws_elastic_beanstalk_environment_test.go | 40 +++++++++++++++++++ ...lastic_beanstalk_environment.html.markdown | 5 ++- 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/builtin/providers/aws/resource_aws_elastic_beanstalk_environment.go b/builtin/providers/aws/resource_aws_elastic_beanstalk_environment.go index 022eb578c..fd5426ce7 100644 --- a/builtin/providers/aws/resource_aws_elastic_beanstalk_environment.go +++ b/builtin/providers/aws/resource_aws_elastic_beanstalk_environment.go @@ -3,6 +3,7 @@ package aws import ( "fmt" "log" + "regexp" "sort" "strings" "time" @@ -60,9 +61,16 @@ func resourceAwsElasticBeanstalkEnvironment() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "cname_prefix": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + Optional: true, + ForceNew: true, + }, "tier": &schema.Schema{ Type: schema.TypeString, Optional: true, + Default: "WebServer", ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { value := v.(string) switch value { @@ -127,7 +135,7 @@ func resourceAwsElasticBeanstalkEnvironmentCreate(d *schema.ResourceData, meta i // Get values from config name := d.Get("name").(string) - cname := d.Get("cname").(string) + cnamePrefix := d.Get("cname_prefix").(string) tier := d.Get("tier").(string) app := d.Get("application").(string) desc := d.Get("description").(string) @@ -153,8 +161,11 @@ func resourceAwsElasticBeanstalkEnvironmentCreate(d *schema.ResourceData, meta i createOpts.Description = aws.String(desc) } - if cname != "" { - createOpts.CNAMEPrefix = aws.String(cname) + if cnamePrefix != "" { + if tier != "WebServer" { + return fmt.Errorf("Cannont set cname_prefix for tier: %s.", tier) + } + createOpts.CNAMEPrefix = aws.String(cnamePrefix) } if tier != "" { @@ -300,6 +311,7 @@ func resourceAwsElasticBeanstalkEnvironmentRead(d *schema.ResourceData, meta int app := d.Get("application").(string) envId := d.Id() + tier := d.Get("tier").(string) log.Printf("[DEBUG] Elastic Beanstalk environment read %s: id %s", d.Get("name").(string), d.Id()) @@ -338,6 +350,22 @@ func resourceAwsElasticBeanstalkEnvironmentRead(d *schema.ResourceData, meta int return err } + if tier == "WebServer" { + beanstalkCnamePrefixRegexp := regexp.MustCompile(`(^[^.]+).\w{2}-\w{4}-\d.elasticbeanstalk.com$`) + var cnamePrefix string + cnamePrefixMatch := beanstalkCnamePrefixRegexp.FindStringSubmatch(*env.CNAME) + + if cnamePrefixMatch == nil { + cnamePrefix = "" + } else { + cnamePrefix = cnamePrefixMatch[1] + } + + if err := d.Set("cname_prefix", cnamePrefix); err != nil { + return err + } + } + return resourceAwsElasticBeanstalkEnvironmentSettingsRead(d, meta) } diff --git a/builtin/providers/aws/resource_aws_elastic_beanstalk_environment_test.go b/builtin/providers/aws/resource_aws_elastic_beanstalk_environment_test.go index 6c2caa20b..15e496a1e 100644 --- a/builtin/providers/aws/resource_aws_elastic_beanstalk_environment_test.go +++ b/builtin/providers/aws/resource_aws_elastic_beanstalk_environment_test.go @@ -3,11 +3,13 @@ package aws import ( "fmt" "log" + "regexp" "testing" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/elasticbeanstalk" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) @@ -48,6 +50,28 @@ func TestAccAWSBeanstalkEnv_tier(t *testing.T) { }) } +func TestAccAWSBeanstalkEnv_cname_prefix(t *testing.T) { + var app elasticbeanstalk.EnvironmentDescription + cnamePrefix := acctest.RandString(8) + beanstalkCnameRegexp := regexp.MustCompile("^" + cnamePrefix + ".+?elasticbeanstalk.com$") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckBeanstalkEnvDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccBeanstalkEnvCnamePrefixConfig(cnamePrefix), + Check: resource.ComposeTestCheckFunc( + testAccCheckBeanstalkEnvExists("aws_elastic_beanstalk_environment.tfenvtest", &app), + resource.TestMatchResourceAttr( + "aws_elastic_beanstalk_environment.tfenvtest", "cname", beanstalkCnameRegexp), + ), + }, + }, + }) +} + func testAccCheckBeanstalkEnvDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn @@ -183,3 +207,19 @@ resource "aws_elastic_beanstalk_environment" "tfenvtest" { solution_stack_name = "64bit Amazon Linux 2015.09 v2.0.4 running Go 1.4" } ` + +func testAccBeanstalkEnvCnamePrefixConfig(randString string) string { + return fmt.Sprintf(` +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}" +cname_prefix = "%s" +solution_stack_name = "64bit Amazon Linux 2015.09 v2.0.4 running Go 1.4" +} +`, randString) +} diff --git a/website/source/docs/providers/aws/r/elastic_beanstalk_environment.html.markdown b/website/source/docs/providers/aws/r/elastic_beanstalk_environment.html.markdown index 6c37d071f..83ac79aeb 100644 --- a/website/source/docs/providers/aws/r/elastic_beanstalk_environment.html.markdown +++ b/website/source/docs/providers/aws/r/elastic_beanstalk_environment.html.markdown @@ -39,6 +39,8 @@ The following arguments are supported: in the application URL * `application` – (Required) Name of the application that contains the version to be deployed +* `cname_prefix` - (Optional) Prefix to use for the fully qualified DNS name of + the Environment. * `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. @@ -52,7 +54,7 @@ off of. Example stacks can be found in the [Amazon API documentation][1] * `wait_for_ready_timeout` - (Default: "10m") The maximum [duration](https://golang.org/pkg/time/#ParseDuration) that Terraform should wait for an Elastic Beanstalk Environment to be in a ready state before timing - out. + out. * `tags` – (Optional) A set of tags to apply to the Environment. **Note:** at this time the Elastic Beanstalk API does not provide a programatic way of changing these tags after initial application @@ -80,6 +82,7 @@ The following attributes are exported: * `all_settings` – List of all option settings configured in the Environment. These are a combination of default settings and their overrides from `settings` in the configuration +* `cname` - Fully qualified DNS name for the Environment. [1]: http://docs.aws.amazon.com/fr_fr/elasticbeanstalk/latest/dg/concepts.platforms.html