Skip to content

Commit

Permalink
fix: replace naked int casts with wrapper to handle data loss
Browse files Browse the repository at this point in the history
  • Loading branch information
jooola committed Jan 21, 2025
1 parent 7a1abfd commit 6b1a1de
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions internal/firewall/attachment_resource.go
Original file line number Diff line number Diff line change
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] = int(id)
vals[i] = util.CastInt(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,7 +233,7 @@ func (a *attachment) ToResourceData(d *schema.ResourceData) {
}
d.Set("label_selectors", lSels)

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

Expand Down
2 changes: 1 addition & 1 deletion internal/loadbalancer/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func setLoadBalancerServiceSchema(d *schema.ResourceData, lb *hcloud.LoadBalance
if len(svc.HTTP.Certificates) > 0 {
certIDs := make([]int, len(svc.HTTP.Certificates))
for i := 0; i < len(svc.HTTP.Certificates); i++ {
certIDs[i] = int(svc.HTTP.Certificates[i].ID)
certIDs[i] = util.CastInt(svc.HTTP.Certificates[i].ID)
}
httpMap["certificates"] = certIDs
}
Expand Down
4 changes: 2 additions & 2 deletions internal/server/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ func setServerSchema(d *schema.ResourceData, s *hcloud.Server) {
func getServerAttributes(d *schema.ResourceData, s *hcloud.Server) map[string]interface{} {
firewallIDs := make([]int, len(s.PublicNet.Firewalls))
for i, firewall := range s.PublicNet.Firewalls {
firewallIDs[i] = int(firewall.Firewall.ID)
firewallIDs[i] = util.CastInt(firewall.Firewall.ID)
}

res := map[string]interface{}{
Expand Down Expand Up @@ -1189,7 +1189,7 @@ func getServerAttributes(d *schema.ResourceData, s *hcloud.Server) map[string]in
}

if s.PlacementGroup != nil {
res["placement_group_id"] = int(s.PlacementGroup.ID)
res["placement_group_id"] = util.CastInt(s.PlacementGroup.ID)
} else {
res["placement_group_id"] = nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/snapshot/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func resourceSnapshotIsNotFound(err error, d *schema.ResourceData) bool {
func setSnapshotSchema(d *schema.ResourceData, s *hcloud.Image) {
d.SetId(util.FormatID(s.ID))
if s.CreatedFrom != nil {
d.Set("server_id", int(s.CreatedFrom.ID))
d.Set("server_id", util.CastInt(s.CreatedFrom.ID))
}
d.Set("description", s.Description)
d.Set("labels", s.Labels)
Expand Down
1 change: 1 addition & 0 deletions internal/util/resourceutil/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"

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

Expand Down

0 comments on commit 6b1a1de

Please sign in to comment.