forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_arm_virtual_network_peering.go
182 lines (149 loc) · 4.7 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
package azurerm
import (
"fmt"
"log"
"net/http"
"sync"
"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/hashicorp/terraform/helper/schema"
)
// 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": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"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
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,
Properties: getVirtualNetworkPeeringProperties(d),
}
peerMutex.Lock()
defer peerMutex.Unlock()
_, err := client.CreateOrUpdate(resGroup, vnetName, name, peer, make(chan struct{}))
if err != nil {
return err
}
read, err := client.Get(resGroup, vnetName, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read Virtual Network Peering %s (resource group %s) ID", name, resGroup)
}
d.SetId(*read.ID)
return resourceArmVirtualNetworkPeeringRead(d, meta)
}
func resourceArmVirtualNetworkPeeringRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).vnetPeeringsClient
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(resGroup, vnetName, name)
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
}
peer := *resp.Properties
// update appropriate values
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
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()
_, err = client.Delete(resGroup, vnetName, name, make(chan struct{}))
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,
},
}
}