Skip to content

Commit

Permalink
DNSv2: Format IPv6 Records (#443)
Browse files Browse the repository at this point in the history
This commit will format IPv6 records in a way that the DNSaaS /
Designate service expects them to be. Notably, it strips the brackets
from the addresses.

Passing IPv6 records with brackets already removed still works.
  • Loading branch information
jtopjian committed Oct 16, 2018
1 parent ce4d90d commit 7aeea85
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 30 deletions.
52 changes: 38 additions & 14 deletions openstack/resource_openstack_dns_recordset_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package openstack
import (
"fmt"
"log"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -52,10 +53,11 @@ func resourceDNSRecordSetV2() *schema.Resource {
ForceNew: false,
},
"records": &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: false,
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeList,
Optional: true,
ForceNew: false,
Elem: &schema.Schema{Type: schema.TypeString},
DiffSuppressFunc: suppressRecordsDiffs,
},
"ttl": &schema.Schema{
Type: schema.TypeInt,
Expand Down Expand Up @@ -85,11 +87,7 @@ func resourceDNSRecordSetV2Create(d *schema.ResourceData, meta interface{}) erro
return fmt.Errorf("Error creating OpenStack DNS client: %s", err)
}

recordsraw := d.Get("records").([]interface{})
records := make([]string, len(recordsraw))
for i, recordraw := range recordsraw {
records[i] = recordraw.(string)
}
records := formatDNSV2Records(d.Get("records").([]interface{}))

createOpts := RecordSetCreateOpts{
recordsets.CreateOpts{
Expand Down Expand Up @@ -173,11 +171,7 @@ func resourceDNSRecordSetV2Update(d *schema.ResourceData, meta interface{}) erro
}

if d.HasChange("records") {
recordsraw := d.Get("records").([]interface{})
records := make([]string, len(recordsraw))
for i, recordraw := range recordsraw {
records[i] = recordraw.(string)
}
records := formatDNSV2Records(d.Get("records").([]interface{}))
updateOpts.Records = records
}

Expand Down Expand Up @@ -274,3 +268,33 @@ func parseDNSV2RecordSetID(id string) (string, string, error) {

return zoneID, recordsetID, nil
}

func formatDNSV2Records(recordsraw []interface{}) []string {
records := make([]string, len(recordsraw))

// Strip out any [ ] characters in the address.
// This is to format IPv6 records in a way that DNSaaS / Designate wants.
re := regexp.MustCompile("[][]")
for i, recordraw := range recordsraw {
record := recordraw.(string)
record = re.ReplaceAllString(record, "")
records[i] = record
}

return records
}

// suppressRecordsDiffs will suppress diffs when the format of a record
// is different yet still a valid DNS record. For example, if a user
// specifies an IPv6 address using [bracket] notation, but the record is
// returned without brackets, it is still a valid record.
func suppressRecordsDiffs(k, old, new string, d *schema.ResourceData) bool {
re := regexp.MustCompile("[][]")
new = re.ReplaceAllString(new, "")

if old == new {
return true
}

return false
}
36 changes: 20 additions & 16 deletions openstack/resource_openstack_dns_recordset_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestAccDNSV2RecordSet_basic(t *testing.T) {
})
}

func TestAccDNSV2RecordSet_readTTL(t *testing.T) {
func TestAccDNSV2RecordSet_ipv6(t *testing.T) {
var recordset recordsets.RecordSet
zoneName := randomZoneName()

Expand All @@ -61,18 +61,22 @@ func TestAccDNSV2RecordSet_readTTL(t *testing.T) {
CheckDestroy: testAccCheckDNSV2RecordSetDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDNSV2RecordSet_readTTL(zoneName),
Config: testAccDNSV2RecordSet_ipv6(zoneName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDNSV2RecordSetExists("openstack_dns_recordset_v2.recordset_1", &recordset),
resource.TestMatchResourceAttr(
"openstack_dns_recordset_v2.recordset_1", "ttl", regexp.MustCompile("^[0-9]+$")),
resource.TestCheckResourceAttr(
"openstack_dns_recordset_v2.recordset_1", "description", "a record set"),
resource.TestCheckResourceAttr(
"openstack_dns_recordset_v2.recordset_1", "records.0", "fd2b:db7f:6ae:dd8d::1"),
resource.TestCheckResourceAttr(
"openstack_dns_recordset_v2.recordset_1", "records.1", "fd2b:db7f:6ae:dd8d::2"),
),
},
},
})
}

func TestAccDNSV2RecordSet_timeout(t *testing.T) {
func TestAccDNSV2RecordSet_readTTL(t *testing.T) {
var recordset recordsets.RecordSet
zoneName := randomZoneName()

Expand All @@ -82,9 +86,11 @@ func TestAccDNSV2RecordSet_timeout(t *testing.T) {
CheckDestroy: testAccCheckDNSV2RecordSetDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDNSV2RecordSet_timeout(zoneName),
Config: testAccDNSV2RecordSet_readTTL(zoneName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDNSV2RecordSetExists("openstack_dns_recordset_v2.recordset_1", &recordset),
resource.TestMatchResourceAttr(
"openstack_dns_recordset_v2.recordset_1", "ttl", regexp.MustCompile("^[0-9]+$")),
),
},
},
Expand Down Expand Up @@ -215,28 +221,26 @@ func testAccDNSV2RecordSet_readTTL(zoneName string) string {
`, zoneName, zoneName)
}

func testAccDNSV2RecordSet_timeout(zoneName string) string {
func testAccDNSV2RecordSet_ipv6(zoneName string) string {
return fmt.Sprintf(`
resource "openstack_dns_zone_v2" "zone_1" {
name = "%s"
email = "email2@example.com"
description = "an updated zone"
description = "a zone"
ttl = 6000
type = "PRIMARY"
}
resource "openstack_dns_recordset_v2" "recordset_1" {
zone_id = "${openstack_dns_zone_v2.zone_1.id}"
name = "%s"
type = "A"
type = "AAAA"
description = "a record set"
ttl = 3000
records = ["10.1.0.3"]
timeouts {
create = "5m"
update = "5m"
delete = "5m"
}
records = [
"[fd2b:db7f:6ae:dd8d::1]",
"fd2b:db7f:6ae:dd8d::2"
]
}
`, zoneName, zoneName)
}

0 comments on commit 7aeea85

Please sign in to comment.