Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jooola committed Jan 9, 2025
1 parent df3f20c commit 73b3094
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 177 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
24 changes: 12 additions & 12 deletions internal/loadbalancer/resource_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package loadbalancer_test

import (
"fmt"
"strconv"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand All @@ -14,6 +13,7 @@ import (
"github.com/hetznercloud/terraform-provider-hcloud/internal/teste2e"
"github.com/hetznercloud/terraform-provider-hcloud/internal/testsupport"
"github.com/hetznercloud/terraform-provider-hcloud/internal/testtemplate"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util"
)

func TestAccLoadBalancerServiceResource_TCP(t *testing.T) {
Expand Down Expand Up @@ -47,7 +47,7 @@ func TestAccLoadBalancerServiceResource_TCP(t *testing.T) {
testsupport.CheckResourceExists(lbResName, loadbalancer.ByID(t, &lb)),
testsupport.LiftTCF(hasService(&lb, 70)),
testsupport.CheckResourceAttrFunc(svcResName, "load_balancer_id", func() string {
return strconv.Itoa(lb.ID)
return util.FormatID(lb.ID)
}),
resource.TestCheckResourceAttr(svcResName, "protocol", "tcp"),
resource.TestCheckResourceAttr(svcResName, "listen_port", "70"),
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestAccLoadBalancerServiceResource_TCP(t *testing.T) {
testsupport.CheckResourceExists(lbResName, loadbalancer.ByID(t, &lb)),
testsupport.LiftTCF(hasService(&lb, 70)),
testsupport.CheckResourceAttrFunc(svcResName, "load_balancer_id", func() string {
return strconv.Itoa(lb.ID)
return util.FormatID(lb.ID)
}),
resource.TestCheckResourceAttr(svcResName, "protocol", "tcp"),
resource.TestCheckResourceAttr(svcResName, "listen_port", "70"),
Expand Down Expand Up @@ -121,7 +121,7 @@ func TestAccLoadBalancerServiceResource_HTTP(t *testing.T) {
testsupport.CheckResourceExists(lbResName, loadbalancer.ByID(t, &lb)),
testsupport.LiftTCF(hasService(&lb, 80)),
testsupport.CheckResourceAttrFunc(svcResName, "load_balancer_id", func() string {
return strconv.Itoa(lb.ID)
return util.FormatID(lb.ID)
}),
resource.TestCheckResourceAttr(svcResName, "protocol", "http"),
resource.TestCheckResourceAttr(svcResName, "listen_port", "80"),
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestAccLoadBalancerServiceResource_HTTP(t *testing.T) {
testsupport.CheckResourceExists(lbResName, loadbalancer.ByID(t, &lb)),
testsupport.LiftTCF(hasService(&lb, 81)),
testsupport.CheckResourceAttrFunc(svcResName, "load_balancer_id", func() string {
return strconv.Itoa(lb.ID)
return util.FormatID(lb.ID)
}),
resource.TestCheckResourceAttr(svcResName, "protocol", "http"),
resource.TestCheckResourceAttr(svcResName, "listen_port", "81"),
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestAccLoadBalancerServiceResource_HTTP(t *testing.T) {
testsupport.CheckResourceExists(lbResName, loadbalancer.ByID(t, &lb)),
testsupport.LiftTCF(hasService(&lb, 81)),
testsupport.CheckResourceAttrFunc(svcResName, "load_balancer_id", func() string {
return strconv.Itoa(lb.ID)
return util.FormatID(lb.ID)
}),
resource.TestCheckResourceAttr(svcResName, "protocol", "http"),
resource.TestCheckResourceAttr(svcResName, "listen_port", "81"),
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestAccLoadBalancerServiceResource_HTTP_StickySessions(t *testing.T) {
testsupport.CheckResourceExists(lbResName, loadbalancer.ByID(t, &lb)),
testsupport.LiftTCF(hasService(&lb, 80)),
testsupport.CheckResourceAttrFunc(svcResName, "load_balancer_id", func() string {
return strconv.Itoa(lb.ID)
return util.FormatID(lb.ID)
}),
resource.TestCheckResourceAttr(svcResName, "protocol", "http"),
resource.TestCheckResourceAttr(svcResName, "http.0.cookie_lifetime", "1800"),
Expand Down Expand Up @@ -293,7 +293,7 @@ func TestAccLoadBalancerServiceResource_HTTPS(t *testing.T) {
testsupport.CheckResourceExists(certData.TFID(), certificate.ByID(t, &cert)),
testsupport.LiftTCF(hasService(&lb, 443)),
testsupport.CheckResourceAttrFunc(svcResName, "http.0.certificates.0", func() string {
return strconv.Itoa(cert.ID)
return util.FormatID(cert.ID)
}),
resource.TestCheckResourceAttr(svcResName, "protocol", "https"),
resource.TestCheckResourceAttr(svcResName, "listen_port", "443"),
Expand Down Expand Up @@ -435,15 +435,15 @@ func TestAccLoadBalancerServiceResource_ChangeListenPort(t *testing.T) {
testsupport.CheckResourceExists(lbResName, loadbalancer.ByID(t, &lb)),
testsupport.LiftTCF(hasService(&lb, 70)),
testsupport.CheckResourceAttrFunc(svcResName, "load_balancer_id", func() string {
return strconv.Itoa(lb.ID)
return util.FormatID(lb.ID)
}),
resource.TestCheckResourceAttr(svcResName, "protocol", "tcp"),
resource.TestCheckResourceAttr(svcResName, "listen_port", "70"),
resource.TestCheckResourceAttr(svcResName, "destination_port", "70"),

testsupport.LiftTCF(hasService(&lb, 443)),
testsupport.CheckResourceAttrFunc(svcResName2, "load_balancer_id", func() string {
return strconv.Itoa(lb.ID)
return util.FormatID(lb.ID)
}),
resource.TestCheckResourceAttr(svcResName2, "protocol", "tcp"),
resource.TestCheckResourceAttr(svcResName2, "listen_port", "443"),
Expand Down Expand Up @@ -473,15 +473,15 @@ func TestAccLoadBalancerServiceResource_ChangeListenPort(t *testing.T) {
testsupport.CheckResourceExists(lbResName, loadbalancer.ByID(t, &lb)),
testsupport.LiftTCF(hasService(&lb, 71)),
testsupport.CheckResourceAttrFunc(svcResName, "load_balancer_id", func() string {
return strconv.Itoa(lb.ID)
return util.FormatID(lb.ID)
}),
resource.TestCheckResourceAttr(svcResName, "protocol", "tcp"),
resource.TestCheckResourceAttr(svcResName, "listen_port", "71"),
resource.TestCheckResourceAttr(svcResName, "destination_port", "70"),

testsupport.LiftTCF(hasService(&lb, 443)),
testsupport.CheckResourceAttrFunc(svcResName2, "load_balancer_id", func() string {
return strconv.Itoa(lb.ID)
return util.FormatID(lb.ID)
}),
resource.TestCheckResourceAttr(svcResName2, "protocol", "tcp"),
resource.TestCheckResourceAttr(svcResName2, "listen_port", "443"),
Expand Down
6 changes: 3 additions & 3 deletions internal/loadbalancer/resource_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package loadbalancer_test

import (
"fmt"
"strconv"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/hetznercloud/terraform-provider-hcloud/internal/teste2e"
"github.com/hetznercloud/terraform-provider-hcloud/internal/testsupport"
"github.com/hetznercloud/terraform-provider-hcloud/internal/testtemplate"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util"
)

func TestAccLoadBalancerTargetResource_ServerTarget(t *testing.T) {
Expand Down Expand Up @@ -63,11 +63,11 @@ func TestAccLoadBalancerTargetResource_ServerTarget(t *testing.T) {
loadbalancer.TargetResourceType+".lb-test-target", "type", "server"),
testsupport.CheckResourceAttrFunc(
loadbalancer.TargetResourceType+".lb-test-target", "load_balancer_id", func() string {
return strconv.Itoa(lb.ID)
return util.FormatID(lb.ID)
}),
testsupport.CheckResourceAttrFunc(
loadbalancer.TargetResourceType+".lb-test-target", "server_id", func() string {
return strconv.Itoa(srv.ID)
return util.FormatID(srv.ID)
}),
testsupport.LiftTCF(hasServerTarget(&lb, &srv)),
),
Expand Down
Loading

0 comments on commit 73b3094

Please sign in to comment.