forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_arm_virtual_network_peering.go
196 lines (160 loc) · 5.62 KB
/
resource_arm_virtual_network_peering.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package azurerm
import (
"fmt"
"log"
"sync"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2017-09-01/network"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
// peerMutex is used to prevet multiple Peering resources being creaed, updated
// or deleted at the same time
var peerMutex = &sync.Mutex{}
func resourceArmVirtualNetworkPeering() *schema.Resource {
return &schema.Resource{
Create: resourceArmVirtualNetworkPeeringCreate,
Read: resourceArmVirtualNetworkPeeringRead,
Update: resourceArmVirtualNetworkPeeringCreate,
Delete: resourceArmVirtualNetworkPeeringDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"resource_group_name": resourceGroupNameSchema(),
"virtual_network_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"remote_virtual_network_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"allow_virtual_network_access": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"allow_forwarded_traffic": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"allow_gateway_transit": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"use_remote_gateways": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
},
}
}
func resourceArmVirtualNetworkPeeringCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).vnetPeeringsClient
ctx := meta.(*ArmClient).StopContext
log.Printf("[INFO] preparing arguments for Azure ARM virtual network peering creation.")
name := d.Get("name").(string)
vnetName := d.Get("virtual_network_name").(string)
resGroup := d.Get("resource_group_name").(string)
peer := network.VirtualNetworkPeering{
Name: &name,
VirtualNetworkPeeringPropertiesFormat: getVirtualNetworkPeeringProperties(d),
}
peerMutex.Lock()
defer peerMutex.Unlock()
future, err := client.CreateOrUpdate(ctx, resGroup, vnetName, name, peer)
if err != nil {
return fmt.Errorf("Error Creating/Updating Virtual Network Peering %q (Network %q / Resource Group %q): %+v", name, vnetName, resGroup, err)
}
err = future.WaitForCompletion(ctx, client.Client)
if err != nil {
return fmt.Errorf("Error waiting for completion of Virtual Network Peering %q (Network %q / Resource Group %q): %+v", name, vnetName, resGroup, err)
}
read, err := client.Get(ctx, resGroup, vnetName, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read ID of Virtual Network Peering %q (resource group %q)", name, resGroup)
}
d.SetId(*read.ID)
return resourceArmVirtualNetworkPeeringRead(d, meta)
}
func resourceArmVirtualNetworkPeeringRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).vnetPeeringsClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
vnetName := id.Path["virtualNetworks"]
name := id.Path["virtualNetworkPeerings"]
resp, err := client.Get(ctx, resGroup, vnetName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Azure virtual network peering %q: %+v", name, err)
}
peer := *resp.VirtualNetworkPeeringPropertiesFormat
// update appropriate values
d.Set("resource_group_name", resGroup)
d.Set("name", resp.Name)
d.Set("virtual_network_name", vnetName)
d.Set("allow_virtual_network_access", peer.AllowVirtualNetworkAccess)
d.Set("allow_forwarded_traffic", peer.AllowForwardedTraffic)
d.Set("allow_gateway_transit", peer.AllowGatewayTransit)
d.Set("use_remote_gateways", peer.UseRemoteGateways)
d.Set("remote_virtual_network_id", peer.RemoteVirtualNetwork.ID)
return nil
}
func resourceArmVirtualNetworkPeeringDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).vnetPeeringsClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
vnetName := id.Path["virtualNetworks"]
name := id.Path["virtualNetworkPeerings"]
peerMutex.Lock()
defer peerMutex.Unlock()
future, err := client.Delete(ctx, resGroup, vnetName, name)
if err != nil {
return fmt.Errorf("Error deleting Virtual Network Peering %q (Network %q / RG %q): %+v", name, vnetName, resGroup, err)
}
err = future.WaitForCompletion(ctx, client.Client)
if err != nil {
return fmt.Errorf("Error waiting for deletion of Virtual Network Peering %q (Network %q / RG %q): %+v", name, vnetName, resGroup, err)
}
return err
}
func getVirtualNetworkPeeringProperties(d *schema.ResourceData) *network.VirtualNetworkPeeringPropertiesFormat {
allowVirtualNetworkAccess := d.Get("allow_virtual_network_access").(bool)
allowForwardedTraffic := d.Get("allow_forwarded_traffic").(bool)
allowGatewayTransit := d.Get("allow_gateway_transit").(bool)
useRemoteGateways := d.Get("use_remote_gateways").(bool)
remoteVirtualNetworkID := d.Get("remote_virtual_network_id").(string)
return &network.VirtualNetworkPeeringPropertiesFormat{
AllowVirtualNetworkAccess: &allowVirtualNetworkAccess,
AllowForwardedTraffic: &allowForwardedTraffic,
AllowGatewayTransit: &allowGatewayTransit,
UseRemoteGateways: &useRemoteGateways,
RemoteVirtualNetwork: &network.SubResource{
ID: &remoteVirtualNetworkID,
},
}
}