Skip to content
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
74 changes: 26 additions & 48 deletions builtin/providers/azurerm/resource_arm_local_network_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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,
Expand All @@ -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

Expand All @@ -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,
Expand All @@ -95,61 +76,58 @@ 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)
}

// resourceArmLocalNetworkGatewayRead goes ahead and reads the state of the corresponding ARM local network gateway.
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)

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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,43 +40,45 @@ 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)
}

return nil
}
}

// 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
Expand Down