-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_source_viettelidc_networking_floatingip_v2.go
164 lines (129 loc) · 3.53 KB
/
data_source_viettelidc_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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package viettelidc
import (
"context"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
)
func dataSourceNetworkingFloatingIPV2() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceNetworkingFloatingIPV2Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"address": {
Type: schema.TypeString,
Optional: true,
},
"pool": {
Type: schema.TypeString,
Optional: true,
},
"port_id": {
Type: schema.TypeString,
Optional: true,
},
"tenant_id": {
Type: schema.TypeString,
Optional: true,
},
"fixed_ip": {
Type: schema.TypeString,
Optional: true,
},
"status": {
Type: schema.TypeString,
Optional: true,
},
"tags": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"dns_name": {
Type: schema.TypeString,
Computed: true,
},
"dns_domain": {
Type: schema.TypeString,
Computed: true,
},
"all_tags": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceNetworkingFloatingIPV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}
listOpts := floatingips.ListOpts{}
if v, ok := d.GetOk("description"); ok {
listOpts.Description = v.(string)
}
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)
}
if v, ok := d.GetOk("status"); ok {
listOpts.Status = v.(string)
}
tags := networkingV2AttributesTags(d)
if len(tags) > 0 {
listOpts.Tags = strings.Join(tags, ",")
}
pages, err := floatingips.List(networkingClient, listOpts).AllPages()
if err != nil {
return diag.Errorf("Unable to list openstack_networking_floatingips_v2: %s", err)
}
var allFloatingIPs []floatingIPExtended
err = floatingips.ExtractFloatingIPsInto(pages, &allFloatingIPs)
if err != nil {
return diag.Errorf("Unable to retrieve openstack_networking_floatingips_v2: %s", err)
}
if len(allFloatingIPs) < 1 {
return diag.Errorf("No openstack_networking_floatingip_v2 found")
}
if len(allFloatingIPs) > 1 {
return diag.Errorf("More than one openstack_networking_floatingip_v2 found")
}
fip := allFloatingIPs[0]
log.Printf("[DEBUG] Retrieved openstack_networking_floatingip_v2 %s: %+v", fip.ID, fip)
d.SetId(fip.ID)
d.Set("description", fip.Description)
d.Set("address", fip.FloatingIP.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("all_tags", fip.Tags)
d.Set("dns_name", fip.DNSName)
d.Set("dns_domain", fip.DNSDomain)
d.Set("region", GetRegion(d, config))
return nil
}