-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Data source - Added for azurerm_local_network_gateway (#12579)
fixes #10834
- Loading branch information
1 parent
d593326
commit f06b006
Showing
4 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
azurerm/internal/services/network/local_network_gateway_data_source.go
This file contains 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,138 @@ | ||
package network | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2020-11-01/network" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location" | ||
|
||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func dataSourceLocalNetworkGateway() *pluginsdk.Resource { | ||
return &pluginsdk.Resource{ | ||
Read: dataSourceLocalNetworkGatewayRead, | ||
|
||
Timeouts: &pluginsdk.ResourceTimeout{ | ||
Read: pluginsdk.DefaultTimeout(5 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*pluginsdk.Schema{ | ||
"name": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(), | ||
|
||
"location": location.SchemaComputed(), | ||
|
||
"gateway_address": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"gateway_fqdn": { | ||
Type: pluginsdk.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"address_space": { | ||
Type: pluginsdk.TypeList, | ||
Computed: true, | ||
Elem: &pluginsdk.Schema{ | ||
Type: pluginsdk.TypeString, | ||
}, | ||
}, | ||
|
||
"bgp_settings": { | ||
Type: pluginsdk.TypeList, | ||
Computed: true, | ||
Elem: &pluginsdk.Resource{ | ||
Schema: map[string]*pluginsdk.Schema{ | ||
"asn": { | ||
Type: pluginsdk.TypeInt, | ||
Required: true, | ||
}, | ||
|
||
"bgp_peering_address": { | ||
Type: pluginsdk.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"peer_weight": { | ||
Type: pluginsdk.TypeInt, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"tags": tags.SchemaDataSource(), | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceLocalNetworkGatewayRead(d *pluginsdk.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Network.LocalNetworkGatewaysClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
name := d.Get("name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
|
||
resp, err := client.Get(ctx, resourceGroup, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Error reading the state of Local Network Gateway %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
if resp.ID != nil { | ||
d.SetId(*resp.ID) | ||
} | ||
|
||
d.Set("name", resp.Name) | ||
d.Set("resource_group_name", resourceGroup) | ||
if location := resp.Location; location != nil { | ||
d.Set("location", azure.NormalizeLocation(*location)) | ||
} | ||
|
||
if props := resp.LocalNetworkGatewayPropertiesFormat; props != nil { | ||
d.Set("gateway_address", props.GatewayIPAddress) | ||
d.Set("gateway_fqdn", props.Fqdn) | ||
|
||
if lnas := props.LocalNetworkAddressSpace; lnas != nil { | ||
d.Set("address_space", lnas.AddressPrefixes) | ||
} | ||
flattenedSettings := flattenLocalNetworkGatewayDataSourceBGPSettings(props.BgpSettings) | ||
if err := d.Set("bgp_settings", flattenedSettings); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return tags.FlattenAndSet(d, resp.Tags) | ||
} | ||
|
||
func flattenLocalNetworkGatewayDataSourceBGPSettings(input *network.BgpSettings) []interface{} { | ||
output := make(map[string]interface{}) | ||
|
||
if input == nil { | ||
return []interface{}{} | ||
} | ||
|
||
output["asn"] = int(*input.Asn) | ||
output["bgp_peering_address"] = *input.BgpPeeringAddress | ||
output["peer_weight"] = int(*input.PeerWeight) | ||
|
||
return []interface{}{output} | ||
} |
68 changes: 68 additions & 0 deletions
68
azurerm/internal/services/network/local_network_gateway_data_source_test.go
This file contains 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,68 @@ | ||
package network_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance/check" | ||
) | ||
|
||
type LocalNetworkGatewayDataSource struct { | ||
} | ||
|
||
func TestAccDataSourceLocalNetworkGateway_complete(t *testing.T) { | ||
data := acceptance.BuildTestData(t, "data.azurerm_local_network_gateway", "test") | ||
r := LocalNetworkGatewayDataSource{} | ||
|
||
data.DataSourceTest(t, []acceptance.TestStep{ | ||
{ | ||
Config: r.complete(data), | ||
Check: acceptance.ComposeTestCheckFunc( | ||
check.That(data.ResourceName).Key("gateway_address").HasValue("127.0.0.1"), | ||
check.That(data.ResourceName).Key("address_space.0").HasValue("127.0.1.0/24"), | ||
check.That(data.ResourceName).Key("address_space.1").HasValue("127.0.0.0/24"), | ||
check.That(data.ResourceName).Key("bgp_settings.#").HasValue("1"), | ||
check.That(data.ResourceName).Key("bgp_settings.0.asn").HasValue("2468"), | ||
check.That(data.ResourceName).Key("bgp_settings.0.bgp_peering_address").HasValue("10.104.1.1"), | ||
check.That(data.ResourceName).Key("bgp_settings.0.peer_weight").HasValue("15"), | ||
), | ||
}, | ||
}) | ||
} | ||
|
||
func (LocalNetworkGatewayDataSource) complete(data acceptance.TestData) string { | ||
return fmt.Sprintf(` | ||
provider "azurerm" { | ||
features {} | ||
} | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-lngw-%d" | ||
location = "%s" | ||
} | ||
resource "azurerm_local_network_gateway" "test" { | ||
name = "acctestlng-%d" | ||
location = azurerm_resource_group.test.location | ||
resource_group_name = azurerm_resource_group.test.name | ||
gateway_address = "127.0.0.1" | ||
address_space = ["127.0.1.0/24", "127.0.0.0/24"] | ||
bgp_settings { | ||
asn = 2468 | ||
bgp_peering_address = "10.104.1.1" | ||
peer_weight = 15 | ||
} | ||
tags = { | ||
environment = "acctest" | ||
} | ||
} | ||
data "azurerm_local_network_gateway" "test" { | ||
name = azurerm_local_network_gateway.test.name | ||
resource_group_name = azurerm_local_network_gateway.test.resource_group_name | ||
} | ||
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger) | ||
} |
This file contains 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
This file contains 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,66 @@ | ||
--- | ||
subcategory: "Network" | ||
layout: "azurerm" | ||
page_title: "Azure Resource Manager: Data Source: azurerm_local_network_gateway" | ||
description: |- | ||
Gets information about an existing Local Network Gateway. | ||
--- | ||
|
||
# Data Source: azurerm_local_network_gateway | ||
|
||
Use this data source to access information about an existing Local Network Gateway. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "azurerm_local_network_gateway" "example" { | ||
name = "existing-local-network-gateway" | ||
resource_group_name = "existing-resources" | ||
} | ||
output "id" { | ||
value = data.azurerm_local_network_gateway.example.id | ||
} | ||
``` | ||
|
||
## Arguments Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (Required) The name of the Local Network Gateway. | ||
|
||
* `resource_group_name` - (Required) The name of the Resource Group where the Local Network Gateway exists. | ||
|
||
## Attributes Reference | ||
|
||
In addition to the Arguments listed above - the following Attributes are exported: | ||
|
||
* `id` - The ID of the Local Network Gateway. | ||
|
||
* `location` - The Azure Region where the Local Network Gateway exists. | ||
|
||
* `address_space` - The list of string CIDRs representing the address spaces the gateway exposes. | ||
|
||
* `bgp_settings` - A `bgp_settings` block as defined below containing the Local Network Gateway's BGP speaker settings. | ||
|
||
* `gateway_address` - The gateway IP address the Local Network Gateway uses. | ||
|
||
* `gateway_fqdn` - The gateway FQDN the Local Network Gateway uses. | ||
|
||
* `tags` - A mapping of tags assigned to the Local Network Gateway. | ||
|
||
--- | ||
|
||
`bgp_settings` exports the following: | ||
|
||
* `asn` - The BGP speaker's ASN. | ||
|
||
* `bgp_peering_address` - The BGP peering address and BGP identifier of this BGP speaker. | ||
|
||
* `peer_weight` - The weight added to routes learned from this BGP speaker. | ||
|
||
## Timeouts | ||
|
||
The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: | ||
|
||
* `read` - (Defaults to 5 minutes) Used when retrieving the Local Network Gateway. |