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

azurerm_private_dns_zone - Ensure consistent ID generation #17729

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions internal/services/privatedns/private_dns_zone_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,10 @@ func dataSourcePrivateDnsZoneRead(d *pluginsdk.ResourceData, meta interface{}) e
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
id := privatezones.NewPrivateDnsZoneID(subscriptionId, resourceGroup, name)
id := privatezones.NewPrivateDnsZoneID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

var resp *privatezones.PrivateZone
if resourceGroup != "" {
if id.ResourceGroupName != "" {
zone, err := client.Get(ctx, id)
if err != nil || zone.Model == nil {
if response.WasNotFound(zone.HttpResponse) {
Expand All @@ -83,23 +81,23 @@ func dataSourcePrivateDnsZoneRead(d *pluginsdk.ResourceData, meta interface{}) e
} else {
resourcesClient := meta.(*clients.Client).Resource.ResourcesClient

zone, err := findPrivateZone(ctx, client, resourcesClient, name)
zone, err := findPrivateZone(ctx, client, resourcesClient, id.PrivateZoneName)
if err != nil {
return err
}

if zone == nil {
return fmt.Errorf("Private DNS Zone %q was not found", name)
return fmt.Errorf("%s was not found", id)
}

resp = &zone.zone
resourceGroup = zone.resourceGroup
id.ResourceGroupName = zone.resourceGroup
}

d.SetId(id.ID())

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
d.Set("name", id.PrivateZoneName)
d.Set("resource_group_name", id.ResourceGroupName)

if props := resp.Properties; props != nil {
d.Set("number_of_record_sets", props.NumberOfRecordSets)
Expand Down
63 changes: 32 additions & 31 deletions internal/services/privatedns/private_dns_zone_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ func TestAccDataSourcePrivateDNSZone_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_private_dns_zone", "test")
r := PrivateDnsZoneDatasource{}

resourceName := "azurerm_private_dns_zone.test"

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("id").MatchesOtherKey(check.That(resourceName).Key("id")),
check.That(data.ResourceName).Key("tags.%").HasValue("0"),
),
},
Expand All @@ -28,10 +31,13 @@ func TestAccDataSourcePrivateDNSZone_tags(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_private_dns_zone", "test")
r := PrivateDnsZoneDatasource{}

resourceName := "azurerm_private_dns_zone.test"

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.tags(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("id").MatchesOtherKey(check.That(resourceName).Key("id")),
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
check.That(data.ResourceName).Key("tags.hello").HasValue("world"),
),
Expand All @@ -42,42 +48,37 @@ func TestAccDataSourcePrivateDNSZone_tags(t *testing.T) {
func TestAccDataSourcePrivateDNSZone_withoutResourceGroupName(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_private_dns_zone", "test")
r := PrivateDnsZoneDatasource{}
resourceGroupName := fmt.Sprintf("acctestRG-%d", data.RandomInteger)

resourceName := "azurerm_private_dns_zone.test"

// This test is split across multiple test steps to avoid an API race
// condition that occures when running multiple test cases in parallel
data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.onlyNamePrep(data, resourceGroupName),
Config: r.template(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(resourceName).Key("id").Exists(),
),
},
{
Config: r.onlyName(data, resourceGroupName),
Config: r.withoutResourceGroupName(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("resource_group_name").HasValue(resourceGroupName),
check.That(data.ResourceName).Key("id").MatchesOtherKey(check.That(resourceName).Key("id")),
check.That(data.ResourceName).Key("resource_group_name").MatchesOtherKey(check.That(resourceName).Key("resource_group_name")),
),
},
})
}

func (PrivateDnsZoneDatasource) basic(data acceptance.TestData) string {
func (r PrivateDnsZoneDatasource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_private_dns_zone" "test" {
name = "acctestzone%d.internal"
resource_group_name = azurerm_resource_group.test.name
}
%[1]s

data "azurerm_private_dns_zone" "test" {
name = azurerm_private_dns_zone.test.name
resource_group_name = azurerm_private_dns_zone.test.resource_group_name
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
`, r.template(data))
}

func (PrivateDnsZoneDatasource) tags(data acceptance.TestData) string {
Expand All @@ -87,12 +88,12 @@ provider "azurerm" {
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
name = "acctestRG-%[1]d"
location = "%[2]s"
}

resource "azurerm_private_dns_zone" "test" {
name = "acctestzone%d.internal"
name = "acctestzone%[1]d.internal"
resource_group_name = azurerm_resource_group.test.name

tags = {
Expand All @@ -104,33 +105,33 @@ data "azurerm_private_dns_zone" "test" {
name = azurerm_private_dns_zone.test.name
resource_group_name = azurerm_private_dns_zone.test.resource_group_name
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
`, data.RandomInteger, data.Locations.Primary)
}

func (PrivateDnsZoneDatasource) onlyNamePrep(data acceptance.TestData, resourceGroupName string) string {
func (PrivateDnsZoneDatasource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "%s"
location = "%s"
name = "acctestRG-%[1]d"
location = "%[2]s"
}

resource "azurerm_private_dns_zone" "test" {
name = "acctestzone%d.internal"
name = "acctestzone%[1]d.internal"
resource_group_name = azurerm_resource_group.test.name
}
`, resourceGroupName, data.Locations.Primary, data.RandomInteger)
`, data.RandomInteger, data.Locations.Primary)
}

func (r PrivateDnsZoneDatasource) onlyName(data acceptance.TestData, resourceGroupName string) string {
func (r PrivateDnsZoneDatasource) withoutResourceGroupName(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
%[1]s

data "azurerm_private_dns_zone" "test" {
name = azurerm_private_dns_zone.test.name
}
`, r.onlyNamePrep(data, resourceGroupName))
`, r.template(data))
}