Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNSv2: Format IPv6 Records #443

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}