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

resource_arm_dns_zone: switch dependency from riviera to azure-sdk-for-go #188

Merged
merged 3 commits into from
Jul 25, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type ArmClient struct {
routeTablesClient network.RouteTablesClient
routesClient network.RoutesClient
dnsClient dns.RecordSetsClient
zonesClient dns.ZonesClient

cdnProfilesClient cdn.ProfilesClient
cdnEndpointsClient cdn.EndpointsClient
Expand Down Expand Up @@ -399,6 +400,12 @@ func (c *Config) getArmClient() (*ArmClient, error) {
dn.Sender = autorest.CreateSender(withRequestLogging())
client.dnsClient = dn

zo := dns.NewZonesClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&zo.Client)
zo.Authorizer = auth
zo.Sender = autorest.CreateSender(withRequestLogging())
client.zonesClient = zo

rgc := resources.NewGroupsClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&rgc.Client)
rgc.Authorizer = auth
Expand Down
29 changes: 25 additions & 4 deletions azurerm/import_arm_dns_zone_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
Expand All @@ -12,18 +11,40 @@ func TestAccAzureRMDnsZone_importBasic(t *testing.T) {
resourceName := "azurerm_dns_zone.test"

ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMDnsZone_basic, ri, ri)
config := testAccAzureRMDnsZone_basic(ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsZoneDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMDnsZone_importBasicWithTags(t *testing.T) {
resourceName := "azurerm_dns_zone.test"

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

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsZoneDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
Expand Down
106 changes: 46 additions & 60 deletions azurerm/resource_arm_dns_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package azurerm

import (
"fmt"
"log"
"net/http"

"github.com/Azure/azure-sdk-for-go/arm/dns"
"github.com/hashicorp/terraform/helper/schema"
"github.com/jen20/riviera/dns"
)

func resourceArmDnsZone() *schema.Resource {
Expand All @@ -19,32 +19,32 @@ func resourceArmDnsZone() *schema.Resource {
},

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

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

"number_of_record_sets": &schema.Schema{
"number_of_record_sets": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"max_number_of_record_sets": &schema.Schema{
"max_number_of_record_sets": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"name_servers": &schema.Schema{
"name_servers": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Expand All @@ -57,81 +57,63 @@ func resourceArmDnsZone() *schema.Resource {
}

func resourceArmDnsZoneCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
client := meta.(*ArmClient).zonesClient

tags := d.Get("tags").(map[string]interface{})
expandedTags := expandTags(tags)

createRequest := rivieraClient.NewRequest()
createRequest.Command = &dns.CreateDNSZone{
Name: d.Get("name").(string),
Location: "global",
ResourceGroupName: d.Get("resource_group_name").(string),
Tags: *expandedTags,
}
name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
location := "global"

createResponse, err := createRequest.Execute()
if err != nil {
return fmt.Errorf("Error creating DNS Zone: %s", err)
}
if !createResponse.IsSuccessful() {
return fmt.Errorf("Error creating DNS Zone: %s", createResponse.Error)
}
tags := d.Get("tags").(map[string]interface{})

readRequest := rivieraClient.NewRequest()
readRequest.Command = &dns.GetDNSZone{
Name: d.Get("name").(string),
ResourceGroupName: d.Get("resource_group_name").(string),
parameters := dns.Zone{
Location: &location,
Tags: expandTags(tags),
}

readResponse, err := readRequest.Execute()
etag := ""
ifNoneMatch := "" // set to empty to allow updates to records after creation
resp, err := client.CreateOrUpdate(resGroup, name, parameters, etag, ifNoneMatch)
if err != nil {
return fmt.Errorf("Error reading DNS Zone: %s", err)
return err
}
if !readResponse.IsSuccessful() {
return fmt.Errorf("Error reading DNS Zone: %s", readResponse.Error)

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

resp := readResponse.Parsed.(*dns.GetDNSZoneResponse)
d.SetId(*resp.ID)

return resourceArmDnsZoneRead(d, meta)
}

func resourceArmDnsZoneRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
zonesClient := meta.(*ArmClient).zonesClient

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup

readRequest := rivieraClient.NewRequestForURI(d.Id())
readRequest.Command = &dns.GetDNSZone{}
resGroup := id.ResourceGroup
name := id.Path["dnszones"]

readResponse, err := readRequest.Execute()
resp, err := zonesClient.Get(resGroup, name)
if err != nil {
return fmt.Errorf("Error reading DNS Zone: %s", err)
return fmt.Errorf("Error reading DNS zone %s (resource group %s): %+v", name, resGroup, err)
}
if !readResponse.IsSuccessful() {
log.Printf("[INFO] Error reading DNS Zone %q - removing from state", d.Id())
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return fmt.Errorf("Error reading DNS Zone: %s", readResponse.Error)
return nil
}

resp := readResponse.Parsed.(*dns.GetDNSZoneResponse)

d.Set("name", name)
d.Set("resource_group_name", resGroup)
d.Set("number_of_record_sets", resp.NumberOfRecordSets)
d.Set("max_number_of_record_sets", resp.MaxNumberOfRecordSets)
d.Set("name", resp.Name)

nameServers := make([]string, 0, len(resp.NameServers))
for _, ns := range resp.NameServers {
nameServers = append(nameServers, *ns)
nameServers := make([]string, 0, len(*resp.NameServers))
for _, ns := range *resp.NameServers {
nameServers = append(nameServers, ns)
}
if err := d.Set("name_servers", nameServers); err != nil {
return err
Expand All @@ -143,18 +125,22 @@ func resourceArmDnsZoneRead(d *schema.ResourceData, meta interface{}) error {
}

func resourceArmDnsZoneDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient

deleteRequest := rivieraClient.NewRequestForURI(d.Id())
deleteRequest.Command = &dns.DeleteDNSZone{}
client := meta.(*ArmClient).zonesClient

deleteResponse, err := deleteRequest.Execute()
id, err := parseAzureResourceID(d.Id())
if err != nil {
return fmt.Errorf("Error deleting DNS Zone: %s", err)
return err
}
if !deleteResponse.IsSuccessful() {
return fmt.Errorf("Error deleting DNS Zone: %s", deleteResponse.Error)

resGroup := id.ResourceGroup
name := id.Path["dnszones"]

etag := ""
_, error := client.Delete(resGroup, name, etag, make(chan struct{}))
err = <-error

if err != nil {
return fmt.Errorf("Error deleting DNS zone %s (resource group %s): %+v", name, resGroup, err)
}

return nil
Expand Down
Loading