Skip to content

Commit

Permalink
chore: fix linter warnings
Browse files Browse the repository at this point in the history
Address all the linter warnings

Signed-off-by: Flavio Castelli <[email protected]>
  • Loading branch information
flavio committed Jan 27, 2023
1 parent 1b979dc commit 760f68e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
22 changes: 11 additions & 11 deletions pkg/info-service/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
16 changes: 12 additions & 4 deletions pkg/info-service/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
30 changes: 24 additions & 6 deletions susepubliccloud/data_source_susepubliccloud_images_ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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,
Expand Down Expand Up @@ -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
}

0 comments on commit 760f68e

Please sign in to comment.