This repository has been archived by the owner on Nov 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
compute_floatingip_associate_v2.go
98 lines (79 loc) · 2.52 KB
/
compute_floatingip_associate_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
package openstack
import (
"fmt"
"strings"
"github.com/hashicorp/terraform/helper/resource"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
nfloatingips "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
)
func parseComputeFloatingIPAssociateId(id string) (string, string, string, error) {
idParts := strings.Split(id, "/")
if len(idParts) < 3 {
return "", "", "", fmt.Errorf("Unable to determine floating ip association ID")
}
floatingIP := idParts[0]
instanceId := idParts[1]
fixedIP := idParts[2]
return floatingIP, instanceId, fixedIP, nil
}
func computeFloatingIPAssociateV2NetworkExists(networkClient *gophercloud.ServiceClient, floatingIP string) (bool, error) {
listOpts := nfloatingips.ListOpts{
FloatingIP: floatingIP,
}
allPages, err := nfloatingips.List(networkClient, listOpts).AllPages()
if err != nil {
return false, err
}
allFips, err := nfloatingips.ExtractFloatingIPs(allPages)
if err != nil {
return false, err
}
if len(allFips) > 1 {
return false, fmt.Errorf("more than one floating IP with %s address found", floatingIP)
}
if len(allFips) == 0 {
return false, nil
}
return true, nil
}
func computeFloatingIPAssociateV2ComputeExists(computeClient *gophercloud.ServiceClient, floatingIP string) (bool, error) {
// If the Network API isn't available, fall back to the deprecated Compute API.
allPages, err := floatingips.List(computeClient).AllPages()
if err != nil {
return false, err
}
allFips, err := floatingips.ExtractFloatingIPs(allPages)
if err != nil {
return false, err
}
for _, f := range allFips {
if f.IP == floatingIP {
return true, nil
}
}
return false, nil
}
func computeFloatingIPAssociateV2CheckAssociation(
computeClient *gophercloud.ServiceClient, instanceId, floatingIP string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
instance, err := servers.Get(computeClient, instanceId).Extract()
if err != nil {
return instance, "", err
}
var associated bool
for _, networkAddresses := range instance.Addresses {
for _, element := range networkAddresses.([]interface{}) {
address := element.(map[string]interface{})
if address["OS-EXT-IPS:type"] == "floating" && address["addr"] == floatingIP {
associated = true
}
}
}
if associated {
return instance, "ASSOCIATED", nil
}
return instance, "NOT_ASSOCIATED", nil
}
}