Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions linode/vpcsubnet/framework_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ func (d *DataSource) Read(

ctx = populateLogAttributes(ctx, data)

vpcId := helper.FrameworkSafeInt64ToInt(data.VPCId.ValueInt64(), &resp.Diagnostics)
vpcID := helper.FrameworkSafeInt64ToInt(data.VPCID.ValueInt64(), &resp.Diagnostics)
id := helper.FrameworkSafeStringToInt(data.ID.ValueString(), &resp.Diagnostics)

if resp.Diagnostics.HasError() {
return
}

vpcSubnet, err := client.GetVPCSubnet(ctx, vpcId, id)
vpcSubnet, err := client.GetVPCSubnet(ctx, vpcID, id)
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Failed to read VPC Subnet %v", data.ID.ValueString()),
Expand Down
30 changes: 20 additions & 10 deletions linode/vpcsubnet/framework_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

type VPCSubnetModel struct {
ID types.String `tfsdk:"id"`
VPCId types.Int64 `tfsdk:"vpc_id"`
VPCID types.Int64 `tfsdk:"vpc_id"`
Label types.String `tfsdk:"label"`
IPv4 types.String `tfsdk:"ipv4"`
Linodes types.List `tfsdk:"linodes"`
Expand Down Expand Up @@ -77,31 +77,41 @@ func FlattenSubnetLinodes(
return &linodesList, diags
}

func (d *VPCSubnetModel) FlattenSubnet(
func (v *VPCSubnetModel) FlattenSubnet(
ctx context.Context,
subnet *linodego.VPCSubnet,
preserveKnown bool,
) diag.Diagnostics {
d.ID = helper.KeepOrUpdateString(d.ID, strconv.Itoa(subnet.ID), preserveKnown)
v.ID = helper.KeepOrUpdateString(v.ID, strconv.Itoa(subnet.ID), preserveKnown)

linodesList, diags := FlattenSubnetLinodes(ctx, subnet.Linodes)
if diags.HasError() {
return diags
}
d.Linodes = helper.KeepOrUpdateValue(d.Linodes, *linodesList, preserveKnown)
v.Linodes = helper.KeepOrUpdateValue(v.Linodes, *linodesList, preserveKnown)

d.Created = helper.KeepOrUpdateValue(
d.Created,
v.Created = helper.KeepOrUpdateValue(
v.Created,
timetypes.NewRFC3339TimePointerValue(subnet.Created),
preserveKnown,
)
d.Updated = helper.KeepOrUpdateValue(
d.Updated,
v.Updated = helper.KeepOrUpdateValue(
v.Updated,
timetypes.NewRFC3339TimePointerValue(subnet.Updated),
preserveKnown,
)
d.Label = helper.KeepOrUpdateString(d.Label, subnet.Label, preserveKnown)
d.IPv4 = helper.KeepOrUpdateString(d.IPv4, subnet.IPv4, preserveKnown)
v.Label = helper.KeepOrUpdateString(v.Label, subnet.Label, preserveKnown)
v.IPv4 = helper.KeepOrUpdateString(v.IPv4, subnet.IPv4, preserveKnown)

return nil
}

func (v *VPCSubnetModel) CopyFrom(other VPCSubnetModel, preserveKnown bool) {
v.ID = helper.KeepOrUpdateValue(v.ID, other.ID, preserveKnown)
v.VPCID = helper.KeepOrUpdateValue(v.VPCID, other.VPCID, preserveKnown)
v.Label = helper.KeepOrUpdateValue(v.Label, other.Label, preserveKnown)
v.IPv4 = helper.KeepOrUpdateValue(v.IPv4, other.IPv4, preserveKnown)
v.Linodes = helper.KeepOrUpdateValue(v.Linodes, other.Linodes, preserveKnown)
v.Created = helper.KeepOrUpdateValue(v.Created, other.Created, preserveKnown)
v.Updated = helper.KeepOrUpdateValue(v.Updated, other.Updated, preserveKnown)
}
25 changes: 12 additions & 13 deletions linode/vpcsubnet/framework_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"strconv"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
Expand Down Expand Up @@ -73,7 +72,7 @@ func (r *Resource) Create(
IPv4: data.IPv4.ValueString(),
}

vpcId := helper.FrameworkSafeInt64ToInt(data.VPCId.ValueInt64(), &resp.Diagnostics)
vpcID := helper.FrameworkSafeInt64ToInt(data.VPCID.ValueInt64(), &resp.Diagnostics)

if resp.Diagnostics.HasError() {
return
Expand All @@ -82,7 +81,7 @@ func (r *Resource) Create(
tflog.Debug(ctx, "client.CreateVPCSubnet(...)", map[string]any{
"options": createOpts,
})
subnet, err := client.CreateVPCSubnet(ctx, createOpts, vpcId)
subnet, err := client.CreateVPCSubnet(ctx, createOpts, vpcID)
if err != nil {
resp.Diagnostics.AddError(
"Failed to create VPC subnet.",
Expand Down Expand Up @@ -123,14 +122,14 @@ func (r *Resource) Read(
return
}

vpcId := helper.FrameworkSafeInt64ToInt(data.VPCId.ValueInt64(), &resp.Diagnostics)
vpcID := helper.FrameworkSafeInt64ToInt(data.VPCID.ValueInt64(), &resp.Diagnostics)
id := helper.FrameworkSafeStringToInt(data.ID.ValueString(), &resp.Diagnostics)

if resp.Diagnostics.HasError() {
return
}

subnet, err := client.GetVPCSubnet(ctx, vpcId, id)
subnet, err := client.GetVPCSubnet(ctx, vpcID, id)
if err != nil {
if linodego.IsNotFound(err) {
resp.Diagnostics.AddWarning(
Expand Down Expand Up @@ -186,8 +185,8 @@ func (r *Resource) Update(
}

if shouldUpdate {
vpcId := helper.FrameworkSafeInt64ToInt(
plan.VPCId.ValueInt64(),
vpcID := helper.FrameworkSafeInt64ToInt(
plan.VPCID.ValueInt64(),
&resp.Diagnostics,
)
id := helper.FrameworkSafeStringToInt(plan.ID.ValueString(), &resp.Diagnostics)
Expand All @@ -199,7 +198,7 @@ func (r *Resource) Update(
tflog.Debug(ctx, "client.UpdateVPCSubnet(...)", map[string]any{
"options": updateOpts,
})
subnet, err := client.UpdateVPCSubnet(ctx, vpcId, id, updateOpts)
subnet, err := client.UpdateVPCSubnet(ctx, vpcID, id, updateOpts)
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Failed to update VPC subnet (%d).", id),
Expand All @@ -211,10 +210,10 @@ func (r *Resource) Update(
if resp.Diagnostics.HasError() {
return
}
} else {
req.State.GetAttribute(ctx, path.Root("updated"), &plan.Updated)
}

plan.CopyFrom(state, true)

// Workaround for Crossplane issue where ID is not
// properly populated in plan
// See TPT-2865 for more details
Expand Down Expand Up @@ -242,15 +241,15 @@ func (r *Resource) Delete(

ctx = populateLogAttributes(ctx, data)

vpcId := helper.FrameworkSafeInt64ToInt(data.VPCId.ValueInt64(), &resp.Diagnostics)
vpcID := helper.FrameworkSafeInt64ToInt(data.VPCID.ValueInt64(), &resp.Diagnostics)
id := helper.FrameworkSafeStringToInt(data.ID.ValueString(), &resp.Diagnostics)

if resp.Diagnostics.HasError() {
return
}

tflog.Debug(ctx, "client.DeleteVPCSubnet(...)")
err := client.DeleteVPCSubnet(ctx, vpcId, id)
err := client.DeleteVPCSubnet(ctx, vpcID, id)
if err != nil {
if lerr, ok := err.(*linodego.Error); (ok && lerr.Code != 404) || !ok {
resp.Diagnostics.AddError(
Expand All @@ -264,7 +263,7 @@ func (r *Resource) Delete(

func populateLogAttributes(ctx context.Context, data VPCSubnetModel) context.Context {
return helper.SetLogFieldBulk(ctx, map[string]any{
"vpc_id": data.VPCId.ValueInt64(),
"vpc_id": data.VPCID.ValueInt64(),
"id": data.ID.ValueString(),
})
}