Skip to content

Commit

Permalink
dns - add parse ID and func (#5779)
Browse files Browse the repository at this point in the history
  • Loading branch information
tracypholmes authored Feb 21, 2020
1 parent a581836 commit 70606d2
Show file tree
Hide file tree
Showing 40 changed files with 1,493 additions and 311 deletions.
38 changes: 38 additions & 0 deletions azurerm/internal/services/dns/parse/dns_a_record.go
Original file line number Diff line number Diff line change
@@ -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
}
86 changes: 86 additions & 0 deletions azurerm/internal/services/dns/parse/dns_a_record_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
38 changes: 38 additions & 0 deletions azurerm/internal/services/dns/parse/dns_aaaa_record.go
Original file line number Diff line number Diff line change
@@ -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
}
86 changes: 86 additions & 0 deletions azurerm/internal/services/dns/parse/dns_aaaa_record_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
38 changes: 38 additions & 0 deletions azurerm/internal/services/dns/parse/dns_caa_record.go
Original file line number Diff line number Diff line change
@@ -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
}
86 changes: 86 additions & 0 deletions azurerm/internal/services/dns/parse/dns_caa_record_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Loading

0 comments on commit 70606d2

Please sign in to comment.