@@ -796,19 +796,24 @@ func (d *versionsPlanModifier) MarkdownDescription(context.Context) string {
796796
797797// PlanModifyObject implements planmodifier.List.
798798func (d * versionsPlanModifier )PlanModifyList (ctx context.Context ,req planmodifier.ListRequest ,resp * planmodifier.ListResponse ) {
799- var data Versions
800- resp .Diagnostics .Append (req .PlanValue .ElementsAs (ctx ,& data ,false )... )
799+ var planVersions Versions
800+ resp .Diagnostics .Append (req .PlanValue .ElementsAs (ctx ,& planVersions ,false )... )
801+ if resp .Diagnostics .HasError () {
802+ return
803+ }
804+ var configVersions Versions
805+ resp .Diagnostics .Append (req .ConfigValue .ElementsAs (ctx ,& configVersions ,false )... )
801806if resp .Diagnostics .HasError () {
802807return
803808}
804809
805- for i := range data {
806- hash ,err := computeDirectoryHash (data [i ].Directory .ValueString ())
810+ for i := range planVersions {
811+ hash ,err := computeDirectoryHash (planVersions [i ].Directory .ValueString ())
807812if err != nil {
808813resp .Diagnostics .AddError ("Client Error" ,fmt .Sprintf ("Failed to compute directory hash: %s" ,err ))
809814return
810815}
811- data [i ].DirectoryHash = types .StringValue (hash )
816+ planVersions [i ].DirectoryHash = types .StringValue (hash )
812817}
813818
814819var lv LastVersionsByHash
@@ -828,9 +833,9 @@ func (d *versionsPlanModifier) PlanModifyList(ctx context.Context, req planmodif
828833}
829834}
830835
831- data .reconcileVersionIDs (lv )
836+ planVersions .reconcileVersionIDs (lv , configVersions )
832837
833- resp .PlanValue ,resp .Diagnostics = types .ListValueFrom (ctx ,req .PlanValue .ElementType (ctx ),data )
838+ resp .PlanValue ,resp .Diagnostics = types .ListValueFrom (ctx ,req .PlanValue .ElementType (ctx ),planVersions )
834839}
835840
836841func NewVersionsPlanModifier () planmodifier.List {
@@ -1166,22 +1171,28 @@ func (v Versions) setPrivateState(ctx context.Context, ps privateState) (diags d
11661171return ps .SetKey (ctx ,LastVersionsKey ,lvBytes )
11671172}
11681173
1169- func (v Versions )reconcileVersionIDs (lv LastVersionsByHash ) {
1170- for i := range v {
1171- prevList ,ok := lv [v [i ].DirectoryHash .ValueString ()]
1174+ func (planVersions Versions )reconcileVersionIDs (lv LastVersionsByHash , configVersions Versions ) {
1175+ for i := range planVersions {
1176+ prevList ,ok := lv [planVersions [i ].DirectoryHash .ValueString ()]
11721177// If not in state, mark as known after apply since we'll create a new version.
11731178// Versions whose Terraform configuration has not changed will have known
11741179// IDs at this point, so we need to set this manually.
11751180if ! ok {
1176- v [i ].ID = NewUUIDUnknown ()
1181+ planVersions [i ].ID = NewUUIDUnknown ()
1182+ // We might have the old randomly generated name in the plan,
1183+ // so unless the user has set it to a new one, we need to set it to
1184+ // unknown so that a new one is generated
1185+ if configVersions [i ].Name .IsNull () {
1186+ planVersions [i ].Name = types .StringUnknown ()
1187+ }
11771188}else {
11781189// More than one candidate, try to match by name
11791190for j ,prev := range prevList {
11801191// If the name is the same, use the existing ID, and remove
11811192// it from the previous version candidates
1182- if v [i ].Name .ValueString ()== prev .Name {
1183- v [i ].ID = UUIDValue (prev .ID )
1184- lv [v [i ].DirectoryHash .ValueString ()]= append (prevList [:j ],prevList [j + 1 :]... )
1193+ if planVersions [i ].Name .ValueString ()== prev .Name {
1194+ planVersions [i ].ID = UUIDValue (prev .ID )
1195+ lv [planVersions [i ].DirectoryHash .ValueString ()]= append (prevList [:j ],prevList [j + 1 :]... )
11851196break
11861197}
11871198}
@@ -1190,14 +1201,14 @@ func (v Versions) reconcileVersionIDs(lv LastVersionsByHash) {
11901201
11911202// For versions whose hash was found in the private state but couldn't be
11921203// matched, use the leftovers in the order they appear
1193- for i := range v {
1194- prevList := lv [v [i ].DirectoryHash .ValueString ()]
1195- if len (prevList )> 0 && v [i ].ID .IsUnknown () {
1196- v [i ].ID = UUIDValue (prevList [0 ].ID )
1197- if v [i ].Name .IsUnknown () {
1198- v [i ].Name = types .StringValue (prevList [0 ].Name )
1204+ for i := range planVersions {
1205+ prevList := lv [planVersions [i ].DirectoryHash .ValueString ()]
1206+ if len (prevList )> 0 && planVersions [i ].ID .IsUnknown () {
1207+ planVersions [i ].ID = UUIDValue (prevList [0 ].ID )
1208+ if planVersions [i ].Name .IsUnknown () {
1209+ planVersions [i ].Name = types .StringValue (prevList [0 ].Name )
11991210}
1200- lv [v [i ].DirectoryHash .ValueString ()]= prevList [1 :]
1211+ lv [planVersions [i ].DirectoryHash .ValueString ()]= prevList [1 :]
12011212}
12021213}
12031214}