-
Notifications
You must be signed in to change notification settings - Fork 10.4k
provider/azurerm: added local network gateway resource #4342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
builtin/providers/azurerm/resource_arm_local_network_gateway.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } |
108 changes: 108 additions & 0 deletions
108
builtin/providers/azurerm/resource_arm_local_network_gateway_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"] | ||
| } | ||
| ` |
48 changes: 48 additions & 0 deletions
48
website/source/docs/providers/azurerm/r/local_network_gateway.html.markdown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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") %>> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| <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> | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jen20 @phinze: not entirely sure how vital this change here is...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 good catch!