Merge pull request #4342 from aznashwan/f-azure-localnets

provider/azure: added local network gateway resource
This commit is contained in:
James Nugent 2015-12-31 09:52:05 -05:00
commit 9da5dec5e7
5 changed files with 302 additions and 4 deletions

View File

@ -39,6 +39,7 @@ func Provider() terraform.ResourceProvider {
ResourcesMap: map[string]*schema.Resource{
"azurerm_resource_group": resourceArmResourceGroup(),
"azurerm_virtual_network": resourceArmVirtualNetwork(),
"azurerm_local_network_gateway": resourceArmLocalNetworkGateway(),
},
ConfigureFunc: providerConfigure,

View File

@ -0,0 +1,136 @@
package azurerm
import (
"fmt"
"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/Azure/azure-sdk-for-go/core/http"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceArmLocalNetworkGateway() *schema.Resource {
return &schema.Resource{
Create: resourceArmLocalNetworkGatewayCreate,
Read: resourceArmLocalNetworkGatewayRead,
Update: resourceArmLocalNetworkGatewayCreate,
Delete: resourceArmLocalNetworkGatewayDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"location": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
StateFunc: azureRMNormalizeLocation,
},
"resource_group_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"gateway_address": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"address_space": &schema.Schema{
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func resourceArmLocalNetworkGatewayCreate(d *schema.ResourceData, meta interface{}) error {
lnetClient := meta.(*ArmClient).localNetConnClient
name := d.Get("name").(string)
location := d.Get("location").(string)
resGroup := d.Get("resource_group_name").(string)
ipAddress := d.Get("gateway_address").(string)
// fetch the 'address_space_prefixes:
prefixes := []string{}
for _, pref := range d.Get("address_space").([]interface{}) {
prefixes = append(prefixes, pref.(string))
}
resp, err := lnetClient.CreateOrUpdate(resGroup, name, network.LocalNetworkGateway{
Name: &name,
Location: &location,
Properties: &network.LocalNetworkGatewayPropertiesFormat{
LocalNetworkAddressSpace: &network.AddressSpace{
AddressPrefixes: &prefixes,
},
GatewayIPAddress: &ipAddress,
},
})
if err != nil {
return fmt.Errorf("Error creating Azure ARM Local Network Gateway '%s': %s", name, err)
}
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
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("gateway_address", resp.Properties.GatewayIPAddress)
prefs := []string{}
if ps := *resp.Properties.LocalNetworkAddressSpace.AddressPrefixes; ps != nil {
prefs = ps
}
d.Set("address_space", prefs)
return nil
}
// resourceArmLocalNetworkGatewayDelete deletes the specified ARM local network gateway.
func resourceArmLocalNetworkGatewayDelete(d *schema.ResourceData, meta interface{}) error {
lnetClient := meta.(*ArmClient).localNetConnClient
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
name := id.Path["localNetworkGateways"]
resGroup := id.ResourceGroup
_, 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)
}
return nil
}

View File

@ -0,0 +1,108 @@
package azurerm
import (
"fmt"
"testing"
"github.com/Azure/azure-sdk-for-go/core/http"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccAzureRMLocalNetworkGateway_basic(t *testing.T) {
name := "azurerm_local_network_gateway.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLocalNetworkGatewayDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAzureRMLocalNetworkGatewayConfig_basic,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLocalNetworkGatewayExists(name),
resource.TestCheckResourceAttr(name, "gateway_address", "127.0.0.1"),
resource.TestCheckResourceAttr(name, "address_space.0", "127.0.0.0/8"),
),
},
},
})
}
// testCheckAzureRMLocalNetworkGatewayExists returns the resurce.TestCheckFunc
// which checks whether or not the expected local network gateway exists both
// in the schema, and on Azure.
func testCheckAzureRMLocalNetworkGatewayExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// first check within the schema for the local network gateway:
res, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Local network gateway '%s' not found.", 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, 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
}
}
func testCheckAzureRMLocalNetworkGatewayDestroy(s *terraform.State) error {
for _, res := range s.RootModule().Resources {
if res.Type != "azurerm_local_network_gateway" {
continue
}
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(resGrp, localNetName)
if err != nil {
return nil
}
if resp.StatusCode != http.StatusNotFound {
return fmt.Errorf("Local network gateway still exists:\n%#v", resp.Properties)
}
}
return nil
}
var testAccAzureRMLocalNetworkGatewayConfig_basic = `
resource "azurerm_resource_group" "test" {
name = "tftestingResourceGroup"
location = "West US"
}
resource "azurerm_local_network_gateway" "test" {
name = "tftestingLocalNetworkGateway"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
gateway_address = "127.0.0.1"
address_space = ["127.0.0.0/8"]
}
`

View File

@ -0,0 +1,48 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_local_network_gateway"
sidebar_current: "docs-azurerm-resource-local-network-gateway"
description: |-
Creates a new local network gateway connection over which specific connections can be configured.
---
# azurerm\_local\_network\_gateway
Creates a new local network gateway connection over which specific connections can be configured.
## Example Usage
```
resource "azurerm_local_network_gateway" "home" {
name = "backHome"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
gateway_address = "12.13.14.15"
address_space = ["10.0.0.0/16"]
}
```
## Argument Reference
The following arguments are supported:
* `name` - (Required) The name of the local network gateway. Changing this
forces a new resource to be created.
* `resource_group_name` - (Required) The name of the resource group in which to
create the local network gateway.
* `location` - (Required) The location/region where the local network gatway is
created. Changing this forces a new resource to be created.
* `gateway_address` - (Required) The IP address of the gatway to which to
connect.
* `address_space` - (Required) The list of string CIDRs representing the
addredss spaces the gateway exposes.
## Attributes Reference
The following attributes are exported:
* `id` - The local network gateway unique ID within Azure.

View File

@ -13,13 +13,18 @@
<li<%= sidebar_current(/^docs-azurerm-resource/) %>>
<a href="#">Resources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-azure-resource-resource-group") %>>
<li<%= sidebar_current("docs-azurerm-resource-resource-group") %>>
<a href="/docs/providers/azurerm/r/resource_group.html">azurerm_resource_group</a>
</li>
<li<%= sidebar_current("docs-azure-resource-virtual-network") %>>
<li<%= sidebar_current("docs-azurerm-resource-virtual-network") %>>
<a href="/docs/providers/azurerm/r/virtual_network.html">azurerm_virtual_network</a>
</li>
<li<%= sidebar_current("docs-azurerm-resource-local-network-gateway") %>>
<a href="/docs/providers/azurerm/r/local_network_gateway.html">azurerm_local_network_gateway</a>
</li>
</ul>
</li>
</ul>