Skip to content

Commit 6be0ffc

Browse files
fix: Instance image diff during Instance import (#2238)
* Populate disk image from instance during Read to avoid import diffs * Address CoPilot feedback * Addressed PR comments * Address more PR comments
1 parent 204b10b commit 6be0ffc

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

linode/instancedisk/framework_model.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package instancedisk
22

33
import (
4+
"context"
45
"strconv"
56

67
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
78
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
89
"github.com/hashicorp/terraform-plugin-framework/types"
10+
"github.com/hashicorp/terraform-plugin-log/tflog"
911
"github.com/linode/linodego"
1012
"github.com/linode/terraform-provider-linode/v3/linode/helper"
1113
)
@@ -72,3 +74,26 @@ func (data *ResourceModel) CopyFrom(other ResourceModel, preserveKnown bool) {
7274
data.Status = helper.KeepOrUpdateValue(data.Status, other.Status, preserveKnown)
7375
data.DiskEncryption = helper.KeepOrUpdateValue(data.DiskEncryption, other.DiskEncryption, preserveKnown)
7476
}
77+
78+
func (data *ResourceModel) PopulateImageFromParentInstance(
79+
ctx context.Context,
80+
client *linodego.Client,
81+
linodeID int,
82+
) {
83+
if !data.Image.IsNull() {
84+
return
85+
}
86+
87+
instance, err := client.GetInstance(ctx, linodeID)
88+
if err != nil {
89+
tflog.Debug(ctx, "Failed to fetch parent instance for disk image fallback", map[string]any{
90+
"linode_id": linodeID,
91+
"error": err.Error(),
92+
})
93+
return
94+
}
95+
96+
if instance.Image != "" {
97+
data.Image = types.StringValue(instance.Image)
98+
}
99+
}

linode/instancedisk/framework_resource.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ func (r *Resource) Create(
5555
return
5656
}
5757

58+
if plan.Image.IsUnknown() {
59+
plan.Image = types.StringNull()
60+
}
61+
5862
helper.SetLogFieldBulk(ctx, map[string]any{"linode_id": plan.LinodeID})
5963
createTimeout, diags := plan.Timeouts.Create(ctx, DefaultVolumeCreateTimeout)
6064
resp.Diagnostics.Append(diags...)
@@ -207,7 +211,10 @@ func (r *Resource) Read(
207211
return
208212
}
209213

214+
state.PopulateImageFromParentInstance(ctx, client, linodeID)
215+
210216
state.FlattenDisk(disk, false)
217+
211218
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
212219
}
213220

@@ -223,6 +230,10 @@ func (r *Resource) Update(
223230
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
224231
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
225232

233+
if plan.Image.IsUnknown() {
234+
plan.Image = types.StringNull()
235+
}
236+
226237
ctx = populateLogAttributes(ctx, state)
227238

228239
updateTimeout, diags := plan.Timeouts.Update(ctx, DefaultVolumeUpdateTimeout)

linode/instancedisk/framework_resource_schema.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ var frameworkResourceSchema = schema.Schema{
8383
"image": schema.StringAttribute{
8484
Description: "An Image ID to deploy the Linode Disk from.",
8585
Optional: true,
86+
Computed: true,
8687
PlanModifiers: []planmodifier.String{
8788
stringplanmodifier.RequiresReplace(),
89+
stringplanmodifier.UseStateForUnknown(),
8890
},
8991
},
9092
"root_pass": schema.StringAttribute{

0 commit comments

Comments
 (0)