aws: Support for weighted Route53 records added

- closes #1155
This commit is contained in:
Radek Simko 2015-04-17 18:02:05 +01:00
parent 851f34b2fb
commit 555f9dfab3
3 changed files with 111 additions and 1 deletions

View File

@ -45,6 +45,17 @@ func resourceAwsRoute53Record() *schema.Resource {
Required: true, Required: true,
}, },
"weight": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"set_identifier": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"records": &schema.Schema{ "records": &schema.Schema{
Type: schema.TypeSet, Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString}, Elem: &schema.Schema{Type: schema.TypeString},
@ -136,7 +147,15 @@ func resourceAwsRoute53RecordCreate(d *schema.ResourceData, meta interface{}) er
changeInfo := respRaw.(*route53.ChangeResourceRecordSetsOutput).ChangeInfo changeInfo := respRaw.(*route53.ChangeResourceRecordSetsOutput).ChangeInfo
// Generate an ID // Generate an ID
d.SetId(fmt.Sprintf("%s_%s_%s", zone, d.Get("name").(string), d.Get("type").(string))) vars := []string{
zone,
d.Get("name").(string),
d.Get("type").(string),
}
if v, ok := d.GetOk("set_identifier"); ok {
vars = append(vars, v.(string))
}
d.SetId(strings.Join(vars, "_"))
// Wait until we are done // Wait until we are done
wait = resource.StateChangeConf{ wait = resource.StateChangeConf{
@ -178,6 +197,10 @@ func resourceAwsRoute53RecordRead(d *schema.ResourceData, meta interface{}) erro
StartRecordType: aws.String(d.Get("type").(string)), StartRecordType: aws.String(d.Get("type").(string)),
} }
if v, ok := d.GetOk("set_identifier"); ok {
lopts.StartRecordIdentifier = aws.String(v.(string))
}
resp, err := conn.ListResourceRecordSets(lopts) resp, err := conn.ListResourceRecordSets(lopts)
if err != nil { if err != nil {
return err return err
@ -194,6 +217,10 @@ func resourceAwsRoute53RecordRead(d *schema.ResourceData, meta interface{}) erro
continue continue
} }
if lopts.StartRecordIdentifier != nil && *record.SetIdentifier != *lopts.StartRecordIdentifier {
continue
}
found = true found = true
err := d.Set("records", flattenResourceRecords(record.ResourceRecords)) err := d.Set("records", flattenResourceRecords(record.ResourceRecords))
@ -201,6 +228,8 @@ func resourceAwsRoute53RecordRead(d *schema.ResourceData, meta interface{}) erro
return fmt.Errorf("[DEBUG] Error setting records for: %s, error: %#v", en, err) return fmt.Errorf("[DEBUG] Error setting records for: %s, error: %#v", en, err)
} }
d.Set("ttl", record.TTL) d.Set("ttl", record.TTL)
d.Set("weight", record.Weight)
d.Set("set_identifier", record.SetIdentifier)
break break
} }
@ -297,6 +326,15 @@ func resourceAwsRoute53RecordBuildSet(d *schema.ResourceData, zoneName string) (
TTL: aws.Long(int64(d.Get("ttl").(int))), TTL: aws.Long(int64(d.Get("ttl").(int))),
ResourceRecords: records, ResourceRecords: records,
} }
if v, ok := d.GetOk("weight"); ok {
rec.Weight = aws.Long(int64(v.(int)))
}
if v, ok := d.GetOk("set_identifier"); ok {
rec.SetIdentifier = aws.String(v.(string))
}
return rec, nil return rec, nil
} }

View File

@ -122,6 +122,23 @@ func TestAccRoute53Record_wildcard(t *testing.T) {
}) })
} }
func TestAccRoute53Record_weighted(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRoute53RecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccRoute53WeightedCNAMERecord,
Check: resource.ComposeTestCheckFunc(
testAccCheckRoute53RecordExists("aws_route53_record.www-dev"),
testAccCheckRoute53RecordExists("aws_route53_record.www-live"),
),
},
},
})
}
func testAccCheckRoute53RecordDestroy(s *terraform.State) error { func testAccCheckRoute53RecordDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).r53conn conn := testAccProvider.Meta().(*AWSClient).r53conn
for _, rs := range s.RootModule().Resources { for _, rs := range s.RootModule().Resources {
@ -282,3 +299,29 @@ resource "aws_route53_record" "default" {
records = ["lalalala"] records = ["lalalala"]
} }
` `
const testAccRoute53WeightedCNAMERecord = `
resource "aws_route53_zone" "main" {
name = "notexample.com"
}
resource "aws_route53_record" "www-dev" {
zone_id = "${aws_route53_zone.main.zone_id}"
name = "www"
type = "CNAME"
ttl = "5"
weight = 10
set_identifier = "dev"
records = ["dev.notexample.com"]
}
resource "aws_route53_record" "www-live" {
zone_id = "${aws_route53_zone.main.zone_id}"
name = "www"
type = "CNAME"
ttl = "5"
weight = 90
set_identifier = "live"
records = ["dev.notexample.com"]
}
`

View File

@ -12,6 +12,8 @@ Provides a Route53 record resource.
## Example Usage ## Example Usage
### Simple routing policy
``` ```
resource "aws_route53_record" "www" { resource "aws_route53_record" "www" {
zone_id = "${aws_route53_zone.primary.zone_id}" zone_id = "${aws_route53_zone.primary.zone_id}"
@ -22,6 +24,30 @@ resource "aws_route53_record" "www" {
} }
``` ```
### Weighted routing policy
See [AWS Route53 Developer Guide](http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/routing-policy.html#routing-policy-weighted) for details.
```
resource "aws_route53_record" "www-dev" {
zone_id = "${aws_route53_zone.primary.zone_id}"
name = "www"
type = "CNAME"
ttl = "5"
weight = 10
set_identifier = "dev"
records = ["dev.example.com"]
}
resource "aws_route53_record" "www-live" {
zone_id = "${aws_route53_zone.primary.zone_id}"
name = "www"
type = "CNAME"
ttl = "5"
weight = 90
set_identifier = "live"
records = ["live.example.com"]
}
```
## Argument Reference ## Argument Reference
The following arguments are supported: The following arguments are supported:
@ -31,6 +57,9 @@ The following arguments are supported:
* `type` - (Required) The record type. * `type` - (Required) The record type.
* `ttl` - (Required) The TTL of the record. * `ttl` - (Required) The TTL of the record.
* `records` - (Required) A string list of records. * `records` - (Required) A string list of records.
* `weight` - (Optional) The weight of weighted record (0-255).
* `set_identifier` - (Optional) Unique identifier to differentiate weighted
record from one another. Required for each weighted record.
## Attributes Reference ## Attributes Reference