-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_openstack_networking_router_interface_v2.go
208 lines (173 loc) · 6.11 KB
/
resource_openstack_networking_router_interface_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
package openstack
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/routers"
"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
)
func resourceNetworkingRouterInterfaceV2() *schema.Resource {
return &schema.Resource{
Create: resourceNetworkingRouterInterfaceV2Create,
Read: resourceNetworkingRouterInterfaceV2Read,
Delete: resourceNetworkingRouterInterfaceV2Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"router_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"subnet_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"port_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}
func resourceNetworkingRouterInterfaceV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
createOpts := routers.AddInterfaceOpts{
SubnetID: d.Get("subnet_id").(string),
PortID: d.Get("port_id").(string),
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
n, err := routers.AddInterface(networkingClient, d.Get("router_id").(string), createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating OpenStack Neutron router interface: %s", err)
}
log.Printf("[INFO] Router interface Port ID: %s", n.PortID)
log.Printf("[DEBUG] Waiting for Router Interface (%s) to become available", n.PortID)
stateConf := &resource.StateChangeConf{
Pending: []string{"BUILD", "PENDING_CREATE", "PENDING_UPDATE"},
Target: []string{"ACTIVE"},
Refresh: waitForRouterInterfaceActive(networkingClient, n.PortID),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
d.SetId(n.PortID)
return resourceNetworkingRouterInterfaceV2Read(d, meta)
}
func resourceNetworkingRouterInterfaceV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
n, err := ports.Get(networkingClient, d.Id()).Extract()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving OpenStack Neutron Router Interface: %s", err)
}
log.Printf("[DEBUG] Retrieved Router Interface %s: %+v", d.Id(), n)
d.Set("router_id", n.DeviceID)
d.Set("port_id", n.ID)
// Set the subnet ID by looking at thet port's FixedIPs.
// If there's more than one FixedIP, do not set the subnet
// as it's not possible to confidently determine which subnet
// belongs to this interface. However, that situation should
// not happen.
if len(n.FixedIPs) == 1 {
d.Set("subnet_id", n.FixedIPs[0].SubnetID)
}
d.Set("region", GetRegion(d, config))
return nil
}
func resourceNetworkingRouterInterfaceV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
stateConf := &resource.StateChangeConf{
Pending: []string{"ACTIVE"},
Target: []string{"DELETED"},
Refresh: waitForRouterInterfaceDelete(networkingClient, d),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error deleting OpenStack Neutron Router Interface: %s", err)
}
d.SetId("")
return nil
}
func waitForRouterInterfaceActive(networkingClient *gophercloud.ServiceClient, rId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
r, err := ports.Get(networkingClient, rId).Extract()
if err != nil {
return nil, "", err
}
log.Printf("[DEBUG] OpenStack Neutron Router Interface: %+v", r)
return r, r.Status, nil
}
}
func waitForRouterInterfaceDelete(networkingClient *gophercloud.ServiceClient, d *schema.ResourceData) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
routerId := d.Get("router_id").(string)
routerInterfaceId := d.Id()
log.Printf("[DEBUG] Attempting to delete OpenStack Router Interface %s.", routerInterfaceId)
removeOpts := routers.RemoveInterfaceOpts{
SubnetID: d.Get("subnet_id").(string),
PortID: d.Get("port_id").(string),
}
r, err := ports.Get(networkingClient, routerInterfaceId).Extract()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok {
log.Printf("[DEBUG] Successfully deleted OpenStack Router Interface %s", routerInterfaceId)
return r, "DELETED", nil
}
return r, "ACTIVE", err
}
_, err = routers.RemoveInterface(networkingClient, routerId, removeOpts).Extract()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok {
log.Printf("[DEBUG] Successfully deleted OpenStack Router Interface %s.", routerInterfaceId)
return r, "DELETED", nil
}
if errCode, ok := err.(gophercloud.ErrUnexpectedResponseCode); ok {
if errCode.Actual == 409 {
log.Printf("[DEBUG] Router Interface %s is still in use.", routerInterfaceId)
return r, "ACTIVE", nil
}
}
return r, "ACTIVE", err
}
log.Printf("[DEBUG] OpenStack Router Interface %s is still active.", routerInterfaceId)
return r, "ACTIVE", nil
}
}