Skip to content

Commit

Permalink
Merge pull request #195 from terraform-providers/dns-ptr-refactor
Browse files Browse the repository at this point in the history
`azurerm_dns_ptr_record` - refactoring to match the DNS updates
  • Loading branch information
tombuildsstuff authored Jul 26, 2017
2 parents 5fe3558 + df0e6e0 commit b0e6828
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 35 deletions.
26 changes: 24 additions & 2 deletions azurerm/import_arm_dns_ptr_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,33 @@ func TestAccAzureRMDnsPtrRecord_importBasic(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsPtrRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMDnsPtrRecord_importWithTags(t *testing.T) {
resourceName := "azurerm_dns_ptr_record.test"

resource.TestStep{
ri := acctest.RandInt()
config := testAccAzureRMDnsPtrRecord_withTags(ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsPtrRecordDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
Expand Down
51 changes: 24 additions & 27 deletions azurerm/resource_arm_dns_ptr_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,44 +48,36 @@ func resourceArmDnsPtrRecord() *schema.Resource {
Required: true,
},

"etag": {
Type: schema.TypeString,
Computed: true,
},

"tags": tagsSchema(),
},
}
}

func resourceArmDnsPtrRecordCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
dnsClient := client.dnsClient
client := meta.(*ArmClient).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,
if err != nil {
return err
}

parameters := dns.RecordSet{
Name: &name,
RecordSetProperties: &props,
RecordSetProperties: &dns.RecordSetProperties{
Metadata: expandTags(tags),
TTL: &ttl,
PtrRecords: &records,
},
}

//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, "")
eTag := ""
ifNoneMatch := "" // set to empty to allow updates to records after creation
resp, err := client.CreateOrUpdate(resGroup, zoneName, name, dns.PTR, parameters, eTag, ifNoneMatch)
if err != nil {
return err
}
Expand Down Expand Up @@ -114,11 +106,12 @@ func resourceArmDnsPtrRecordRead(d *schema.ResourceData, meta interface{}) error

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
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

return fmt.Errorf("Error reading DNS PTR record %s: %+v", name, err)
}

d.Set("name", name)
Expand Down Expand Up @@ -148,9 +141,13 @@ func resourceArmDnsPtrRecordDelete(d *schema.ResourceData, meta interface{}) err
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)
resp, err := dnsClient.Delete(resGroup, zoneName, name, dns.PTR, "")
if err != nil {
if resp.StatusCode == http.StatusNotFound {
return nil
}

return fmt.Errorf("Error deleting DNS PTR Record %s: %+v", name, err)
}

return nil
Expand Down
13 changes: 9 additions & 4 deletions azurerm/resource_arm_dns_ptr_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,14 @@ func testCheckAzureRMDnsPtrRecordDestroy(s *terraform.State) error {
resp, err := conn.Get(resourceGroup, zoneName, ptrName, dns.PTR)

if err != nil {
return nil
}
if resp.StatusCode == http.StatusNotFound {
return nil
}

if resp.StatusCode != http.StatusNotFound {
return fmt.Errorf("DNS PTR record still exists:\n%#v", resp.RecordSetProperties)
return err
}

return fmt.Errorf("DNS PTR record still exists:\n%#v", resp)
}

return nil
Expand All @@ -151,6 +152,7 @@ resource "azurerm_resource_group" "test" {
name = "acctestRG_%[1]d"
location = "West US"
}
resource "azurerm_dns_zone" "test" {
name = "acctestzone%[1]d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
Expand All @@ -172,6 +174,7 @@ resource "azurerm_resource_group" "test" {
name = "acctestRG_%[1]d"
location = "West US"
}
resource "azurerm_dns_zone" "test" {
name = "acctestzone%[1]d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
Expand All @@ -193,6 +196,7 @@ resource "azurerm_resource_group" "test" {
name = "acctestRG_%[1]d"
location = "West US"
}
resource "azurerm_dns_zone" "test" {
name = "acctestzone%[1]d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
Expand All @@ -219,6 +223,7 @@ resource "azurerm_resource_group" "test" {
name = "acctestRG_%[1]d"
location = "West US"
}
resource "azurerm_dns_zone" "test" {
name = "acctestzone%[1]d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
Expand Down
2 changes: 0 additions & 2 deletions website/docs/r/dns_ptr_record.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ The following attributes are exported:

* `id` - The DNS PTR Record ID.

* `etag` - The etag of the record set.

## Import

PTR records can be imported using the `resource id`, e.g.
Expand Down

0 comments on commit b0e6828

Please sign in to comment.