-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Changes from 3 commits
6a1bb2d
f37e300
93f38a0
020e0d1
8653819
e16ed18
e17b6db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
}, | ||
}, | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"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}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this contain zero records? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. resourceArmDnsPtrRecordCreateOrUpdate There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, "", "") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
|
||
log.Printf("[INFO] here is actual %v", id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we remove this comment? |
||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this last one should be There was a problem hiding this comment. Choose a reason for hiding this commentThe 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("zone_name", zoneName) | ||
d.Set("ttl", resp.TTL) | ||
|
||
if resp.PtrRecords != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done