-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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 #141 from echuvyrov/dnsptrrecord
[MS] Adding DNS PTR Record
- Loading branch information
Showing
17 changed files
with
2,002 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package azurerm | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccAzureRMDnsPtrRecord_importBasic(t *testing.T) { | ||
resourceName := "azurerm_dns_ptr_record.test" | ||
|
||
ri := acctest.RandInt() | ||
config := testAccAzureRMDnsPtrRecord_basic(ri) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMDnsPtrRecordDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: config, | ||
}, | ||
|
||
resource.TestStep{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} |
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,183 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/Azure/azure-sdk-for-go/arm/dns" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceArmDnsPtrRecord() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmDnsPtrRecordCreateOrUpdate, | ||
Read: resourceArmDnsPtrRecordRead, | ||
Update: resourceArmDnsPtrRecordCreateOrUpdate, | ||
Delete: resourceArmDnsPtrRecordDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"resource_group_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"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, | ||
}, | ||
|
||
"etag": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"tags": tagsSchema(), | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmDnsPtrRecordCreateOrUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient) | ||
dnsClient := client.dnsClient | ||
|
||
name := d.Get("name").(string) | ||
resGroup := d.Get("resource_group_name").(string) | ||
zoneName := d.Get("zone_name").(string) | ||
ttl := int64(d.Get("ttl").(int)) | ||
eTag := d.Get("etag").(string) | ||
|
||
tags := d.Get("tags").(map[string]interface{}) | ||
metadata := expandTags(tags) | ||
|
||
records, err := expandAzureRmDnsPtrRecords(d) | ||
props := dns.RecordSetProperties{ | ||
Metadata: metadata, | ||
TTL: &ttl, | ||
PtrRecords: &records, | ||
} | ||
|
||
parameters := dns.RecordSet{ | ||
Name: &name, | ||
RecordSetProperties: &props, | ||
} | ||
|
||
//last parameter is set to empty to allow updates to records after creation | ||
// (per SDK, set it to '*' to prevent updates, all other values are ignored) | ||
resp, err := dnsClient.CreateOrUpdate(resGroup, zoneName, name, dns.PTR, parameters, eTag, "") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.ID == nil { | ||
return fmt.Errorf("Cannot read DNS PTR Record %s (resource group %s) ID", name, resGroup) | ||
} | ||
|
||
d.SetId(*resp.ID) | ||
|
||
return resourceArmDnsPtrRecordRead(d, meta) | ||
} | ||
|
||
func resourceArmDnsPtrRecordRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient) | ||
dnsClient := client.dnsClient | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resGroup := id.ResourceGroup | ||
name := id.Path["PTR"] | ||
zoneName := id.Path["dnszones"] | ||
|
||
resp, err := dnsClient.Get(resGroup, zoneName, name, dns.PTR) | ||
if err != nil { | ||
return fmt.Errorf("Error reading DNS PTR record %s: %v", name, err) | ||
} | ||
if resp.StatusCode == http.StatusNotFound { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
d.Set("name", name) | ||
d.Set("resource_group_name", resGroup) | ||
d.Set("zone_name", zoneName) | ||
d.Set("ttl", resp.TTL) | ||
d.Set("etag", resp.Etag) | ||
|
||
if err := d.Set("records", flattenAzureRmDnsPtrRecords(resp.PtrRecords)); err != nil { | ||
return err | ||
} | ||
flattenAndSetTags(d, resp.Metadata) | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmDnsPtrRecordDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient) | ||
dnsClient := client.dnsClient | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resGroup := id.ResourceGroup | ||
name := id.Path["PTR"] | ||
zoneName := id.Path["dnszones"] | ||
|
||
resp, error := dnsClient.Delete(resGroup, zoneName, name, dns.PTR, "") | ||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("Error deleting DNS PTR Record %s: %s", name, error) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func flattenAzureRmDnsPtrRecords(records *[]dns.PtrRecord) []string { | ||
results := make([]string, 0, len(*records)) | ||
|
||
if records != nil { | ||
for _, record := range *records { | ||
results = append(results, *record.Ptrdname) | ||
} | ||
} | ||
|
||
return results | ||
} | ||
|
||
func expandAzureRmDnsPtrRecords(d *schema.ResourceData) ([]dns.PtrRecord, error) { | ||
recordStrings := d.Get("records").(*schema.Set).List() | ||
records := make([]dns.PtrRecord, len(recordStrings)) | ||
|
||
for i, v := range recordStrings { | ||
fqdn := v.(string) | ||
records[i] = dns.PtrRecord{ | ||
Ptrdname: &fqdn, | ||
} | ||
} | ||
|
||
return records, nil | ||
} |
Oops, something went wrong.