This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_opentelekomcloud_networking_floatingip_v2.go
234 lines (197 loc) · 6.39 KB
/
resource_opentelekomcloud_networking_floatingip_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
package opentelekomcloud
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/layer3/floatingips"
)
const (
PoolID = "0a2228f2-7f8a-45f1-8e09-9039e1d09975"
PoolName = "admin_external_net"
)
func resourceNetworkingFloatingIPV2() *schema.Resource {
return &schema.Resource{
Create: resourceNetworkFloatingIPV2Create,
Read: resourceNetworkFloatingIPV2Read,
Update: resourceNetworkFloatingIPV2Update,
Delete: resourceNetworkFloatingIPV2Delete,
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": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"address": {
Type: schema.TypeString,
Computed: true,
},
"pool": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "admin_external_net",
ValidateFunc: validation.StringInSlice([]string{
"admin_external_net",
}, true),
},
"port_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"fixed_ip": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"value_specs": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceNetworkFloatingIPV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud network client: %s", err)
}
createOpts := FloatingIPCreateOpts{
floatingips.CreateOpts{
FloatingNetworkID: PoolID,
PortID: d.Get("port_id").(string),
TenantID: d.Get("tenant_id").(string),
FixedIP: d.Get("fixed_ip").(string),
},
MapValueSpecs(d),
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
floatingIP, err := floatingips.Create(networkingClient, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error allocating floating IP: %s", err)
}
log.Printf("[DEBUG] Waiting for OpenTelekomCloud Neutron Floating IP (%s) to become available.", floatingIP.ID)
stateConf := &resource.StateChangeConf{
Target: []string{"ACTIVE"},
Refresh: waitForFloatingIPActive(networkingClient, floatingIP.ID),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
d.SetId(floatingIP.ID)
return resourceNetworkFloatingIPV2Read(d, meta)
}
func resourceNetworkFloatingIPV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud network client: %s", err)
}
floatingIP, err := floatingips.Get(networkingClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "floating IP")
}
d.Set("address", floatingIP.FloatingIP)
d.Set("port_id", floatingIP.PortID)
d.Set("fixed_ip", floatingIP.FixedIP)
d.Set("tenant_id", floatingIP.TenantID)
d.Set("pool", PoolName)
d.Set("region", GetRegion(d, config))
return nil
}
func resourceNetworkFloatingIPV2Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud network client: %s", err)
}
var updateOpts floatingips.UpdateOpts
if d.HasChange("port_id") {
portID := d.Get("port_id").(string)
updateOpts.PortID = &portID
}
log.Printf("[DEBUG] Update Options: %#v", updateOpts)
_, err = floatingips.Update(networkingClient, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating floating IP: %s", err)
}
return resourceNetworkFloatingIPV2Read(d, meta)
}
func resourceNetworkFloatingIPV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud network client: %s", err)
}
stateConf := &resource.StateChangeConf{
Pending: []string{"ACTIVE"},
Target: []string{"DELETED"},
Refresh: waitForFloatingIPDelete(networkingClient, d.Id()),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 5 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error deleting OpenTelekomCloud Neutron Floating IP: %s", err)
}
d.SetId("")
return nil
}
func waitForFloatingIPActive(networkingClient *golangsdk.ServiceClient, fId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
f, err := floatingips.Get(networkingClient, fId).Extract()
if err != nil {
return nil, "", err
}
log.Printf("[DEBUG] OpenTelekomCloud Neutron Floating IP: %+v", f)
if f.Status == "DOWN" || f.Status == "ACTIVE" {
return f, "ACTIVE", nil
}
return f, "", nil
}
}
func waitForFloatingIPDelete(networkingClient *golangsdk.ServiceClient, fId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
log.Printf("[DEBUG] Attempting to delete OpenTelekomCloud Floating IP %s.\n", fId)
f, err := floatingips.Get(networkingClient, fId).Extract()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
log.Printf("[DEBUG] Successfully deleted OpenTelekomCloud Floating IP %s", fId)
return f, "DELETED", nil
}
return f, "ACTIVE", err
}
err = floatingips.Delete(networkingClient, fId).ExtractErr()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
log.Printf("[DEBUG] Successfully deleted OpenTelekomCloud Floating IP %s", fId)
return f, "DELETED", nil
}
return f, "ACTIVE", err
}
log.Printf("[DEBUG] OpenTelekomCloud Floating IP %s still active.\n", fId)
return f, "ACTIVE", nil
}
}