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
/
data_source_openstack_networking_floatingip_v2.go
107 lines (87 loc) · 2.37 KB
/
data_source_openstack_networking_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
package openstack
import (
"fmt"
"log"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceNetworkingFloatingIPV2() *schema.Resource {
return &schema.Resource{
Read: dataSourceNetworkingFloatingIPV2Read,
Schema: map[string]*schema.Schema{
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"pool": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"port_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"tenant_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"fixed_ip": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"status": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}
func dataSourceNetworkingFloatingIPV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
networkingClient, err := config.networkingV2Client(GetRegion(d, config))
listOpts := floatingips.ListOpts{}
if v, ok := d.GetOk("address"); ok {
listOpts.FloatingIP = v.(string)
}
if v, ok := d.GetOk("tenant_id"); ok {
listOpts.TenantID = v.(string)
}
if v, ok := d.GetOk("pool"); ok {
listOpts.FloatingNetworkID = v.(string)
}
if v, ok := d.GetOk("port_id"); ok {
listOpts.PortID = v.(string)
}
if v, ok := d.GetOk("fixed_ip"); ok {
listOpts.FixedIP = v.(string)
}
pages, err := floatingips.List(networkingClient, listOpts).AllPages()
if err != nil {
return fmt.Errorf("Unable to list Floating IPs: %s", err)
}
allFloatingIPs, err := floatingips.ExtractFloatingIPs(pages)
if err != nil {
return fmt.Errorf("Unable to retrieve Floating IPs: %s", err)
}
if len(allFloatingIPs) < 1 {
return fmt.Errorf("No Floating IP found")
}
if len(allFloatingIPs) > 1 {
return fmt.Errorf("More than one Floating IP found")
}
fip := allFloatingIPs[0]
log.Printf("[DEBUG] Retrieved Floating IP %s: %+v", fip.ID, fip)
d.SetId(fip.ID)
d.Set("address", fip.FloatingIP)
d.Set("pool", fip.FloatingNetworkID)
d.Set("port_id", fip.PortID)
d.Set("fixed_ip", fip.FixedIP)
d.Set("tenant_id", fip.TenantID)
d.Set("status", fip.Status)
d.Set("region", GetRegion(d, config))
return nil
}