From 008dc970b63e5a5d274f5efb44a1a014db11868f Mon Sep 17 00:00:00 2001 From: James Nugent Date: Thu, 17 Dec 2015 11:22:17 -0500 Subject: [PATCH] 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. --- .../resource_arm_local_network_gateway.go | 76 +++++++------------ ...resource_arm_local_network_gateway_test.go | 38 +++++----- 2 files changed, 47 insertions(+), 67 deletions(-) diff --git a/builtin/providers/azurerm/resource_arm_local_network_gateway.go b/builtin/providers/azurerm/resource_arm_local_network_gateway.go index a99a35d0d..ae91d665f 100644 --- a/builtin/providers/azurerm/resource_arm_local_network_gateway.go +++ b/builtin/providers/azurerm/resource_arm_local_network_gateway.go @@ -2,20 +2,17 @@ package azurerm import ( "fmt" - "log" "github.com/Azure/azure-sdk-for-go/arm/network" "github.com/Azure/azure-sdk-for-go/core/http" "github.com/hashicorp/terraform/helper/schema" ) -// resourceArmLocalNetworkGateway returns the schema.Resource -// associated to an Azure local network gateway. func resourceArmLocalNetworkGateway() *schema.Resource { return &schema.Resource{ Create: resourceArmLocalNetworkGatewayCreate, Read: resourceArmLocalNetworkGatewayRead, - Update: resourceArmLocalNetworkGatewayUpdate, + Update: resourceArmLocalNetworkGatewayCreate, Delete: resourceArmLocalNetworkGatewayDelete, Schema: map[string]*schema.Schema{ @@ -38,11 +35,6 @@ func resourceArmLocalNetworkGateway() *schema.Resource { ForceNew: true, }, - "resource_guid": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - }, - "gateway_address": &schema.Schema{ Type: schema.TypeString, 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 { lnetClient := meta.(*ArmClient).localNetConnClient @@ -68,25 +59,15 @@ func resourceArmLocalNetworkGatewayCreate(d *schema.ResourceData, meta interface resGroup := d.Get("resource_group_name").(string) ipAddress := d.Get("gateway_address").(string) - // NOTE: due to the including-but-different relationship between the ASM - // 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: + // fetch the 'address_space_prefixes: prefixes := []string{} - for _, pref := range d.Get("addres_space").([]interface{}) { + for _, pref := range d.Get("address_space").([]interface{}) { prefixes = append(prefixes, pref.(string)) } - // NOTE: result ignored here; review below... resp, err := lnetClient.CreateOrUpdate(resGroup, name, network.LocalNetworkGateway{ Name: &name, Location: &location, - Type: &typ, Properties: &network.LocalNetworkGatewayPropertiesFormat{ LocalNetworkAddressSpace: &network.AddressSpace{ AddressPrefixes: &prefixes, @@ -95,12 +76,11 @@ func resourceArmLocalNetworkGatewayCreate(d *schema.ResourceData, meta interface }, }) 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) + return resourceArmLocalNetworkGatewayRead(d, meta) } @@ -108,25 +88,27 @@ func resourceArmLocalNetworkGatewayCreate(d *schema.ResourceData, meta interface func resourceArmLocalNetworkGatewayRead(d *schema.ResourceData, meta interface{}) error { lnetClient := meta.(*ArmClient).localNetConnClient - name := d.Get("name").(string) - 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 - } + id, err := parseAzureResourceID(d.Id()) 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) } - d.Set("resource_guid", *lnet.Properties.ResourceGUID) - d.Set("gateway_address", *lnet.Properties.GatewayIPAddress) + d.Set("gateway_address", resp.Properties.GatewayIPAddress) prefs := []string{} - if ps := *lnet.Properties.LocalNetworkAddressSpace.AddressPrefixes; ps != nil { + if ps := *resp.Properties.LocalNetworkAddressSpace.AddressPrefixes; ps != nil { prefs = ps } d.Set("address_space", prefs) @@ -134,22 +116,18 @@ func resourceArmLocalNetworkGatewayRead(d *schema.ResourceData, meta interface{} 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. func resourceArmLocalNetworkGatewayDelete(d *schema.ResourceData, meta interface{}) error { lnetClient := meta.(*ArmClient).localNetConnClient - name := d.Get("name").(string) - resGroup := d.Get("resource_group_name").(string) + id, err := parseAzureResourceID(d.Id()) + 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 { return fmt.Errorf("Error issuing Azure ARM delete request of local network gateway '%s': %s", name, err) } diff --git a/builtin/providers/azurerm/resource_arm_local_network_gateway_test.go b/builtin/providers/azurerm/resource_arm_local_network_gateway_test.go index 5a27240b0..889a57e6e 100644 --- a/builtin/providers/azurerm/resource_arm_local_network_gateway_test.go +++ b/builtin/providers/azurerm/resource_arm_local_network_gateway_test.go @@ -40,22 +40,23 @@ func testCheckAzureRMLocalNetworkGatewayExists(name string) resource.TestCheckFu return fmt.Errorf("Local network gateway '%s' not found.", name) } - // then, extranct the name and the resource group: - localNetName := res.Primary.Attributes["name"] - resGrp, hasResGrp := res.Primary.Attributes["resource_group_name"] - if !hasResGrp { - return fmt.Errorf("Local network gateway '%s' has no resource group set.", name) + // then, extract the name and the resource group: + id, err := parseAzureResourceID(res.Primary.ID) + if err != nil { + return err } + localNetName := id.Path["localNetworkGateways"] + resGrp := id.ResourceGroup // and finally, check that it exists on Azure: lnetClient := testAccProvider.Meta().(*ArmClient).localNetConnClient - resp, err := lnetClient.Get(resGrp, name) - if resp.StatusCode == http.StatusNotFound { - return fmt.Errorf("Local network gateway '%s' (resource group '%s') does not exist on Azure.", localNetName, resGrp) - } - + resp, err := lnetClient.Get(resGrp, localNetName) 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) } @@ -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 { - for _, rs := range s.RootModule().Resources { - if rs.Type != "azurerm_local_network_gateway" { + for _, res := range s.RootModule().Resources { + if res.Type != "azurerm_local_network_gateway" { continue } - name := rs.Primary.Attributes["name"] - resourceGroup := rs.Primary.Attributes["resource_group_name"] + id, err := parseAzureResourceID(res.Primary.ID) + if err != nil { + return err + } + localNetName := id.Path["localNetworkGateways"] + resGrp := id.ResourceGroup lnetClient := testAccProvider.Meta().(*ArmClient).localNetConnClient - resp, err := lnetClient.Get(resourceGroup, name) + resp, err := lnetClient.Get(resGrp, localNetName) if err != nil { return nil