Skip to content

Commit e67f141

Browse files
pmcatomineystack72
authored andcommitted
provider/azurerm: create virtual_network_peering resource (hashicorp#8168)
TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMVirtualNetworkPeering -timeout 120m === RUN TestAccAzureRMVirtualNetworkPeering_importBasic --- PASS: TestAccAzureRMVirtualNetworkPeering_importBasic (225.50s) === RUN TestAccAzureRMVirtualNetworkPeering_basic --- PASS: TestAccAzureRMVirtualNetworkPeering_basic (216.95s) === RUN TestAccAzureRMVirtualNetworkPeering_update --- PASS: TestAccAzureRMVirtualNetworkPeering_update (266.97s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 709.545s
1 parent e917b33 commit e67f141

7 files changed

Lines changed: 542 additions & 0 deletions

File tree

builtin/providers/azurerm/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type ArmClient struct {
4545
vnetGatewayConnectionsClient network.VirtualNetworkGatewayConnectionsClient
4646
vnetGatewayClient network.VirtualNetworkGatewaysClient
4747
vnetClient network.VirtualNetworksClient
48+
vnetPeeringsClient network.VirtualNetworkPeeringsClient
4849
routeTablesClient network.RouteTablesClient
4950
routesClient network.RoutesClient
5051

@@ -257,6 +258,12 @@ func (c *Config) getArmClient() (*ArmClient, error) {
257258
vnc.Sender = autorest.CreateSender(withRequestLogging())
258259
client.vnetClient = vnc
259260

261+
vnpc := network.NewVirtualNetworkPeeringsClient(c.SubscriptionID)
262+
setUserAgent(&vnpc.Client)
263+
vnpc.Authorizer = spt
264+
vnpc.Sender = autorest.CreateSender(withRequestLogging())
265+
client.vnetPeeringsClient = vnpc
266+
260267
rtc := network.NewRouteTablesClient(c.SubscriptionID)
261268
setUserAgent(&rtc.Client)
262269
rtc.Authorizer = spt
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 TestAccAzureRMVirtualNetworkPeering_importBasic(t *testing.T) {
12+
resourceName := "azurerm_virtual_network_peering.test1"
13+
14+
ri := acctest.RandInt()
15+
config := fmt.Sprintf(testAccAzureRMVirtualNetworkPeering_basic, ri, ri, ri, ri, ri)
16+
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: func() { testAccPreCheck(t) },
19+
Providers: testAccProviders,
20+
CheckDestroy: testCheckAzureRMVirtualNetworkPeeringDestroy,
21+
Steps: []resource.TestStep{
22+
resource.TestStep{
23+
Config: config,
24+
},
25+
26+
resource.TestStep{
27+
ResourceName: resourceName,
28+
ImportState: true,
29+
ImportStateVerify: true,
30+
ImportStateVerifyIgnore: []string{"resource_group_name"},
31+
},
32+
},
33+
})
34+
}

builtin/providers/azurerm/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func Provider() terraform.ResourceProvider {
6868
"azurerm_virtual_machine": resourceArmVirtualMachine(),
6969
"azurerm_virtual_machine_scale_set": resourceArmVirtualMachineScaleSet(),
7070
"azurerm_virtual_network": resourceArmVirtualNetwork(),
71+
"azurerm_virtual_network_peering": resourceArmVirtualNetworkPeering(),
7172

7273
// These resources use the Riviera SDK
7374
"azurerm_dns_a_record": resourceArmDnsARecord(),
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package azurerm
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
"sync"
8+
9+
"github.com/Azure/azure-sdk-for-go/arm/network"
10+
"github.com/hashicorp/terraform/helper/schema"
11+
)
12+
13+
// peerMutex is used to prevet multiple Peering resources being creaed, updated
14+
// or deleted at the same time
15+
var peerMutex = &sync.Mutex{}
16+
17+
func resourceArmVirtualNetworkPeering() *schema.Resource {
18+
return &schema.Resource{
19+
Create: resourceArmVirtualNetworkPeeringCreate,
20+
Read: resourceArmVirtualNetworkPeeringRead,
21+
Update: resourceArmVirtualNetworkPeeringCreate,
22+
Delete: resourceArmVirtualNetworkPeeringDelete,
23+
Importer: &schema.ResourceImporter{
24+
State: schema.ImportStatePassthrough,
25+
},
26+
27+
Schema: map[string]*schema.Schema{
28+
"name": {
29+
Type: schema.TypeString,
30+
Required: true,
31+
ForceNew: true,
32+
},
33+
34+
"resource_group_name": {
35+
Type: schema.TypeString,
36+
Required: true,
37+
ForceNew: true,
38+
},
39+
40+
"virtual_network_name": {
41+
Type: schema.TypeString,
42+
Required: true,
43+
ForceNew: true,
44+
},
45+
46+
"remote_virtual_network_id": {
47+
Type: schema.TypeString,
48+
Required: true,
49+
ForceNew: true,
50+
},
51+
52+
"allow_virtual_network_access": {
53+
Type: schema.TypeBool,
54+
Optional: true,
55+
Computed: true,
56+
},
57+
58+
"allow_forwarded_traffic": {
59+
Type: schema.TypeBool,
60+
Optional: true,
61+
Computed: true,
62+
},
63+
64+
"allow_gateway_transit": {
65+
Type: schema.TypeBool,
66+
Optional: true,
67+
Computed: true,
68+
},
69+
70+
"use_remote_gateways": {
71+
Type: schema.TypeBool,
72+
Optional: true,
73+
Computed: true,
74+
},
75+
},
76+
}
77+
}
78+
79+
func resourceArmVirtualNetworkPeeringCreate(d *schema.ResourceData, meta interface{}) error {
80+
client := meta.(*ArmClient).vnetPeeringsClient
81+
82+
log.Printf("[INFO] preparing arguments for Azure ARM virtual network peering creation.")
83+
84+
name := d.Get("name").(string)
85+
vnetName := d.Get("virtual_network_name").(string)
86+
resGroup := d.Get("resource_group_name").(string)
87+
88+
peer := network.VirtualNetworkPeering{
89+
Name: &name,
90+
Properties: getVirtualNetworkPeeringProperties(d),
91+
}
92+
93+
peerMutex.Lock()
94+
defer peerMutex.Unlock()
95+
96+
_, err := client.CreateOrUpdate(resGroup, vnetName, name, peer, make(chan struct{}))
97+
if err != nil {
98+
return err
99+
}
100+
101+
read, err := client.Get(resGroup, vnetName, name)
102+
if err != nil {
103+
return err
104+
}
105+
if read.ID == nil {
106+
return fmt.Errorf("Cannot read Virtual Network Peering %s (resource group %s) ID", name, resGroup)
107+
}
108+
109+
d.SetId(*read.ID)
110+
111+
return resourceArmVirtualNetworkPeeringRead(d, meta)
112+
}
113+
114+
func resourceArmVirtualNetworkPeeringRead(d *schema.ResourceData, meta interface{}) error {
115+
client := meta.(*ArmClient).vnetPeeringsClient
116+
117+
id, err := parseAzureResourceID(d.Id())
118+
if err != nil {
119+
return err
120+
}
121+
resGroup := id.ResourceGroup
122+
vnetName := id.Path["virtualNetworks"]
123+
name := id.Path["virtualNetworkPeerings"]
124+
125+
resp, err := client.Get(resGroup, vnetName, name)
126+
if resp.StatusCode == http.StatusNotFound {
127+
d.SetId("")
128+
return nil
129+
}
130+
if err != nil {
131+
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
132+
}
133+
peer := *resp.Properties
134+
135+
// update appropriate values
136+
d.Set("name", resp.Name)
137+
d.Set("virtual_network_name", vnetName)
138+
d.Set("allow_virtual_network_access", peer.AllowVirtualNetworkAccess)
139+
d.Set("allow_forwarded_traffic", peer.AllowForwardedTraffic)
140+
d.Set("allow_gateway_transit", peer.AllowGatewayTransit)
141+
d.Set("use_remote_gateways", peer.UseRemoteGateways)
142+
d.Set("remote_virtual_network_id", peer.RemoteVirtualNetwork.ID)
143+
144+
return nil
145+
}
146+
147+
func resourceArmVirtualNetworkPeeringDelete(d *schema.ResourceData, meta interface{}) error {
148+
client := meta.(*ArmClient).vnetPeeringsClient
149+
150+
id, err := parseAzureResourceID(d.Id())
151+
if err != nil {
152+
return err
153+
}
154+
resGroup := id.ResourceGroup
155+
vnetName := id.Path["virtualNetworks"]
156+
name := id.Path["virtualNetworkPeerings"]
157+
158+
peerMutex.Lock()
159+
defer peerMutex.Unlock()
160+
161+
_, err = client.Delete(resGroup, vnetName, name, make(chan struct{}))
162+
163+
return err
164+
}
165+
166+
func getVirtualNetworkPeeringProperties(d *schema.ResourceData) *network.VirtualNetworkPeeringPropertiesFormat {
167+
allowVirtualNetworkAccess := d.Get("allow_virtual_network_access").(bool)
168+
allowForwardedTraffic := d.Get("allow_forwarded_traffic").(bool)
169+
allowGatewayTransit := d.Get("allow_gateway_transit").(bool)
170+
useRemoteGateways := d.Get("use_remote_gateways").(bool)
171+
remoteVirtualNetworkID := d.Get("remote_virtual_network_id").(string)
172+
173+
return &network.VirtualNetworkPeeringPropertiesFormat{
174+
AllowVirtualNetworkAccess: &allowVirtualNetworkAccess,
175+
AllowForwardedTraffic: &allowForwardedTraffic,
176+
AllowGatewayTransit: &allowGatewayTransit,
177+
UseRemoteGateways: &useRemoteGateways,
178+
RemoteVirtualNetwork: &network.SubResource{
179+
ID: &remoteVirtualNetworkID,
180+
},
181+
}
182+
}

0 commit comments

Comments
 (0)