From b57a3094f6e050a7473eb10f78313c751c1da02a Mon Sep 17 00:00:00 2001 From: stack72 Date: Tue, 9 Feb 2016 16:57:42 +0000 Subject: [PATCH] provider/digitalocean: Addsa FQDN out to the `digitalocean_record` resource. This is a computed field --- .../resource_digitalocean_record.go | 18 +++++++++++++ .../resource_digitalocean_record_test.go | 25 +++++++++++++++++++ .../docs/providers/do/r/record.html.markdown | 1 + 3 files changed, 44 insertions(+) diff --git a/builtin/providers/digitalocean/resource_digitalocean_record.go b/builtin/providers/digitalocean/resource_digitalocean_record.go index 5e8218c79c06..faf3da086654 100644 --- a/builtin/providers/digitalocean/resource_digitalocean_record.go +++ b/builtin/providers/digitalocean/resource_digitalocean_record.go @@ -62,6 +62,11 @@ func resourceDigitalOceanRecord() *schema.Resource { Computed: true, ForceNew: true, }, + + "fqdn": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, }, } } @@ -146,6 +151,10 @@ func resourceDigitalOceanRecordRead(d *schema.ResourceData, meta interface{}) er d.Set("priority", strconv.Itoa(rec.Priority)) d.Set("port", strconv.Itoa(rec.Port)) + en := constructFqdn(rec.Name, d.Get("domain").(string)) + log.Printf("[DEBUG] Constructred FQDN: %s", en) + d.Set("fqdn", en) + return nil } @@ -196,3 +205,12 @@ func resourceDigitalOceanRecordDelete(d *schema.ResourceData, meta interface{}) return nil } + +func constructFqdn(name, domain string) string { + rn := strings.ToLower(strings.TrimSuffix(name, ".")) + domain = strings.TrimSuffix(domain, ".") + if !strings.HasSuffix(rn, domain) { + rn = strings.Join([]string{name, domain}, ".") + } + return rn +} diff --git a/builtin/providers/digitalocean/resource_digitalocean_record_test.go b/builtin/providers/digitalocean/resource_digitalocean_record_test.go index 9552e031e651..5fef6888984f 100644 --- a/builtin/providers/digitalocean/resource_digitalocean_record_test.go +++ b/builtin/providers/digitalocean/resource_digitalocean_record_test.go @@ -5,12 +5,35 @@ import ( "strconv" "testing" + "strings" + "github.com/digitalocean/godo" "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) +func TestDigitalOceanRecordConstructFqdn(t *testing.T) { + cases := []struct { + Input, Output string + }{ + {"www", "www.nonexample.com"}, + {"dev.www", "dev.www.nonexample.com"}, + {"*", "*.nonexample.com"}, + {"nonexample.com", "nonexample.com"}, + {"test.nonexample.com", "test.nonexample.com"}, + {"test.nonexample.com.", "test.nonexample.com"}, + } + + domain := "nonexample.com" + for _, tc := range cases { + actual := constructFqdn(tc.Input, domain) + if actual != tc.Output { + t.Fatalf("input: %s\noutput: %s", tc.Input, actual) + } + } +} + func TestAccDigitalOceanRecord_Basic(t *testing.T) { var record godo.DomainRecord domain := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10)) @@ -31,6 +54,8 @@ func TestAccDigitalOceanRecord_Basic(t *testing.T) { "digitalocean_record.foobar", "domain", domain), resource.TestCheckResourceAttr( "digitalocean_record.foobar", "value", "192.168.0.10"), + resource.TestCheckResourceAttr( + "digitalocean_record.foobar", "fqdn", strings.Join([]string{"terraform", domain}, ".")), ), }, }, diff --git a/website/source/docs/providers/do/r/record.html.markdown b/website/source/docs/providers/do/r/record.html.markdown index 938fb5a591b2..11cd1b2a5fdc 100644 --- a/website/source/docs/providers/do/r/record.html.markdown +++ b/website/source/docs/providers/do/r/record.html.markdown @@ -45,4 +45,5 @@ The following arguments are supported: The following attributes are exported: * `id` - The record ID +* `fqdn` - The FQDN of the record