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

dns_zone: Compute resourceId from the parser, add state migration, switch dns package to generated IDs #10786

Merged
merged 5 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 5 additions & 6 deletions azurerm/internal/services/dns/dns_a_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,15 @@ func resourceDnsARecord() *schema.Resource {
func resourceDnsARecordCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Dns.RecordSetsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
defer cancel()

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
zoneName := d.Get("zone_name").(string)

resourceId := parse.NewARecordID(subscriptionId, resGroup, zoneName, name)

if d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, zoneName, name, dns.A)
if err != nil {
Expand Down Expand Up @@ -133,16 +136,12 @@ func resourceDnsARecordCreateUpdate(d *schema.ResourceData, meta interface{}) er
return fmt.Errorf("Error creating/updating DNS A Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}

resp, err := client.Get(ctx, resGroup, zoneName, name, dns.A)
_, err := client.Get(ctx, resGroup, zoneName, name, dns.A)
if err != nil {
return fmt.Errorf("Error retrieving DNS A Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}
favoretti marked this conversation as resolved.
Show resolved Hide resolved

if resp.ID == nil {
return fmt.Errorf("Error retrieving DNS A Record %q (Zone %q / Resource Group %q): ID was nil", name, zoneName, resGroup)
}

d.SetId(*resp.ID)
d.SetId(resourceId.ID())

return resourceDnsARecordRead(d, meta)
}
Expand Down
11 changes: 5 additions & 6 deletions azurerm/internal/services/dns/dns_aaaa_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ func resourceDnsAAAARecord() *schema.Resource {
func resourceDnsAaaaRecordCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Dns.RecordSetsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
defer cancel()

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
zoneName := d.Get("zone_name").(string)

resourceId := parse.NewAaaaRecordID(subscriptionId, resGroup, zoneName, name)

if d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, zoneName, name, dns.AAAA)
if err != nil {
Expand Down Expand Up @@ -137,16 +140,12 @@ func resourceDnsAaaaRecordCreateUpdate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error creating/updating DNS AAAA Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}

resp, err := client.Get(ctx, resGroup, zoneName, name, dns.AAAA)
_, err := client.Get(ctx, resGroup, zoneName, name, dns.AAAA)
if err != nil {
return fmt.Errorf("Error retrieving DNS AAAA Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}
favoretti marked this conversation as resolved.
Show resolved Hide resolved

if resp.ID == nil {
return fmt.Errorf("Error retrieving DNS AAAA Record %q (Zone %q / Resource Group %q): ID was nil", name, zoneName, resGroup)
}

d.SetId(*resp.ID)
d.SetId(resourceId.ID())

return resourceDnsAaaaRecordRead(d, meta)
}
Expand Down
11 changes: 5 additions & 6 deletions azurerm/internal/services/dns/dns_caa_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,15 @@ func resourceDnsCaaRecord() *schema.Resource {
func resourceDnsCaaRecordCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Dns.RecordSetsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
defer cancel()

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
zoneName := d.Get("zone_name").(string)

resourceId := parse.NewCaaRecordID(subscriptionId, resGroup, zoneName, name)

if d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, zoneName, name, dns.CAA)
if err != nil {
Expand Down Expand Up @@ -138,16 +141,12 @@ func resourceDnsCaaRecordCreateUpdate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error creating/updating DNS CAA Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}

resp, err := client.Get(ctx, resGroup, zoneName, name, dns.CAA)
_, err := client.Get(ctx, resGroup, zoneName, name, dns.CAA)
if err != nil {
return fmt.Errorf("Error retrieving DNS CAA Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}
favoretti marked this conversation as resolved.
Show resolved Hide resolved

if resp.ID == nil {
return fmt.Errorf("Cannot read DNS CAA Record %s (resource group %s) ID", name, resGroup)
}

d.SetId(*resp.ID)
d.SetId(resourceId.ID())

return resourceDnsCaaRecordRead(d, meta)
}
Expand Down
11 changes: 5 additions & 6 deletions azurerm/internal/services/dns/dns_cname_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@ func resourceDnsCNameRecord() *schema.Resource {
func resourceDnsCNameRecordCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Dns.RecordSetsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
defer cancel()

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
zoneName := d.Get("zone_name").(string)

resourceId := parse.NewCnameRecordID(subscriptionId, resGroup, zoneName, name)

if d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, zoneName, name, dns.CNAME)
if err != nil {
Expand Down Expand Up @@ -134,16 +137,12 @@ func resourceDnsCNameRecordCreateUpdate(d *schema.ResourceData, meta interface{}
return fmt.Errorf("Error creating/updating CNAME Record %q (DNS Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}

resp, err := client.Get(ctx, resGroup, zoneName, name, dns.CNAME)
_, err := client.Get(ctx, resGroup, zoneName, name, dns.CNAME)
if err != nil {
return fmt.Errorf("Error retrieving CNAME Record %q (DNS Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}
favoretti marked this conversation as resolved.
Show resolved Hide resolved

if resp.ID == nil {
return fmt.Errorf("Error retrieving CNAME Record %q (DNS Zone %q / Resource Group %q): ID was nil", name, zoneName, resGroup)
}

d.SetId(*resp.ID)
d.SetId(resourceId.ID())

return resourceDnsCNameRecordRead(d, meta)
}
Expand Down
11 changes: 5 additions & 6 deletions azurerm/internal/services/dns/dns_mx_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,15 @@ func resourceDnsMxRecord() *schema.Resource {
func resourceDnsMxRecordCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Dns.RecordSetsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
defer cancel()

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
zoneName := d.Get("zone_name").(string)

resourceId := parse.NewMxRecordID(subscriptionId, resGroup, zoneName, name)

if d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, zoneName, name, dns.MX)
if err != nil {
Expand Down Expand Up @@ -126,16 +129,12 @@ func resourceDnsMxRecordCreateUpdate(d *schema.ResourceData, meta interface{}) e
return fmt.Errorf("Error creating/updating DNS MX Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}

resp, err := client.Get(ctx, resGroup, zoneName, name, dns.MX)
_, err := client.Get(ctx, resGroup, zoneName, name, dns.MX)
if err != nil {
return fmt.Errorf("Error retrieving DNS MX Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}
favoretti marked this conversation as resolved.
Show resolved Hide resolved

if resp.ID == nil {
return fmt.Errorf("Cannot read DNS MX Record %s (resource group %s) ID", name, resGroup)
}

d.SetId(*resp.ID)
d.SetId(resourceId.ID())

return resourceDnsMxRecordRead(d, meta)
}
Expand Down
11 changes: 5 additions & 6 deletions azurerm/internal/services/dns/dns_ns_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@ func resourceDnsNsRecord() *schema.Resource {
func resourceDnsNsRecordCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Dns.RecordSetsClient
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
defer cancel()

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
zoneName := d.Get("zone_name").(string)

resourceId := parse.NewNsRecordID(subscriptionId, resGroup, zoneName, name)

existing, err := client.Get(ctx, resGroup, zoneName, name, dns.NS)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
Expand Down Expand Up @@ -114,16 +117,12 @@ func resourceDnsNsRecordCreate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error creating DNS NS Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}

resp, err := client.Get(ctx, resGroup, zoneName, name, dns.NS)
_, err = client.Get(ctx, resGroup, zoneName, name, dns.NS)
if err != nil {
return fmt.Errorf("Error retrieving DNS NS Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}
favoretti marked this conversation as resolved.
Show resolved Hide resolved

if resp.ID == nil {
return fmt.Errorf("Cannot read DNS NS Record %s (resource group %s) ID", name, resGroup)
}

d.SetId(*resp.ID)
d.SetId(resourceId.ID())

return resourceDnsNsRecordRead(d, meta)
}
Expand Down
11 changes: 5 additions & 6 deletions azurerm/internal/services/dns/dns_ptr_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,15 @@ func resourceDnsPtrRecord() *schema.Resource {
func resourceDnsPtrRecordCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Dns.RecordSetsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
defer cancel()

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
zoneName := d.Get("zone_name").(string)

resourceId := parse.NewPtrRecordID(subscriptionId, resGroup, zoneName, name)

if d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, zoneName, name, dns.PTR)
if err != nil {
Expand Down Expand Up @@ -110,16 +113,12 @@ func resourceDnsPtrRecordCreateUpdate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error creating/updating DNS PTR Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}

resp, err := client.Get(ctx, resGroup, zoneName, name, dns.PTR)
_, err := client.Get(ctx, resGroup, zoneName, name, dns.PTR)
if err != nil {
return fmt.Errorf("Error retrieving DNS PTR Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}
favoretti marked this conversation as resolved.
Show resolved Hide resolved

if resp.ID == nil {
return fmt.Errorf("Cannot read DNS PTR Record %s (resource group %s) ID", name, resGroup)
}

d.SetId(*resp.ID)
d.SetId(resourceId.ID())

return resourceDnsPtrRecordRead(d, meta)
}
Expand Down
11 changes: 5 additions & 6 deletions azurerm/internal/services/dns/dns_srv_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,15 @@ func resourceDnsSrvRecord() *schema.Resource {
func resourceDnsSrvRecordCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Dns.RecordSetsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
defer cancel()

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
zoneName := d.Get("zone_name").(string)

resourceId := parse.NewSrvRecordID(subscriptionId, resGroup, zoneName, name)

if d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, zoneName, name, dns.SRV)
if err != nil {
Expand Down Expand Up @@ -133,16 +136,12 @@ func resourceDnsSrvRecordCreateUpdate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error creating/updating DNS SRV Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}

resp, err := client.Get(ctx, resGroup, zoneName, name, dns.SRV)
_, err := client.Get(ctx, resGroup, zoneName, name, dns.SRV)
if err != nil {
return fmt.Errorf("Error retrieving DNS SRV Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}
favoretti marked this conversation as resolved.
Show resolved Hide resolved

if resp.ID == nil {
return fmt.Errorf("Cannot read DNS SRV Record %s (resource group %s) ID", name, resGroup)
}

d.SetId(*resp.ID)
d.SetId(resourceId.ID())

return resourceDnsSrvRecordRead(d, meta)
}
Expand Down
11 changes: 5 additions & 6 deletions azurerm/internal/services/dns/dns_txt_record_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,15 @@ func resourceDnsTxtRecord() *schema.Resource {
func resourceDnsTxtRecordCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Dns.RecordSetsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
defer cancel()

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
zoneName := d.Get("zone_name").(string)

resourceId := parse.NewTxtRecordID(subscriptionId, resGroup, zoneName, name)

if d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, zoneName, name, dns.TXT)
if err != nil {
Expand Down Expand Up @@ -119,16 +122,12 @@ func resourceDnsTxtRecordCreateUpdate(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error creating/updating DNS TXT Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}

resp, err := client.Get(ctx, resGroup, zoneName, name, dns.TXT)
_, err := client.Get(ctx, resGroup, zoneName, name, dns.TXT)
if err != nil {
return fmt.Errorf("Error retrieving DNS TXT Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}
favoretti marked this conversation as resolved.
Show resolved Hide resolved

if resp.ID == nil {
return fmt.Errorf("Cannot read DNS TXT Record %s (resource group %s) ID", name, resGroup)
}

d.SetId(*resp.ID)
d.SetId(resourceId.ID())

return resourceDnsTxtRecordRead(d, meta)
}
Expand Down
6 changes: 5 additions & 1 deletion azurerm/internal/services/dns/dns_zone_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ func dataSourceDnsZoneRead(d *schema.ResourceData, meta interface{}) error {
if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("failed reading ID for DNS Zone %q (Resource Group %q)", name, resourceGroup)
}
d.SetId(*resp.ID)
resourceId, err := parse.DnsZoneID(*resp.ID)
if err != nil {
return fmt.Errorf("Error parsing DNS Zone ID %q", *resp.ID)
}
d.SetId(resourceId.ID())
favoretti marked this conversation as resolved.
Show resolved Hide resolved

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
Expand Down
17 changes: 11 additions & 6 deletions azurerm/internal/services/dns/dns_zone_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"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/services/dns/migration"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
Expand All @@ -27,6 +28,11 @@ func resourceDnsZone() *schema.Resource {
Update: resourceDnsZoneCreateUpdate,
Delete: resourceDnsZoneDelete,

SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
migration.DnsZoneV0ToV1(),
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Expand Down Expand Up @@ -144,12 +150,15 @@ func resourceDnsZone() *schema.Resource {
func resourceDnsZoneCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Dns.ZonesClient
recordSetsClient := meta.(*clients.Client).Dns.RecordSetsClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)

resourceId := parse.NewDnsZoneID(subscriptionId, resGroup, name)

if d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, name)
if err != nil {
Expand Down Expand Up @@ -177,7 +186,7 @@ func resourceDnsZoneCreateUpdate(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("Error creating/updating DNS Zone %q (Resource Group %q): %s", name, resGroup, err)
}

resp, err := client.Get(ctx, resGroup, name)
_, err := client.Get(ctx, resGroup, name)
if err != nil {
return fmt.Errorf("Error retrieving DNS Zone %q (Resource Group %q): %s", name, resGroup, err)
}
favoretti marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -201,11 +210,7 @@ func resourceDnsZoneCreateUpdate(d *schema.ResourceData, meta interface{}) error
}
}

if resp.ID == nil {
return fmt.Errorf("Cannot read DNS Zone %q (Resource Group %q) ID", name, resGroup)
}

d.SetId(*resp.ID)
d.SetId(resourceId.ID())

return resourceDnsZoneRead(d, meta)
}
Expand Down
Loading