diff --git a/azurerm/internal/services/dns/parse/dns_a_record.go b/azurerm/internal/services/dns/parse/dns_a_record.go new file mode 100644 index 000000000000..dd85593c276f --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_a_record.go @@ -0,0 +1,38 @@ +package parse + +import ( + "fmt" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" +) + +type DnsARecordId struct { + ResourceGroup string + ZoneName string + Name string +} + +func DnsARecordID(input string) (*DnsARecordId, error) { + id, err := azure.ParseAzureResourceID(input) + if err != nil { + return nil, fmt.Errorf("[ERROR] Unable to parse DNS A Record ID %q: %+v", input, err) + } + + record := DnsARecordId{ + ResourceGroup: id.ResourceGroup, + } + + if record.ZoneName, err = id.PopSegment("dnszones"); err != nil { + return nil, err + } + + if record.Name, err = id.PopSegment("A"); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &record, nil +} diff --git a/azurerm/internal/services/dns/parse/dns_a_record_test.go b/azurerm/internal/services/dns/parse/dns_a_record_test.go new file mode 100644 index 000000000000..cd48c1c80645 --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_a_record_test.go @@ -0,0 +1,86 @@ +package parse + +import ( + "testing" +) + +func TestDnsARecordId(t *testing.T) { + testData := []struct { + Name string + Input string + Expected *DnsARecordId + }{ + { + Name: "Empty", + Input: "", + Expected: nil, + }, + { + Name: "No Resource Groups Segment", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000", + Expected: nil, + }, + { + Name: "No Resource Groups Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/", + Expected: nil, + }, + { + Name: "Resource Group ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/", + Expected: nil, + }, + { + Name: "Missing DNS Zones Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/", + Expected: nil, + }, + { + Name: "DNS Zone ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1", + Expected: nil, + }, + { + Name: "Missing A Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/A/", + Expected: nil, + }, + { + Name: "DNS A Record ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/A/myrecord1", + Expected: &DnsARecordId{ + ResourceGroup: "resGroup1", + ZoneName: "zone1", + Name: "myrecord1", + }, + }, + { + Name: "Wrong Casing", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/a/myrecord1", + Expected: nil, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Name) + + actual, err := DnsARecordID(v.Input) + if err != nil { + if v.Expected == nil { + continue + } + + t.Fatalf("Expected a value but got an error: %s", err) + } + + if actual.Name != v.Expected.Name { + t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) + } + if actual.ZoneName != v.Expected.ZoneName { + t.Fatalf("Expected %q but got %q for ZoneName", v.Expected.ZoneName, actual.ZoneName) + } + if actual.ResourceGroup != v.Expected.ResourceGroup { + t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup) + } + } +} diff --git a/azurerm/internal/services/dns/parse/dns_aaaa_record.go b/azurerm/internal/services/dns/parse/dns_aaaa_record.go new file mode 100644 index 000000000000..e9b7b1bb10a3 --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_aaaa_record.go @@ -0,0 +1,38 @@ +package parse + +import ( + "fmt" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" +) + +type DnsAaaaRecordId struct { + ResourceGroup string + ZoneName string + Name string +} + +func DnsAaaaRecordID(input string) (*DnsAaaaRecordId, error) { + id, err := azure.ParseAzureResourceID(input) + if err != nil { + return nil, fmt.Errorf("[ERROR] Unable to parse DNS AAAA Record ID %q: %+v", input, err) + } + + record := DnsAaaaRecordId{ + ResourceGroup: id.ResourceGroup, + } + + if record.ZoneName, err = id.PopSegment("dnszones"); err != nil { + return nil, err + } + + if record.Name, err = id.PopSegment("AAAA"); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &record, nil +} diff --git a/azurerm/internal/services/dns/parse/dns_aaaa_record_test.go b/azurerm/internal/services/dns/parse/dns_aaaa_record_test.go new file mode 100644 index 000000000000..36dc9458b744 --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_aaaa_record_test.go @@ -0,0 +1,86 @@ +package parse + +import ( + "testing" +) + +func TestDnsAAAARecordId(t *testing.T) { + testData := []struct { + Name string + Input string + Expected *DnsAaaaRecordId + }{ + { + Name: "Empty", + Input: "", + Expected: nil, + }, + { + Name: "No Resource Groups Segment", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000", + Expected: nil, + }, + { + Name: "No Resource Groups Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/", + Expected: nil, + }, + { + Name: "Resource Group ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/", + Expected: nil, + }, + { + Name: "Missing DNS Zones Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/", + Expected: nil, + }, + { + Name: "DNS Zone ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1", + Expected: nil, + }, + { + Name: "Missing AAAA Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/AAAA/", + Expected: nil, + }, + { + Name: "DNS AAAA Record ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/AAAA/myrecord1", + Expected: &DnsAaaaRecordId{ + ResourceGroup: "resGroup1", + ZoneName: "zone1", + Name: "myrecord1", + }, + }, + { + Name: "Wrong Casing", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/aaaa/myrecord1", + Expected: nil, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Name) + + actual, err := DnsAaaaRecordID(v.Input) + if err != nil { + if v.Expected == nil { + continue + } + + t.Fatalf("Expected a value but got an error: %s", err) + } + + if actual.Name != v.Expected.Name { + t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) + } + if actual.ZoneName != v.Expected.ZoneName { + t.Fatalf("Expected %q but got %q for ZoneName", v.Expected.ZoneName, actual.ZoneName) + } + if actual.ResourceGroup != v.Expected.ResourceGroup { + t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup) + } + } +} diff --git a/azurerm/internal/services/dns/parse/dns_caa_record.go b/azurerm/internal/services/dns/parse/dns_caa_record.go new file mode 100644 index 000000000000..1ae6b4afc175 --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_caa_record.go @@ -0,0 +1,38 @@ +package parse + +import ( + "fmt" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" +) + +type DnsCaaRecordId struct { + ResourceGroup string + ZoneName string + Name string +} + +func DnsCaaRecordID(input string) (*DnsCaaRecordId, error) { + id, err := azure.ParseAzureResourceID(input) + if err != nil { + return nil, fmt.Errorf("[ERROR] Unable to parse DNS CAA Record ID %q: %+v", input, err) + } + + record := DnsCaaRecordId{ + ResourceGroup: id.ResourceGroup, + } + + if record.ZoneName, err = id.PopSegment("dnszones"); err != nil { + return nil, err + } + + if record.Name, err = id.PopSegment("CAA"); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &record, nil +} diff --git a/azurerm/internal/services/dns/parse/dns_caa_record_test.go b/azurerm/internal/services/dns/parse/dns_caa_record_test.go new file mode 100644 index 000000000000..0ee8c66689c7 --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_caa_record_test.go @@ -0,0 +1,86 @@ +package parse + +import ( + "testing" +) + +func TestDnsCaaRecordId(t *testing.T) { + testData := []struct { + Name string + Input string + Expected *DnsCaaRecordId + }{ + { + Name: "Empty", + Input: "", + Expected: nil, + }, + { + Name: "No Resource Groups Segment", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000", + Expected: nil, + }, + { + Name: "No Resource Groups Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/", + Expected: nil, + }, + { + Name: "Resource Group ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/", + Expected: nil, + }, + { + Name: "Missing DNS Zones Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/", + Expected: nil, + }, + { + Name: "DNS Zone ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1", + Expected: nil, + }, + { + Name: "Missing CAA Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/CAA/", + Expected: nil, + }, + { + Name: "DNS CAA Record ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/CAA/myrecord1", + Expected: &DnsCaaRecordId{ + ResourceGroup: "resGroup1", + ZoneName: "zone1", + Name: "myrecord1", + }, + }, + { + Name: "Wrong Casing", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/caa/myrecord1", + Expected: nil, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Name) + + actual, err := DnsCaaRecordID(v.Input) + if err != nil { + if v.Expected == nil { + continue + } + + t.Fatalf("Expected a value but got an error: %s", err) + } + + if actual.Name != v.Expected.Name { + t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) + } + if actual.ZoneName != v.Expected.ZoneName { + t.Fatalf("Expected %q but got %q for ZoneName", v.Expected.ZoneName, actual.ZoneName) + } + if actual.ResourceGroup != v.Expected.ResourceGroup { + t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup) + } + } +} diff --git a/azurerm/internal/services/dns/parse/dns_cname_record.go b/azurerm/internal/services/dns/parse/dns_cname_record.go new file mode 100644 index 000000000000..f43d8c4ff0dc --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_cname_record.go @@ -0,0 +1,38 @@ +package parse + +import ( + "fmt" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" +) + +type DnsCNameRecordId struct { + ResourceGroup string + ZoneName string + Name string +} + +func DnsCNameRecordID(input string) (*DnsCNameRecordId, error) { + id, err := azure.ParseAzureResourceID(input) + if err != nil { + return nil, fmt.Errorf("[ERROR] Unable to parse DNS CNAME Record ID %q: %+v", input, err) + } + + record := DnsCNameRecordId{ + ResourceGroup: id.ResourceGroup, + } + + if record.ZoneName, err = id.PopSegment("dnszones"); err != nil { + return nil, err + } + + if record.Name, err = id.PopSegment("CNAME"); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &record, nil +} diff --git a/azurerm/internal/services/dns/parse/dns_cname_record_test.go b/azurerm/internal/services/dns/parse/dns_cname_record_test.go new file mode 100644 index 000000000000..b0f4d55157b8 --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_cname_record_test.go @@ -0,0 +1,86 @@ +package parse + +import ( + "testing" +) + +func TestDnsCNameRecordId(t *testing.T) { + testData := []struct { + Name string + Input string + Expected *DnsCNameRecordId + }{ + { + Name: "Empty", + Input: "", + Expected: nil, + }, + { + Name: "No Resource Groups Segment", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000", + Expected: nil, + }, + { + Name: "No Resource Groups Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/", + Expected: nil, + }, + { + Name: "Resource Group ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/", + Expected: nil, + }, + { + Name: "Missing DNS Zones Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/", + Expected: nil, + }, + { + Name: "DNS Zone ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1", + Expected: nil, + }, + { + Name: "Missing CNAME Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/CNAME/", + Expected: nil, + }, + { + Name: "DNS CNAME Record ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/CNAME/myrecord1", + Expected: &DnsCNameRecordId{ + ResourceGroup: "resGroup1", + ZoneName: "zone1", + Name: "myrecord1", + }, + }, + { + Name: "Wrong Casing", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/cname/myrecord1", + Expected: nil, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Name) + + actual, err := DnsCNameRecordID(v.Input) + if err != nil { + if v.Expected == nil { + continue + } + + t.Fatalf("Expected a value but got an error: %s", err) + } + + if actual.Name != v.Expected.Name { + t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) + } + if actual.ZoneName != v.Expected.ZoneName { + t.Fatalf("Expected %q but got %q for ZoneName", v.Expected.ZoneName, actual.ZoneName) + } + if actual.ResourceGroup != v.Expected.ResourceGroup { + t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup) + } + } +} diff --git a/azurerm/internal/services/dns/parse/dns_mx_record.go b/azurerm/internal/services/dns/parse/dns_mx_record.go new file mode 100644 index 000000000000..17bb2798b541 --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_mx_record.go @@ -0,0 +1,38 @@ +package parse + +import ( + "fmt" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" +) + +type DnsMxRecordId struct { + ResourceGroup string + ZoneName string + Name string +} + +func DnsMxRecordID(input string) (*DnsMxRecordId, error) { + id, err := azure.ParseAzureResourceID(input) + if err != nil { + return nil, fmt.Errorf("[ERROR] Unable to parse DNS MX Record ID %q: %+v", input, err) + } + + record := DnsMxRecordId{ + ResourceGroup: id.ResourceGroup, + } + + if record.ZoneName, err = id.PopSegment("dnszones"); err != nil { + return nil, err + } + + if record.Name, err = id.PopSegment("MX"); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &record, nil +} diff --git a/azurerm/internal/services/dns/parse/dns_mx_record_test.go b/azurerm/internal/services/dns/parse/dns_mx_record_test.go new file mode 100644 index 000000000000..2b565b675c5d --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_mx_record_test.go @@ -0,0 +1,86 @@ +package parse + +import ( + "testing" +) + +func TestDnsMxRecordId(t *testing.T) { + testData := []struct { + Name string + Input string + Expected *DnsMxRecordId + }{ + { + Name: "Empty", + Input: "", + Expected: nil, + }, + { + Name: "No Resource Groups Segment", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000", + Expected: nil, + }, + { + Name: "No Resource Groups Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/", + Expected: nil, + }, + { + Name: "Resource Group ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/", + Expected: nil, + }, + { + Name: "Missing DNS Zones Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/", + Expected: nil, + }, + { + Name: "DNS Zone ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1", + Expected: nil, + }, + { + Name: "Missing MX Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/MX/", + Expected: nil, + }, + { + Name: "DNS MX Record ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/MX/myrecord1", + Expected: &DnsMxRecordId{ + ResourceGroup: "resGroup1", + ZoneName: "zone1", + Name: "myrecord1", + }, + }, + { + Name: "Wrong Casing", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/mx/myrecord1", + Expected: nil, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Name) + + actual, err := DnsMxRecordID(v.Input) + if err != nil { + if v.Expected == nil { + continue + } + + t.Fatalf("Expected a value but got an error: %s", err) + } + + if actual.Name != v.Expected.Name { + t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) + } + if actual.ZoneName != v.Expected.ZoneName { + t.Fatalf("Expected %q but got %q for ZoneName", v.Expected.ZoneName, actual.ZoneName) + } + if actual.ResourceGroup != v.Expected.ResourceGroup { + t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup) + } + } +} diff --git a/azurerm/internal/services/dns/parse/dns_ns_record.go b/azurerm/internal/services/dns/parse/dns_ns_record.go new file mode 100644 index 000000000000..4bef6f9ea0b7 --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_ns_record.go @@ -0,0 +1,38 @@ +package parse + +import ( + "fmt" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" +) + +type DnsNsRecordId struct { + ResourceGroup string + ZoneName string + Name string +} + +func DnsNsRecordID(input string) (*DnsNsRecordId, error) { + id, err := azure.ParseAzureResourceID(input) + if err != nil { + return nil, fmt.Errorf("[ERROR] Unable to parse DNS NS Record ID %q: %+v", input, err) + } + + record := DnsNsRecordId{ + ResourceGroup: id.ResourceGroup, + } + + if record.ZoneName, err = id.PopSegment("dnszones"); err != nil { + return nil, err + } + + if record.Name, err = id.PopSegment("NS"); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &record, nil +} diff --git a/azurerm/internal/services/dns/parse/dns_ns_record_test.go b/azurerm/internal/services/dns/parse/dns_ns_record_test.go new file mode 100644 index 000000000000..35b2ce5c37f1 --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_ns_record_test.go @@ -0,0 +1,86 @@ +package parse + +import ( + "testing" +) + +func TestDnsNsRecordId(t *testing.T) { + testData := []struct { + Name string + Input string + Expected *DnsNsRecordId + }{ + { + Name: "Empty", + Input: "", + Expected: nil, + }, + { + Name: "No Resource Groups Segment", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000", + Expected: nil, + }, + { + Name: "No Resource Groups Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/", + Expected: nil, + }, + { + Name: "Resource Group ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/", + Expected: nil, + }, + { + Name: "Missing DNS Zones Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/", + Expected: nil, + }, + { + Name: "DNS Zone ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1", + Expected: nil, + }, + { + Name: "Missing NS Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/NS/", + Expected: nil, + }, + { + Name: "DNS NS Record ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/NS/myrecord1", + Expected: &DnsNsRecordId{ + ResourceGroup: "resGroup1", + ZoneName: "zone1", + Name: "myrecord1", + }, + }, + { + Name: "Wrong Casing", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/ns/myrecord1", + Expected: nil, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Name) + + actual, err := DnsNsRecordID(v.Input) + if err != nil { + if v.Expected == nil { + continue + } + + t.Fatalf("Expected a value but got an error: %s", err) + } + + if actual.Name != v.Expected.Name { + t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) + } + if actual.ZoneName != v.Expected.ZoneName { + t.Fatalf("Expected %q but got %q for ZoneName", v.Expected.ZoneName, actual.ZoneName) + } + if actual.ResourceGroup != v.Expected.ResourceGroup { + t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup) + } + } +} diff --git a/azurerm/internal/services/dns/parse/dns_ptr_record.go b/azurerm/internal/services/dns/parse/dns_ptr_record.go new file mode 100644 index 000000000000..0926b71e3890 --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_ptr_record.go @@ -0,0 +1,38 @@ +package parse + +import ( + "fmt" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" +) + +type DnsPtrRecordId struct { + ResourceGroup string + ZoneName string + Name string +} + +func DnsPtrRecordID(input string) (*DnsPtrRecordId, error) { + id, err := azure.ParseAzureResourceID(input) + if err != nil { + return nil, fmt.Errorf("[ERROR] Unable to parse DNS PTR Record ID %q: %+v", input, err) + } + + record := DnsPtrRecordId{ + ResourceGroup: id.ResourceGroup, + } + + if record.ZoneName, err = id.PopSegment("dnszones"); err != nil { + return nil, err + } + + if record.Name, err = id.PopSegment("PTR"); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &record, nil +} diff --git a/azurerm/internal/services/dns/parse/dns_ptr_record_test.go b/azurerm/internal/services/dns/parse/dns_ptr_record_test.go new file mode 100644 index 000000000000..7dfd26213717 --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_ptr_record_test.go @@ -0,0 +1,86 @@ +package parse + +import ( + "testing" +) + +func TestDnsPtrRecordId(t *testing.T) { + testData := []struct { + Name string + Input string + Expected *DnsPtrRecordId + }{ + { + Name: "Empty", + Input: "", + Expected: nil, + }, + { + Name: "No Resource Groups Segment", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000", + Expected: nil, + }, + { + Name: "No Resource Groups Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/", + Expected: nil, + }, + { + Name: "Resource Group ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/", + Expected: nil, + }, + { + Name: "Missing DNS Zones Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/", + Expected: nil, + }, + { + Name: "DNS Zone ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1", + Expected: nil, + }, + { + Name: "Missing PTR Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/PTR/", + Expected: nil, + }, + { + Name: "DNS PTR Record ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/PTR/myrecord1", + Expected: &DnsPtrRecordId{ + ResourceGroup: "resGroup1", + ZoneName: "zone1", + Name: "myrecord1", + }, + }, + { + Name: "Wrong Casing", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/ptr/myrecord1", + Expected: nil, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Name) + + actual, err := DnsPtrRecordID(v.Input) + if err != nil { + if v.Expected == nil { + continue + } + + t.Fatalf("Expected a value but got an error: %s", err) + } + + if actual.Name != v.Expected.Name { + t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) + } + if actual.ZoneName != v.Expected.ZoneName { + t.Fatalf("Expected %q but got %q for ZoneName", v.Expected.ZoneName, actual.ZoneName) + } + if actual.ResourceGroup != v.Expected.ResourceGroup { + t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup) + } + } +} diff --git a/azurerm/internal/services/dns/parse/dns_srv_record.go b/azurerm/internal/services/dns/parse/dns_srv_record.go new file mode 100644 index 000000000000..ec4d9802705c --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_srv_record.go @@ -0,0 +1,38 @@ +package parse + +import ( + "fmt" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" +) + +type DnsSrvRecordId struct { + ResourceGroup string + ZoneName string + Name string +} + +func DnsSrvRecordID(input string) (*DnsSrvRecordId, error) { + id, err := azure.ParseAzureResourceID(input) + if err != nil { + return nil, fmt.Errorf("[ERROR] Unable to parse DNS SRV Record ID %q: %+v", input, err) + } + + record := DnsSrvRecordId{ + ResourceGroup: id.ResourceGroup, + } + + if record.ZoneName, err = id.PopSegment("dnszones"); err != nil { + return nil, err + } + + if record.Name, err = id.PopSegment("SRV"); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &record, nil +} diff --git a/azurerm/internal/services/dns/parse/dns_srv_record_test.go b/azurerm/internal/services/dns/parse/dns_srv_record_test.go new file mode 100644 index 000000000000..00f6131a7c3a --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_srv_record_test.go @@ -0,0 +1,86 @@ +package parse + +import ( + "testing" +) + +func TestDnsSrvRecordId(t *testing.T) { + testData := []struct { + Name string + Input string + Expected *DnsSrvRecordId + }{ + { + Name: "Empty", + Input: "", + Expected: nil, + }, + { + Name: "No Resource Groups Segment", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000", + Expected: nil, + }, + { + Name: "No Resource Groups Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/", + Expected: nil, + }, + { + Name: "Resource Group ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/", + Expected: nil, + }, + { + Name: "Missing DNS Zones Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/", + Expected: nil, + }, + { + Name: "DNS Zone ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1", + Expected: nil, + }, + { + Name: "Missing SRV Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/SRV/", + Expected: nil, + }, + { + Name: "DNS SRV Record ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/SRV/myrecord1", + Expected: &DnsSrvRecordId{ + ResourceGroup: "resGroup1", + ZoneName: "zone1", + Name: "myrecord1", + }, + }, + { + Name: "Wrong Casing", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/srv/myrecord1", + Expected: nil, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Name) + + actual, err := DnsSrvRecordID(v.Input) + if err != nil { + if v.Expected == nil { + continue + } + + t.Fatalf("Expected a value but got an error: %s", err) + } + + if actual.Name != v.Expected.Name { + t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) + } + if actual.ZoneName != v.Expected.ZoneName { + t.Fatalf("Expected %q but got %q for ZoneName", v.Expected.ZoneName, actual.ZoneName) + } + if actual.ResourceGroup != v.Expected.ResourceGroup { + t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup) + } + } +} diff --git a/azurerm/internal/services/dns/parse/dns_txt_record.go b/azurerm/internal/services/dns/parse/dns_txt_record.go new file mode 100644 index 000000000000..dc4344ac78b9 --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_txt_record.go @@ -0,0 +1,38 @@ +package parse + +import ( + "fmt" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" +) + +type DnsTxtRecordId struct { + ResourceGroup string + ZoneName string + Name string +} + +func DnsTxtRecordID(input string) (*DnsTxtRecordId, error) { + id, err := azure.ParseAzureResourceID(input) + if err != nil { + return nil, fmt.Errorf("[ERROR] Unable to parse DNS TXT Record ID %q: %+v", input, err) + } + + record := DnsTxtRecordId{ + ResourceGroup: id.ResourceGroup, + } + + if record.ZoneName, err = id.PopSegment("dnszones"); err != nil { + return nil, err + } + + if record.Name, err = id.PopSegment("TXT"); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &record, nil +} diff --git a/azurerm/internal/services/dns/parse/dns_txt_record_test.go b/azurerm/internal/services/dns/parse/dns_txt_record_test.go new file mode 100644 index 000000000000..6c32e244024d --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_txt_record_test.go @@ -0,0 +1,86 @@ +package parse + +import ( + "testing" +) + +func TestDnsTxtRecordId(t *testing.T) { + testData := []struct { + Name string + Input string + Expected *DnsTxtRecordId + }{ + { + Name: "Empty", + Input: "", + Expected: nil, + }, + { + Name: "No Resource Groups Segment", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000", + Expected: nil, + }, + { + Name: "No Resource Groups Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/", + Expected: nil, + }, + { + Name: "Resource Group ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/", + Expected: nil, + }, + { + Name: "Missing DNS Zones Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/", + Expected: nil, + }, + { + Name: "DNS Zone ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1", + Expected: nil, + }, + { + Name: "Missing TXT Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/TXT/", + Expected: nil, + }, + { + Name: "DNS TXT Record ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/TXT/myrecord1", + Expected: &DnsTxtRecordId{ + ResourceGroup: "resGroup1", + ZoneName: "zone1", + Name: "myrecord1", + }, + }, + { + Name: "Wrong Casing", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1/txt/myrecord1", + Expected: nil, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Name) + + actual, err := DnsTxtRecordID(v.Input) + if err != nil { + if v.Expected == nil { + continue + } + + t.Fatalf("Expected a value but got an error: %s", err) + } + + if actual.Name != v.Expected.Name { + t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) + } + if actual.ZoneName != v.Expected.ZoneName { + t.Fatalf("Expected %q but got %q for ZoneName", v.Expected.ZoneName, actual.ZoneName) + } + if actual.ResourceGroup != v.Expected.ResourceGroup { + t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup) + } + } +} diff --git a/azurerm/internal/services/dns/parse/dns_zone.go b/azurerm/internal/services/dns/parse/dns_zone.go new file mode 100644 index 000000000000..9d2fb359d068 --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_zone.go @@ -0,0 +1,33 @@ +package parse + +import ( + "fmt" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" +) + +type DnsZoneId struct { + ResourceGroup string + Name string +} + +func DnsZoneID(input string) (*DnsZoneId, error) { + id, err := azure.ParseAzureResourceID(input) + if err != nil { + return nil, fmt.Errorf("[ERROR] Unable to parse DNS Zone ID %q: %+v", input, err) + } + + zone := DnsZoneId{ + ResourceGroup: id.ResourceGroup, + } + + if zone.Name, err = id.PopSegment("dnszones"); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &zone, nil +} diff --git a/azurerm/internal/services/dns/parse/dns_zone_test.go b/azurerm/internal/services/dns/parse/dns_zone_test.go new file mode 100644 index 000000000000..3d970377aa14 --- /dev/null +++ b/azurerm/internal/services/dns/parse/dns_zone_test.go @@ -0,0 +1,73 @@ +package parse + +import ( + "testing" +) + +func TestDnsZoneId(t *testing.T) { + testData := []struct { + Name string + Input string + Expected *DnsZoneId + }{ + { + Name: "Empty", + Input: "", + Expected: nil, + }, + { + Name: "No Resource Groups Segment", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000", + Expected: nil, + }, + { + Name: "No Resource Groups Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/", + Expected: nil, + }, + { + Name: "Resource Group ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/", + Expected: nil, + }, + { + Name: "Missing DNS Zones Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/", + Expected: nil, + }, + { + Name: "DNS Zone ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/dnszones/zone1", + Expected: &DnsZoneId{ + Name: "zone1", + ResourceGroup: "resGroup1", + }, + }, + { + Name: "Wrong Casing", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/Dnszones/zone1", + Expected: nil, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Name) + + actual, err := DnsZoneID(v.Input) + if err != nil { + if v.Expected == nil { + continue + } + + t.Fatalf("Expected a value but got an error: %s", err) + } + + if actual.Name != v.Expected.Name { + t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) + } + + if actual.ResourceGroup != v.Expected.ResourceGroup { + t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup) + } + } +} diff --git a/azurerm/internal/services/dns/resource_arm_dns_a_record.go b/azurerm/internal/services/dns/resource_arm_dns_a_record.go index a7a919eb49d2..a7798f49283a 100644 --- a/azurerm/internal/services/dns/resource_arm_dns_a_record.go +++ b/azurerm/internal/services/dns/resource_arm_dns_a_record.go @@ -11,7 +11,9 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" + azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) @@ -22,9 +24,6 @@ func resourceArmDnsARecord() *schema.Resource { Read: resourceArmDnsARecordRead, Update: resourceArmDnsARecordCreateUpdate, Delete: resourceArmDnsARecordDelete, - Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, - }, Timeouts: &schema.ResourceTimeout{ Create: schema.DefaultTimeout(30 * time.Minute), @@ -32,6 +31,10 @@ func resourceArmDnsARecord() *schema.Resource { Update: schema.DefaultTimeout(30 * time.Minute), Delete: schema.DefaultTimeout(30 * time.Minute), }, + Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { + _, err := parse.DnsARecordID(id) + return err + }), Schema: map[string]*schema.Schema{ "name": { @@ -150,27 +153,24 @@ func resourceArmDnsARecordRead(d *schema.ResourceData, meta interface{}) error { ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsARecordID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["A"] - zoneName := id.Path["dnszones"] - - resp, err := dnsClient.Get(ctx, resGroup, zoneName, name, dns.A) + resp, err := dnsClient.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.A) if err != nil { if utils.ResponseWasNotFound(resp.Response) { d.SetId("") return nil } - return fmt.Errorf("Error reading DNS A record %s: %+v", name, err) + return fmt.Errorf("Error reading DNS A record %s: %+v", id.Name, err) } - d.Set("name", name) - d.Set("resource_group_name", resGroup) - d.Set("zone_name", zoneName) + d.Set("name", id.Name) + d.Set("resource_group_name", id.ResourceGroup) + d.Set("zone_name", id.ZoneName) + d.Set("fqdn", resp.Fqdn) d.Set("ttl", resp.TTL) @@ -192,18 +192,14 @@ func resourceArmDnsARecordDelete(d *schema.ResourceData, meta interface{}) error ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsARecordID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["A"] - zoneName := id.Path["dnszones"] - - resp, err := dnsClient.Delete(ctx, resGroup, zoneName, name, dns.A, "") + resp, err := dnsClient.Delete(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.A, "") if resp.StatusCode != http.StatusOK { - return fmt.Errorf("Error deleting DNS A Record %s: %+v", name, err) + return fmt.Errorf("Error deleting DNS A Record %s: %+v", id.Name, err) } return nil diff --git a/azurerm/internal/services/dns/resource_arm_dns_aaaa_record.go b/azurerm/internal/services/dns/resource_arm_dns_aaaa_record.go index 657bcfc287bb..ba4788b3a33a 100644 --- a/azurerm/internal/services/dns/resource_arm_dns_aaaa_record.go +++ b/azurerm/internal/services/dns/resource_arm_dns_aaaa_record.go @@ -12,7 +12,9 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" + azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) @@ -23,9 +25,6 @@ func resourceArmDnsAAAARecord() *schema.Resource { Read: resourceArmDnsAaaaRecordRead, Update: resourceArmDnsAaaaRecordCreateUpdate, Delete: resourceArmDnsAaaaRecordDelete, - Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, - }, Timeouts: &schema.ResourceTimeout{ Create: schema.DefaultTimeout(30 * time.Minute), @@ -34,6 +33,11 @@ func resourceArmDnsAAAARecord() *schema.Resource { Delete: schema.DefaultTimeout(30 * time.Minute), }, + Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { + _, err := parse.DnsAaaaRecordID(id) + return err + }), + Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -152,27 +156,24 @@ func resourceArmDnsAaaaRecordRead(d *schema.ResourceData, meta interface{}) erro ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsAaaaRecordID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["AAAA"] - zoneName := id.Path["dnszones"] - - resp, err := dnsClient.Get(ctx, resGroup, zoneName, name, dns.AAAA) + resp, err := dnsClient.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.AAAA) if err != nil { if utils.ResponseWasNotFound(resp.Response) { d.SetId("") return nil } - return fmt.Errorf("Error reading DNS AAAA record %s: %v", name, err) + return fmt.Errorf("Error reading DNS AAAA record %s: %v", id.Name, err) } - d.Set("name", name) - d.Set("resource_group_name", resGroup) - d.Set("zone_name", zoneName) + d.Set("name", id.Name) + d.Set("resource_group_name", id.ResourceGroup) + d.Set("zone_name", id.ZoneName) + d.Set("fqdn", resp.Fqdn) d.Set("ttl", resp.TTL) @@ -194,18 +195,14 @@ func resourceArmDnsAaaaRecordDelete(d *schema.ResourceData, meta interface{}) er ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsAaaaRecordID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["AAAA"] - zoneName := id.Path["dnszones"] - - resp, err := dnsClient.Delete(ctx, resGroup, zoneName, name, dns.AAAA, "") + resp, err := dnsClient.Delete(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.AAAA, "") if resp.StatusCode != http.StatusOK { - return fmt.Errorf("Error deleting DNS AAAA Record %s: %+v", name, err) + return fmt.Errorf("Error deleting DNS AAAA Record %s: %+v", id.Name, err) } return nil diff --git a/azurerm/internal/services/dns/resource_arm_dns_caa_record.go b/azurerm/internal/services/dns/resource_arm_dns_caa_record.go index 65db838855bb..7cf9ba5944bf 100644 --- a/azurerm/internal/services/dns/resource_arm_dns_caa_record.go +++ b/azurerm/internal/services/dns/resource_arm_dns_caa_record.go @@ -15,7 +15,9 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" + azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) @@ -26,9 +28,6 @@ func resourceArmDnsCaaRecord() *schema.Resource { Read: resourceArmDnsCaaRecordRead, Update: resourceArmDnsCaaRecordCreateUpdate, Delete: resourceArmDnsCaaRecordDelete, - Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, - }, Timeouts: &schema.ResourceTimeout{ Create: schema.DefaultTimeout(30 * time.Minute), @@ -37,6 +36,11 @@ func resourceArmDnsCaaRecord() *schema.Resource { Delete: schema.DefaultTimeout(30 * time.Minute), }, + Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { + _, err := parse.DnsCaaRecordID(id) + return err + }), + Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -155,27 +159,24 @@ func resourceArmDnsCaaRecordRead(d *schema.ResourceData, meta interface{}) error ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsCaaRecordID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["CAA"] - zoneName := id.Path["dnszones"] - - resp, err := client.Get(ctx, resGroup, zoneName, name, dns.CAA) + resp, err := client.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.CAA) if err != nil { if utils.ResponseWasNotFound(resp.Response) { d.SetId("") return nil } - return fmt.Errorf("Error reading DNS CAA record %s: %v", name, err) + return fmt.Errorf("Error reading DNS CAA record %s: %v", id.Name, err) } - d.Set("name", name) - d.Set("resource_group_name", resGroup) - d.Set("zone_name", zoneName) + d.Set("name", id.Name) + d.Set("resource_group_name", id.ResourceGroup) + d.Set("zone_name", id.ZoneName) + d.Set("ttl", resp.TTL) d.Set("fqdn", resp.Fqdn) @@ -190,18 +191,14 @@ func resourceArmDnsCaaRecordDelete(d *schema.ResourceData, meta interface{}) err ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsCaaRecordID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["CAA"] - zoneName := id.Path["dnszones"] - - resp, err := client.Delete(ctx, resGroup, zoneName, name, dns.CAA, "") + resp, err := client.Delete(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.CAA, "") if resp.StatusCode != http.StatusOK { - return fmt.Errorf("Error deleting DNS CAA Record %s: %+v", name, err) + return fmt.Errorf("Error deleting DNS CAA Record %s: %+v", id.Name, err) } return nil diff --git a/azurerm/internal/services/dns/resource_arm_dns_cname_record.go b/azurerm/internal/services/dns/resource_arm_dns_cname_record.go index 9d62608bc817..8ab073a76968 100644 --- a/azurerm/internal/services/dns/resource_arm_dns_cname_record.go +++ b/azurerm/internal/services/dns/resource_arm_dns_cname_record.go @@ -11,7 +11,9 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" + azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) @@ -22,9 +24,6 @@ func resourceArmDnsCNameRecord() *schema.Resource { Read: resourceArmDnsCNameRecordRead, Update: resourceArmDnsCNameRecordCreateUpdate, Delete: resourceArmDnsCNameRecordDelete, - Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, - }, Timeouts: &schema.ResourceTimeout{ Create: schema.DefaultTimeout(30 * time.Minute), @@ -33,6 +32,11 @@ func resourceArmDnsCNameRecord() *schema.Resource { Delete: schema.DefaultTimeout(30 * time.Minute), }, + Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { + _, err := parse.DnsCNameRecordID(id) + return err + }), + Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -150,27 +154,24 @@ func resourceArmDnsCNameRecordRead(d *schema.ResourceData, meta interface{}) err ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsCNameRecordID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["CNAME"] - zoneName := id.Path["dnszones"] - - resp, err := dnsClient.Get(ctx, resGroup, zoneName, name, dns.CNAME) + resp, err := dnsClient.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.CNAME) if err != nil { if utils.ResponseWasNotFound(resp.Response) { d.SetId("") return nil } - return fmt.Errorf("Error retrieving CNAME Record %s (DNS Zone %q / Resource Group %q): %+v", name, zoneName, resGroup, err) + return fmt.Errorf("Error retrieving CNAME Record %s (DNS Zone %q / Resource Group %q): %+v", id.Name, id.ZoneName, id.ResourceGroup, err) } - d.Set("name", name) - d.Set("resource_group_name", resGroup) - d.Set("zone_name", zoneName) + d.Set("name", id.Name) + d.Set("resource_group_name", id.ResourceGroup) + d.Set("zone_name", id.ZoneName) + d.Set("fqdn", resp.Fqdn) d.Set("ttl", resp.TTL) @@ -196,18 +197,14 @@ func resourceArmDnsCNameRecordDelete(d *schema.ResourceData, meta interface{}) e ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsCNameRecordID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["CNAME"] - zoneName := id.Path["dnszones"] - - resp, err := dnsClient.Delete(ctx, resGroup, zoneName, name, dns.CNAME, "") + resp, err := dnsClient.Delete(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.CNAME, "") if resp.StatusCode != http.StatusOK { - return fmt.Errorf("Error deleting CNAME Record %q (DNS Zone %q / Resource Group %q): %+v", name, zoneName, resGroup, err) + return fmt.Errorf("Error deleting CNAME Record %q (DNS Zone %q / Resource Group %q): %+v", id.Name, id.ZoneName, id.ResourceGroup, err) } return nil diff --git a/azurerm/internal/services/dns/resource_arm_dns_mx_record.go b/azurerm/internal/services/dns/resource_arm_dns_mx_record.go index 42f29f95d987..a244b4c65eb9 100644 --- a/azurerm/internal/services/dns/resource_arm_dns_mx_record.go +++ b/azurerm/internal/services/dns/resource_arm_dns_mx_record.go @@ -14,7 +14,9 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" + azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) @@ -25,9 +27,6 @@ func resourceArmDnsMxRecord() *schema.Resource { Read: resourceArmDnsMxRecordRead, Update: resourceArmDnsMxRecordCreateUpdate, Delete: resourceArmDnsMxRecordDelete, - Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, - }, Timeouts: &schema.ResourceTimeout{ Create: schema.DefaultTimeout(30 * time.Minute), @@ -36,6 +35,11 @@ func resourceArmDnsMxRecord() *schema.Resource { Delete: schema.DefaultTimeout(30 * time.Minute), }, + Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { + _, err := parse.DnsMxRecordID(id) + return err + }), + Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -143,27 +147,24 @@ func resourceArmDnsMxRecordRead(d *schema.ResourceData, meta interface{}) error ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsMxRecordID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["MX"] - zoneName := id.Path["dnszones"] - - resp, err := client.Get(ctx, resGroup, zoneName, name, dns.MX) + resp, err := client.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.MX) if err != nil { if utils.ResponseWasNotFound(resp.Response) { d.SetId("") return nil } - return fmt.Errorf("Error reading DNS MX record %s: %v", name, err) + return fmt.Errorf("Error reading DNS MX record %s: %v", id.Name, err) } - d.Set("name", name) - d.Set("resource_group_name", resGroup) - d.Set("zone_name", zoneName) + d.Set("name", id.Name) + d.Set("resource_group_name", id.ResourceGroup) + d.Set("zone_name", id.ZoneName) + d.Set("ttl", resp.TTL) d.Set("fqdn", resp.Fqdn) @@ -178,18 +179,14 @@ func resourceArmDnsMxRecordDelete(d *schema.ResourceData, meta interface{}) erro ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsMxRecordID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["MX"] - zoneName := id.Path["dnszones"] - - resp, err := client.Delete(ctx, resGroup, zoneName, name, dns.MX, "") + resp, err := client.Delete(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.MX, "") if resp.StatusCode != http.StatusOK { - return fmt.Errorf("Error deleting DNS MX Record %s: %+v", name, err) + return fmt.Errorf("Error deleting DNS MX Record %s: %+v", id.Name, err) } return nil diff --git a/azurerm/internal/services/dns/resource_arm_dns_ns_record.go b/azurerm/internal/services/dns/resource_arm_dns_ns_record.go index f1d679108371..db47b9a0aa8e 100644 --- a/azurerm/internal/services/dns/resource_arm_dns_ns_record.go +++ b/azurerm/internal/services/dns/resource_arm_dns_ns_record.go @@ -11,7 +11,9 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" + azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) @@ -22,9 +24,6 @@ func resourceArmDnsNsRecord() *schema.Resource { Read: resourceArmDnsNsRecordRead, Update: resourceArmDnsNsRecordUpdate, Delete: resourceArmDnsNsRecordDelete, - Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, - }, Timeouts: &schema.ResourceTimeout{ Create: schema.DefaultTimeout(30 * time.Minute), @@ -32,6 +31,10 @@ func resourceArmDnsNsRecord() *schema.Resource { Update: schema.DefaultTimeout(30 * time.Minute), Delete: schema.DefaultTimeout(30 * time.Minute), }, + Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { + _, err := parse.DnsNsRecordID(id) + return err + }), Schema: map[string]*schema.Schema{ "name": { @@ -180,27 +183,24 @@ func resourceArmDnsNsRecordRead(d *schema.ResourceData, meta interface{}) error ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsNsRecordID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["NS"] - zoneName := id.Path["dnszones"] - - resp, err := dnsClient.Get(ctx, resGroup, zoneName, name, dns.NS) + resp, err := dnsClient.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.NS) if err != nil { if utils.ResponseWasNotFound(resp.Response) { d.SetId("") return nil } - return fmt.Errorf("Error reading DNS NS record %s: %+v", name, err) + return fmt.Errorf("Error reading DNS NS record %s: %+v", id.Name, err) } - d.Set("name", name) - d.Set("resource_group_name", resGroup) - d.Set("zone_name", zoneName) + d.Set("name", id.Name) + d.Set("resource_group_name", id.ResourceGroup) + d.Set("zone_name", id.ZoneName) + d.Set("ttl", resp.TTL) d.Set("fqdn", resp.Fqdn) @@ -218,18 +218,14 @@ func resourceArmDnsNsRecordDelete(d *schema.ResourceData, meta interface{}) erro ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsNsRecordID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["NS"] - zoneName := id.Path["dnszones"] - - resp, err := dnsClient.Delete(ctx, resGroup, zoneName, name, dns.NS, "") + resp, err := dnsClient.Delete(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.NS, "") if resp.StatusCode != http.StatusOK { - return fmt.Errorf("Error deleting DNS NS Record %s: %+v", name, err) + return fmt.Errorf("Error deleting DNS NS Record %s: %+v", id.Name, err) } return nil diff --git a/azurerm/internal/services/dns/resource_arm_dns_ptr_record.go b/azurerm/internal/services/dns/resource_arm_dns_ptr_record.go index 25272c1c2dd5..8f85a7614154 100644 --- a/azurerm/internal/services/dns/resource_arm_dns_ptr_record.go +++ b/azurerm/internal/services/dns/resource_arm_dns_ptr_record.go @@ -11,7 +11,9 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" + azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) @@ -22,9 +24,6 @@ func resourceArmDnsPtrRecord() *schema.Resource { Read: resourceArmDnsPtrRecordRead, Update: resourceArmDnsPtrRecordCreateUpdate, Delete: resourceArmDnsPtrRecordDelete, - Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, - }, Timeouts: &schema.ResourceTimeout{ Create: schema.DefaultTimeout(30 * time.Minute), @@ -33,6 +32,10 @@ func resourceArmDnsPtrRecord() *schema.Resource { Delete: schema.DefaultTimeout(30 * time.Minute), }, + Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { + _, err := parse.DnsPtrRecordID(id) + return err + }), Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -128,28 +131,24 @@ func resourceArmDnsPtrRecordRead(d *schema.ResourceData, meta interface{}) error ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsPtrRecordID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["PTR"] - zoneName := id.Path["dnszones"] - - resp, err := dnsClient.Get(ctx, resGroup, zoneName, name, dns.PTR) + resp, err := dnsClient.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.PTR) if err != nil { if utils.ResponseWasNotFound(resp.Response) { d.SetId("") return nil } - return fmt.Errorf("Error reading DNS PTR record %s: %+v", name, err) + return fmt.Errorf("Error reading DNS PTR record %s: %+v", id.Name, err) } - d.Set("name", name) - d.Set("resource_group_name", resGroup) - d.Set("zone_name", zoneName) + d.Set("name", id.Name) + d.Set("resource_group_name", id.ResourceGroup) + d.Set("zone_name", id.ZoneName) d.Set("ttl", resp.TTL) d.Set("fqdn", resp.Fqdn) @@ -165,22 +164,18 @@ func resourceArmDnsPtrRecordDelete(d *schema.ResourceData, meta interface{}) err ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsPtrRecordID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["PTR"] - zoneName := id.Path["dnszones"] - - resp, err := dnsClient.Delete(ctx, resGroup, zoneName, name, dns.PTR, "") + resp, err := dnsClient.Delete(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.PTR, "") if err != nil { if resp.StatusCode == http.StatusNotFound { return nil } - return fmt.Errorf("Error deleting DNS PTR Record %s: %+v", name, err) + return fmt.Errorf("Error deleting DNS PTR Record %s: %+v", id.Name, err) } return nil diff --git a/azurerm/internal/services/dns/resource_arm_dns_srv_record.go b/azurerm/internal/services/dns/resource_arm_dns_srv_record.go index d6f7749e3758..456afc758020 100644 --- a/azurerm/internal/services/dns/resource_arm_dns_srv_record.go +++ b/azurerm/internal/services/dns/resource_arm_dns_srv_record.go @@ -13,7 +13,9 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" + azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) @@ -24,9 +26,6 @@ func resourceArmDnsSrvRecord() *schema.Resource { Read: resourceArmDnsSrvRecordRead, Update: resourceArmDnsSrvRecordCreateUpdate, Delete: resourceArmDnsSrvRecordDelete, - Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, - }, Timeouts: &schema.ResourceTimeout{ Create: schema.DefaultTimeout(30 * time.Minute), @@ -34,7 +33,10 @@ func resourceArmDnsSrvRecord() *schema.Resource { Update: schema.DefaultTimeout(30 * time.Minute), Delete: schema.DefaultTimeout(30 * time.Minute), }, - + Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { + _, err := parse.DnsSrvRecordID(id) + return err + }), Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -152,27 +154,23 @@ func resourceArmDnsSrvRecordRead(d *schema.ResourceData, meta interface{}) error ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsSrvRecordID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["SRV"] - zoneName := id.Path["dnszones"] - - resp, err := client.Get(ctx, resGroup, zoneName, name, dns.SRV) + resp, err := client.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.SRV) if err != nil { if utils.ResponseWasNotFound(resp.Response) { d.SetId("") return nil } - return fmt.Errorf("Error reading DNS SRV record %s: %v", name, err) + return fmt.Errorf("Error reading DNS SRV record %s: %v", id.Name, err) } - d.Set("name", name) - d.Set("resource_group_name", resGroup) - d.Set("zone_name", zoneName) + d.Set("name", id.Name) + d.Set("resource_group_name", id.ResourceGroup) + d.Set("zone_name", id.ZoneName) d.Set("ttl", resp.TTL) d.Set("fqdn", resp.Fqdn) @@ -187,18 +185,14 @@ func resourceArmDnsSrvRecordDelete(d *schema.ResourceData, meta interface{}) err ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsSrvRecordID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["SRV"] - zoneName := id.Path["dnszones"] - - resp, err := client.Delete(ctx, resGroup, zoneName, name, dns.SRV, "") + resp, err := client.Delete(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.SRV, "") if resp.StatusCode != http.StatusOK { - return fmt.Errorf("Error deleting DNS SRV Record %s: %+v", name, err) + return fmt.Errorf("Error deleting DNS SRV Record %s: %+v", id.Name, err) } return nil diff --git a/azurerm/internal/services/dns/resource_arm_dns_txt_record.go b/azurerm/internal/services/dns/resource_arm_dns_txt_record.go index abf1e2eaae9c..ad7bd70533ff 100644 --- a/azurerm/internal/services/dns/resource_arm_dns_txt_record.go +++ b/azurerm/internal/services/dns/resource_arm_dns_txt_record.go @@ -11,7 +11,9 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" + azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) @@ -22,9 +24,6 @@ func resourceArmDnsTxtRecord() *schema.Resource { Read: resourceArmDnsTxtRecordRead, Update: resourceArmDnsTxtRecordCreateUpdate, Delete: resourceArmDnsTxtRecordDelete, - Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, - }, Timeouts: &schema.ResourceTimeout{ Create: schema.DefaultTimeout(30 * time.Minute), @@ -32,7 +31,10 @@ func resourceArmDnsTxtRecord() *schema.Resource { Update: schema.DefaultTimeout(30 * time.Minute), Delete: schema.DefaultTimeout(30 * time.Minute), }, - + Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { + _, err := parse.DnsTxtRecordID(id) + return err + }), Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -134,27 +136,23 @@ func resourceArmDnsTxtRecordRead(d *schema.ResourceData, meta interface{}) error ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsTxtRecordID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["TXT"] - zoneName := id.Path["dnszones"] - - resp, err := client.Get(ctx, resGroup, zoneName, name, dns.TXT) + resp, err := client.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.TXT) if err != nil { if utils.ResponseWasNotFound(resp.Response) { d.SetId("") return nil } - return fmt.Errorf("Error reading DNS TXT record %s: %+v", name, err) + return fmt.Errorf("Error reading DNS TXT record %s: %+v", id.Name, err) } - d.Set("name", name) - d.Set("resource_group_name", resGroup) - d.Set("zone_name", zoneName) + d.Set("name", id.Name) + d.Set("resource_group_name", id.ResourceGroup) + d.Set("zone_name", id.ZoneName) d.Set("ttl", resp.TTL) d.Set("fqdn", resp.Fqdn) @@ -169,18 +167,14 @@ func resourceArmDnsTxtRecordDelete(d *schema.ResourceData, meta interface{}) err ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsTxtRecordID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["TXT"] - zoneName := id.Path["dnszones"] - - resp, err := client.Delete(ctx, resGroup, zoneName, name, dns.TXT, "") + resp, err := client.Delete(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.TXT, "") if resp.StatusCode != http.StatusOK { - return fmt.Errorf("Error deleting DNS TXT Record %s: %+v", name, err) + return fmt.Errorf("Error deleting DNS TXT Record %s: %+v", id.Name, err) } return nil diff --git a/azurerm/internal/services/dns/resource_arm_dns_zone.go b/azurerm/internal/services/dns/resource_arm_dns_zone.go index 53d04a030d38..58258a85f94f 100644 --- a/azurerm/internal/services/dns/resource_arm_dns_zone.go +++ b/azurerm/internal/services/dns/resource_arm_dns_zone.go @@ -11,7 +11,9 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" + azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) @@ -22,9 +24,6 @@ func resourceArmDnsZone() *schema.Resource { Read: resourceArmDnsZoneRead, Update: resourceArmDnsZoneCreateUpdate, Delete: resourceArmDnsZoneDelete, - Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, - }, Timeouts: &schema.ResourceTimeout{ Create: schema.DefaultTimeout(30 * time.Minute), @@ -33,6 +32,10 @@ func resourceArmDnsZone() *schema.Resource { Delete: schema.DefaultTimeout(30 * time.Minute), }, + Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { + _, err := parse.DnsZoneID(id) + return err + }), Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -118,25 +121,23 @@ func resourceArmDnsZoneRead(d *schema.ResourceData, meta interface{}) error { ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsZoneID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["dnszones"] - - resp, err := zonesClient.Get(ctx, resGroup, name) + resp, err := zonesClient.Get(ctx, id.ResourceGroup, id.Name) if err != nil { if utils.ResponseWasNotFound(resp.Response) { d.SetId("") return nil } - return fmt.Errorf("Error reading DNS Zone %q (Resource Group %q): %+v", name, resGroup, err) + return fmt.Errorf("Error reading DNS Zone %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) } - d.Set("name", name) - d.Set("resource_group_name", resGroup) + d.Set("name", id.Name) + d.Set("resource_group_name", id.ResourceGroup) + d.Set("number_of_record_sets", resp.NumberOfRecordSets) d.Set("max_number_of_record_sets", resp.MaxNumberOfRecordSets) @@ -156,28 +157,25 @@ func resourceArmDnsZoneDelete(d *schema.ResourceData, meta interface{}) error { ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.DnsZoneID(d.Id()) if err != nil { return err } - resGroup := id.ResourceGroup - name := id.Path["dnszones"] - etag := "" - future, err := client.Delete(ctx, resGroup, name, etag) + future, err := client.Delete(ctx, id.ResourceGroup, id.Name, etag) if err != nil { if response.WasNotFound(future.Response()) { return nil } - return fmt.Errorf("Error deleting DNS zone %s (resource group %s): %+v", name, resGroup, err) + return fmt.Errorf("Error deleting DNS zone %s (resource group %s): %+v", id.Name, id.ResourceGroup, err) } if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { if response.WasNotFound(future.Response()) { return nil } - return fmt.Errorf("Error deleting DNS zone %s (resource group %s): %+v", name, resGroup, err) + return fmt.Errorf("Error deleting DNS zone %s (resource group %s): %+v", id.Name, id.ResourceGroup, err) } return nil diff --git a/azurerm/internal/services/dns/tests/resource_arm_dns_a_record_test.go b/azurerm/internal/services/dns/tests/resource_arm_dns_a_record_test.go index e3ed5c60671a..6e5247c37f49 100644 --- a/azurerm/internal/services/dns/tests/resource_arm_dns_a_record_test.go +++ b/azurerm/internal/services/dns/tests/resource_arm_dns_a_record_test.go @@ -11,6 +11,7 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" ) func TestAccAzureRMDnsARecord_basic(t *testing.T) { @@ -211,20 +212,18 @@ func testCheckAzureRMDnsARecordExists(resourceName string) resource.TestCheckFun return fmt.Errorf("Not found: %s", resourceName) } - aName := rs.Primary.Attributes["name"] - zoneName := rs.Primary.Attributes["zone_name"] - resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] - if !hasResourceGroup { - return fmt.Errorf("Bad: no resource group found in state for DNS A record: %s", aName) + id, err := parse.DnsARecordID(rs.Primary.ID) + if err != nil { + return err } - resp, err := conn.Get(ctx, resourceGroup, zoneName, aName, dns.A) + resp, err := conn.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.A) if err != nil { return fmt.Errorf("Bad: Get A RecordSet: %+v", err) } if resp.StatusCode == http.StatusNotFound { - return fmt.Errorf("Bad: DNS A record %s (resource group: %s) does not exist", aName, resourceGroup) + return fmt.Errorf("Bad: DNS A record %s (resource group: %s) does not exist", id.Name, id.ResourceGroup) } return nil @@ -240,11 +239,11 @@ func testCheckAzureRMDnsARecordDestroy(s *terraform.State) error { continue } - aName := rs.Primary.Attributes["name"] - zoneName := rs.Primary.Attributes["zone_name"] - resourceGroup := rs.Primary.Attributes["resource_group_name"] - - resp, err := conn.Get(ctx, resourceGroup, zoneName, aName, dns.A) + id, err := parse.DnsARecordID(rs.Primary.ID) + if err != nil { + return err + } + resp, err := conn.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.A) if err != nil { if resp.StatusCode == http.StatusNotFound { diff --git a/azurerm/internal/services/dns/tests/resource_arm_dns_aaaa_record_test.go b/azurerm/internal/services/dns/tests/resource_arm_dns_aaaa_record_test.go index a06dcc54a05e..2fd943c43726 100644 --- a/azurerm/internal/services/dns/tests/resource_arm_dns_aaaa_record_test.go +++ b/azurerm/internal/services/dns/tests/resource_arm_dns_aaaa_record_test.go @@ -11,6 +11,7 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" ) func TestAccAzureRMDnsAAAARecord_basic(t *testing.T) { @@ -238,20 +239,18 @@ func testCheckAzureRMDnsAaaaRecordExists(resourceName string) resource.TestCheck return fmt.Errorf("Not found: %s", resourceName) } - aaaaName := rs.Primary.Attributes["name"] - zoneName := rs.Primary.Attributes["zone_name"] - resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] - if !hasResourceGroup { - return fmt.Errorf("Bad: no resource group found in state for DNS AAAA record: %s", aaaaName) + id, err := parse.DnsAaaaRecordID(rs.Primary.ID) + if err != nil { + return err } - resp, err := conn.Get(ctx, resourceGroup, zoneName, aaaaName, dns.AAAA) + resp, err := conn.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.AAAA) if err != nil { return fmt.Errorf("Bad: Get AAAA RecordSet: %+v", err) } if resp.StatusCode == http.StatusNotFound { - return fmt.Errorf("Bad: DNS AAAA record %s (resource group: %s) does not exist", aaaaName, resourceGroup) + return fmt.Errorf("Bad: DNS AAAA record %s (resource group: %s) does not exist", id.Name, id.ResourceGroup) } return nil @@ -267,11 +266,11 @@ func testCheckAzureRMDnsAaaaRecordDestroy(s *terraform.State) error { continue } - aaaaName := rs.Primary.Attributes["name"] - zoneName := rs.Primary.Attributes["zone_name"] - resourceGroup := rs.Primary.Attributes["resource_group_name"] - - resp, err := conn.Get(ctx, resourceGroup, zoneName, aaaaName, dns.AAAA) + id, err := parse.DnsAaaaRecordID(rs.Primary.ID) + if err != nil { + return err + } + resp, err := conn.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.AAAA) if err != nil { if resp.StatusCode == http.StatusNotFound { diff --git a/azurerm/internal/services/dns/tests/resource_arm_dns_caa_record_test.go b/azurerm/internal/services/dns/tests/resource_arm_dns_caa_record_test.go index 72adf5d97c83..7428c114ee7e 100644 --- a/azurerm/internal/services/dns/tests/resource_arm_dns_caa_record_test.go +++ b/azurerm/internal/services/dns/tests/resource_arm_dns_caa_record_test.go @@ -11,6 +11,7 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" ) func TestAccAzureRMDnsCaaRecord_basic(t *testing.T) { @@ -124,20 +125,18 @@ func testCheckAzureRMDnsCaaRecordExists(resourceName string) resource.TestCheckF return fmt.Errorf("Not found: %s", resourceName) } - caaName := rs.Primary.Attributes["name"] - zoneName := rs.Primary.Attributes["zone_name"] - resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] - if !hasResourceGroup { - return fmt.Errorf("Bad: no resource group found in state for DNS CAA record: %s", caaName) + id, err := parse.DnsCaaRecordID(rs.Primary.ID) + if err != nil { + return err } - resp, err := conn.Get(ctx, resourceGroup, zoneName, caaName, dns.CAA) + resp, err := conn.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.CAA) if err != nil { return fmt.Errorf("Bad: Get CAA RecordSet: %+v", err) } if resp.StatusCode == http.StatusNotFound { - return fmt.Errorf("Bad: DNS CAA record %s (resource group: %s) does not exist", caaName, resourceGroup) + return fmt.Errorf("Bad: DNS CAA record %s (resource group: %s) does not exist", id.Name, id.ResourceGroup) } return nil @@ -153,11 +152,11 @@ func testCheckAzureRMDnsCaaRecordDestroy(s *terraform.State) error { continue } - caaName := rs.Primary.Attributes["name"] - zoneName := rs.Primary.Attributes["zone_name"] - resourceGroup := rs.Primary.Attributes["resource_group_name"] - - resp, err := conn.Get(ctx, resourceGroup, zoneName, caaName, dns.CAA) + id, err := parse.DnsCaaRecordID(rs.Primary.ID) + if err != nil { + return err + } + resp, err := conn.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.CAA) if err != nil { if resp.StatusCode == http.StatusNotFound { diff --git a/azurerm/internal/services/dns/tests/resource_arm_dns_cname_record_test.go b/azurerm/internal/services/dns/tests/resource_arm_dns_cname_record_test.go index 0e630f6e2fba..2f988dd3205c 100644 --- a/azurerm/internal/services/dns/tests/resource_arm_dns_cname_record_test.go +++ b/azurerm/internal/services/dns/tests/resource_arm_dns_cname_record_test.go @@ -11,6 +11,7 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" ) func TestAccAzureRMDnsCNameRecord_basic(t *testing.T) { @@ -227,20 +228,18 @@ func testCheckAzureRMDnsCNameRecordExists(resourceName string) resource.TestChec return fmt.Errorf("Not found: %s", resourceName) } - cnameName := rs.Primary.Attributes["name"] - zoneName := rs.Primary.Attributes["zone_name"] - resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] - if !hasResourceGroup { - return fmt.Errorf("Bad: no resource group found in state for DNS CNAME record: %s", cnameName) + id, err := parse.DnsCNameRecordID(rs.Primary.ID) + if err != nil { + return err } - resp, err := conn.Get(ctx, resourceGroup, zoneName, cnameName, dns.CNAME) + resp, err := conn.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.CNAME) if err != nil { return fmt.Errorf("Bad: Get CNAME RecordSet: %v", err) } if resp.StatusCode == http.StatusNotFound { - return fmt.Errorf("Bad: DNS CNAME record %s (resource group: %s) does not exist", cnameName, resourceGroup) + return fmt.Errorf("Bad: DNS CNAME record %s (resource group: %s) does not exist", id.Name, id.ResourceGroup) } return nil @@ -256,11 +255,12 @@ func testCheckAzureRMDnsCNameRecordDestroy(s *terraform.State) error { continue } - cnameName := rs.Primary.Attributes["name"] - zoneName := rs.Primary.Attributes["zone_name"] - resourceGroup := rs.Primary.Attributes["resource_group_name"] + id, err := parse.DnsCNameRecordID(rs.Primary.ID) + if err != nil { + return err + } - resp, err := conn.Get(ctx, resourceGroup, zoneName, cnameName, dns.CNAME) + resp, err := conn.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.CNAME) if err != nil { if resp.StatusCode == http.StatusNotFound { diff --git a/azurerm/internal/services/dns/tests/resource_arm_dns_mx_record_test.go b/azurerm/internal/services/dns/tests/resource_arm_dns_mx_record_test.go index 0587fe54c99b..6b25c2792f85 100644 --- a/azurerm/internal/services/dns/tests/resource_arm_dns_mx_record_test.go +++ b/azurerm/internal/services/dns/tests/resource_arm_dns_mx_record_test.go @@ -11,6 +11,7 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" ) func TestAccAzureRMDnsMxRecord_basic(t *testing.T) { @@ -144,20 +145,18 @@ func testCheckAzureRMDnsMxRecordExists(resourceName string) resource.TestCheckFu return fmt.Errorf("Not found: %s", resourceName) } - mxName := rs.Primary.Attributes["name"] - zoneName := rs.Primary.Attributes["zone_name"] - resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] - if !hasResourceGroup { - return fmt.Errorf("Bad: no resource group found in state for DNS MX record: %s", mxName) + id, err := parse.DnsMxRecordID(rs.Primary.ID) + if err != nil { + return err } - resp, err := conn.Get(ctx, resourceGroup, zoneName, mxName, dns.MX) + resp, err := conn.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.MX) if err != nil { return fmt.Errorf("Bad: Get MX RecordSet: %+v", err) } if resp.StatusCode == http.StatusNotFound { - return fmt.Errorf("Bad: DNS MX record %s (resource group: %s) does not exist", mxName, resourceGroup) + return fmt.Errorf("Bad: DNS MX record %s (resource group: %s) does not exist", id.Name, id.ResourceGroup) } return nil @@ -173,11 +172,12 @@ func testCheckAzureRMDnsMxRecordDestroy(s *terraform.State) error { continue } - mxName := rs.Primary.Attributes["name"] - zoneName := rs.Primary.Attributes["zone_name"] - resourceGroup := rs.Primary.Attributes["resource_group_name"] + id, err := parse.DnsMxRecordID(rs.Primary.ID) + if err != nil { + return err + } - resp, err := conn.Get(ctx, resourceGroup, zoneName, mxName, dns.MX) + resp, err := conn.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.MX) if err != nil { if resp.StatusCode == http.StatusNotFound { diff --git a/azurerm/internal/services/dns/tests/resource_arm_dns_ns_record_test.go b/azurerm/internal/services/dns/tests/resource_arm_dns_ns_record_test.go index f4d159818a33..edddb3065827 100644 --- a/azurerm/internal/services/dns/tests/resource_arm_dns_ns_record_test.go +++ b/azurerm/internal/services/dns/tests/resource_arm_dns_ns_record_test.go @@ -11,6 +11,7 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" ) func TestAccAzureRMDnsNsRecord_basic(t *testing.T) { @@ -124,20 +125,18 @@ func testCheckAzureRMDnsNsRecordExists(resourceName string) resource.TestCheckFu return fmt.Errorf("Not found: %s", resourceName) } - nsName := rs.Primary.Attributes["name"] - zoneName := rs.Primary.Attributes["zone_name"] - resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] - if !hasResourceGroup { - return fmt.Errorf("Bad: no resource group found in state for DNS NS record: %s", nsName) + id, err := parse.DnsNsRecordID(rs.Primary.ID) + if err != nil { + return err } - resp, err := conn.Get(ctx, resourceGroup, zoneName, nsName, dns.NS) + resp, err := conn.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.NS) if err != nil { return fmt.Errorf("Bad: Get DNS NS Record: %+v", err) } if resp.StatusCode == http.StatusNotFound { - return fmt.Errorf("Bad: DNS NS record %s (resource group: %s) does not exist", nsName, resourceGroup) + return fmt.Errorf("Bad: DNS NS record %s (resource group: %s) does not exist", id.Name, id.ResourceGroup) } return nil @@ -153,11 +152,12 @@ func testCheckAzureRMDnsNsRecordDestroy(s *terraform.State) error { continue } - nsName := rs.Primary.Attributes["name"] - zoneName := rs.Primary.Attributes["zone_name"] - resourceGroup := rs.Primary.Attributes["resource_group_name"] + id, err := parse.DnsNsRecordID(rs.Primary.ID) + if err != nil { + return err + } - resp, err := conn.Get(ctx, resourceGroup, zoneName, nsName, dns.NS) + resp, err := conn.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.NS) if err != nil { return nil diff --git a/azurerm/internal/services/dns/tests/resource_arm_dns_ptr_record_test.go b/azurerm/internal/services/dns/tests/resource_arm_dns_ptr_record_test.go index db31482bf55b..bd5036605e82 100644 --- a/azurerm/internal/services/dns/tests/resource_arm_dns_ptr_record_test.go +++ b/azurerm/internal/services/dns/tests/resource_arm_dns_ptr_record_test.go @@ -11,6 +11,7 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" ) func TestAccAzureRMDnsPtrRecord_basic(t *testing.T) { @@ -126,20 +127,18 @@ func testCheckAzureRMDnsPtrRecordExists(resourceName string) resource.TestCheckF return fmt.Errorf("Not found: %s", resourceName) } - ptrName := rs.Primary.Attributes["name"] - zoneName := rs.Primary.Attributes["zone_name"] - resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] - if !hasResourceGroup { - return fmt.Errorf("Bad: no resource group found in state for DNS PTR record: %s", ptrName) + id, err := parse.DnsPtrRecordID(rs.Primary.ID) + if err != nil { + return err } - resp, err := conn.Get(ctx, resourceGroup, zoneName, ptrName, dns.PTR) + resp, err := conn.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.PTR) if err != nil { return fmt.Errorf("Bad: Get PTR RecordSet: %+v", err) } if resp.StatusCode == http.StatusNotFound { - return fmt.Errorf("Bad: DNS PTR record %s (resource group: %s) does not exist", ptrName, resourceGroup) + return fmt.Errorf("Bad: DNS PTR record %s (resource group: %s) does not exist", id.Name, id.ResourceGroup) } return nil @@ -155,11 +154,12 @@ func testCheckAzureRMDnsPtrRecordDestroy(s *terraform.State) error { continue } - ptrName := rs.Primary.Attributes["name"] - zoneName := rs.Primary.Attributes["zone_name"] - resourceGroup := rs.Primary.Attributes["resource_group_name"] + id, err := parse.DnsPtrRecordID(rs.Primary.ID) + if err != nil { + return err + } - resp, err := conn.Get(ctx, resourceGroup, zoneName, ptrName, dns.PTR) + resp, err := conn.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.PTR) if err != nil { if resp.StatusCode == http.StatusNotFound { diff --git a/azurerm/internal/services/dns/tests/resource_arm_dns_srv_record_test.go b/azurerm/internal/services/dns/tests/resource_arm_dns_srv_record_test.go index 0e10c85b932d..3077e7e6afd5 100644 --- a/azurerm/internal/services/dns/tests/resource_arm_dns_srv_record_test.go +++ b/azurerm/internal/services/dns/tests/resource_arm_dns_srv_record_test.go @@ -11,6 +11,7 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" ) func TestAccAzureRMDnsSrvRecord_basic(t *testing.T) { @@ -124,20 +125,18 @@ func testCheckAzureRMDnsSrvRecordExists(resourceName string) resource.TestCheckF return fmt.Errorf("Not found: %s", resourceName) } - srvName := rs.Primary.Attributes["name"] - zoneName := rs.Primary.Attributes["zone_name"] - resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] - if !hasResourceGroup { - return fmt.Errorf("Bad: no resource group found in state for DNS SRV record: %s", srvName) + id, err := parse.DnsSrvRecordID(rs.Primary.ID) + if err != nil { + return err } - resp, err := conn.Get(ctx, resourceGroup, zoneName, srvName, dns.SRV) + resp, err := conn.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.SRV) if err != nil { return fmt.Errorf("Bad: Get SRV RecordSet: %+v", err) } if resp.StatusCode == http.StatusNotFound { - return fmt.Errorf("Bad: DNS SRV record %s (resource group: %s) does not exist", srvName, resourceGroup) + return fmt.Errorf("Bad: DNS SRV record %s (resource group: %s) does not exist", id.Name, id.ResourceGroup) } return nil @@ -153,11 +152,12 @@ func testCheckAzureRMDnsSrvRecordDestroy(s *terraform.State) error { continue } - srvName := rs.Primary.Attributes["name"] - zoneName := rs.Primary.Attributes["zone_name"] - resourceGroup := rs.Primary.Attributes["resource_group_name"] + id, err := parse.DnsSrvRecordID(rs.Primary.ID) + if err != nil { + return err + } - resp, err := conn.Get(ctx, resourceGroup, zoneName, srvName, dns.SRV) + resp, err := conn.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.SRV) if err != nil { if resp.StatusCode == http.StatusNotFound { diff --git a/azurerm/internal/services/dns/tests/resource_arm_dns_txt_record_test.go b/azurerm/internal/services/dns/tests/resource_arm_dns_txt_record_test.go index 8bf2d3301d00..69d534b084c8 100644 --- a/azurerm/internal/services/dns/tests/resource_arm_dns_txt_record_test.go +++ b/azurerm/internal/services/dns/tests/resource_arm_dns_txt_record_test.go @@ -11,6 +11,7 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" ) func TestAccAzureRMDnsTxtRecord_basic(t *testing.T) { @@ -124,20 +125,18 @@ func testCheckAzureRMDnsTxtRecordExists(resourceName string) resource.TestCheckF return fmt.Errorf("Not found: %s", resourceName) } - txtName := rs.Primary.Attributes["name"] - zoneName := rs.Primary.Attributes["zone_name"] - resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] - if !hasResourceGroup { - return fmt.Errorf("Bad: no resource group found in state for DNS TXT record: %s", txtName) + id, err := parse.DnsTxtRecordID(rs.Primary.ID) + if err != nil { + return err } - resp, err := conn.Get(ctx, resourceGroup, zoneName, txtName, dns.TXT) + resp, err := conn.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.TXT) if err != nil { return fmt.Errorf("Bad: Get TXT RecordSet: %+v", err) } if resp.StatusCode == http.StatusNotFound { - return fmt.Errorf("Bad: DNS TXT record %s (resource group: %s) does not exist", txtName, resourceGroup) + return fmt.Errorf("Bad: DNS TXT record %s (resource group: %s) does not exist", id.Name, id.ResourceGroup) } return nil @@ -153,11 +152,12 @@ func testCheckAzureRMDnsTxtRecordDestroy(s *terraform.State) error { continue } - txtName := rs.Primary.Attributes["name"] - zoneName := rs.Primary.Attributes["zone_name"] - resourceGroup := rs.Primary.Attributes["resource_group_name"] + id, err := parse.DnsTxtRecordID(rs.Primary.ID) + if err != nil { + return err + } - resp, err := conn.Get(ctx, resourceGroup, zoneName, txtName, dns.TXT) + resp, err := conn.Get(ctx, id.ResourceGroup, id.ZoneName, id.Name, dns.TXT) if err != nil { if resp.StatusCode == http.StatusNotFound { diff --git a/azurerm/internal/services/dns/tests/resource_arm_dns_zone_test.go b/azurerm/internal/services/dns/tests/resource_arm_dns_zone_test.go index b886117ae7de..2c84dd76a660 100644 --- a/azurerm/internal/services/dns/tests/resource_arm_dns_zone_test.go +++ b/azurerm/internal/services/dns/tests/resource_arm_dns_zone_test.go @@ -10,6 +10,7 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse" ) func TestAccAzureRMDnsZone_basic(t *testing.T) { @@ -96,19 +97,18 @@ func testCheckAzureRMDnsZoneExists(resourceName string) resource.TestCheckFunc { return fmt.Errorf("Not found: %s", resourceName) } - zoneName := rs.Primary.Attributes["name"] - resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] - if !hasResourceGroup { - return fmt.Errorf("Bad: no resource group found in state for DNS zone: %s", zoneName) + id, err := parse.DnsZoneID(rs.Primary.ID) + if err != nil { + return err } - resp, err := client.Get(ctx, resourceGroup, zoneName) + resp, err := client.Get(ctx, id.ResourceGroup, id.Name) if err != nil { return fmt.Errorf("Bad: Get DNS zone: %+v", err) } if resp.StatusCode == http.StatusNotFound { - return fmt.Errorf("Bad: DNS zone %s (resource group: %s) does not exist", zoneName, resourceGroup) + return fmt.Errorf("Bad: DNS zone %s (resource group: %s) does not exist", id.Name, id.ResourceGroup) } return nil @@ -124,10 +124,12 @@ func testCheckAzureRMDnsZoneDestroy(s *terraform.State) error { continue } - zoneName := rs.Primary.Attributes["name"] - resourceGroup := rs.Primary.Attributes["resource_group_name"] + id, err := parse.DnsZoneID(rs.Primary.ID) + if err != nil { + return err + } - resp, err := conn.Get(ctx, resourceGroup, zoneName) + resp, err := conn.Get(ctx, id.ResourceGroup, id.Name) if err != nil { if resp.StatusCode == http.StatusNotFound { return nil