diff --git a/builtin/providers/azurerm/provider.go b/builtin/providers/azurerm/provider.go index 53c5c97a68..9f6476c53a 100644 --- a/builtin/providers/azurerm/provider.go +++ b/builtin/providers/azurerm/provider.go @@ -37,8 +37,9 @@ func Provider() terraform.ResourceProvider { }, ResourcesMap: map[string]*schema.Resource{ - "azurerm_resource_group": resourceArmResourceGroup(), - "azurerm_virtual_network": resourceArmVirtualNetwork(), + "azurerm_resource_group": resourceArmResourceGroup(), + "azurerm_virtual_network": resourceArmVirtualNetwork(), + "azurerm_local_network_gateway": resourceArmLocalNetworkGateway(), }, ConfigureFunc: providerConfigure, diff --git a/builtin/providers/azurerm/resource_arm_local_network_gateway.go b/builtin/providers/azurerm/resource_arm_local_network_gateway.go new file mode 100644 index 0000000000..ae91d665fc --- /dev/null +++ b/builtin/providers/azurerm/resource_arm_local_network_gateway.go @@ -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 +} diff --git a/builtin/providers/azurerm/resource_arm_local_network_gateway_test.go b/builtin/providers/azurerm/resource_arm_local_network_gateway_test.go new file mode 100644 index 0000000000..889a57e6eb --- /dev/null +++ b/builtin/providers/azurerm/resource_arm_local_network_gateway_test.go @@ -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"] +} +` diff --git a/website/source/docs/providers/azurerm/r/local_network_gateway.html.markdown b/website/source/docs/providers/azurerm/r/local_network_gateway.html.markdown new file mode 100644 index 0000000000..1f2bbe0f98 --- /dev/null +++ b/website/source/docs/providers/azurerm/r/local_network_gateway.html.markdown @@ -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. diff --git a/website/source/layouts/azurerm.erb b/website/source/layouts/azurerm.erb index f52a1bce23..fe00f7939d 100644 --- a/website/source/layouts/azurerm.erb +++ b/website/source/layouts/azurerm.erb @@ -13,13 +13,18 @@ > Resources