@@ -796,19 +796,24 @@ func (d *versionsPlanModifier) MarkdownDescription(context.Context) string {
796
796
797
797
// PlanModifyObject implements planmodifier.List.
798
798
func (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 )... )
801
806
if resp .Diagnostics .HasError () {
802
807
return
803
808
}
804
809
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 ())
807
812
if err != nil {
808
813
resp .Diagnostics .AddError ("Client Error" , fmt .Sprintf ("Failed to compute directory hash: %s" , err ))
809
814
return
810
815
}
811
- data [i ].DirectoryHash = types .StringValue (hash )
816
+ planVersions [i ].DirectoryHash = types .StringValue (hash )
812
817
}
813
818
814
819
var lv LastVersionsByHash
@@ -828,9 +833,9 @@ func (d *versionsPlanModifier) PlanModifyList(ctx context.Context, req planmodif
828
833
}
829
834
}
830
835
831
- data .reconcileVersionIDs (lv )
836
+ planVersions .reconcileVersionIDs (lv , configVersions )
832
837
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 )
834
839
}
835
840
836
841
func NewVersionsPlanModifier () planmodifier.List {
@@ -1166,22 +1171,28 @@ func (v Versions) setPrivateState(ctx context.Context, ps privateState) (diags d
1166
1171
return ps .SetKey (ctx , LastVersionsKey , lvBytes )
1167
1172
}
1168
1173
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 ()]
1172
1177
// If not in state, mark as known after apply since we'll create a new version.
1173
1178
// Versions whose Terraform configuration has not changed will have known
1174
1179
// IDs at this point, so we need to set this manually.
1175
1180
if ! 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
+ }
1177
1188
} else {
1178
1189
// More than one candidate, try to match by name
1179
1190
for j , prev := range prevList {
1180
1191
// If the name is the same, use the existing ID, and remove
1181
1192
// 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 :]... )
1185
1196
break
1186
1197
}
1187
1198
}
@@ -1190,14 +1201,14 @@ func (v Versions) reconcileVersionIDs(lv LastVersionsByHash) {
1190
1201
1191
1202
// For versions whose hash was found in the private state but couldn't be
1192
1203
// 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 )
1199
1210
}
1200
- lv [v [i ].DirectoryHash .ValueString ()] = prevList [1 :]
1211
+ lv [planVersions [i ].DirectoryHash .ValueString ()] = prevList [1 :]
1201
1212
}
1202
1213
}
1203
1214
}
0 commit comments