Skip to content
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

Data source - Added for azurerm_local_network_gateway #12579

Merged
merged 3 commits into from
Jul 15, 2021
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
138 changes: 138 additions & 0 deletions azurerm/internal/services/network/local_network_gateway_data_source.go
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}
}
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)
}
1 change: 1 addition & 0 deletions azurerm/internal/services/network/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource {
"azurerm_virtual_network": dataSourceVirtualNetwork(),
"azurerm_web_application_firewall_policy": dataWebApplicationFirewallPolicy(),
"azurerm_virtual_wan": dataSourceVirtualWan(),
"azurerm_local_network_gateway": dataSourceLocalNetworkGateway(),
}
}

Expand Down
66 changes: 66 additions & 0 deletions website/docs/d/local_network_gateway.html.markdown
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.