-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_openstack_networking_subnet_route_v2.go
224 lines (174 loc) · 5.52 KB
/
resource_openstack_networking_subnet_route_v2.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package openstack
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
)
func resourceNetworkingSubnetRouteV2() *schema.Resource {
return &schema.Resource{
Create: resourceNetworkingSubnetRouteV2Create,
Read: resourceNetworkingSubnetRouteV2Read,
Delete: resourceNetworkingSubnetRouteV2Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"subnet_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"destination_cidr": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"next_hop": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceNetworkingSubnetRouteV2Create(d *schema.ResourceData, meta interface{}) error {
subnetId := d.Get("subnet_id").(string)
osMutexKV.Lock(subnetId)
defer osMutexKV.Unlock(subnetId)
var destCidr string = d.Get("destination_cidr").(string)
var nextHop string = d.Get("next_hop").(string)
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
subnet, err := subnets.Get(networkingClient, subnetId).Extract()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving OpenStack Neutron Subnet: %s", err)
}
var updateOpts subnets.UpdateOpts
var routeExists bool = false
var rts []subnets.HostRoute = subnet.HostRoutes
for _, r := range rts {
if r.DestinationCIDR == destCidr && r.NextHop == nextHop {
routeExists = true
break
}
}
if routeExists {
return fmt.Errorf("Subnet %s has route already", subnetId)
}
if destCidr != "" && nextHop != "" {
r := subnets.HostRoute{DestinationCIDR: destCidr, NextHop: nextHop}
log.Printf("[INFO] Adding route %s", r)
rts = append(rts, r)
}
updateOpts.HostRoutes = &rts
log.Printf("[DEBUG] Updating Subnet %s with options: %+v", subnetId, updateOpts)
_, err = subnets.Update(networkingClient, subnetId, updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating OpenStack Neutron Subnet: %s", err)
}
id := fmt.Sprintf("%s-route-%s-%s", subnetId, destCidr, nextHop)
d.SetId(id)
return resourceNetworkingSubnetRouteV2Read(d, meta)
}
func resourceNetworkingSubnetRouteV2Read(d *schema.ResourceData, meta interface{}) error {
subnetId := d.Get("subnet_id").(string)
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
destCidr := d.Get("destination_cidr").(string)
nextHop := d.Get("next_hop").(string)
routeIDParts := []string{}
if d.Id() != "" && strings.Contains(d.Id(), "-route-") {
routeIDParts = strings.Split(d.Id(), "-route-")
routeLastIDParts := strings.Split(routeIDParts[1], "-")
if subnetId == "" {
subnetId = routeIDParts[0]
d.Set("subnet_id", subnetId)
}
if destCidr == "" {
destCidr = routeLastIDParts[0]
}
if nextHop == "" {
nextHop = routeLastIDParts[1]
}
}
subnet, err := subnets.Get(networkingClient, subnetId).Extract()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving OpenStack Neutron Subnet: %s", err)
}
log.Printf("[DEBUG] Retrieved Subnet %s: %+v", subnetId, subnet)
var exists bool
for _, r := range subnet.HostRoutes {
if r.DestinationCIDR == destCidr && r.NextHop == nextHop {
exists = true
}
}
if !exists {
return fmt.Errorf("Route doesn't exist")
}
d.Set("next_hop", nextHop)
d.Set("destination_cidr", destCidr)
d.Set("region", GetRegion(d, config))
return nil
}
func resourceNetworkingSubnetRouteV2Delete(d *schema.ResourceData, meta interface{}) error {
subnetId := d.Get("subnet_id").(string)
osMutexKV.Lock(subnetId)
defer osMutexKV.Unlock(subnetId)
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
subnet, err := subnets.Get(networkingClient, subnetId).Extract()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok {
return nil
}
return fmt.Errorf("Error retrieving OpenStack Neutron Subnet: %s", err)
}
var updateOpts subnets.UpdateOpts
var destCidr string = d.Get("destination_cidr").(string)
var nextHop string = d.Get("next_hop").(string)
var oldRts []subnets.HostRoute = subnet.HostRoutes
var newRts []subnets.HostRoute
for _, r := range oldRts {
if r.DestinationCIDR != destCidr || r.NextHop != nextHop {
newRts = append(newRts, r)
}
}
if len(oldRts) == len(newRts) {
return fmt.Errorf("Route did not exist already")
}
r := subnets.HostRoute{DestinationCIDR: destCidr, NextHop: nextHop}
log.Printf("[INFO] Deleting route %s", r)
updateOpts.HostRoutes = &newRts
log.Printf("[DEBUG] Updating Subnet %s with options: %+v", subnetId, updateOpts)
_, err = subnets.Update(networkingClient, subnetId, updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating OpenStack Neutron Subnet: %s", err)
}
return nil
}