forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_openstack_lb_member_v1.go
215 lines (180 loc) · 5.69 KB
/
resource_openstack_lb_member_v1.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
package openstack
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/lbaas/members"
)
func resourceLBMemberV1() *schema.Resource {
return &schema.Resource{
Create: resourceLBMemberV1Create,
Read: resourceLBMemberV1Read,
Update: resourceLBMemberV1Update,
Delete: resourceLBMemberV1Delete,
Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
DefaultFunc: envDefaultFuncAllowMissing("OS_REGION_NAME"),
},
"tenant_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"pool_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"address": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"port": &schema.Schema{
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"weight": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"admin_state_up": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
ForceNew: false,
Computed: true,
},
},
}
}
func resourceLBMemberV1Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
createOpts := members.CreateOpts{
TenantID: d.Get("tenant_id").(string),
PoolID: d.Get("pool_id").(string),
Address: d.Get("address").(string),
ProtocolPort: d.Get("port").(int),
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
m, err := members.Create(networkingClient, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating OpenStack LB member: %s", err)
}
log.Printf("[INFO] LB member ID: %s", m.ID)
log.Printf("[DEBUG] Waiting for OpenStack LB member (%s) to become available.", m.ID)
stateConf := &resource.StateChangeConf{
Pending: []string{"PENDING_CREATE"},
Target: []string{"ACTIVE"},
Refresh: waitForLBMemberActive(networkingClient, m.ID),
Timeout: 2 * time.Minute,
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return err
}
d.SetId(m.ID)
return resourceLBMemberV1Read(d, meta)
}
func resourceLBMemberV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
m, err := members.Get(networkingClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "LB member")
}
log.Printf("[DEBUG] Retreived OpenStack LB member %s: %+v", d.Id(), m)
d.Set("weight", m.Weight)
d.Set("admin_state_up", m.AdminStateUp)
return nil
}
func resourceLBMemberV1Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
var updateOpts members.UpdateOpts
if d.HasChange("admin_state_up") {
asu := d.Get("admin_state_up").(bool)
updateOpts.AdminStateUp = asu
}
log.Printf("[DEBUG] Updating LB member %s with options: %+v", d.Id(), updateOpts)
_, err = members.Update(networkingClient, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating OpenStack LB member: %s", err)
}
return resourceLBMemberV1Read(d, meta)
}
func resourceLBMemberV1Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(d.Get("region").(string))
if err != nil {
return fmt.Errorf("Error creating OpenStack networking client: %s", err)
}
err = members.Delete(networkingClient, d.Id()).ExtractErr()
if err != nil {
CheckDeleted(d, err, "LB member")
}
stateConf := &resource.StateChangeConf{
Pending: []string{"ACTIVE", "PENDING_DELETE"},
Target: []string{"DELETED"},
Refresh: waitForLBMemberDelete(networkingClient, d.Id()),
Timeout: 2 * time.Minute,
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error deleting OpenStack LB member: %s", err)
}
d.SetId("")
return nil
}
func waitForLBMemberActive(networkingClient *gophercloud.ServiceClient, memberId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
m, err := members.Get(networkingClient, memberId).Extract()
if err != nil {
return nil, "", err
}
log.Printf("[DEBUG] OpenStack LB member: %+v", m)
if m.Status == "ACTIVE" {
return m, "ACTIVE", nil
}
return m, m.Status, nil
}
}
func waitForLBMemberDelete(networkingClient *gophercloud.ServiceClient, memberId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
log.Printf("[DEBUG] Attempting to delete OpenStack LB member %s", memberId)
m, err := members.Get(networkingClient, memberId).Extract()
if err != nil {
errCode, ok := err.(*gophercloud.UnexpectedResponseCodeError)
if !ok {
return m, "ACTIVE", err
}
if errCode.Actual == 404 {
log.Printf("[DEBUG] Successfully deleted OpenStack LB member %s", memberId)
return m, "DELETED", nil
}
}
log.Printf("[DEBUG] OpenStack LB member %s still active.", memberId)
return m, "ACTIVE", nil
}
}