Skip to content

Commit

Permalink
Merge pull request #4841 from phires/private-dns-aaaa
Browse files Browse the repository at this point in the history
new resource azurerm_private_dns_aaaa_record
  • Loading branch information
tombuildsstuff authored Nov 11, 2019
2 parents 87eaf7a + 1fa1324 commit 51f715c
Show file tree
Hide file tree
Showing 5 changed files with 577 additions and 0 deletions.
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_postgresql_virtual_network_rule": resourceArmPostgreSQLVirtualNetworkRule(),
"azurerm_private_dns_zone": resourceArmPrivateDnsZone(),
"azurerm_private_dns_a_record": resourceArmPrivateDnsARecord(),
"azurerm_private_dns_aaaa_record": resourceArmPrivateDnsAaaaRecord(),
"azurerm_private_dns_cname_record": resourceArmPrivateDnsCNameRecord(),
"azurerm_private_dns_ptr_record": resourceArmPrivateDnsPtrRecord(),
"azurerm_private_dns_zone_virtual_network_link": resourceArmPrivateDnsZoneVirtualNetworkLink(),
Expand Down
206 changes: 206 additions & 0 deletions azurerm/resource_arm_private_dns_aaaa_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package azurerm

import (
"fmt"
"net/http"
"time"

"github.com/Azure/azure-sdk-for-go/services/privatedns/mgmt/2018-09-01/privatedns"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"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/features"
"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"
)

func resourceArmPrivateDnsAaaaRecord() *schema.Resource {
return &schema.Resource{
Create: resourceArmPrivateDnsAaaaRecordCreateUpdate,
Read: resourceArmPrivateDnsAaaaRecordRead,
Update: resourceArmPrivateDnsAaaaRecordCreateUpdate,
Delete: resourceArmPrivateDnsAaaaRecordDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

// TODO: make this case sensitive once the API's fixed https://github.com/Azure/azure-rest-api-specs/issues/6641
"resource_group_name": azure.SchemaResourceGroupNameDiffSuppress(),

"zone_name": {
Type: schema.TypeString,
Required: true,
},

"records": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},

"ttl": {
Type: schema.TypeInt,
Required: true,
},

"tags": tags.Schema(),
},
}
}

func resourceArmPrivateDnsAaaaRecordCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).PrivateDns.RecordSetsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*ArmClient).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() {
existing, err := client.Get(ctx, resGroup, zoneName, privatedns.AAAA, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Private DNS AAAA Record %q (Private Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_private_dns_aaaa_record", *existing.ID)
}
}

ttl := int64(d.Get("ttl").(int))
t := d.Get("tags").(map[string]interface{})

parameters := privatedns.RecordSet{
Name: &name,
RecordSetProperties: &privatedns.RecordSetProperties{
Metadata: tags.Expand(t),
TTL: &ttl,
AaaaRecords: expandAzureRmPrivateDnsAaaaRecords(d),
},
}

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

resp, err := client.Get(ctx, resGroup, zoneName, privatedns.AAAA, name)
if err != nil {
return fmt.Errorf("Error retrieving Private DNS AAAA Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err)
}

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

d.SetId(*resp.ID)

return resourceArmPrivateDnsAaaaRecordRead(d, meta)
}

func resourceArmPrivateDnsAaaaRecordRead(d *schema.ResourceData, meta interface{}) error {
dnsClient := meta.(*ArmClient).PrivateDns.RecordSetsClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()

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

resGroup := id.ResourceGroup
name := id.Path["AAAA"]
zoneName := id.Path["privateDnsZones"]

resp, err := dnsClient.Get(ctx, resGroup, zoneName, privatedns.AAAA, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading Private DNS AAAA record %s: %+v", name, err)
}

d.Set("name", name)
d.Set("resource_group_name", resGroup)
d.Set("zone_name", zoneName)
d.Set("ttl", resp.TTL)

if err := d.Set("records", flattenAzureRmPrivateDnsAaaaRecords(resp.AaaaRecords)); err != nil {
return err
}
return tags.FlattenAndSet(d, resp.Metadata)
}

func resourceArmPrivateDnsAaaaRecordDelete(d *schema.ResourceData, meta interface{}) error {
dnsClient := meta.(*ArmClient).PrivateDns.RecordSetsClient
ctx, cancel := timeouts.ForDelete(meta.(*ArmClient).StopContext, d)
defer cancel()

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

resGroup := id.ResourceGroup
name := id.Path["AAAA"]
zoneName := id.Path["privateDnsZones"]

resp, err := dnsClient.Delete(ctx, resGroup, zoneName, privatedns.AAAA, name, "")
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Error deleting Private DNS AAAA Record %s: %+v", name, err)
}

return nil
}

func flattenAzureRmPrivateDnsAaaaRecords(records *[]privatedns.AaaaRecord) []string {
results := make([]string, 0)
if records == nil {
return results
}

for _, record := range *records {
if record.Ipv6Address == nil {
continue
}

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

return results
}

func expandAzureRmPrivateDnsAaaaRecords(d *schema.ResourceData) *[]privatedns.AaaaRecord {
recordStrings := d.Get("records").(*schema.Set).List()
records := make([]privatedns.AaaaRecord, len(recordStrings))

for i, v := range recordStrings {
ipv6 := v.(string)
records[i] = privatedns.AaaaRecord{
Ipv6Address: &ipv6,
}
}

return &records
}
Loading

0 comments on commit 51f715c

Please sign in to comment.