Skip to content

Commit

Permalink
Merge pull request #141 from echuvyrov/dnsptrrecord
Browse files Browse the repository at this point in the history
[MS] Adding DNS PTR Record
  • Loading branch information
tombuildsstuff authored Jul 3, 2017
2 parents 3d3f9cd + 85f6e81 commit bd01190
Show file tree
Hide file tree
Showing 17 changed files with 2,002 additions and 13 deletions.
8 changes: 8 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/Azure/azure-sdk-for-go/arm/containerregistry"
"github.com/Azure/azure-sdk-for-go/arm/containerservice"
"github.com/Azure/azure-sdk-for-go/arm/disk"
"github.com/Azure/azure-sdk-for-go/arm/dns"
"github.com/Azure/azure-sdk-for-go/arm/documentdb"
"github.com/Azure/azure-sdk-for-go/arm/eventhub"
"github.com/Azure/azure-sdk-for-go/arm/keyvault"
Expand Down Expand Up @@ -71,6 +72,7 @@ type ArmClient struct {
vnetPeeringsClient network.VirtualNetworkPeeringsClient
routeTablesClient network.RouteTablesClient
routesClient network.RoutesClient
dnsClient dns.RecordSetsClient

cdnProfilesClient cdn.ProfilesClient
cdnEndpointsClient cdn.EndpointsClient
Expand Down Expand Up @@ -373,6 +375,12 @@ func (c *Config) getArmClient() (*ArmClient, error) {
rc.Sender = autorest.CreateSender(withRequestLogging())
client.routesClient = rc

dn := dns.NewRecordSetsClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&dn.Client)
dn.Authorizer = auth
dn.Sender = autorest.CreateSender(withRequestLogging())
client.dnsClient = dn

rgc := resources.NewGroupsClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&rgc.Client)
rgc.Authorizer = auth
Expand Down
32 changes: 32 additions & 0 deletions azurerm/import_arm_dns_ptr_record_test.go
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,
},
},
})
}
16 changes: 8 additions & 8 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_container_registry": resourceArmContainerRegistry(),
"azurerm_container_service": resourceArmContainerService(),
"azurerm_cosmosdb_account": resourceArmCosmosDBAccount(),
"azurerm_dns_ptr_record": resourceArmDnsPtrRecord(),

"azurerm_eventhub": resourceArmEventHub(),
"azurerm_eventhub_authorization_rule": resourceArmEventHubAuthorizationRule(),
Expand Down Expand Up @@ -250,20 +251,19 @@ func registerAzureResourceProvidersWithSubscription(providerList []resources.Pro
var err error
providerRegistrationOnce.Do(func() {
providers := map[string]struct{}{
"Microsoft.Cache": struct{}{},
"Microsoft.Cdn": struct{}{},
"Microsoft.Compute": struct{}{},
"Microsoft.Cache": struct{}{},
"Microsoft.ContainerRegistry": struct{}{},
"Microsoft.ContainerService": struct{}{},
"Microsoft.EventHub": struct{}{},
"Microsoft.Insights": struct{}{},
"Microsoft.KeyVault": struct{}{},
"Microsoft.Network": struct{}{},
"Microsoft.Resources": struct{}{},
"Microsoft.Cdn": struct{}{},
"Microsoft.Storage": struct{}{},
"Microsoft.Sql": struct{}{},
"Microsoft.Search": struct{}{},
"Microsoft.Resources": struct{}{},
"Microsoft.ServiceBus": struct{}{},
"Microsoft.Sql": struct{}{},
"Microsoft.Storage": struct{}{},
"Microsoft.KeyVault": struct{}{},
"Microsoft.EventHub": struct{}{},
}

// filter out any providers already registered
Expand Down
183 changes: 183 additions & 0 deletions azurerm/resource_arm_dns_ptr_record.go
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
}
Loading

0 comments on commit bd01190

Please sign in to comment.