@@ -2,76 +2,114 @@ package floatingip
22
33import (
44 "context"
5+ "crypto/sha1"
6+ "fmt"
7+ "strconv"
8+ "strings"
59
610 "github.com/hetznercloud/terraform-provider-hcloud/internal/hcclient"
11+ "github.com/hetznercloud/terraform-provider-hcloud/internal/util/datasourceutil"
712
813 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
914
1015 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1116 "github.com/hetznercloud/hcloud-go/hcloud"
1217)
1318
14- // DataSourceType is the type name of the Hetzner Cloud Floating IP resource.
15- const DataSourceType = "hcloud_floating_ip"
19+ const (
20+ // DataSourceType is the type name of the Hetzner Cloud Floating IP resource.
21+ DataSourceType = "hcloud_floating_ip"
22+
23+ // DataSourceListType is the type name to receive a list of Hetzner Cloud Floating IPs resources.
24+ DataSourceListType = "hcloud_floating_ips"
25+ )
26+
27+ // getCommonDataSchema returns a new common schema used by all floating ip data sources.
28+ func getCommonDataSchema () map [string ]* schema.Schema {
29+ return map [string ]* schema.Schema {
30+ "id" : {
31+ Type : schema .TypeInt ,
32+ Optional : true ,
33+ Computed : true ,
34+ },
35+ "name" : {
36+ Type : schema .TypeString ,
37+ Optional : true ,
38+ },
39+ "type" : {
40+ Type : schema .TypeString ,
41+ Computed : true ,
42+ },
43+ "description" : {
44+ Type : schema .TypeString ,
45+ Computed : true ,
46+ },
47+ "home_location" : {
48+ Type : schema .TypeString ,
49+ Computed : true ,
50+ },
51+ "server_id" : {
52+ Type : schema .TypeInt ,
53+ Computed : true ,
54+ },
55+ "ip_address" : {
56+ Type : schema .TypeString ,
57+ Optional : true ,
58+ Computed : true ,
59+ },
60+ "ip_network" : {
61+ Type : schema .TypeString ,
62+ Computed : true ,
63+ },
64+ "labels" : {
65+ Type : schema .TypeMap ,
66+ Computed : true ,
67+ },
68+ "delete_protection" : {
69+ Type : schema .TypeBool ,
70+ Computed : true ,
71+ },
72+ }
73+ }
1674
1775// DataSource creates a new Terraform schema for the hcloud_floating_ip data
1876// source.
1977func DataSource () * schema.Resource {
2078 return & schema.Resource {
2179 ReadContext : dataSourceHcloudFloatingIPRead ,
22- Schema : map [string ]* schema.Schema {
23- "id" : {
24- Type : schema .TypeInt ,
25- Optional : true ,
26- Computed : true ,
27- },
28- "name" : {
29- Type : schema .TypeString ,
30- Optional : true ,
31- },
32- "type" : {
33- Type : schema .TypeString ,
34- Computed : true ,
35- },
36- "description" : {
37- Type : schema .TypeString ,
38- Computed : true ,
39- },
40- "home_location" : {
41- Type : schema .TypeString ,
42- Computed : true ,
80+ Schema : datasourceutil .MergeSchema (
81+ getCommonDataSchema (),
82+ map [string ]* schema.Schema {
83+ "selector" : {
84+ Type : schema .TypeString ,
85+ Optional : true ,
86+ Deprecated : "Please use the with_selector property instead." ,
87+ ConflictsWith : []string {"with_selector" },
88+ },
89+ "with_selector" : {
90+ Type : schema .TypeString ,
91+ Optional : true ,
92+ ConflictsWith : []string {"selector" },
93+ },
4394 },
44- "server_id" : {
45- Type : schema .TypeInt ,
95+ ),
96+ }
97+ }
98+
99+ func DataSourceList () * schema.Resource {
100+ return & schema.Resource {
101+ ReadContext : dataSourceHcloudFloatingIPListRead ,
102+ Schema : map [string ]* schema.Schema {
103+ "floating_ips" : {
104+ Type : schema .TypeList ,
46105 Computed : true ,
106+ Elem : & schema.Resource {
107+ Schema : getCommonDataSchema (),
108+ },
47109 },
48- "ip_address " : {
110+ "with_selector " : {
49111 Type : schema .TypeString ,
50112 Optional : true ,
51- Computed : true ,
52- },
53- "ip_network" : {
54- Type : schema .TypeString ,
55- Computed : true ,
56- },
57- "labels" : {
58- Type : schema .TypeMap ,
59- Computed : true ,
60- },
61- "selector" : {
62- Type : schema .TypeString ,
63- Optional : true ,
64- Deprecated : "Please use the with_selector property instead." ,
65- ConflictsWith : []string {"with_selector" },
66- },
67- "with_selector" : {
68- Type : schema .TypeString ,
69- Optional : true ,
70- ConflictsWith : []string {"selector" },
71- },
72- "delete_protection" : {
73- Type : schema .TypeBool ,
74- Computed : true ,
75113 },
76114 },
77115 }
@@ -148,3 +186,31 @@ func dataSourceHcloudFloatingIPRead(ctx context.Context, d *schema.ResourceData,
148186
149187 return diag .Errorf ("please specify a id, ip_address or a selector to lookup the FloatingIP" )
150188}
189+
190+ func dataSourceHcloudFloatingIPListRead (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
191+ client := m .(* hcloud.Client )
192+
193+ selector := d .Get ("with_selector" ).(string )
194+
195+ var allIPs []* hcloud.FloatingIP
196+ opts := hcloud.FloatingIPListOpts {
197+ ListOpts : hcloud.ListOpts {
198+ LabelSelector : selector ,
199+ },
200+ }
201+ allIPs , err := client .FloatingIP .AllWithOpts (ctx , opts )
202+ if err != nil {
203+ return hcclient .ErrorToDiag (err )
204+ }
205+
206+ ids := make ([]string , len (allIPs ))
207+ tfIPs := make ([]map [string ]interface {}, len (allIPs ))
208+ for i , ip := range allIPs {
209+ ids [i ] = strconv .Itoa (ip .ID )
210+ tfIPs [i ] = getFloatingIPAttributes (ip )
211+ }
212+ d .Set ("floating_ips" , tfIPs )
213+ d .SetId (fmt .Sprintf ("%x" , sha1 .Sum ([]byte (strings .Join (ids , "" )))))
214+
215+ return nil
216+ }
0 commit comments