Skip to content

Commit

Permalink
Introduce ignore_errors argument to all data sources
Browse files Browse the repository at this point in the history
Signed-off-by: Sergei Ivanov <sergei_ivanov@mail.ru>
  • Loading branch information
sergei-ivanov committed Jun 17, 2019
1 parent fdfbe50 commit b77c566
Show file tree
Hide file tree
Showing 22 changed files with 229 additions and 24 deletions.
11 changes: 10 additions & 1 deletion dns/data_dns_a_record_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ func dataSourceDnsARecordSet() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"ignore_errors": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"addrs": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Expand All @@ -26,11 +31,15 @@ func dataSourceDnsARecordSet() *schema.Resource {

func dataSourceDnsARecordSetRead(d *schema.ResourceData, meta interface{}) error {
host := d.Get("host").(string)
ignore := d.Get("ignore_errors").(bool)

a, _, err := lookupIP(host)
if err != nil {
if err != nil && !ignore {
return fmt.Errorf("error looking up A records for %q: %s", host, err)
}
if a == nil {
a = []string{}
}
sort.Strings(a)

d.Set("addrs", a)
Expand Down
12 changes: 11 additions & 1 deletion dns/data_dns_a_record_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ func TestAccDataDnsARecordSet_Basic(t *testing.T) {
},
"time-c.nist.gov",
},
{
`
data "dns_a_record_set" "non-existent" {
host = "jolly.roger"
ignore_errors = true
}
`,
"non-existent",
[]string{},
"jolly.roger",
},
}

for _, test := range tests {
Expand All @@ -61,5 +72,4 @@ func TestAccDataDnsARecordSet_Basic(t *testing.T) {
},
})
}

}
11 changes: 10 additions & 1 deletion dns/data_dns_aaaa_record_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ func dataSourceDnsAAAARecordSet() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"ignore_errors": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"addrs": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Expand All @@ -26,11 +31,15 @@ func dataSourceDnsAAAARecordSet() *schema.Resource {

func dataSourceDnsAAAARecordSetRead(d *schema.ResourceData, meta interface{}) error {
host := d.Get("host").(string)
ignore := d.Get("ignore_errors").(bool)

_, aaaa, err := lookupIP(host)
if err != nil {
if err != nil && !ignore {
return fmt.Errorf("error looking up AAAA records for %q: %s", host, err)
}
if aaaa == nil {
aaaa = []string{}
}
sort.Strings(aaaa)

d.Set("addrs", aaaa)
Expand Down
12 changes: 11 additions & 1 deletion dns/data_dns_aaaa_record_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ func TestAccDataDnsAAAARecordSet_Basic(t *testing.T) {
},
"example.com",
},
{
`
data "dns_aaaa_record_set" "non-existent" {
host = "jolly.roger"
ignore_errors = true
}
`,
"non-existent",
[]string{},
"jolly.roger",
},
}

for _, test := range tests {
Expand All @@ -49,5 +60,4 @@ func TestAccDataDnsAAAARecordSet_Basic(t *testing.T) {
},
})
}

}
9 changes: 7 additions & 2 deletions dns/data_dns_cname_record_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ func dataSourceDnsCnameRecordSet() *schema.Resource {
Required: true,
ForceNew: true,
},

"ignore_errors": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"cname": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -28,9 +32,10 @@ func dataSourceDnsCnameRecordSet() *schema.Resource {

func dataSourceDnsCnameRecordSetRead(d *schema.ResourceData, meta interface{}) error {
host := d.Get("host").(string)
ignore := d.Get("ignore_errors").(bool)

cname, err := net.LookupCNAME(host)
if err != nil {
if err != nil && !ignore {
return fmt.Errorf("error looking up CNAME records for %q: %s", host, err)
}

Expand Down
20 changes: 18 additions & 2 deletions dns/data_dns_cname_record_set_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dns

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
Expand All @@ -9,6 +10,7 @@ import (
func TestAccDataDnsCnameRecordSet_Basic(t *testing.T) {
tests := []struct {
DataSourceBlock string
DataSourceName string
Expected string
Host string
}{
Expand All @@ -18,25 +20,39 @@ func TestAccDataDnsCnameRecordSet_Basic(t *testing.T) {
host = "www.hashicorp.com"
}
`,
"foo",
"hashicorp.netlifyglobalcdn.com.",
"www.hashicorp.com",
},
{
`
data "dns_cname_record_set" "non-existent" {
host = "jolly.roger"
ignore_errors = true
}
`,
"non-existent",
"",
"jolly.roger",
},
}

for _, test := range tests {
recordName := fmt.Sprintf("data.dns_cname_record_set.%s", test.DataSourceName)

resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: test.DataSourceBlock,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.dns_cname_record_set.foo", "cname", test.Expected),
resource.TestCheckResourceAttr(recordName, "cname", test.Expected),
),
},
{
Config: test.DataSourceBlock,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.dns_cname_record_set.foo", "id", test.Host),
resource.TestCheckResourceAttr(recordName, "id", test.Host),
),
},
},
Expand Down
11 changes: 10 additions & 1 deletion dns/data_dns_mx_record_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ func dataSourceDnsMXRecordSet() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"ignore_errors": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"mx": &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Resource{
Expand All @@ -38,11 +43,15 @@ func dataSourceDnsMXRecordSet() *schema.Resource {

func dataSourceDnsMXRecordSetRead(d *schema.ResourceData, meta interface{}) error {
domain := d.Get("domain").(string)
ignore := d.Get("ignore_errors").(bool)

records, err := net.LookupMX(domain)
if err != nil {
if err != nil && !ignore {
return fmt.Errorf("error looking up MX records for %q: %s", domain, err)
}
if records == nil {
records = []*net.MX{}
}

// Sort by preference ascending, and host alphabetically
sort.Slice(records, func(i, j int) bool {
Expand Down
11 changes: 11 additions & 0 deletions dns/data_dns_mx_record_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ func TestAccDataDnsMXRecordSet_Basic(t *testing.T) {
},
"google.com",
},
{
`
data "dns_mx_record_set" "non-existent" {
domain = "jolly.roger"
ignore_errors = true
}
`,
"non-existent",
[]map[string]string{},
"jolly.roger",
},
}

for _, test := range tests {
Expand Down
11 changes: 10 additions & 1 deletion dns/data_dns_ns_record_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ func dataSourceDnsNSRecordSet() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"ignore_errors": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"nameservers": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Expand All @@ -27,11 +32,15 @@ func dataSourceDnsNSRecordSet() *schema.Resource {

func dataSourceDnsNSRecordSetRead(d *schema.ResourceData, meta interface{}) error {
host := d.Get("host").(string)
ignore := d.Get("ignore_errors").(bool)

nsRecords, err := net.LookupNS(host)
if err != nil {
if err != nil && !ignore {
return fmt.Errorf("error looking up NS records for %q: %s", host, err)
}
if nsRecords == nil {
nsRecords = []*net.NS{}
}

nameservers := make([]string, len(nsRecords))
for i, record := range nsRecords {
Expand Down
14 changes: 12 additions & 2 deletions dns/data_dns_ns_record_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,23 @@ func TestAccDataDnsNSRecordSet_Basic(t *testing.T) {
[]string{
// These results may change if terraform.io moves to a new DNS host.
// If you suspect the expected results have changed here, confirm
// with e.g. dig terraform.io NS
// with e.g. `dig terraform.io NS`
"sam.ns.cloudflare.com.",
"zara.ns.cloudflare.com.",
},
"terraform.io",
},
{
`
data "dns_ns_record_set" "non-existent" {
host = "jolly.roger"
ignore_errors = true
}
`,
"non-existent",
[]string{},
"jolly.roger",
},
}

for _, test := range tests {
Expand All @@ -53,5 +64,4 @@ func TestAccDataDnsNSRecordSet_Basic(t *testing.T) {
},
})
}

}
16 changes: 13 additions & 3 deletions dns/data_dns_ptr_record_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ func dataSourceDnsPtrRecordSet() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"ignore_errors": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"ptr": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -25,15 +30,20 @@ func dataSourceDnsPtrRecordSet() *schema.Resource {

func dataSourceDnsPtrRecordSetRead(d *schema.ResourceData, meta interface{}) error {
ipAddress := d.Get("ip_address").(string)
ignore := d.Get("ignore_errors").(bool)

names, err := net.LookupAddr(ipAddress)
if err != nil {
if err != nil && !ignore {
return fmt.Errorf("error looking up PTR records for %q: %s", ipAddress, err)
}
if len(names) == 0 {
if len(names) > 0 {
d.Set("ptr", names[0])
} else if ignore {
d.Set("ptr", "")
} else {
return fmt.Errorf("error looking up PTR records for %q: no records found", ipAddress)
}

d.Set("ptr", names[0])
d.SetId(ipAddress)

return nil
Expand Down
20 changes: 18 additions & 2 deletions dns/data_dns_ptr_record_set_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dns

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
Expand All @@ -9,6 +10,7 @@ import (
func TestAccDataDnsPtrRecordSet_Basic(t *testing.T) {
tests := []struct {
DataSourceBlock string
DataSourceName string
Expected string
IPAddress string
}{
Expand All @@ -18,25 +20,39 @@ func TestAccDataDnsPtrRecordSet_Basic(t *testing.T) {
ip_address = "8.8.8.8"
}
`,
"foo",
"google-public-dns-a.google.com.",
"8.8.8.8",
},
{
`
data "dns_ptr_record_set" "non-existent" {
ip_address = "255.255.255.255"
ignore_errors = true
}
`,
"non-existent",
"",
"255.255.255.255",
},
}

for _, test := range tests {
recordName := fmt.Sprintf("data.dns_ptr_record_set.%s", test.DataSourceName)

resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: test.DataSourceBlock,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.dns_ptr_record_set.foo", "ptr", test.Expected),
resource.TestCheckResourceAttr(recordName, "ptr", test.Expected),
),
},
{
Config: test.DataSourceBlock,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.dns_ptr_record_set.foo", "id", test.IPAddress),
resource.TestCheckResourceAttr(recordName, "id", test.IPAddress),
),
},
},
Expand Down
Loading

0 comments on commit b77c566

Please sign in to comment.