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_compute_floatingip_v2.go
148 lines (123 loc) · 3.77 KB
/
resource_opentelekomcloud_compute_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
package opentelekomcloud
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/compute/v2/extensions/floatingips"
"time"
)
func resourceComputeFloatingIPV2() *schema.Resource {
return &schema.Resource{
Create: resourceComputeFloatingIPV2Create,
Read: resourceComputeFloatingIPV2Read,
Update: nil,
Delete: resourceComputeFloatingIPV2Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"pool": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "admin_external_net",
},
"address": {
Type: schema.TypeString,
Computed: true,
},
"fixed_ip": {
Type: schema.TypeString,
Computed: true,
},
"instance_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceComputeFloatingIPV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
computeClient, err := config.computeV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud compute client: %s", err)
}
createOpts := &floatingips.CreateOpts{
Pool: d.Get("pool").(string),
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
newFip, err := floatingips.Create(computeClient, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating Floating IP: %s", err)
}
d.SetId(newFip.ID)
return resourceComputeFloatingIPV2Read(d, meta)
}
func resourceComputeFloatingIPV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
computeClient, err := config.computeV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud compute client: %s", err)
}
fip, err := floatingips.Get(computeClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "floating ip")
}
log.Printf("[DEBUG] Retrieved Floating IP %s: %+v", d.Id(), fip)
d.Set("pool", fip.Pool)
d.Set("instance_id", fip.InstanceID)
d.Set("address", fip.IP)
d.Set("fixed_ip", fip.FixedIP)
d.Set("region", GetRegion(d, config))
return nil
}
func FloatingIPV2StateRefreshFunc(computeClient *golangsdk.ServiceClient, d *schema.ResourceData) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
s, err := floatingips.Get(computeClient, d.Id()).Extract()
if err != nil {
err = CheckDeleted(d, err, "Floating IP")
if err != nil {
return s, "", err
} else {
log.Printf("[DEBUG] Successfully deleted Floating IP %s", d.Id())
return s, "DELETED", nil
}
}
log.Printf("[DEBUG] Floating IP %s still active.\n", d.Id())
return s, "ACTIVE", nil
}
}
func resourceComputeFloatingIPV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
computeClient, err := config.computeV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud compute client: %s", err)
}
log.Printf("[DEBUG] Attempting to delete Floating IP %s.\n", d.Id())
err = floatingips.Delete(computeClient, d.Id()).ExtractErr()
if err != nil {
return err
}
stateConf := &resource.StateChangeConf{
Pending: []string{"ACTIVE"},
Target: []string{"DELETED"},
Refresh: FloatingIPV2StateRefreshFunc(computeClient, d),
Timeout: d.Timeout(schema.TimeoutDelete),
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("Error deleting OpenTelekomCloud Floating IP: %s", err)
}
return nil
}