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

New Data Source: azurerm_virtual_network_gateway #796

Merged
merged 1 commit into from
Feb 5, 2018
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
219 changes: 219 additions & 0 deletions azurerm/data_source_virtual_network_gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package azurerm

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmVirtualNetworkGateway() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmVirtualNetworkGatewayRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"resource_group_name": resourceGroupNameForDataSourceSchema(),

"location": locationForDataSourceSchema(),

"type": {
Type: schema.TypeString,
Computed: true,
},

"vpn_type": {
Type: schema.TypeString,
Computed: true,
},

"enable_bgp": {
Type: schema.TypeBool,
Computed: true,
},

"active_active": {
Type: schema.TypeBool,
Computed: true,
},

"sku": {
Type: schema.TypeString,
Computed: true,
},

"ip_configuration": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"private_ip_address_allocation": {
Type: schema.TypeString,
Computed: true,
},
"subnet_id": {
Type: schema.TypeString,
Computed: true,
},
"public_ip_address_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"vpn_client_configuration": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"address_space": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"root_certificate": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"public_cert_data": {
Type: schema.TypeString,
Computed: true,
},
},
},
Set: hashVirtualNetworkGatewayRootCert,
},
"revoked_certificate": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"thumbprint": {
Type: schema.TypeString,
Computed: true,
},
},
},
Set: hashVirtualNetworkGatewayRevokedCert,
},
},
},
},

"bgp_settings": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"asn": {
Type: schema.TypeInt,
Computed: true,
},
"peering_address": {
Type: schema.TypeString,
Computed: true,
},
"peer_weight": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},

"default_local_network_gateway_id": {
Type: schema.TypeString,
Computed: true,
},

"tags": tagsForDataSourceSchema(),
},
}
}

func dataSourceArmVirtualNetworkGatewayRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).vnetGatewayClient
ctx := meta.(*ArmClient).StopContext

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)

resp, err := client.Get(ctx, resGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Virtual Network Gateway %q (Resource Group %q) was not found!", name, resGroup)
}

return fmt.Errorf("Error making Read request on AzureRM Virtual Network Gateway %q (Resource Group %q): %+v", name, resGroup, err)
}

d.SetId(*resp.ID)

d.Set("name", resp.Name)
d.Set("resource_group_name", resGroup)

if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}

if resp.VirtualNetworkGatewayPropertiesFormat != nil {
gw := *resp.VirtualNetworkGatewayPropertiesFormat

d.Set("type", string(gw.GatewayType))
d.Set("enable_bgp", gw.EnableBgp)
d.Set("active_active", gw.ActiveActive)

if string(gw.VpnType) != "" {
d.Set("vpn_type", string(gw.VpnType))
}

if gw.GatewayDefaultSite != nil {
d.Set("default_local_network_gateway_id", gw.GatewayDefaultSite.ID)
}

if gw.Sku != nil {
d.Set("sku", string(gw.Sku.Name))
}

d.Set("ip_configuration", flattenArmVirtualNetworkGatewayIPConfigurations(gw.IPConfigurations))

if gw.VpnClientConfiguration != nil {
vpnConfigFlat := flattenArmVirtualNetworkGatewayVpnClientConfig(gw.VpnClientConfiguration)
if err := d.Set("vpn_client_configuration", vpnConfigFlat); err != nil {
return fmt.Errorf("Error setting `vpn_client_configuration`: %+v", err)
}
}

if gw.BgpSettings != nil {
bgpSettingsFlat := flattenArmVirtualNetworkGatewayBgpSettings(gw.BgpSettings)
if err := d.Set("bgp_settings", bgpSettingsFlat); err != nil {
return fmt.Errorf("Error setting `bgp_settings`: %+v", err)
}
}
}

flattenAndSetTags(d, resp.Tags)

return nil
}
79 changes: 79 additions & 0 deletions azurerm/data_source_virtual_network_gateway_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMDataSourceVirtualNetworkGateway_basic(t *testing.T) {
ri := acctest.RandInt()
config := testAccAzureRMDataSourceVirtualNetworkGateway_basic(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMVirtualNetworkGatewayDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMVirtualNetworkGatewayExists("data.azurerm_virtual_network_gateway.test"),
),
},
},
})
}

func testAccAzureRMDataSourceVirtualNetworkGateway_basic(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_virtual_network" "test" {
name = "acctestvn-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
address_space = ["10.0.0.0/16"]
}

resource "azurerm_subnet" "test" {
name = "GatewaySubnet"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
address_prefix = "10.0.1.0/24"
}

resource "azurerm_public_ip" "test" {
name = "acctestpip-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
public_ip_address_allocation = "Dynamic"
}

resource "azurerm_virtual_network_gateway" "test" {
name = "acctestvng-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"

type = "Vpn"
vpn_type = "RouteBased"
sku = "Basic"

ip_configuration {
public_ip_address_id = "${azurerm_public_ip.test.id}"
private_ip_address_allocation = "Dynamic"
subnet_id = "${azurerm_subnet.test.id}"
}
}

data "azurerm_virtual_network_gateway" "test" {
name = "${azurerm_virtual_network_gateway.test.name}"
resource_group_name = "${azurerm_virtual_network_gateway.test.resource_group_name}"
}
`, rInt, location, rInt, rInt, rInt)
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_subnet": dataSourceArmSubnet(),
"azurerm_subscription": dataSourceArmSubscription(),
"azurerm_virtual_network": dataSourceArmVirtualNetwork(),
"azurerm_virtual_network_gateway": dataSourceArmVirtualNetworkGateway(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
6 changes: 5 additions & 1 deletion website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@
<a href="/docs/providers/azurerm/d/subscription.html">azurerm_subscription</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-virtual-network") %>>
<li<%= sidebar_current("docs-azurerm-datasource-virtual-network-x") %>>
<a href="/docs/providers/azurerm/d/virtual_network.html">azurerm_virtual_network</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-virtual-network-gateway") %>>
<a href="/docs/providers/azurerm/d/virtual_network_gateway.html">azurerm_virtual_network_gateway</a>
</li>

</ul>
</li>

Expand Down
2 changes: 1 addition & 1 deletion website/docs/d/virtual_network.html.markdown
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_virtual_network"
sidebar_current: "docs-azurerm-datasource-virtual-network"
sidebar_current: "docs-azurerm-datasource-virtual-network-x"
description: |-
Get information about the specified Virtual Network.
---
Expand Down
Loading