Skip to content

Commit

Permalink
provider/aws: Add support for cname_prefix to `aws_elastic_beanstal…
Browse files Browse the repository at this point in the history
…k_environment`.
  • Loading branch information
dharrisio committed Mar 31, 2016
1 parent 71995ea commit d016404
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"regexp"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -109,7 +117,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)
Expand All @@ -131,8 +139,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 != "" {
Expand Down Expand Up @@ -278,6 +289,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())

Expand Down Expand Up @@ -316,6 +328,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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ description: |-

# aws\_elastic\_beanstalk\_<wbr>environment

Provides an Elastic Beanstalk Environment Resource. Elastic Beanstalk allows
you to deploy and manage applications in the AWS cloud without worrying about
Provides an Elastic Beanstalk Environment Resource. Elastic Beanstalk allows
you to deploy and manage applications in the AWS cloud without worrying about
the infrastructure that runs those applications.

Environments are often things such as `development`, `integration`, or
Environments are often things such as `development`, `integration`, or
`production`.

## Example Usage
Expand All @@ -35,19 +35,21 @@ resource "aws_elastic_beanstalk_environment" "tfenvtest" {

The following arguments are supported:

* `name` - (Required) A unique name for this Environment. This name is used
* `name` - (Required) A unique name for this Environment. This name is used
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
* `description` - (Optional) Short description of the Environment
* `tier` - (Optional) Elastic Beanstalk Environment tier. Valid values are `Worker`
* `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.
* `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)
* `solution_stack_name` – (Optional) A solution stack to base your environment
off of. Example stacks can be found in the [Amazon API documentation][1]
* `template_name` – (Optional) The name of the Elastic Beanstalk Configuration
* `template_name` – (Optional) The name of the Elastic Beanstalk Configuration
template to use in deployment
* `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
Expand All @@ -59,7 +61,7 @@ changing these tags after initial application

The `setting` and `all_settings` mappings support the following format:

* `namespace` - (Optional) unique namespace identifying the option's
* `namespace` - (Optional) unique namespace identifying the option's
associated AWS resource
* `name` - (Optional) name of the configuration option
* `value` - (Optional) value for the configuration option
Expand All @@ -70,14 +72,13 @@ The following attributes are exported:

* `name`
* `description`
* `tier` - the environment tier specified.
* `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
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


0 comments on commit d016404

Please sign in to comment.