forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_openstack_networking_router_route_v2.go
214 lines (167 loc) · 5.42 KB
/
resource_openstack_networking_router_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
package openstack
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/layer3/routers"
)
func resourceNetworkingRouterRouteV2() *schema.Resource {
return &schema.Resource{
Create: resourceNetworkingRouterRouteV2Create,
Read: resourceNetworkingRouterRouteV2Read,
Delete: resourceNetworkingRouterRouteV2Delete,
Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("OS_REGION_NAME", ""),
},
"router_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 resourceNetworkingRouterRouteV2Create(d *schema.ResourceData, meta interface{}) error {
routerId := d.Get("router_id").(string)
osMutexKV.Lock(routerId)
defer osMutexKV.Unlock(routerId)
var destCidr string = d.Get("destination_cidr").(string)
var nextHop string = d.Get("next_hop").(string)
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
n, err := routers.Get(networkingClient, routerId).Extract()
if err != nil {
httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError)
if !ok {
return fmt.Errorf("Error retrieving OpenStack Neutron Router: %s", err)
}
if httpError.Actual == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving OpenStack Neutron Router: %s", err)
}
var updateOpts routers.UpdateOpts
var routeExists bool = false
var rts []routers.Route = n.Routes
for _, r := range rts {
if r.DestinationCIDR == destCidr && r.NextHop == nextHop {
routeExists = true
break
}
}
if !routeExists {
if destCidr != "" && nextHop != "" {
r := routers.Route{DestinationCIDR: destCidr, NextHop: nextHop}
log.Printf(
"[INFO] Adding route %s", r)
rts = append(rts, r)
}
updateOpts.Routes = rts
log.Printf("[DEBUG] Updating Router %s with options: %+v", routerId, updateOpts)
_, err = routers.Update(networkingClient, routerId, updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating OpenStack Neutron Router: %s", err)
}
d.SetId(fmt.Sprintf("%s-route-%s-%s", routerId, destCidr, nextHop))
} else {
log.Printf("[DEBUG] Router %s has route already", routerId)
}
return resourceNetworkingRouterRouteV2Read(d, meta)
}
func resourceNetworkingRouterRouteV2Read(d *schema.ResourceData, meta interface{}) error {
routerId := d.Get("router_id").(string)
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
n, err := routers.Get(networkingClient, routerId).Extract()
if err != nil {
httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError)
if !ok {
return fmt.Errorf("Error retrieving OpenStack Neutron Router: %s", err)
}
if httpError.Actual == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving OpenStack Neutron Router: %s", err)
}
log.Printf("[DEBUG] Retrieved Router %s: %+v", routerId, n)
var destCidr string = d.Get("destination_cidr").(string)
var nextHop string = d.Get("next_hop").(string)
d.Set("next_hop", "")
d.Set("destination_cidr", "")
for _, r := range n.Routes {
if r.DestinationCIDR == destCidr && r.NextHop == nextHop {
d.Set("destination_cidr", destCidr)
d.Set("next_hop", nextHop)
break
}
}
return nil
}
func resourceNetworkingRouterRouteV2Delete(d *schema.ResourceData, meta interface{}) error {
routerId := d.Get("router_id").(string)
osMutexKV.Lock(routerId)
defer osMutexKV.Unlock(routerId)
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
n, err := routers.Get(networkingClient, routerId).Extract()
if err != nil {
httpError, ok := err.(*gophercloud.UnexpectedResponseCodeError)
if !ok {
return fmt.Errorf("Error retrieving OpenStack Neutron Router: %s", err)
}
if httpError.Actual == 404 {
return nil
}
return fmt.Errorf("Error retrieving OpenStack Neutron Router: %s", err)
}
var updateOpts routers.UpdateOpts
var destCidr string = d.Get("destination_cidr").(string)
var nextHop string = d.Get("next_hop").(string)
var oldRts []routers.Route = n.Routes
var newRts []routers.Route
for _, r := range oldRts {
if r.DestinationCIDR != destCidr || r.NextHop != nextHop {
newRts = append(newRts, r)
}
}
if len(oldRts) != len(newRts) {
r := routers.Route{DestinationCIDR: destCidr, NextHop: nextHop}
log.Printf(
"[INFO] Deleting route %s", r)
updateOpts.Routes = newRts
log.Printf("[DEBUG] Updating Router %s with options: %+v", routerId, updateOpts)
_, err = routers.Update(networkingClient, routerId, updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating OpenStack Neutron Router: %s", err)
}
} else {
return fmt.Errorf("Route did not exist already")
}
return nil
}