Skip to content

Commit 8a780e1

Browse files
Merge pull request #796 from terraform-providers/virtual-network-gateway-data-source
New Data Source: `azurerm_virtual_network_gateway`
2 parents 514af66 + d02303a commit 8a780e1

6 files changed

+415
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
package azurerm
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform/helper/schema"
7+
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
8+
)
9+
10+
func dataSourceArmVirtualNetworkGateway() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceArmVirtualNetworkGatewayRead,
13+
14+
Schema: map[string]*schema.Schema{
15+
"name": {
16+
Type: schema.TypeString,
17+
Required: true,
18+
},
19+
20+
"resource_group_name": resourceGroupNameForDataSourceSchema(),
21+
22+
"location": locationForDataSourceSchema(),
23+
24+
"type": {
25+
Type: schema.TypeString,
26+
Computed: true,
27+
},
28+
29+
"vpn_type": {
30+
Type: schema.TypeString,
31+
Computed: true,
32+
},
33+
34+
"enable_bgp": {
35+
Type: schema.TypeBool,
36+
Computed: true,
37+
},
38+
39+
"active_active": {
40+
Type: schema.TypeBool,
41+
Computed: true,
42+
},
43+
44+
"sku": {
45+
Type: schema.TypeString,
46+
Computed: true,
47+
},
48+
49+
"ip_configuration": {
50+
Type: schema.TypeList,
51+
Computed: true,
52+
Elem: &schema.Resource{
53+
Schema: map[string]*schema.Schema{
54+
"name": {
55+
Type: schema.TypeString,
56+
Computed: true,
57+
},
58+
"private_ip_address_allocation": {
59+
Type: schema.TypeString,
60+
Computed: true,
61+
},
62+
"subnet_id": {
63+
Type: schema.TypeString,
64+
Computed: true,
65+
},
66+
"public_ip_address_id": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
},
70+
},
71+
},
72+
},
73+
74+
"vpn_client_configuration": {
75+
Type: schema.TypeList,
76+
Computed: true,
77+
Elem: &schema.Resource{
78+
Schema: map[string]*schema.Schema{
79+
"address_space": {
80+
Type: schema.TypeList,
81+
Computed: true,
82+
Elem: &schema.Schema{
83+
Type: schema.TypeString,
84+
},
85+
},
86+
"root_certificate": {
87+
Type: schema.TypeSet,
88+
Computed: true,
89+
Elem: &schema.Resource{
90+
Schema: map[string]*schema.Schema{
91+
"name": {
92+
Type: schema.TypeString,
93+
Computed: true,
94+
},
95+
"public_cert_data": {
96+
Type: schema.TypeString,
97+
Computed: true,
98+
},
99+
},
100+
},
101+
Set: hashVirtualNetworkGatewayRootCert,
102+
},
103+
"revoked_certificate": {
104+
Type: schema.TypeSet,
105+
Computed: true,
106+
Elem: &schema.Resource{
107+
Schema: map[string]*schema.Schema{
108+
"name": {
109+
Type: schema.TypeString,
110+
Computed: true,
111+
},
112+
"thumbprint": {
113+
Type: schema.TypeString,
114+
Computed: true,
115+
},
116+
},
117+
},
118+
Set: hashVirtualNetworkGatewayRevokedCert,
119+
},
120+
},
121+
},
122+
},
123+
124+
"bgp_settings": {
125+
Type: schema.TypeList,
126+
Computed: true,
127+
Elem: &schema.Resource{
128+
Schema: map[string]*schema.Schema{
129+
"asn": {
130+
Type: schema.TypeInt,
131+
Computed: true,
132+
},
133+
"peering_address": {
134+
Type: schema.TypeString,
135+
Computed: true,
136+
},
137+
"peer_weight": {
138+
Type: schema.TypeInt,
139+
Computed: true,
140+
},
141+
},
142+
},
143+
},
144+
145+
"default_local_network_gateway_id": {
146+
Type: schema.TypeString,
147+
Computed: true,
148+
},
149+
150+
"tags": tagsForDataSourceSchema(),
151+
},
152+
}
153+
}
154+
155+
func dataSourceArmVirtualNetworkGatewayRead(d *schema.ResourceData, meta interface{}) error {
156+
client := meta.(*ArmClient).vnetGatewayClient
157+
ctx := meta.(*ArmClient).StopContext
158+
159+
name := d.Get("name").(string)
160+
resGroup := d.Get("resource_group_name").(string)
161+
162+
resp, err := client.Get(ctx, resGroup, name)
163+
if err != nil {
164+
if utils.ResponseWasNotFound(resp.Response) {
165+
return fmt.Errorf("Virtual Network Gateway %q (Resource Group %q) was not found!", name, resGroup)
166+
}
167+
168+
return fmt.Errorf("Error making Read request on AzureRM Virtual Network Gateway %q (Resource Group %q): %+v", name, resGroup, err)
169+
}
170+
171+
d.SetId(*resp.ID)
172+
173+
d.Set("name", resp.Name)
174+
d.Set("resource_group_name", resGroup)
175+
176+
if location := resp.Location; location != nil {
177+
d.Set("location", azureRMNormalizeLocation(*location))
178+
}
179+
180+
if resp.VirtualNetworkGatewayPropertiesFormat != nil {
181+
gw := *resp.VirtualNetworkGatewayPropertiesFormat
182+
183+
d.Set("type", string(gw.GatewayType))
184+
d.Set("enable_bgp", gw.EnableBgp)
185+
d.Set("active_active", gw.ActiveActive)
186+
187+
if string(gw.VpnType) != "" {
188+
d.Set("vpn_type", string(gw.VpnType))
189+
}
190+
191+
if gw.GatewayDefaultSite != nil {
192+
d.Set("default_local_network_gateway_id", gw.GatewayDefaultSite.ID)
193+
}
194+
195+
if gw.Sku != nil {
196+
d.Set("sku", string(gw.Sku.Name))
197+
}
198+
199+
d.Set("ip_configuration", flattenArmVirtualNetworkGatewayIPConfigurations(gw.IPConfigurations))
200+
201+
if gw.VpnClientConfiguration != nil {
202+
vpnConfigFlat := flattenArmVirtualNetworkGatewayVpnClientConfig(gw.VpnClientConfiguration)
203+
if err := d.Set("vpn_client_configuration", vpnConfigFlat); err != nil {
204+
return fmt.Errorf("Error setting `vpn_client_configuration`: %+v", err)
205+
}
206+
}
207+
208+
if gw.BgpSettings != nil {
209+
bgpSettingsFlat := flattenArmVirtualNetworkGatewayBgpSettings(gw.BgpSettings)
210+
if err := d.Set("bgp_settings", bgpSettingsFlat); err != nil {
211+
return fmt.Errorf("Error setting `bgp_settings`: %+v", err)
212+
}
213+
}
214+
}
215+
216+
flattenAndSetTags(d, resp.Tags)
217+
218+
return nil
219+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package azurerm
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/acctest"
8+
"github.com/hashicorp/terraform/helper/resource"
9+
)
10+
11+
func TestAccAzureRMDataSourceVirtualNetworkGateway_basic(t *testing.T) {
12+
ri := acctest.RandInt()
13+
config := testAccAzureRMDataSourceVirtualNetworkGateway_basic(ri, testLocation())
14+
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { testAccPreCheck(t) },
17+
Providers: testAccProviders,
18+
CheckDestroy: testCheckAzureRMVirtualNetworkGatewayDestroy,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: config,
22+
Check: resource.ComposeTestCheckFunc(
23+
testCheckAzureRMVirtualNetworkGatewayExists("data.azurerm_virtual_network_gateway.test"),
24+
),
25+
},
26+
},
27+
})
28+
}
29+
30+
func testAccAzureRMDataSourceVirtualNetworkGateway_basic(rInt int, location string) string {
31+
return fmt.Sprintf(`
32+
resource "azurerm_resource_group" "test" {
33+
name = "acctestRG-%d"
34+
location = "%s"
35+
}
36+
37+
resource "azurerm_virtual_network" "test" {
38+
name = "acctestvn-%d"
39+
location = "${azurerm_resource_group.test.location}"
40+
resource_group_name = "${azurerm_resource_group.test.name}"
41+
address_space = ["10.0.0.0/16"]
42+
}
43+
44+
resource "azurerm_subnet" "test" {
45+
name = "GatewaySubnet"
46+
resource_group_name = "${azurerm_resource_group.test.name}"
47+
virtual_network_name = "${azurerm_virtual_network.test.name}"
48+
address_prefix = "10.0.1.0/24"
49+
}
50+
51+
resource "azurerm_public_ip" "test" {
52+
name = "acctestpip-%d"
53+
location = "${azurerm_resource_group.test.location}"
54+
resource_group_name = "${azurerm_resource_group.test.name}"
55+
public_ip_address_allocation = "Dynamic"
56+
}
57+
58+
resource "azurerm_virtual_network_gateway" "test" {
59+
name = "acctestvng-%d"
60+
location = "${azurerm_resource_group.test.location}"
61+
resource_group_name = "${azurerm_resource_group.test.name}"
62+
63+
type = "Vpn"
64+
vpn_type = "RouteBased"
65+
sku = "Basic"
66+
67+
ip_configuration {
68+
public_ip_address_id = "${azurerm_public_ip.test.id}"
69+
private_ip_address_allocation = "Dynamic"
70+
subnet_id = "${azurerm_subnet.test.id}"
71+
}
72+
}
73+
74+
data "azurerm_virtual_network_gateway" "test" {
75+
name = "${azurerm_virtual_network_gateway.test.name}"
76+
resource_group_name = "${azurerm_virtual_network_gateway.test.resource_group_name}"
77+
}
78+
`, rInt, location, rInt, rInt, rInt)
79+
}

azurerm/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func Provider() terraform.ResourceProvider {
8383
"azurerm_subnet": dataSourceArmSubnet(),
8484
"azurerm_subscription": dataSourceArmSubscription(),
8585
"azurerm_virtual_network": dataSourceArmVirtualNetwork(),
86+
"azurerm_virtual_network_gateway": dataSourceArmVirtualNetworkGateway(),
8687
},
8788

8889
ResourcesMap: map[string]*schema.Resource{

website/azurerm.erb

+5-1
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,14 @@
7979
<a href="/docs/providers/azurerm/d/subscription.html">azurerm_subscription</a>
8080
</li>
8181

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

86+
<li<%= sidebar_current("docs-azurerm-datasource-virtual-network-gateway") %>>
87+
<a href="/docs/providers/azurerm/d/virtual_network_gateway.html">azurerm_virtual_network_gateway</a>
88+
</li>
89+
8690
</ul>
8791
</li>
8892

website/docs/d/virtual_network.html.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: "azurerm"
33
page_title: "Azure Resource Manager: azurerm_virtual_network"
4-
sidebar_current: "docs-azurerm-datasource-virtual-network"
4+
sidebar_current: "docs-azurerm-datasource-virtual-network-x"
55
description: |-
66
Get information about the specified Virtual Network.
77
---

0 commit comments

Comments
 (0)