-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_openstack_networking_floatingip_associate_v2.go
139 lines (110 loc) · 3.64 KB
/
resource_openstack_networking_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
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
package openstack
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
)
func resourceNetworkingFloatingIPAssociateV2() *schema.Resource {
return &schema.Resource{
Create: resourceNetworkingFloatingIPAssociateV2Create,
Read: resourceNetworkingFloatingIPAssociateV2Read,
Delete: resourceNetworkingFloatingIPAssociateV2Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"floating_ip": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"port_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceNetworkingFloatingIPAssociateV2Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack network client: %s", err)
}
floatingIP := d.Get("floating_ip").(string)
portID := d.Get("port_id").(string)
floatingIPID, err := resourceNetworkingFloatingIPAssociateV2IP2ID(networkingClient, floatingIP)
if err != nil {
return fmt.Errorf("Unable to get ID of floating IP: %s", err)
}
updateOpts := floatingips.UpdateOpts{
PortID: &portID,
}
log.Printf("[DEBUG] Floating IP Associate Create Options: %#v", updateOpts)
_, err = floatingips.Update(networkingClient, floatingIPID, updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error associating floating IP %s to port %s: %s",
floatingIPID, portID, err)
}
d.SetId(floatingIPID)
return resourceNetworkFloatingIPV2Read(d, meta)
}
func resourceNetworkingFloatingIPAssociateV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack network client: %s", err)
}
floatingIP, err := floatingips.Get(networkingClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "floating IP")
}
d.Set("floating_ip", floatingIP.FloatingIP)
d.Set("port_id", floatingIP.PortID)
d.Set("region", GetRegion(d, config))
return nil
}
func resourceNetworkingFloatingIPAssociateV2Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenStack network client: %s", err)
}
portID := d.Get("port_id").(string)
updateOpts := floatingips.UpdateOpts{
PortID: nil,
}
log.Printf("[DEBUG] Floating IP Delete Options: %#v", updateOpts)
_, err = floatingips.Update(networkingClient, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error disassociating floating IP %s from port %s: %s",
d.Id(), portID, err)
}
return nil
}
func resourceNetworkingFloatingIPAssociateV2IP2ID(client *gophercloud.ServiceClient, floatingIP string) (string, error) {
listOpts := floatingips.ListOpts{
FloatingIP: floatingIP,
}
allPages, err := floatingips.List(client, listOpts).AllPages()
if err != nil {
return "", err
}
allFloatingIPs, err := floatingips.ExtractFloatingIPs(allPages)
if err != nil {
return "", err
}
if len(allFloatingIPs) != 1 {
return "", fmt.Errorf("unable to determine the ID of %s", floatingIP)
}
return allFloatingIPs[0].ID, nil
}