Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: upgrade hcloud-go to v2 #1062

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
14 changes: 8 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/hashicorp/terraform-plugin-mux v0.17.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.35.0
github.com/hashicorp/terraform-plugin-testing v1.11.0
github.com/hetznercloud/hcloud-go v1.59.2
github.com/hetznercloud/hcloud-go/v2 v2.17.1
github.com/joho/godotenv v1.5.1
github.com/stretchr/testify v1.10.0
golang.org/x/net v0.34.0
Expand Down Expand Up @@ -47,20 +47,22 @@ require (
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
Expand Down
101 changes: 18 additions & 83 deletions go.sum

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions hcloud/plugin_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/terraform-provider-hcloud/internal/datacenter"
"github.com/hetznercloud/terraform-provider-hcloud/internal/loadbalancertype"
"github.com/hetznercloud/terraform-provider-hcloud/internal/location"
Expand Down Expand Up @@ -136,9 +136,9 @@ func (p *PluginProvider) Configure(ctx context.Context, req provider.ConfigureRe
)
}
if data.PollFunction.ValueString() == "constant" {
opts = append(opts, hcloud.WithPollBackoffFunc(hcloud.ConstantBackoff(pollInterval)))
opts = append(opts, hcloud.WithPollOpts(hcloud.PollOpts{BackoffFunc: hcloud.ConstantBackoff(pollInterval)}))
} else {
opts = append(opts, hcloud.WithPollBackoffFunc(hcloud.ExponentialBackoff(2, pollInterval)))
opts = append(opts, hcloud.WithPollOpts(hcloud.PollOpts{BackoffFunc: hcloud.ExponentialBackoff(2, pollInterval)}))
}
}

Expand Down
6 changes: 3 additions & 3 deletions hcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/terraform-provider-hcloud/internal/certificate"
"github.com/hetznercloud/terraform-provider-hcloud/internal/firewall"
"github.com/hetznercloud/terraform-provider-hcloud/internal/floatingip"
Expand Down Expand Up @@ -136,9 +136,9 @@ func providerConfigure(_ context.Context, d *schema.ResourceData) (interface{},
}
pollFunction, ok := d.GetOk("poll_function")
if ok && pollFunction == "constant" {
opts = append(opts, hcloud.WithPollBackoffFunc(hcloud.ConstantBackoff(pollInterval)))
opts = append(opts, hcloud.WithPollOpts(hcloud.PollOpts{BackoffFunc: hcloud.ConstantBackoff(pollInterval)}))
} else {
opts = append(opts, hcloud.WithPollBackoffFunc(hcloud.ExponentialBackoff(2, pollInterval)))
opts = append(opts, hcloud.WithPollOpts(hcloud.PollOpts{BackoffFunc: hcloud.ExponentialBackoff(2, pollInterval)}))
}
}
if logging.LogLevel() != "" {
Expand Down
8 changes: 4 additions & 4 deletions internal/certificate/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package certificate
import (
"context"
"fmt"
"strconv"

"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/hcloud-go/v2/hcloud"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util/datasourceutil"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util/hcloudutil"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util/merge"
Expand Down Expand Up @@ -112,7 +112,7 @@ func dataSourceHcloudCertificateRead(ctx context.Context, d *schema.ResourceData
client := m.(*hcloud.Client)

if id, ok := d.GetOk("id"); ok {
cert, _, err := client.Certificate.GetByID(ctx, id.(int))
cert, _, err := client.Certificate.GetByID(ctx, util.CastInt64(id))
if err != nil {
return hcloudutil.ErrorToDiag(err)
}
Expand Down Expand Up @@ -174,7 +174,7 @@ func dataSourceHcloudCertificateListRead(ctx context.Context, d *schema.Resource
ids := make([]string, len(allCertificates))
tfCertificates := make([]map[string]interface{}, len(allCertificates))
for i, certificate := range allCertificates {
ids[i] = strconv.Itoa(certificate.ID)
ids[i] = util.FormatID(certificate.ID)
tfCertificates[i] = getCertificateAttributes(certificate)
}
d.Set("certificates", tfCertificates)
Expand Down
18 changes: 6 additions & 12 deletions internal/certificate/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"
"errors"
"log"
"strconv"
"time"

"github.com/hashicorp/go-cty/cty"
"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/hcloud-go/v2/hcloud"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util/hcloudutil"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util/timeutil"
)
Expand Down Expand Up @@ -201,7 +201,7 @@ func createUploadedResource(ctx context.Context, d *schema.ResourceData, m inter
if err != nil {
return hcloudutil.ErrorToDiag(err)
}
d.SetId(strconv.Itoa(res.ID))
d.SetId(util.FormatID(res.ID))
return readResource(ctx, d, m)
}

Expand Down Expand Up @@ -230,7 +230,7 @@ func createManagedResource(ctx context.Context, d *schema.ResourceData, m interf
if err != nil {
return hcloudutil.ErrorToDiag(err)
}
d.SetId(strconv.Itoa(res.Certificate.ID))
d.SetId(util.FormatID(res.Certificate.ID))
if err := hcloudutil.WaitForAction(ctx, &c.Action, res.Action); err != nil {
return hcloudutil.ErrorToDiag(err)
}
Expand Down Expand Up @@ -268,13 +268,7 @@ func resourceCertificateNotFound(err error, d *schema.ResourceData) bool {
}

func setCertificateSchema(d *schema.ResourceData, cert *hcloud.Certificate) {
for key, val := range getCertificateAttributes(cert) {
if key == "id" {
d.SetId(strconv.Itoa(val.(int)))
} else {
d.Set(key, val)
}
}
util.SetSchemaFromAttributes(d, getCertificateAttributes(cert))
}

func getCertificateAttributes(cert *hcloud.Certificate) map[string]interface{} {
Expand Down Expand Up @@ -331,7 +325,7 @@ func updateResource(ctx context.Context, d *schema.ResourceData, m interface{})
func deleteResource(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*hcloud.Client)

certID, err := strconv.Atoi(d.Id())
certID, err := util.ParseID(d.Id())
if err != nil {
log.Printf("[WARN] invalid certificate id (%s), removing from state: %v", d.Id(), err)
d.SetId("")
Expand Down
2 changes: 1 addition & 1 deletion internal/certificate/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/terraform-provider-hcloud/internal/certificate"
"github.com/hetznercloud/terraform-provider-hcloud/internal/teste2e"
"github.com/hetznercloud/terraform-provider-hcloud/internal/testsupport"
Expand Down
6 changes: 3 additions & 3 deletions internal/certificate/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/terraform-provider-hcloud/internal/testsupport"
"github.com/hetznercloud/terraform-provider-hcloud/internal/testtemplate"
)
Expand Down Expand Up @@ -45,8 +45,8 @@ func Sweep(r string) error {
}

// ByID returns a function that obtains a certificate by its ID.
func ByID(t *testing.T, cert *hcloud.Certificate) func(*hcloud.Client, int) bool {
return func(c *hcloud.Client, id int) bool {
func ByID(t *testing.T, cert *hcloud.Certificate) func(*hcloud.Client, int64) bool {
return func(c *hcloud.Client, id int64) bool {
found, _, err := c.Certificate.GetByID(context.Background(), id)
if err != nil {
t.Fatalf("find certificate %d: %v", id, err)
Expand Down
16 changes: 8 additions & 8 deletions internal/datacenter/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"sort"
"strconv"

"github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
Expand All @@ -14,7 +13,8 @@ import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util/datasourceutil"
"github.com/hetznercloud/terraform-provider-hcloud/internal/util/hcloudutil"
)
Expand Down Expand Up @@ -50,12 +50,12 @@ func newResourceData(ctx context.Context, in *hcloud.Datacenter) (resourceData,
var diags diag.Diagnostics
var newDiags diag.Diagnostics

data.ID = types.Int64Value(int64(in.ID))
data.ID = types.Int64Value(in.ID)
data.Name = types.StringValue(in.Name)
data.Description = types.StringValue(in.Description)

data.Location, newDiags = types.MapValue(types.StringType, map[string]attr.Value{
"id": types.StringValue(strconv.Itoa(in.Location.ID)),
"id": types.StringValue(util.FormatID(in.Location.ID)),
"name": types.StringValue(in.Location.Name),
"description": types.StringValue(in.Location.Description),
"country": types.StringValue(in.Location.Country),
Expand All @@ -67,11 +67,11 @@ func newResourceData(ctx context.Context, in *hcloud.Datacenter) (resourceData,

supportedServerTypeIDs := make([]int64, len(in.ServerTypes.Supported))
for i, v := range in.ServerTypes.Supported {
supportedServerTypeIDs[i] = int64(v.ID)
supportedServerTypeIDs[i] = v.ID
}
availableServerTypeIDs := make([]int64, len(in.ServerTypes.Available))
for i, v := range in.ServerTypes.Available {
availableServerTypeIDs[i] = int64(v.ID)
availableServerTypeIDs[i] = v.ID
}
sort.Slice(supportedServerTypeIDs, func(i, j int) bool { return supportedServerTypeIDs[i] < supportedServerTypeIDs[j] })
sort.Slice(availableServerTypeIDs, func(i, j int) bool { return availableServerTypeIDs[i] < availableServerTypeIDs[j] })
Expand Down Expand Up @@ -186,7 +186,7 @@ func (d *dataSource) Read(ctx context.Context, req datasource.ReadRequest, resp

switch {
case !data.ID.IsNull():
result, _, err = d.client.Datacenter.GetByID(ctx, int(data.ID.ValueInt64()))
result, _, err = d.client.Datacenter.GetByID(ctx, data.ID.ValueInt64())
if err != nil {
resp.Diagnostics.Append(hcloudutil.APIErrorDiagnostics(err)...)
return
Expand Down Expand Up @@ -302,7 +302,7 @@ func newResourceDataList(ctx context.Context, in []*hcloud.Datacenter) (resource
datacenters := make([]resourceData, len(in))

for i, item := range in {
datacenterIDs[i] = strconv.Itoa(item.ID)
datacenterIDs[i] = util.FormatID(item.ID)
names[i] = item.Name
descriptions[i] = item.Description

Expand Down
2 changes: 1 addition & 1 deletion internal/deprecation/deprecation_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
resourceschema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

type DeprecationModel struct { // nolint:revive
Expand Down
2 changes: 1 addition & 1 deletion internal/deprecation/deprecation_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

func TestNewDeprecationModel(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/deprecation/deprecation_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

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

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

func AddToSchema(s map[string]*schema.Schema) map[string]*schema.Schema {
Expand Down
24 changes: 12 additions & 12 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/hcloud"
"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
Loading
Loading