provider/azurerm: Fix up network gateway tests

This diff represents the changes  necessary to make local network
gateway tests pass:

- Parse the resource ID instead of relying on attributes
- Remove unecessary logging (which is handled via the autorest wrapper)
- Resource GUID is removed - if this is actually required for anything
  we may need to find a way to supress it during apply, as we get
  spurious diffs in plans otherwise.
- Various typos fixed.
This commit is contained in:
James Nugent 2015-12-17 11:22:17 -05:00 committed by Nashwan Azhari
parent 1eb129a99b
commit 008dc970b6
2 changed files with 47 additions and 67 deletions

View File

@ -2,20 +2,17 @@ package azurerm
import ( import (
"fmt" "fmt"
"log"
"github.com/Azure/azure-sdk-for-go/arm/network" "github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/Azure/azure-sdk-for-go/core/http" "github.com/Azure/azure-sdk-for-go/core/http"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
) )
// resourceArmLocalNetworkGateway returns the schema.Resource
// associated to an Azure local network gateway.
func resourceArmLocalNetworkGateway() *schema.Resource { func resourceArmLocalNetworkGateway() *schema.Resource {
return &schema.Resource{ return &schema.Resource{
Create: resourceArmLocalNetworkGatewayCreate, Create: resourceArmLocalNetworkGatewayCreate,
Read: resourceArmLocalNetworkGatewayRead, Read: resourceArmLocalNetworkGatewayRead,
Update: resourceArmLocalNetworkGatewayUpdate, Update: resourceArmLocalNetworkGatewayCreate,
Delete: resourceArmLocalNetworkGatewayDelete, Delete: resourceArmLocalNetworkGatewayDelete,
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
@ -38,11 +35,6 @@ func resourceArmLocalNetworkGateway() *schema.Resource {
ForceNew: true, ForceNew: true,
}, },
"resource_guid": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"gateway_address": &schema.Schema{ "gateway_address": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
@ -59,7 +51,6 @@ func resourceArmLocalNetworkGateway() *schema.Resource {
} }
} }
// resourceArmLocalNetworkGatewayCreate goes ahead and creates the specified ARM local network gateway.
func resourceArmLocalNetworkGatewayCreate(d *schema.ResourceData, meta interface{}) error { func resourceArmLocalNetworkGatewayCreate(d *schema.ResourceData, meta interface{}) error {
lnetClient := meta.(*ArmClient).localNetConnClient lnetClient := meta.(*ArmClient).localNetConnClient
@ -68,25 +59,15 @@ func resourceArmLocalNetworkGatewayCreate(d *schema.ResourceData, meta interface
resGroup := d.Get("resource_group_name").(string) resGroup := d.Get("resource_group_name").(string)
ipAddress := d.Get("gateway_address").(string) ipAddress := d.Get("gateway_address").(string)
// NOTE: due to the including-but-different relationship between the ASM // fetch the 'address_space_prefixes:
// and ARM APIs, one may set the following local network gateway type to
// "Classic" and basically get an old ASM local network connection through
// the ARM API. This functionality is redundant with respect to the old
// ASM-based implementation which we already have, so we just use the
// new Resource Manager APIs here:
typ := "Resource Manager"
// fetch the 'address_space_prefix'es:
prefixes := []string{} prefixes := []string{}
for _, pref := range d.Get("addres_space").([]interface{}) { for _, pref := range d.Get("address_space").([]interface{}) {
prefixes = append(prefixes, pref.(string)) prefixes = append(prefixes, pref.(string))
} }
// NOTE: result ignored here; review below...
resp, err := lnetClient.CreateOrUpdate(resGroup, name, network.LocalNetworkGateway{ resp, err := lnetClient.CreateOrUpdate(resGroup, name, network.LocalNetworkGateway{
Name: &name, Name: &name,
Location: &location, Location: &location,
Type: &typ,
Properties: &network.LocalNetworkGatewayPropertiesFormat{ Properties: &network.LocalNetworkGatewayPropertiesFormat{
LocalNetworkAddressSpace: &network.AddressSpace{ LocalNetworkAddressSpace: &network.AddressSpace{
AddressPrefixes: &prefixes, AddressPrefixes: &prefixes,
@ -95,12 +76,11 @@ func resourceArmLocalNetworkGatewayCreate(d *schema.ResourceData, meta interface
}, },
}) })
if err != nil { if err != nil {
return fmt.Errorf("Error reading the state of Azure ARM Local Network Gateway '%s': %s", name, err) return fmt.Errorf("Error creating Azure ARM Local Network Gateway '%s': %s", name, err)
} }
// NOTE: we either call read here or basically repeat the reading process
// with the ignored network.LocalNetworkGateway result of the above:
d.SetId(*resp.ID) d.SetId(*resp.ID)
return resourceArmLocalNetworkGatewayRead(d, meta) return resourceArmLocalNetworkGatewayRead(d, meta)
} }
@ -108,25 +88,27 @@ func resourceArmLocalNetworkGatewayCreate(d *schema.ResourceData, meta interface
func resourceArmLocalNetworkGatewayRead(d *schema.ResourceData, meta interface{}) error { func resourceArmLocalNetworkGatewayRead(d *schema.ResourceData, meta interface{}) error {
lnetClient := meta.(*ArmClient).localNetConnClient lnetClient := meta.(*ArmClient).localNetConnClient
name := d.Get("name").(string) id, err := parseAzureResourceID(d.Id())
resGroup := d.Get("resource_group_name").(string)
log.Printf("[INFO] Sending GET request to Azure ARM for local network gateway '%s'.", name)
lnet, err := lnetClient.Get(resGroup, name)
if lnet.StatusCode == http.StatusNotFound {
// it means that the resource has been deleted in the meantime...
d.SetId("")
return nil
}
if err != nil { if err != nil {
return err
}
name := id.Path["localNetworkGateways"]
resGroup := id.ResourceGroup
resp, err := lnetClient.Get(resGroup, name)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading the state of Azure ARM local network gateway '%s': %s", name, err) return fmt.Errorf("Error reading the state of Azure ARM local network gateway '%s': %s", name, err)
} }
d.Set("resource_guid", *lnet.Properties.ResourceGUID) d.Set("gateway_address", resp.Properties.GatewayIPAddress)
d.Set("gateway_address", *lnet.Properties.GatewayIPAddress)
prefs := []string{} prefs := []string{}
if ps := *lnet.Properties.LocalNetworkAddressSpace.AddressPrefixes; ps != nil { if ps := *resp.Properties.LocalNetworkAddressSpace.AddressPrefixes; ps != nil {
prefs = ps prefs = ps
} }
d.Set("address_space", prefs) d.Set("address_space", prefs)
@ -134,22 +116,18 @@ func resourceArmLocalNetworkGatewayRead(d *schema.ResourceData, meta interface{}
return nil return nil
} }
// resourceArmLocalNetworkGatewayUpdate goes ahead and updates the corresponding ARM local network gateway.
func resourceArmLocalNetworkGatewayUpdate(d *schema.ResourceData, meta interface{}) error {
// NOTE: considering the idempotency, we can safely call create again on
// update. This has been written out in order to ensure clarity,
return resourceArmLocalNetworkGatewayCreate(d, meta)
}
// resourceArmLocalNetworkGatewayDelete deletes the specified ARM local network gateway. // resourceArmLocalNetworkGatewayDelete deletes the specified ARM local network gateway.
func resourceArmLocalNetworkGatewayDelete(d *schema.ResourceData, meta interface{}) error { func resourceArmLocalNetworkGatewayDelete(d *schema.ResourceData, meta interface{}) error {
lnetClient := meta.(*ArmClient).localNetConnClient lnetClient := meta.(*ArmClient).localNetConnClient
name := d.Get("name").(string) id, err := parseAzureResourceID(d.Id())
resGroup := d.Get("resource_group_name").(string) if err != nil {
return err
}
name := id.Path["localNetworkGateways"]
resGroup := id.ResourceGroup
log.Printf("[INFO] Sending Azure ARM delete request for local network gateway '%s'.", name) _, err = lnetClient.Delete(resGroup, name)
_, err := lnetClient.Delete(resGroup, name)
if err != nil { if err != nil {
return fmt.Errorf("Error issuing Azure ARM delete request of local network gateway '%s': %s", name, err) return fmt.Errorf("Error issuing Azure ARM delete request of local network gateway '%s': %s", name, err)
} }

View File

@ -40,22 +40,23 @@ func testCheckAzureRMLocalNetworkGatewayExists(name string) resource.TestCheckFu
return fmt.Errorf("Local network gateway '%s' not found.", name) return fmt.Errorf("Local network gateway '%s' not found.", name)
} }
// then, extranct the name and the resource group: // then, extract the name and the resource group:
localNetName := res.Primary.Attributes["name"] id, err := parseAzureResourceID(res.Primary.ID)
resGrp, hasResGrp := res.Primary.Attributes["resource_group_name"] if err != nil {
if !hasResGrp { return err
return fmt.Errorf("Local network gateway '%s' has no resource group set.", name)
} }
localNetName := id.Path["localNetworkGateways"]
resGrp := id.ResourceGroup
// and finally, check that it exists on Azure: // and finally, check that it exists on Azure:
lnetClient := testAccProvider.Meta().(*ArmClient).localNetConnClient lnetClient := testAccProvider.Meta().(*ArmClient).localNetConnClient
resp, err := lnetClient.Get(resGrp, name) resp, err := lnetClient.Get(resGrp, localNetName)
if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("Local network gateway '%s' (resource group '%s') does not exist on Azure.", localNetName, resGrp)
}
if err != nil { if err != nil {
if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("Local network gateway '%s' (resource group '%s') does not exist on Azure.", localNetName, resGrp)
}
return fmt.Errorf("Error reading the state of local network gateway '%s'.", localNetName) return fmt.Errorf("Error reading the state of local network gateway '%s'.", localNetName)
} }
@ -63,20 +64,21 @@ func testCheckAzureRMLocalNetworkGatewayExists(name string) resource.TestCheckFu
} }
} }
// testCheckAzureRMLocalNetworkGatewayDestroy is the resurce.TestCheckFunc
// which checks whether or not the expected local network gateway still
// exists on Azure.
func testCheckAzureRMLocalNetworkGatewayDestroy(s *terraform.State) error { func testCheckAzureRMLocalNetworkGatewayDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources { for _, res := range s.RootModule().Resources {
if rs.Type != "azurerm_local_network_gateway" { if res.Type != "azurerm_local_network_gateway" {
continue continue
} }
name := rs.Primary.Attributes["name"] id, err := parseAzureResourceID(res.Primary.ID)
resourceGroup := rs.Primary.Attributes["resource_group_name"] if err != nil {
return err
}
localNetName := id.Path["localNetworkGateways"]
resGrp := id.ResourceGroup
lnetClient := testAccProvider.Meta().(*ArmClient).localNetConnClient lnetClient := testAccProvider.Meta().(*ArmClient).localNetConnClient
resp, err := lnetClient.Get(resourceGroup, name) resp, err := lnetClient.Get(resGrp, localNetName)
if err != nil { if err != nil {
return nil return nil