-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_openstack_compute_interface_attach_v2.go
245 lines (198 loc) · 6.57 KB
/
resource_openstack_compute_interface_attach_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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package openstack
import (
"fmt"
"log"
"strings"
"time"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/attachinterfaces"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceComputeInterfaceAttachV2() *schema.Resource {
return &schema.Resource{
Create: resourceComputeInterfaceAttachV2Create,
Read: resourceComputeInterfaceAttachV2Read,
Delete: resourceComputeInterfaceAttachV2Delete,
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,
},
"port_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"network_id"},
},
"network_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ConflictsWith: []string{"port_id"},
},
"instance_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"fixed_ip": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceComputeInterfaceAttachV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
computeClient, err := config.computeV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
instanceId := d.Get("instance_id").(string)
var portId string
if v, ok := d.GetOk("port_id"); ok {
portId = v.(string)
}
var networkId string
if v, ok := d.GetOk("network_id"); ok {
networkId = v.(string)
}
if networkId == "" && portId == "" {
return fmt.Errorf("Must set one of network_id and port_id")
}
// For some odd reason the API takes an array of IPs, but you can only have one element in the array.
var fixedIPs []attachinterfaces.FixedIP
if v, ok := d.GetOk("fixed_ip"); ok {
fixedIPs = append(fixedIPs, attachinterfaces.FixedIP{IPAddress: v.(string)})
}
attachOpts := attachinterfaces.CreateOpts{
PortID: portId,
NetworkID: networkId,
FixedIPs: fixedIPs,
}
log.Printf("[DEBUG] Creating interface attachment: %#v", attachOpts)
attachment, err := attachinterfaces.Create(computeClient, instanceId, attachOpts).Extract()
if err != nil {
return err
}
stateConf := &resource.StateChangeConf{
Pending: []string{"ATTACHING"},
Target: []string{"ATTACHED"},
Refresh: resourceComputeInterfaceAttachV2AttachFunc(computeClient, instanceId, attachment.PortID),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 5 * time.Second,
MinTimeout: 5 * time.Second,
}
if _, err = stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error attaching interface: %s", err)
}
log.Printf("[DEBUG] Created interface attachment: %#v", attachment)
// Use the instance ID and attachment ID as the resource ID.
id := fmt.Sprintf("%s/%s", instanceId, attachment.PortID)
d.SetId(id)
return resourceComputeInterfaceAttachV2Read(d, meta)
}
func resourceComputeInterfaceAttachV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
computeClient, err := config.computeV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
instanceId, attachmentId, err := parseComputeInterfaceAttachId(d.Id())
if err != nil {
return err
}
attachment, err := attachinterfaces.Get(computeClient, instanceId, attachmentId).Extract()
if err != nil {
return CheckDeleted(d, err, "compute_interface_attach")
}
log.Printf("[DEBUG] Retrieved interface attachment: %#v", attachment)
d.Set("port_id", attachment.PortID)
d.Set("network_id", attachment.NetID)
d.Set("region", GetRegion(d, config))
return nil
}
func resourceComputeInterfaceAttachV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
computeClient, err := config.computeV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
instanceId, attachmentId, err := parseComputeInterfaceAttachId(d.Id())
if err != nil {
return err
}
stateConf := &resource.StateChangeConf{
Pending: []string{""},
Target: []string{"DETACHED"},
Refresh: resourceComputeInterfaceAttachV2DetachFunc(computeClient, instanceId, attachmentId),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 5 * time.Second,
MinTimeout: 5 * time.Second,
}
if _, err = stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error detaching interface: %s", err)
}
return nil
}
func resourceComputeInterfaceAttachV2AttachFunc(
computeClient *gophercloud.ServiceClient, instanceId, attachmentId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
va, err := attachinterfaces.Get(computeClient, instanceId, attachmentId).Extract()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok {
return va, "ATTACHING", nil
}
return va, "", err
}
return va, "ATTACHED", nil
}
}
func resourceComputeInterfaceAttachV2DetachFunc(
computeClient *gophercloud.ServiceClient, instanceId, attachmentId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
log.Printf("[DEBUG] Attempting to detach OpenStack interface %s from instance %s",
attachmentId, instanceId)
va, err := attachinterfaces.Get(computeClient, instanceId, attachmentId).Extract()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok {
return va, "DETACHED", nil
}
return va, "", err
}
err = attachinterfaces.Delete(computeClient, instanceId, attachmentId).ExtractErr()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok {
return va, "DETACHED", nil
}
if _, ok := err.(gophercloud.ErrDefault400); ok {
return nil, "", nil
}
return nil, "", err
}
log.Printf("[DEBUG] Interface Attachment (%s) is still active.", attachmentId)
return nil, "", nil
}
}
func parseComputeInterfaceAttachId(id string) (string, string, error) {
idParts := strings.Split(id, "/")
if len(idParts) < 2 {
return "", "", fmt.Errorf("Unable to determine interface attachment ID")
}
instanceId := idParts[0]
attachmentId := idParts[1]
return instanceId, attachmentId, nil
}