Skip to content

Commit

Permalink
Merge pull request #5794 from terraform-providers/2.0-dns
Browse files Browse the repository at this point in the history
2.0-dns
  • Loading branch information
tombuildsstuff authored Feb 18, 2020
2 parents 4717ba4 + 30be1a4 commit a8c4a69
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 551 deletions.
39 changes: 0 additions & 39 deletions azurerm/internal/services/dns/data_source_dns_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,6 @@ func dataSourceArmDnsZone() *schema.Resource {
Set: schema.HashString,
},

"zone_type": {
Type: schema.TypeString,
Computed: true,
Deprecated: "Private DNS Zones are now supported through a separate resource in Azure & Terraform",
},

"registration_virtual_network_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"resolution_virtual_network_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"tags": tags.SchemaDataSource(),
},
}
Expand Down Expand Up @@ -114,27 +96,6 @@ func dataSourceArmDnsZoneRead(d *schema.ResourceData, meta interface{}) error {
if props := resp.ZoneProperties; props != nil {
d.Set("number_of_record_sets", props.NumberOfRecordSets)
d.Set("max_number_of_record_sets", props.MaxNumberOfRecordSets)
d.Set("zone_type", props.ZoneType)

registrationVNets := make([]string, 0)
if rvns := props.RegistrationVirtualNetworks; rvns != nil {
for _, rvn := range *rvns {
registrationVNets = append(registrationVNets, *rvn.ID)
}
}
if err := d.Set("registration_virtual_network_ids", registrationVNets); err != nil {
return err
}

resolutionVNets := make([]string, 0)
if rvns := props.ResolutionVirtualNetworks; rvns != nil {
for _, rvn := range *rvns {
resolutionVNets = append(resolutionVNets, *rvn.ID)
}
}
if err := d.Set("resolution_virtual_network_ids", resolutionVNets); err != nil {
return err
}

nameServers := make([]string, 0)
if ns := props.NameServers; ns != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ func resourceArmDnsCNameRecord() *schema.Resource {
Required: true,
},

"records": {
Type: schema.TypeString,
Optional: true,
Removed: "Use `record` instead. This attribute will be removed in a future version",
},

"record": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -73,7 +67,7 @@ func resourceArmDnsCNameRecord() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ValidateFunc: azure.ValidateResourceID,
ConflictsWith: []string{"records"},
ConflictsWith: []string{"record"},
},

"tags": tags.Schema(),
Expand Down
165 changes: 83 additions & 82 deletions azurerm/internal/services/dns/resource_arm_dns_ns_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (

func resourceArmDnsNsRecord() *schema.Resource {
return &schema.Resource{
Create: resourceArmDnsNsRecordCreateUpdate,
Create: resourceArmDnsNsRecordCreate,
Read: resourceArmDnsNsRecordRead,
Update: resourceArmDnsNsRecordCreateUpdate,
Update: resourceArmDnsNsRecordUpdate,
Delete: resourceArmDnsNsRecordDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand All @@ -45,30 +45,14 @@ func resourceArmDnsNsRecord() *schema.Resource {
"zone_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"records": {
Type: schema.TypeList,
//TODO: add `Required: true` once we remove the `record` attribute
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
ConflictsWith: []string{"record"},
},

"record": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Deprecated: "This field has been replaced by `records`",
ConflictsWith: []string{"records"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"nsdname": {
Type: schema.TypeString,
Required: true,
},
},
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

Expand All @@ -87,16 +71,16 @@ func resourceArmDnsNsRecord() *schema.Resource {
}
}

func resourceArmDnsNsRecordCreateUpdate(d *schema.ResourceData, meta interface{}) error {
func resourceArmDnsNsRecordCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Dns.RecordSetsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

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

if features.ShouldResourcesBeImported() && d.IsNewResource() {
if features.ShouldResourcesBeImported() {
existing, err := client.Get(ctx, resGroup, zoneName, name, dns.NS)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
Expand All @@ -112,19 +96,22 @@ func resourceArmDnsNsRecordCreateUpdate(d *schema.ResourceData, meta interface{}
ttl := int64(d.Get("ttl").(int))
t := d.Get("tags").(map[string]interface{})

recordsRaw := d.Get("records").([]interface{})
records := expandAzureRmDnsNsRecords(recordsRaw)

parameters := dns.RecordSet{
Name: &name,
RecordSetProperties: &dns.RecordSetProperties{
Metadata: tags.Expand(t),
TTL: &ttl,
NsRecords: expandAzureRmDnsNsRecords(d),
NsRecords: records,
},
}

eTag := ""
ifNoneMatch := "" // set to empty to allow updates to records after creation
if _, err := client.CreateOrUpdate(ctx, resGroup, zoneName, name, dns.NS, parameters, eTag, ifNoneMatch); err != nil {
return fmt.Errorf("Error creating/updating DNS NS Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
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)
Expand All @@ -141,6 +128,53 @@ func resourceArmDnsNsRecordCreateUpdate(d *schema.ResourceData, meta interface{}
return resourceArmDnsNsRecordRead(d, meta)
}

func resourceArmDnsNsRecordUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Dns.RecordSetsClient
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
if err != nil {
return err
}

resGroup := id.ResourceGroup
name := id.Path["NS"]
zoneName := id.Path["dnszones"]

existing, err := client.Get(ctx, resGroup, zoneName, name, dns.NS)
if err != nil {
return fmt.Errorf("Error retrieving NS %q (DNS Zone %q / Resource Group %q): %+v", name, zoneName, resGroup, err)
}

if existing.RecordSetProperties == nil {
return fmt.Errorf("Error retrieving NS %q (DNS Zone %q / Resource Group %q): `properties` was nil", name, zoneName, resGroup)
}

if d.HasChange("records") {
recordsRaw := d.Get("records").([]interface{})
records := expandAzureRmDnsNsRecords(recordsRaw)
existing.RecordSetProperties.NsRecords = records
}

if d.HasChange("tags") {
t := d.Get("tags").(map[string]interface{})
existing.RecordSetProperties.Metadata = tags.Expand(t)
}

if d.HasChange("ttl") {
existing.RecordSetProperties.TTL = utils.Int64(int64(d.Get("ttl").(int)))
}

eTag := ""
ifNoneMatch := "" // set to empty to allow updates to records after creation
if _, err := client.CreateOrUpdate(ctx, resGroup, zoneName, name, dns.NS, existing, eTag, ifNoneMatch); err != nil {
return fmt.Errorf("Error updating DNS NS Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}

return resourceArmDnsNsRecordRead(d, meta)
}

func resourceArmDnsNsRecordRead(d *schema.ResourceData, meta interface{}) error {
dnsClient := meta.(*clients.Client).Dns.RecordSetsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
Expand Down Expand Up @@ -170,13 +204,10 @@ func resourceArmDnsNsRecordRead(d *schema.ResourceData, meta interface{}) error
d.Set("ttl", resp.TTL)
d.Set("fqdn", resp.Fqdn)

if err := d.Set("records", flattenAzureRmDnsNsRecords(resp.NsRecords)); err != nil {
return fmt.Errorf("Error settings `records`: %+v", err)
}

//TODO: remove this once we remove the `record` attribute
if err := d.Set("record", flattenAzureRmDnsNsRecordsSet(resp.NsRecords)); err != nil {
return fmt.Errorf("Error settings `record`: %+v", err)
if props := resp.RecordSetProperties; props != nil {
if err := d.Set("records", flattenAzureRmDnsNsRecords(props.NsRecords)); err != nil {
return fmt.Errorf("Error settings `records`: %+v", err)
}
}

return tags.FlattenAndSet(d, resp.Metadata)
Expand Down Expand Up @@ -204,63 +235,33 @@ func resourceArmDnsNsRecordDelete(d *schema.ResourceData, meta interface{}) erro
return nil
}

//TODO: remove this once we remove the `record` attribute
func flattenAzureRmDnsNsRecordsSet(records *[]dns.NsRecord) []map[string]interface{} {
results := make([]map[string]interface{}, 0, len(*records))

if records != nil {
for _, record := range *records {
nsRecord := make(map[string]interface{})
nsRecord["nsdname"] = *record.Nsdname
results = append(results, nsRecord)
}
func flattenAzureRmDnsNsRecords(records *[]dns.NsRecord) []interface{} {
if records == nil {
return []interface{}{}
}

return results
}

func flattenAzureRmDnsNsRecords(records *[]dns.NsRecord) []string {
results := make([]string, 0, len(*records))

if records != nil {
for _, record := range *records {
results = append(results, *record.Nsdname)
results := make([]interface{}, 0)
for _, record := range *records {
if record.Nsdname == nil {
continue
}

results = append(results, *record.Nsdname)
}

return results
}

func expandAzureRmDnsNsRecords(d *schema.ResourceData) *[]dns.NsRecord {
var records []dns.NsRecord
func expandAzureRmDnsNsRecords(input []interface{}) *[]dns.NsRecord {
records := make([]dns.NsRecord, len(input))
for i, v := range input {
record := v.(string)

//TODO: remove this once we remove the `record` attribute
if d.HasChange("records") || !d.HasChange("record") {
recordStrings := d.Get("records").([]interface{})
records = make([]dns.NsRecord, len(recordStrings))
for i, v := range recordStrings {
record := v.(string)

nsRecord := dns.NsRecord{
Nsdname: &record,
}

records[i] = nsRecord
}
} else {
recordList := d.Get("record").(*schema.Set).List()
if len(recordList) != 0 {
records = make([]dns.NsRecord, len(recordList))
for i, v := range recordList {
record := v.(map[string]interface{})
nsdname := record["nsdname"].(string)
nsRecord := dns.NsRecord{
Nsdname: &nsdname,
}

records[i] = nsRecord
}
nsRecord := dns.NsRecord{
Nsdname: &record,
}

records[i] = nsRecord
}
return &records
}
Loading

0 comments on commit a8c4a69

Please sign in to comment.