-
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6a1bb2d
DNS PTR Reocrd - Initial Work
echuvyrov f37e300
DNS PTR Record - Initial PR
echuvyrov 93f38a0
Added import test, updated docs, converted test strings into funcs to…
echuvyrov 020e0d1
Made changes suggested for the PR
echuvyrov 8653819
Added tag validation to tests
echuvyrov e16ed18
Cleaning up some unnecessary code
tombuildsstuff e17b6db
Adding in etags / updating the casing of ttl
tombuildsstuff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,186 @@ | ||
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: resourceArmDnsPtrRecordCreateOrUpdate, | ||
Read: resourceArmDnsPtrRecordRead, | ||
Update: resourceArmDnsPtrRecordCreateOrUpdate, | ||
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}, | ||
Set: schema.HashString, | ||
}, | ||
|
||
"ttl": &schema.Schema{ | ||
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 | ||
} | ||
|
||
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: %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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can this contain zero records?
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.
It cannot, it would be invalid