From 2a3d752834b356bfd76e67a943a82fc9bbddb808 Mon Sep 17 00:00:00 2001 From: Andrew Starr-Bochicchio Date: Tue, 30 May 2017 11:29:56 -0500 Subject: [PATCH] Add support for changing TTL on DigitalOcean domain records. Fixes #12631 (#14805) --- .../resource_digitalocean_record.go | 21 +++++++++++++++++++ .../resource_digitalocean_record_test.go | 5 +++++ .../docs/providers/do/r/record.html.markdown | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/builtin/providers/digitalocean/resource_digitalocean_record.go b/builtin/providers/digitalocean/resource_digitalocean_record.go index c22da8bb2..11d8a8e86 100644 --- a/builtin/providers/digitalocean/resource_digitalocean_record.go +++ b/builtin/providers/digitalocean/resource_digitalocean_record.go @@ -57,6 +57,12 @@ func resourceDigitalOceanRecord() *schema.Resource { ForceNew: true, }, + "ttl": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "value": { Type: schema.TypeString, Optional: true, @@ -94,6 +100,12 @@ func resourceDigitalOceanRecordCreate(d *schema.ResourceData, meta interface{}) return fmt.Errorf("Failed to parse port as an integer: %v", err) } } + if ttl := d.Get("ttl").(string); ttl != "" { + newRecord.TTL, err = strconv.Atoi(ttl) + if err != nil { + return fmt.Errorf("Failed to parse ttl as an integer: %v", err) + } + } if weight := d.Get("weight").(string); weight != "" { newRecord.Weight, err = strconv.Atoi(weight) if err != nil { @@ -146,6 +158,7 @@ func resourceDigitalOceanRecordRead(d *schema.ResourceData, meta interface{}) er d.Set("weight", strconv.Itoa(rec.Weight)) d.Set("priority", strconv.Itoa(rec.Priority)) d.Set("port", strconv.Itoa(rec.Port)) + d.Set("ttl", strconv.Itoa(rec.TTL)) en := constructFqdn(rec.Name, d.Get("domain").(string)) log.Printf("[DEBUG] Constructed FQDN: %s", en) @@ -168,6 +181,14 @@ func resourceDigitalOceanRecordUpdate(d *schema.ResourceData, meta interface{}) editRecord.Name = v.(string) } + if d.HasChange("ttl") { + newTTL := d.Get("ttl").(string) + editRecord.TTL, err = strconv.Atoi(newTTL) + if err != nil { + return fmt.Errorf("Failed to parse ttl as an integer: %v", err) + } + } + log.Printf("[DEBUG] record update configuration: %#v", editRecord) _, _, err = client.Domains.EditRecord(context.Background(), domain, id, &editRecord) if err != nil { diff --git a/builtin/providers/digitalocean/resource_digitalocean_record_test.go b/builtin/providers/digitalocean/resource_digitalocean_record_test.go index 72f588a4a..b3def11b9 100644 --- a/builtin/providers/digitalocean/resource_digitalocean_record_test.go +++ b/builtin/providers/digitalocean/resource_digitalocean_record_test.go @@ -85,6 +85,8 @@ func TestAccDigitalOceanRecord_Updated(t *testing.T) { "digitalocean_record.foobar", "value", "192.168.0.10"), resource.TestCheckResourceAttr( "digitalocean_record.foobar", "type", "A"), + resource.TestCheckResourceAttr( + "digitalocean_record.foobar", "ttl", "1800"), ), }, { @@ -101,6 +103,8 @@ func TestAccDigitalOceanRecord_Updated(t *testing.T) { "digitalocean_record.foobar", "value", "192.168.0.11"), resource.TestCheckResourceAttr( "digitalocean_record.foobar", "type", "A"), + resource.TestCheckResourceAttr( + "digitalocean_record.foobar", "ttl", "90"), ), }, }, @@ -341,6 +345,7 @@ resource "digitalocean_record" "foobar" { name = "terraform" value = "192.168.0.11" type = "A" + ttl = 90 }` const testAccCheckDigitalOceanRecordConfig_cname = ` diff --git a/website/source/docs/providers/do/r/record.html.markdown b/website/source/docs/providers/do/r/record.html.markdown index a9ddb50d6..5160b49fc 100644 --- a/website/source/docs/providers/do/r/record.html.markdown +++ b/website/source/docs/providers/do/r/record.html.markdown @@ -40,6 +40,7 @@ The following arguments are supported: * `port` - (Optional) The port of the record, for SRV records. * `priority` - (Optional) The priority of the record, for MX and SRV records. +* `ttl` - (Optional) The time to live for the record, in seconds. ## Attributes Reference @@ -47,4 +48,3 @@ The following attributes are exported: * `id` - The record ID * `fqdn` - The FQDN of the record -