mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-03 20:30:28 -06:00
terraform: Avoid having multiple primaries
This commit is contained in:
parent
f398708be2
commit
4fe05428b3
@ -1264,17 +1264,46 @@ func (c *walkContext) persistState(r *Resource) {
|
||||
rs.Dependencies = r.Dependencies
|
||||
|
||||
// Assign the instance state to the proper location
|
||||
if r.Flags&FlagTainted != 0 {
|
||||
if r.Flags&FlagDeposed != 0 {
|
||||
// We were previously the primary and have been deposed, so
|
||||
// now we are the final tainted resource
|
||||
r.TaintedIndex = len(rs.Tainted) - 1
|
||||
rs.Tainted[r.TaintedIndex] = r.State
|
||||
|
||||
} else if r.Flags&FlagTainted != 0 {
|
||||
if r.TaintedIndex >= 0 {
|
||||
// Tainted with a pre-existing index, just update that spot
|
||||
rs.Tainted[r.TaintedIndex] = r.State
|
||||
|
||||
} else if r.Flags&FlagReplacePrimary != 0 {
|
||||
// We just replaced the primary, so restore the primary
|
||||
rs.Primary = rs.Tainted[len(rs.Tainted)-1]
|
||||
|
||||
// Set ourselves as tainted
|
||||
rs.Tainted[len(rs.Tainted)-1] = r.State
|
||||
|
||||
} else {
|
||||
// Newly tainted, so append it to the list, update the
|
||||
// index, and remove the primary.
|
||||
rs.Tainted = append(rs.Tainted, r.State)
|
||||
rs.Primary = nil
|
||||
r.TaintedIndex = len(rs.Tainted) - 1
|
||||
rs.Primary = nil
|
||||
}
|
||||
|
||||
} else if r.Flags&FlagReplacePrimary != 0 {
|
||||
// If the ID is blank (there was an error), then we leave
|
||||
// the primary that exists, and do not store this as a tainted
|
||||
// instance
|
||||
if r.State.ID == "" {
|
||||
return
|
||||
}
|
||||
|
||||
// Push the old primary into the tainted state
|
||||
rs.Tainted = append(rs.Tainted, rs.Primary)
|
||||
|
||||
// Set this as the new primary
|
||||
rs.Primary = r.State
|
||||
|
||||
} else {
|
||||
// The primary instance, so just set it directly
|
||||
rs.Primary = r.State
|
||||
|
@ -557,8 +557,11 @@ func graphAddDiff(g *depgraph.Graph, d *ModuleDiff) error {
|
||||
Target: newN,
|
||||
})
|
||||
|
||||
// Set the CreateBeforeDestroy flag on the old noun
|
||||
rn.Resource.Flags |= FlagCreateBeforeDestroy
|
||||
// Set the ReplacePrimary flag on the new instance so that
|
||||
// it will become the new primary, and Diposed flag on the
|
||||
// existing instance so that it will step down
|
||||
rn.Resource.Flags |= FlagReplacePrimary
|
||||
newNode.Resource.Flags |= FlagDeposed
|
||||
|
||||
} else {
|
||||
dep := &depgraph.Dependency{
|
||||
|
@ -704,10 +704,15 @@ func TestGraphAddDiff_createBeforeDestroy(t *testing.T) {
|
||||
t.Fatalf("bad:\n\n%s\n\nexpected:\n\n%s", actual, expected)
|
||||
}
|
||||
|
||||
// Verify the flag is set
|
||||
// Verify the flags are set
|
||||
r := g.Noun("aws_instance.bar")
|
||||
if r.Meta.(*GraphNodeResource).Resource.Flags&FlagCreateBeforeDestroy == 0 {
|
||||
t.Fatalf("missing FlagCreateBeforeDestroy")
|
||||
if r.Meta.(*GraphNodeResource).Resource.Flags&FlagReplacePrimary == 0 {
|
||||
t.Fatalf("missing FlagReplacePrimary")
|
||||
}
|
||||
|
||||
r = g.Noun("aws_instance.bar (destroy)")
|
||||
if r.Meta.(*GraphNodeResource).Resource.Flags&FlagDeposed == 0 {
|
||||
t.Fatalf("missing FlagDeposed")
|
||||
}
|
||||
|
||||
// Verify that our original structure has not been modified
|
||||
|
@ -47,7 +47,8 @@ const (
|
||||
FlagTainted
|
||||
FlagOrphan
|
||||
FlagHasTainted
|
||||
FlagCreateBeforeDestroy
|
||||
FlagReplacePrimary
|
||||
FlagDeposed
|
||||
)
|
||||
|
||||
// InstanceInfo is used to hold information about the instance and/or
|
||||
|
Loading…
Reference in New Issue
Block a user