-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3849 from patst/azurerm_private_dns_a_record
New resource: azurerm_private_dns_zone_a_record
- Loading branch information
Showing
6 changed files
with
567 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/privatedns/mgmt/2018-09-01/privatedns" | ||
"github.com/hashicorp/terraform/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/utils" | ||
) | ||
|
||
func resourceArmPrivateDnsARecord() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmPrivateDnsARecordCreateUpdate, | ||
Read: resourceArmPrivateDnsARecordRead, | ||
Update: resourceArmPrivateDnsARecordCreateUpdate, | ||
Delete: resourceArmPrivateDnsARecordDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
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": tagsSchema(), | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmPrivateDnsARecordCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).privateDns.RecordSetsClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
name := d.Get("name").(string) | ||
resGroup := d.Get("resource_group_name").(string) | ||
zoneName := d.Get("zone_name").(string) | ||
|
||
if requireResourcesToBeImported && d.IsNewResource() { | ||
existing, err := client.Get(ctx, resGroup, zoneName, privatedns.A, name) | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(existing.Response) { | ||
return fmt.Errorf("Error checking for presence of existing Private DNS A 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_a_record", *existing.ID) | ||
} | ||
} | ||
|
||
ttl := int64(d.Get("ttl").(int)) | ||
tags := d.Get("tags").(map[string]interface{}) | ||
|
||
parameters := privatedns.RecordSet{ | ||
Name: &name, | ||
RecordSetProperties: &privatedns.RecordSetProperties{ | ||
Metadata: expandTags(tags), | ||
TTL: &ttl, | ||
ARecords: expandAzureRmPrivateDnsARecords(d), | ||
}, | ||
} | ||
|
||
eTag := "" | ||
ifNoneMatch := "" // set to empty to allow updates to records after creation | ||
if _, err := client.CreateOrUpdate(ctx, resGroup, zoneName, privatedns.A, name, parameters, eTag, ifNoneMatch); err != nil { | ||
return fmt.Errorf("Error creating/updating Private DNS A Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err) | ||
} | ||
|
||
resp, err := client.Get(ctx, resGroup, zoneName, privatedns.A, name) | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving Private DNS A Record %q (Zone %q / Resource Group %q): %s", name, zoneName, resGroup, err) | ||
} | ||
|
||
if resp.ID == nil { | ||
return fmt.Errorf("Cannot read Private DNS A Record %s (resource group %s) ID", name, resGroup) | ||
} | ||
|
||
d.SetId(*resp.ID) | ||
|
||
return resourceArmPrivateDnsARecordRead(d, meta) | ||
} | ||
|
||
func resourceArmPrivateDnsARecordRead(d *schema.ResourceData, meta interface{}) error { | ||
dnsClient := meta.(*ArmClient).privateDns.RecordSetsClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resGroup := id.ResourceGroup | ||
name := id.Path["A"] | ||
zoneName := id.Path["privateDnsZones"] | ||
|
||
resp, err := dnsClient.Get(ctx, resGroup, zoneName, privatedns.A, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error reading Private DNS A 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", flattenAzureRmPrivateDnsARecords(resp.ARecords)); err != nil { | ||
return err | ||
} | ||
flattenAndSetTags(d, resp.Metadata) | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmPrivateDnsARecordDelete(d *schema.ResourceData, meta interface{}) error { | ||
dnsClient := meta.(*ArmClient).privateDns.RecordSetsClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resGroup := id.ResourceGroup | ||
name := id.Path["A"] | ||
zoneName := id.Path["privateDnsZones"] | ||
|
||
resp, err := dnsClient.Delete(ctx, resGroup, zoneName, privatedns.A, name, "") | ||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("Error deleting Private DNS A Record %s: %+v", name, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func flattenAzureRmPrivateDnsARecords(records *[]privatedns.ARecord) []string { | ||
results := make([]string, 0) | ||
if records == nil { | ||
return results | ||
} | ||
|
||
for _, record := range *records { | ||
if record.Ipv4Address == nil { | ||
continue | ||
} | ||
|
||
results = append(results, *record.Ipv4Address) | ||
} | ||
|
||
return results | ||
} | ||
|
||
func expandAzureRmPrivateDnsARecords(d *schema.ResourceData) *[]privatedns.ARecord { | ||
recordStrings := d.Get("records").(*schema.Set).List() | ||
records := make([]privatedns.ARecord, len(recordStrings)) | ||
|
||
for i, v := range recordStrings { | ||
ipv4 := v.(string) | ||
records[i] = privatedns.ARecord{ | ||
Ipv4Address: &ipv4, | ||
} | ||
} | ||
|
||
return &records | ||
} |
Oops, something went wrong.