From fbe8c70dfc6a377baacbec4f5eb0cc9164abfdd5 Mon Sep 17 00:00:00 2001 From: jo Date: Fri, 6 Sep 2024 15:40:30 +0200 Subject: [PATCH] refactor: add data source ListID util --- internal/certificate/data_source.go | 4 +--- internal/datacenter/data_source.go | 5 ++--- internal/firewall/data_source.go | 5 +---- internal/floatingip/data_source.go | 5 +---- internal/image/data_source.go | 5 +---- internal/loadbalancer/data_source.go | 5 +---- internal/location/data_source.go | 6 ++---- internal/network/data_source.go | 5 +---- internal/placementgroup/data_source.go | 5 +---- internal/primaryip/data_source.go | 5 +---- internal/server/data_source.go | 5 +---- internal/servertype/data_source.go | 6 ++---- internal/util/datasourceutil/id.go | 14 ++++++++++++++ internal/util/datasourceutil/id_test.go | 19 +++++++++++++++++++ internal/volume/data_source.go | 5 +---- 15 files changed, 49 insertions(+), 50 deletions(-) create mode 100644 internal/util/datasourceutil/id.go create mode 100644 internal/util/datasourceutil/id_test.go diff --git a/internal/certificate/data_source.go b/internal/certificate/data_source.go index c02a214f9..b32bc7391 100644 --- a/internal/certificate/data_source.go +++ b/internal/certificate/data_source.go @@ -2,10 +2,8 @@ package certificate import ( "context" - "crypto/sha1" "fmt" "strconv" - "strings" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -178,7 +176,7 @@ func dataSourceHcloudCertificateListRead(ctx context.Context, d *schema.Resource tfCertificates[i] = getCertificateAttributes(certificate) } d.Set("certificates", tfCertificates) - d.SetId(fmt.Sprintf("%x", sha1.Sum([]byte(strings.Join(ids, ""))))) + d.SetId(datasourceutil.ListID(ids)) return nil } diff --git a/internal/datacenter/data_source.go b/internal/datacenter/data_source.go index 13627b03c..aaa06e75f 100644 --- a/internal/datacenter/data_source.go +++ b/internal/datacenter/data_source.go @@ -2,12 +2,10 @@ package datacenter import ( "context" - "crypto/sha1" _ "embed" "fmt" "sort" "strconv" - "strings" "github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator" "github.com/hashicorp/terraform-plugin-framework/attr" @@ -18,6 +16,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hetznercloud/hcloud-go/hcloud" + "github.com/hetznercloud/terraform-provider-hcloud/internal/util/datasourceutil" "github.com/hetznercloud/terraform-provider-hcloud/internal/util/hcloudutil" ) @@ -305,7 +304,7 @@ func newResourceDataList(ctx context.Context, in []*hcloud.Datacenter) (resource datacenters[i] = datacenter } - data.ID = types.StringValue(fmt.Sprintf("%x", sha1.Sum([]byte(strings.Join(datacenterIDs, ""))))) + data.ID = types.StringValue(datasourceutil.ListID(datacenterIDs)) data.DatacenterIDs, newDiags = types.ListValueFrom(ctx, types.StringType, datacenterIDs) diags.Append(newDiags...) diff --git a/internal/firewall/data_source.go b/internal/firewall/data_source.go index c4fadd4e6..45820bc2a 100644 --- a/internal/firewall/data_source.go +++ b/internal/firewall/data_source.go @@ -2,12 +2,9 @@ package firewall import ( "context" - "crypto/sha1" - "fmt" "log" "sort" "strconv" - "strings" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -212,7 +209,7 @@ func dataSourceHcloudFirewallListRead(ctx context.Context, d *schema.ResourceDat tfFirewalls[i] = getFirewallAttributes(firewall) } d.Set("firewalls", tfFirewalls) - d.SetId(fmt.Sprintf("%x", sha1.Sum([]byte(strings.Join(ids, ""))))) + d.SetId(datasourceutil.ListID(ids)) return nil } diff --git a/internal/floatingip/data_source.go b/internal/floatingip/data_source.go index 0d769d205..0a8c5a1c0 100644 --- a/internal/floatingip/data_source.go +++ b/internal/floatingip/data_source.go @@ -2,10 +2,7 @@ package floatingip import ( "context" - "crypto/sha1" - "fmt" "strconv" - "strings" "github.com/hetznercloud/terraform-provider-hcloud/internal/util/datasourceutil" "github.com/hetznercloud/terraform-provider-hcloud/internal/util/hcloudutil" @@ -210,7 +207,7 @@ func dataSourceHcloudFloatingIPListRead(ctx context.Context, d *schema.ResourceD tfIPs[i] = getFloatingIPAttributes(ip) } d.Set("floating_ips", tfIPs) - d.SetId(fmt.Sprintf("%x", sha1.Sum([]byte(strings.Join(ids, ""))))) + d.SetId(datasourceutil.ListID(ids)) return nil } diff --git a/internal/image/data_source.go b/internal/image/data_source.go index aec77571b..299e5fc60 100644 --- a/internal/image/data_source.go +++ b/internal/image/data_source.go @@ -2,12 +2,9 @@ package image import ( "context" - "crypto/sha1" - "fmt" "log" "sort" "strconv" - "strings" "time" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -274,7 +271,7 @@ func dataSourceHcloudImageListRead(ctx context.Context, d *schema.ResourceData, tfImages[i] = getImageAttributes(image) } d.Set("images", tfImages) - d.SetId(fmt.Sprintf("%x", sha1.Sum([]byte(strings.Join(ids, ""))))) + d.SetId(datasourceutil.ListID(ids)) return nil } diff --git a/internal/loadbalancer/data_source.go b/internal/loadbalancer/data_source.go index ab9200b6f..bf33d6b16 100644 --- a/internal/loadbalancer/data_source.go +++ b/internal/loadbalancer/data_source.go @@ -2,10 +2,7 @@ package loadbalancer import ( "context" - "crypto/sha1" - "fmt" "strconv" - "strings" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -326,7 +323,7 @@ func dataSourceHcloudLoadBalancerListRead(ctx context.Context, d *schema.Resourc tfLoadBalancers[i] = getLoadBalancerAttributes(loadBalancer) } d.Set("load_balancers", tfLoadBalancers) - d.SetId(fmt.Sprintf("%x", sha1.Sum([]byte(strings.Join(ids, ""))))) + d.SetId(datasourceutil.ListID(ids)) return nil } diff --git a/internal/location/data_source.go b/internal/location/data_source.go index fcd9e3854..5161cf98f 100644 --- a/internal/location/data_source.go +++ b/internal/location/data_source.go @@ -2,11 +2,8 @@ package location import ( "context" - "crypto/sha1" _ "embed" - "fmt" "strconv" - "strings" "github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator" "github.com/hashicorp/terraform-plugin-framework/attr" @@ -17,6 +14,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hetznercloud/hcloud-go/hcloud" + "github.com/hetznercloud/terraform-provider-hcloud/internal/util/datasourceutil" "github.com/hetznercloud/terraform-provider-hcloud/internal/util/hcloudutil" ) @@ -287,7 +285,7 @@ func newResourceDataList(ctx context.Context, in []*hcloud.Location) (resourceDa locations[i] = location } - data.ID = types.StringValue(fmt.Sprintf("%x", sha1.Sum([]byte(strings.Join(locationIDs, ""))))) + data.ID = types.StringValue(datasourceutil.ListID(locationIDs)) data.LocationIDs, newDiags = types.ListValueFrom(ctx, types.StringType, locationIDs) diags.Append(newDiags...) diff --git a/internal/network/data_source.go b/internal/network/data_source.go index 9028c26d0..7e5e1f77f 100644 --- a/internal/network/data_source.go +++ b/internal/network/data_source.go @@ -2,10 +2,7 @@ package network import ( "context" - "crypto/sha1" - "fmt" "strconv" - "strings" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -172,7 +169,7 @@ func dataSourceHcloudNetworkListRead(ctx context.Context, d *schema.ResourceData tsNetworks[i] = getNetworkAttributes(firewall) } d.Set("networks", tsNetworks) - d.SetId(fmt.Sprintf("%x", sha1.Sum([]byte(strings.Join(ids, ""))))) + d.SetId(datasourceutil.ListID(ids)) return nil } diff --git a/internal/placementgroup/data_source.go b/internal/placementgroup/data_source.go index 7307dd5a8..d47985d5e 100644 --- a/internal/placementgroup/data_source.go +++ b/internal/placementgroup/data_source.go @@ -1,12 +1,9 @@ package placementgroup import ( - "crypto/sha1" - "fmt" "log" "sort" "strconv" - "strings" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -170,7 +167,7 @@ func dataSourceHcloudPlacementGroupListRead(ctx context.Context, d *schema.Resou tfPlacementGroups[i] = getAttributes(firewall) } d.Set("placement_groups", tfPlacementGroups) - d.SetId(fmt.Sprintf("%x", sha1.Sum([]byte(strings.Join(ids, ""))))) + d.SetId(datasourceutil.ListID(ids)) return nil } diff --git a/internal/primaryip/data_source.go b/internal/primaryip/data_source.go index 1a0616bd4..c20bd3e81 100644 --- a/internal/primaryip/data_source.go +++ b/internal/primaryip/data_source.go @@ -2,10 +2,7 @@ package primaryip import ( "context" - "crypto/sha1" - "fmt" "strconv" - "strings" "github.com/hetznercloud/terraform-provider-hcloud/internal/util/datasourceutil" "github.com/hetznercloud/terraform-provider-hcloud/internal/util/hcloudutil" @@ -200,7 +197,7 @@ func dataSourceHcloudPrimaryIPListRead(ctx context.Context, d *schema.ResourceDa tfIPs[i] = getPrimaryIPAttributes(ip) } d.Set("primary_ips", tfIPs) - d.SetId(fmt.Sprintf("%x", sha1.Sum([]byte(strings.Join(ids, ""))))) + d.SetId(datasourceutil.ListID(ids)) return nil } diff --git a/internal/server/data_source.go b/internal/server/data_source.go index b4c49c201..465c4fb92 100644 --- a/internal/server/data_source.go +++ b/internal/server/data_source.go @@ -2,10 +2,7 @@ package server import ( "context" - "crypto/sha1" - "fmt" "strconv" - "strings" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -260,7 +257,7 @@ func dataSourceHcloudServerListRead(ctx context.Context, d *schema.ResourceData, tfServers[i] = getServerAttributes(d, server) } d.Set("servers", tfServers) - d.SetId(fmt.Sprintf("%x", sha1.Sum([]byte(strings.Join(ids, ""))))) + d.SetId(datasourceutil.ListID(ids)) return nil } diff --git a/internal/servertype/data_source.go b/internal/servertype/data_source.go index 11a19f856..3b0d3e9dd 100644 --- a/internal/servertype/data_source.go +++ b/internal/servertype/data_source.go @@ -2,10 +2,7 @@ package servertype import ( "context" - "crypto/sha1" - "fmt" "strconv" - "strings" "time" "github.com/hetznercloud/terraform-provider-hcloud/internal/deprecation" @@ -13,6 +10,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hetznercloud/hcloud-go/hcloud" + "github.com/hetznercloud/terraform-provider-hcloud/internal/util/datasourceutil" "github.com/hetznercloud/terraform-provider-hcloud/internal/util/hcloudutil" ) @@ -205,7 +203,7 @@ func dataSourceHcloudServerTypeListRead(ctx context.Context, d *schema.ResourceD tfServerTypes[i] = tfServerType } - d.SetId(fmt.Sprintf("%x", sha1.Sum([]byte(strings.Join(ids, ""))))) + d.SetId(datasourceutil.ListID(ids)) d.Set("server_type_ids", ids) d.Set("names", names) d.Set("descriptions", descriptions) diff --git a/internal/util/datasourceutil/id.go b/internal/util/datasourceutil/id.go new file mode 100644 index 000000000..dce2c6542 --- /dev/null +++ b/internal/util/datasourceutil/id.go @@ -0,0 +1,14 @@ +package datasourceutil + +import ( + "crypto/sha1" + "fmt" +) + +func ListID[T any](ids []T) string { + var b []byte + for _, id := range ids { + b = fmt.Append(b, id) + } + return fmt.Sprintf("%x", sha1.Sum(b)) +} diff --git a/internal/util/datasourceutil/id_test.go b/internal/util/datasourceutil/id_test.go new file mode 100644 index 000000000..966f3b586 --- /dev/null +++ b/internal/util/datasourceutil/id_test.go @@ -0,0 +1,19 @@ +package datasourceutil + +import ( + "crypto/sha1" + "fmt" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestListID(t *testing.T) { + expected := "40bd001563085fc35165329ea1ff5c5ecbdbbeef" + + assert.Equal(t, expected, ListID([]string{"1", "2", "3"})) + assert.Equal(t, expected, ListID([]int{1, 2, 3})) + // Regression test + assert.Equal(t, expected, fmt.Sprintf("%x", sha1.Sum([]byte(strings.Join([]string{"1", "2", "3"}, ""))))) +} diff --git a/internal/volume/data_source.go b/internal/volume/data_source.go index b7554a81c..e3fd1097c 100644 --- a/internal/volume/data_source.go +++ b/internal/volume/data_source.go @@ -2,10 +2,7 @@ package volume import ( "context" - "crypto/sha1" - "fmt" "strconv" - "strings" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -210,7 +207,7 @@ func dataSourceHcloudVolumeListRead(ctx context.Context, d *schema.ResourceData, tfVolume[i] = getVolumeAttributes(volume) } d.Set("volumes", tfVolume) - d.SetId(fmt.Sprintf("%x", sha1.Sum([]byte(strings.Join(ids, ""))))) + d.SetId(datasourceutil.ListID(ids)) return nil }