Skip to content

Commit

Permalink
refactor: verify low confidence
Browse files Browse the repository at this point in the history
  • Loading branch information
jooola committed Jan 10, 2025
1 parent 1fdb51e commit 1699bef
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 103 deletions.
22 changes: 11 additions & 11 deletions internal/firewall/attachment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"fmt"
"log"
"sort"
"strconv"

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

"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util/hcloudutil"
)

Expand Down Expand Up @@ -170,21 +170,21 @@ func deleteAttachment(ctx context.Context, d *schema.ResourceData, m interface{}
}

type attachment struct {
FirewallID int
ServerIDs []int
FirewallID int64
ServerIDs []int64
LabelSelectors []string
}

// FromResourceData copies the contents of d into a
func (a *attachment) FromResourceData(d *schema.ResourceData) error {
// The terraform schema definition above ensures this is always set and
// of the correct type. Thus there is no need to check such things.
a.FirewallID = d.Get("firewall_id").(int)
a.FirewallID = util.CastInt64(d.Get("firewall_id"))

srvIDs, ok := d.GetOk("server_ids")
if ok {
for _, v := range srvIDs.(*schema.Set).List() {
a.ServerIDs = append(a.ServerIDs, v.(int))
a.ServerIDs = append(a.ServerIDs, util.CastInt64(v))
}
sort.Slice(a.ServerIDs, func(i, j int) bool {
return a.ServerIDs[i] < a.ServerIDs[j]
Expand Down Expand Up @@ -216,7 +216,7 @@ func (a *attachment) ToResourceData(d *schema.ResourceData) {
if len(a.ServerIDs) > 0 {
vals := make([]interface{}, len(a.ServerIDs))
for i, id := range a.ServerIDs {
vals[i] = id
vals[i] = int(id)
}
f := d.Get("server_ids").(*schema.Set).F // Returns a default value if server_ids is not present in HCL.
srvIDs = schema.NewSet(f, vals)
Expand All @@ -233,8 +233,8 @@ func (a *attachment) ToResourceData(d *schema.ResourceData) {
}
d.Set("label_selectors", lSels)

d.Set("firewall_id", a.FirewallID)
d.SetId(strconv.Itoa(a.FirewallID))
d.Set("firewall_id", int(a.FirewallID))
d.SetId(util.FormatID(a.FirewallID))
}

// FromFirewall reads the attachment data from fw into a.
Expand Down Expand Up @@ -285,7 +285,7 @@ func (a *attachment) AllResources() []hcloud.FirewallResource {
func (a *attachment) DiffResources(o attachment) ([]hcloud.FirewallResource, []hcloud.FirewallResource) {
var more, less []hcloud.FirewallResource // nolint: prealloc

aSrvs := make(map[int]bool, len(a.ServerIDs))
aSrvs := make(map[int64]bool, len(a.ServerIDs))
for _, id := range a.ServerIDs {
aSrvs[id] = true
}
Expand All @@ -307,7 +307,7 @@ func (a *attachment) DiffResources(o attachment) ([]hcloud.FirewallResource, []h
less = append(less, labelSelectorResource(ls))
}

oSrvs := make(map[int]bool, len(o.ServerIDs))
oSrvs := make(map[int64]bool, len(o.ServerIDs))
for _, id := range o.ServerIDs {
oSrvs[id] = true
}
Expand All @@ -332,7 +332,7 @@ func (a *attachment) DiffResources(o attachment) ([]hcloud.FirewallResource, []h
return less, more
}

func serverResource(id int) hcloud.FirewallResource {
func serverResource(id int64) hcloud.FirewallResource {
return hcloud.FirewallResource{
Type: hcloud.FirewallResourceTypeServer,
Server: &hcloud.FirewallResourceServer{ID: id},
Expand Down
34 changes: 17 additions & 17 deletions internal/firewall/attachment_resource_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package firewall

import (
"strconv"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util"
)

func TestAttachment_FromResourceData(t *testing.T) {
Expand All @@ -27,7 +27,7 @@ func TestAttachment_FromResourceData(t *testing.T) {
},
att: attachment{
FirewallID: 4711,
ServerIDs: []int{1, 2, 3},
ServerIDs: []int64{1, 2, 3},
LabelSelectors: []string{"key1=value1", "key2=value2"},
},
},
Expand All @@ -39,7 +39,7 @@ func TestAttachment_FromResourceData(t *testing.T) {
},
att: attachment{
FirewallID: 4712,
ServerIDs: []int{4, 5, 6},
ServerIDs: []int64{4, 5, 6},
},
},
{
Expand Down Expand Up @@ -94,15 +94,15 @@ func TestAttachment_ToResourceData(t *testing.T) {
name: "server_ids and label_selectors present",
att: attachment{
FirewallID: 4711,
ServerIDs: []int{1, 2, 3},
ServerIDs: []int64{1, 2, 3},
LabelSelectors: []string{"key1=value1", "key2=value2"},
},
},
{
name: "only server_ids present",
att: attachment{
FirewallID: 4712,
ServerIDs: []int{4, 5, 6},
ServerIDs: []int64{4, 5, 6},
},
},
{
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestAttachment_ToResourceData(t *testing.T) {
},
att: attachment{
FirewallID: 4714,
ServerIDs: []int{1, 2, 3},
ServerIDs: []int64{1, 2, 3},
},
},
}
Expand All @@ -143,16 +143,16 @@ func TestAttachment_ToResourceData(t *testing.T) {

tt.att.ToResourceData(data)

assert.Equal(t, data.Id(), strconv.Itoa(tt.att.FirewallID))
assert.Equal(t, data.Get("firewall_id"), tt.att.FirewallID)
assert.Equal(t, data.Id(), util.FormatID(tt.att.FirewallID))
assert.Equal(t, data.Get("firewall_id"), int(tt.att.FirewallID))

srvIDdata, ok := data.GetOk("server_ids")
if len(tt.att.ServerIDs) > 0 {
assert.True(t, ok, "expected data to contain server_ids")

// Need to iterate as the types of the slices don't match: []int vs []interface{}
for _, id := range tt.att.ServerIDs {
assert.Contains(t, srvIDdata.(*schema.Set).List(), id)
assert.Contains(t, srvIDdata.(*schema.Set).List(), int(id))
}
} else {
assert.False(t, ok, "expected no server_ids in data")
Expand Down Expand Up @@ -195,7 +195,7 @@ func TestAttachment_FromFirewall(t *testing.T) {
},
att: attachment{
FirewallID: 4712,
ServerIDs: []int{1, 2},
ServerIDs: []int64{1, 2},
},
},
{
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestAttachment_AllResources(t *testing.T) {
name: "servers and label selectors attached",
att: attachment{
FirewallID: 4712,
ServerIDs: []int{1, 2},
ServerIDs: []int64{1, 2},
LabelSelectors: []string{"key1=value1", "key2=value2"},
},
res: []hcloud.FirewallResource{
Expand Down Expand Up @@ -290,25 +290,25 @@ func TestAttachment_DiffResources(t *testing.T) {
name: "nothing changed",
att: attachment{
FirewallID: 4711,
ServerIDs: []int{1, 2, 3},
ServerIDs: []int64{1, 2, 3},
LabelSelectors: []string{"key1=value1", "key2=value2"},
},
other: attachment{
FirewallID: 4711,
ServerIDs: []int{1, 2, 3},
ServerIDs: []int64{1, 2, 3},
LabelSelectors: []string{"key1=value1", "key2=value2"},
},
},
{
name: "resources in att but not in other",
att: attachment{
FirewallID: 4711,
ServerIDs: []int{1, 2, 3},
ServerIDs: []int64{1, 2, 3},
LabelSelectors: []string{"key1=value1", "key2=value2"},
},
other: attachment{
FirewallID: 4711,
ServerIDs: []int{1, 2},
ServerIDs: []int64{1, 2},
LabelSelectors: []string{"key1=value1"},
},
more: []hcloud.FirewallResource{
Expand All @@ -320,12 +320,12 @@ func TestAttachment_DiffResources(t *testing.T) {
name: "resources in other but not in att",
att: attachment{
FirewallID: 4711,
ServerIDs: []int{1, 2},
ServerIDs: []int64{1, 2},
LabelSelectors: []string{"key1=value1"},
},
other: attachment{
FirewallID: 4711,
ServerIDs: []int{1, 2, 3},
ServerIDs: []int64{1, 2, 3},
LabelSelectors: []string{"key1=value1", "key2=value2"},
},
less: []hcloud.FirewallResource{
Expand Down
Loading

0 comments on commit 1699bef

Please sign in to comment.