Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MS] Adding DNS PTR Record #141

Merged
merged 7 commits into from
Jul 3, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ func Provider() terraform.ResourceProvider {

"azurerm_application_insights": resourceArmApplicationInsights(),

//PTR record uses Azure Go SDK
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: can we move this sorted alphabetically under the Azure SDK for Go comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"azurerm_dns_ptr_record": resourceArmDnsPtrRecord(),
// These resources use the Riviera SDK
"azurerm_dns_a_record": resourceArmDnsARecord(),
"azurerm_dns_aaaa_record": resourceArmDnsAAAARecord(),
Expand Down
169 changes: 169 additions & 0 deletions azurerm/resource_arm_dns_ptr_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
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: resourceArmDnsPtrRecordCreate,
Read: resourceArmDnsPtrRecordRead,
Update: resourceArmDnsPtrRecordCreate,
Delete: resourceArmDnsPtrRecordDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

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

"resource_group_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

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

"records": &schema.Schema{
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this contain zero records?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It cannot, it would be invalid

Set: schema.HashString,
},

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

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

func resourceArmDnsPtrRecordCreate(d *schema.ResourceData, meta interface{}) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resourceArmDnsPtrRecordCreateOrUpdate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

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))
tags := d.Get("tags").(map[string]interface{})

metadata := expandTags(tags)

recordStrings := d.Get("records").(*schema.Set).List()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'd generally tend to pull this out into a separate function - could we update this to match?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

records := make([]dns.PtrRecord, len(recordStrings))

for i, v := range recordStrings {
fqdn := v.(string)
records[i] = dns.PtrRecord{
Ptrdname: &fqdn,
}
}

props := dns.RecordSetProperties{
Metadata: metadata,
TTL: &ttl,
PtrRecords: &records,
}

parameters := dns.RecordSet{
Name: &name,
RecordSetProperties: &props,
}

_, err := dnsClient.CreateOrUpdate(resGroup, zoneName, name, dns.PTR, parameters, "", "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not obvious what the last two parameters are and why are they empty strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, changed second-to-last parameter to properly be the eTag of the record and added comments explaining why the last parameter is empty

if err != nil {
return err
}

rec, err := dnsClient.Get(resGroup, zoneName, name, dns.PTR)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the dnsClient.CreateOrUpdate method's "response" return value already contains the resource ID, there is no need to call Get just to retrieve the ID value.

rec, err := dnsClient.CreateOrUpdate(...)

if err != nil {
    return err
}
d.SetId(*rec.ID)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, changed

if err != nil {
return err
}

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

d.SetId(*rec.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: %s", name, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this last one should be %+v as it's an error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed, thanks for catching that!

}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

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

if resp.PtrRecords != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'd generally pull this out into a Flatten method - could we update this to match?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

records := make([]string, 0, len(*resp.PtrRecords))
for _, record := range *resp.PtrRecords {
records = append(records, *record.Ptrdname)
}

if err := d.Set("records", records); 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
}
Loading