From 760f68ea8204f108c9fec1832d38fb16f6fec10d Mon Sep 17 00:00:00 2001 From: Flavio Castelli Date: Fri, 27 Jan 2023 10:17:42 +0100 Subject: [PATCH] chore: fix linter warnings Address all the linter warnings Signed-off-by: Flavio Castelli --- pkg/info-service/images.go | 22 +++++++------- pkg/info-service/images_test.go | 16 +++++++--- .../data_source_susepubliccloud_images_ids.go | 30 +++++++++++++++---- 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/pkg/info-service/images.go b/pkg/info-service/images.go index c1ab735..9313b39 100644 --- a/pkg/info-service/images.go +++ b/pkg/info-service/images.go @@ -14,17 +14,17 @@ import ( // Image describes an object returned by // https://susepubliccloudinfo.suse.com/VERSION/FRAMEWORK/REGION/images.json // -// { -// "name": "suse-sles-15-sp1-v20190624-hvm-ssd-x86_64", -// "state": "active", -// "replacementname": "", -// "replacementid": "", -// "publishedon": "20190624", -// "deprecatedon": "", -// "region": "eu-central-1", -// "id": "ami-0352b14942c00b04b", -// "deletedon": "" -// }, +// { +// "name": "suse-sles-15-sp1-v20190624-hvm-ssd-x86_64", +// "state": "active", +// "replacementname": "", +// "replacementid": "", +// "publishedon": "20190624", +// "deprecatedon": "", +// "region": "eu-central-1", +// "id": "ami-0352b14942c00b04b", +// "deletedon": "" +// }, type Image struct { Name string `json:"name"` State string `jsong:"state"` diff --git a/pkg/info-service/images_test.go b/pkg/info-service/images_test.go index 319f093..5b2ab1a 100644 --- a/pkg/info-service/images_test.go +++ b/pkg/info-service/images_test.go @@ -35,7 +35,9 @@ func TestValidRequestForActiveImages(t *testing.T) { return } defer file.Close() - io.Copy(w, file) + if _, err := io.Copy(w, file); err != nil { + t.Fatalf("unexpected error %v", err) + } })) defer ts.Close() @@ -77,7 +79,9 @@ func TestFilterImages(t *testing.T) { return } defer file.Close() - io.Copy(w, file) + if _, err := io.Copy(w, file); err != nil { + t.Fatalf("unexpected error %v", err) + } })) defer ts.Close() @@ -120,7 +124,9 @@ func TestSortAscendingImages(t *testing.T) { return } defer file.Close() - io.Copy(w, file) + if _, err := io.Copy(w, file); err != nil { + t.Fatalf("unexpected error %v", err) + } })) defer ts.Close() @@ -174,7 +180,9 @@ func TestSortDescendingImages(t *testing.T) { return } defer file.Close() - io.Copy(w, file) + if _, err := io.Copy(w, file); err != nil { + t.Fatalf("unexpected error %v", err) + } })) defer ts.Close() diff --git a/susepubliccloud/data_source_susepubliccloud_images_ids.go b/susepubliccloud/data_source_susepubliccloud_images_ids.go index 6da3d9c..3094959 100644 --- a/susepubliccloud/data_source_susepubliccloud_images_ids.go +++ b/susepubliccloud/data_source_susepubliccloud_images_ids.go @@ -2,10 +2,10 @@ package susepubliccloud import ( "fmt" + "hash/crc32" "log" - "github.com/SUSE/terraform-provider-susepubliccloud/pkg/info-service" - "github.com/hashicorp/terraform-plugin-sdk/helper/hashcode" + images "github.com/SUSE/terraform-provider-susepubliccloud/pkg/info-service" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/helper/validation" ) @@ -18,7 +18,7 @@ func dataSourceSUSEPublicCloudImageIDs() *schema.Resource { Type: schema.TypeString, Optional: true, ForceNew: true, - ValidateFunc: validation.ValidateRegexp, + ValidateFunc: validation.StringIsValidRegExp, }, "cloud": { Type: schema.TypeString, @@ -102,8 +102,26 @@ func dataSourceSUSEPublicCloudImageIDsRead(d *schema.ResourceData, meta interfac imageIDs = append(imageIDs, image.ID) } - d.SetId(fmt.Sprintf("%d", hashcode.String(fmt.Sprintf("%+v", params)))) - d.Set("ids", imageIDs) + d.SetId(fmt.Sprintf("%d", stringTohashcode(fmt.Sprintf("%+v", params)))) + return d.Set("ids", imageIDs) +} - return nil +// String hashes a string to a unique hashcode. +// +// Copied from hashicorp/terraform-plugin-sdk/helper/hashcode/hashcode.go +// Because this is going to be dropped in future releases of the library +// +// crc32 returns a uint32, but for our use we need +// and non negative integer. Here we cast to an integer +// and invert it if the result is negative. +func stringTohashcode(s string) int { + v := int(crc32.ChecksumIEEE([]byte(s))) + if v >= 0 { + return v + } + if -v >= 0 { + return -v + } + // v == MinInt + return 0 }