Skip to content

Commit

Permalink
refactor: add data source ListID util
Browse files Browse the repository at this point in the history
  • Loading branch information
jooola committed Sep 6, 2024
1 parent ed5156b commit fbe8c70
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 50 deletions.
4 changes: 1 addition & 3 deletions internal/certificate/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
5 changes: 2 additions & 3 deletions internal/datacenter/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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...)
Expand Down
5 changes: 1 addition & 4 deletions internal/firewall/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 1 addition & 4 deletions internal/floatingip/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
5 changes: 1 addition & 4 deletions internal/image/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ package image

import (
"context"
"crypto/sha1"
"fmt"
"log"
"sort"
"strconv"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 1 addition & 4 deletions internal/loadbalancer/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package loadbalancer

import (
"context"
"crypto/sha1"
"fmt"
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

Expand Down Expand Up @@ -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
}
6 changes: 2 additions & 4 deletions internal/location/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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...)
Expand Down
5 changes: 1 addition & 4 deletions internal/network/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package network

import (
"context"
"crypto/sha1"
"fmt"
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

Expand Down Expand Up @@ -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
}
5 changes: 1 addition & 4 deletions internal/placementgroup/data_source.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 1 addition & 4 deletions internal/primaryip/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
5 changes: 1 addition & 4 deletions internal/server/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package server

import (
"context"
"crypto/sha1"
"fmt"
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

Expand Down Expand Up @@ -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
}
6 changes: 2 additions & 4 deletions internal/servertype/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package servertype

import (
"context"
"crypto/sha1"
"fmt"
"strconv"
"strings"
"time"

"github.com/hetznercloud/terraform-provider-hcloud/internal/deprecation"

"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"
)

Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions internal/util/datasourceutil/id.go
Original file line number Diff line number Diff line change
@@ -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))
}
19 changes: 19 additions & 0 deletions internal/util/datasourceutil/id_test.go
Original file line number Diff line number Diff line change
@@ -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"}, "")))))
}
5 changes: 1 addition & 4 deletions internal/volume/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package volume

import (
"context"
"crypto/sha1"
"fmt"
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

Expand Down Expand Up @@ -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
}

0 comments on commit fbe8c70

Please sign in to comment.