provider/aws: Add support for `cname_prefix` to `aws_elastic_beanstalk_environment`.

This commit is contained in:
David Harris 2016-03-29 17:59:38 -06:00
parent c19d84bce7
commit bb1d4ee886
3 changed files with 75 additions and 4 deletions

View File

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

View File

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

View File

@ -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.
@ -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