Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

data source azurerm_dns_zone: make resource_group_name required #7680

Merged
merged 4 commits into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions azurerm/internal/services/dns/dns_zone_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2018-05-01/dns"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -77,14 +77,12 @@ func dataSourceArmDnsZoneRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error reading DNS Zone %q (Resource Group %q): %+v", name, resourceGroup, err)
}
} else {
rgClient := meta.(*clients.Client).Resource.GroupsClient

resp, resourceGroup, err = findZone(client, rgClient, ctx, name)
resp, resourceGroup, err = findZone(client, ctx, name)
if err != nil {
return err
}

if resourceGroup == "" {
if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("Error: DNS Zone %q was not found", name)
}
}
Expand All @@ -109,26 +107,33 @@ func dataSourceArmDnsZoneRead(d *schema.ResourceData, meta interface{}) error {
return tags.FlattenAndSet(d, resp.Tags)
}

func findZone(client *dns.ZonesClient, rgClient *resources.GroupsClient, ctx context.Context, name string) (dns.Zone, string, error) {
groups, err := rgClient.List(ctx, "", nil)
func findZone(client *dns.ZonesClient, ctx context.Context, name string) (dns.Zone, string, error) {
njuCZ marked this conversation as resolved.
Show resolved Hide resolved
zonesIterator, err := client.ListComplete(ctx, nil)
if err != nil {
return dns.Zone{}, "", fmt.Errorf("Error listing Resource Groups: %+v", err)
return dns.Zone{}, "", fmt.Errorf("listing DNS Zones: %+v", err)
njuCZ marked this conversation as resolved.
Show resolved Hide resolved
}

for _, g := range groups.Values() {
resourceGroup := *g.Name

zones, err := client.ListByResourceGroup(ctx, resourceGroup, nil)
if err != nil {
return dns.Zone{}, "", fmt.Errorf("Error listing DNS Zones (Resource Group: %s): %+v", resourceGroup, err)
}

for _, z := range zones.Values() {
if *z.Name == name {
return z, resourceGroup, nil
var found *dns.Zone
for zonesIterator.NotDone() {
zone := zonesIterator.Value()
if zone.Name != nil && *zone.Name == name {
if found != nil {
return dns.Zone{}, "", fmt.Errorf("found multiple DNS zones with name %q, please specify the resource group", name)
njuCZ marked this conversation as resolved.
Show resolved Hide resolved
}
found = &zone
}
if err := zonesIterator.NextWithContext(ctx); err != nil {
return dns.Zone{}, "", fmt.Errorf("listing DNS Zones: %+v", err)
njuCZ marked this conversation as resolved.
Show resolved Hide resolved
}
}

if found == nil || found.ID == nil {
return dns.Zone{}, "", fmt.Errorf("could not find DNS zone with name: %q", name)
njuCZ marked this conversation as resolved.
Show resolved Hide resolved
}

return dns.Zone{}, "", nil
id, err := parse.DnsZoneID(*found.ID)
if err != nil {
return dns.Zone{}, "", fmt.Errorf("DNS zone id not valid: %+v", err)
njuCZ marked this conversation as resolved.
Show resolved Hide resolved
}
return *found, id.ResourceGroup, nil
njuCZ marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func TestAccDataSourceAzureRMDNSZone_tags(t *testing.T) {

func TestAccDataSourceAzureRMDNSZone_withoutResourceGroupName(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_dns_zone", "test")
resourceGroupName := fmt.Sprintf("acctestRG-%d", data.RandomInteger)
// resource group of DNS zone is always small case
resourceGroupName := fmt.Sprintf("acctestrg-%d", data.RandomInteger)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Expand Down