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 ( import (
"fmt" "fmt"
"log" "log"
"regexp"
"sort" "sort"
"strings" "strings"
"time" "time"
@ -60,9 +61,16 @@ func resourceAwsElasticBeanstalkEnvironment() *schema.Resource {
Type: schema.TypeString, Type: schema.TypeString,
Computed: true, Computed: true,
}, },
"cname_prefix": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
},
"tier": &schema.Schema{ "tier": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Default: "WebServer",
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string) value := v.(string)
switch value { switch value {
@ -127,7 +135,7 @@ func resourceAwsElasticBeanstalkEnvironmentCreate(d *schema.ResourceData, meta i
// Get values from config // Get values from config
name := d.Get("name").(string) name := d.Get("name").(string)
cname := d.Get("cname").(string) cnamePrefix := d.Get("cname_prefix").(string)
tier := d.Get("tier").(string) tier := d.Get("tier").(string)
app := d.Get("application").(string) app := d.Get("application").(string)
desc := d.Get("description").(string) desc := d.Get("description").(string)
@ -153,8 +161,11 @@ func resourceAwsElasticBeanstalkEnvironmentCreate(d *schema.ResourceData, meta i
createOpts.Description = aws.String(desc) createOpts.Description = aws.String(desc)
} }
if cname != "" { if cnamePrefix != "" {
createOpts.CNAMEPrefix = aws.String(cname) if tier != "WebServer" {
return fmt.Errorf("Cannont set cname_prefix for tier: %s.", tier)
}
createOpts.CNAMEPrefix = aws.String(cnamePrefix)
} }
if tier != "" { if tier != "" {
@ -300,6 +311,7 @@ func resourceAwsElasticBeanstalkEnvironmentRead(d *schema.ResourceData, meta int
app := d.Get("application").(string) app := d.Get("application").(string)
envId := d.Id() envId := d.Id()
tier := d.Get("tier").(string)
log.Printf("[DEBUG] Elastic Beanstalk environment read %s: id %s", d.Get("name").(string), d.Id()) 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 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) return resourceAwsElasticBeanstalkEnvironmentSettingsRead(d, meta)
} }

View File

@ -3,11 +3,13 @@ package aws
import ( import (
"fmt" "fmt"
"log" "log"
"regexp"
"testing" "testing"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/elasticbeanstalk" "github.com/aws/aws-sdk-go/service/elasticbeanstalk"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "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 { func testAccCheckBeanstalkEnvDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).elasticbeanstalkconn 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" 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 in the application URL
* `application` (Required) Name of the application that contains the version * `application` (Required) Name of the application that contains the version
to be deployed 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 * `description` - (Optional) Short description of the Environment
* `tier` - (Optional) Elastic Beanstalk Environment tier. Valid values are `Worker` * `tier` - (Optional) Elastic Beanstalk Environment tier. Valid values are `Worker`
or `WebServer`. If tier is left blank `WebServer` will be used. 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 * `wait_for_ready_timeout` - (Default: "10m") The maximum
[duration](https://golang.org/pkg/time/#ParseDuration) that Terraform should [duration](https://golang.org/pkg/time/#ParseDuration) that Terraform should
wait for an Elastic Beanstalk Environment to be in a ready state before timing 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 * `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 this time the Elastic Beanstalk API does not provide a programatic way of
changing these tags after initial application 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 * `all_settings`  List of all option settings configured in the Environment. These
are a combination of default settings and their overrides from `settings` in are a combination of default settings and their overrides from `settings` in
the configuration the configuration
* `cname` - Fully qualified DNS name for the Environment.
[1]: http://docs.aws.amazon.com/fr_fr/elasticbeanstalk/latest/dg/concepts.platforms.html [1]: http://docs.aws.amazon.com/fr_fr/elasticbeanstalk/latest/dg/concepts.platforms.html