This repository has been archived by the owner on Dec 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
resource_telefonicaopencloud_compute_floatingip_v2.go
148 lines (122 loc) · 3.92 KB
/
resource_telefonicaopencloud_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 telefonicaopencloud
import (
"fmt"
"log"
"time"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
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": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"pool": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("OS_POOL_NAME", nil),
},
"address": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"fixed_ip": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"instance_id": &schema.Schema{
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 TelefonicaOpenCloud 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 TelefonicaOpenCloud 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 *gophercloud.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 TelefonicaOpenCloud compute client: %s", err)
}
log.Printf("[DEBUG] Attempting to deleting Floating IP %s", d.Id())
if err := floatingips.Delete(computeClient, d.Id()).ExtractErr(); err != nil {
return fmt.Errorf("Error deleting Floating IP: %s", 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 TelefonicaOpenCloud Floating IP: %s", err)
}
return nil
}