forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_arm_local_network_gateway.go
181 lines (147 loc) · 4.86 KB
/
resource_arm_local_network_gateway.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
package azurerm
import (
"fmt"
"net/http"
"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceArmLocalNetworkGateway() *schema.Resource {
return &schema.Resource{
Create: resourceArmLocalNetworkGatewayCreate,
Read: resourceArmLocalNetworkGatewayRead,
Update: resourceArmLocalNetworkGatewayCreate,
Delete: resourceArmLocalNetworkGatewayDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"location": locationSchema(),
"resource_group_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"gateway_address": {
Type: schema.TypeString,
Required: true,
},
"address_space": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func resourceArmLocalNetworkGatewayCreate(d *schema.ResourceData, meta interface{}) error {
lnetClient := meta.(*ArmClient).localNetConnClient
name := d.Get("name").(string)
location := d.Get("location").(string)
resGroup := d.Get("resource_group_name").(string)
ipAddress := d.Get("gateway_address").(string)
// fetch the 'address_space_prefixes:
prefixes := []string{}
for _, pref := range d.Get("address_space").([]interface{}) {
prefixes = append(prefixes, pref.(string))
}
gateway := network.LocalNetworkGateway{
Name: &name,
Location: &location,
LocalNetworkGatewayPropertiesFormat: &network.LocalNetworkGatewayPropertiesFormat{
LocalNetworkAddressSpace: &network.AddressSpace{
AddressPrefixes: &prefixes,
},
GatewayIPAddress: &ipAddress,
},
}
_, error := lnetClient.CreateOrUpdate(resGroup, name, gateway, make(chan struct{}))
err := <-error
if err != nil {
return fmt.Errorf("Error creating Azure ARM Local Network Gateway '%s': %s", name, err)
}
read, err := lnetClient.Get(resGroup, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read Virtual Network %s (resource group %s) ID", name, resGroup)
}
d.SetId(*read.ID)
return resourceArmLocalNetworkGatewayRead(d, meta)
}
// resourceArmLocalNetworkGatewayRead goes ahead and reads the state of the corresponding ARM local network gateway.
func resourceArmLocalNetworkGatewayRead(d *schema.ResourceData, meta interface{}) error {
lnetClient := meta.(*ArmClient).localNetConnClient
resGroup, name, err := resourceGroupAndLocalNetworkGatewayFromId(d.Id())
if err != nil {
return err
}
resp, err := lnetClient.Get(resGroup, name)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading the state of Azure ARM local network gateway '%s': %s", name, err)
}
d.Set("resource_group_name", resGroup)
d.Set("name", resp.Name)
d.Set("location", azureRMNormalizeLocation(*resp.Location))
d.Set("gateway_address", resp.LocalNetworkGatewayPropertiesFormat.GatewayIPAddress)
prefs := []string{}
if ps := *resp.LocalNetworkGatewayPropertiesFormat.LocalNetworkAddressSpace.AddressPrefixes; ps != nil {
prefs = ps
}
d.Set("address_space", prefs)
return nil
}
// resourceArmLocalNetworkGatewayDelete deletes the specified ARM local network gateway.
func resourceArmLocalNetworkGatewayDelete(d *schema.ResourceData, meta interface{}) error {
lnetClient := meta.(*ArmClient).localNetConnClient
resGroup, name, err := resourceGroupAndLocalNetworkGatewayFromId(d.Id())
if err != nil {
return err
}
deleteResp, error := lnetClient.Delete(resGroup, name, make(chan struct{}))
resp := <-deleteResp
err = <-error
if resp.StatusCode == http.StatusNotFound {
return nil
}
if err != nil {
return fmt.Errorf("Error issuing Azure ARM delete request of local network gateway '%s': %s", name, err)
}
return nil
}
func resourceGroupAndLocalNetworkGatewayFromId(localNetworkGatewayId string) (string, string, error) {
id, err := parseAzureResourceID(localNetworkGatewayId)
if err != nil {
return "", "", err
}
name := id.Path["localNetworkGateways"]
resGroup := id.ResourceGroup
return resGroup, name, nil
}
func retrieveLocalNetworkGatewayById(localNetworkGatewayId string, meta interface{}) (*network.LocalNetworkGateway, bool, error) {
lnetClient := meta.(*ArmClient).localNetConnClient
resGroup, name, err := resourceGroupAndLocalNetworkGatewayFromId(localNetworkGatewayId)
if err != nil {
return nil, false, errwrap.Wrapf("Error Getting LocalNetworkGateway Name and Group: {{err}}", err)
}
resp, err := lnetClient.Get(resGroup, name)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
return nil, false, nil
}
return nil, false, fmt.Errorf("Error making Read request on Azure LocalNetworkGateway %s: %s", name, err)
}
return &resp, true, nil
}