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
/
resource_openstack_networking_floatingip_associate_v2.go
118 lines (95 loc) · 3.41 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
package openstack
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"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": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"floating_ip": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"port_id": {
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)
fipID, err := networkingFloatingIPV2ID(networkingClient, floatingIP)
if err != nil {
return fmt.Errorf("Unable to get ID of openstack_networking_floatingip_v2: %s", err)
}
updateOpts := floatingips.UpdateOpts{
PortID: &portID,
}
log.Printf("[DEBUG] openstack_networking_floatingip_associate_v2 create options: %#v", updateOpts)
_, err = floatingips.Update(networkingClient, fipID, updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error associating openstack_networking_floatingip_v2 %s to openstack_networking_port_v2 %s: %s",
fipID, portID, err)
}
d.SetId(fipID)
log.Printf("[DEBUG] Created association between openstack_networking_floatingip_v2 %s and openstack_networking_port_v2 %s",
fipID, portID)
return resourceNetworkingFloatingIPAssociateV2Read(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)
}
fip, err := floatingips.Get(networkingClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "Error getting openstack_networking_floatingip_v2")
}
log.Printf("[DEBUG] Retrieved openstack_networking_floatingip_v2 %s: %#v", d.Id(), fip)
d.Set("floating_ip", fip.FloatingIP)
d.Set("port_id", fip.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: new(string),
}
log.Printf("[DEBUG] openstack_networking_floatingip_v2 disassociating options: %#v", updateOpts)
_, err = floatingips.Update(networkingClient, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error disassociating openstack_networking_floatingip_v2 %s from openstack_networking_port_v2 %s: %s",
d.Id(), portID, err)
}
return nil
}