return error on resource type mismatch

A resource move is invalid if the two resource( instance)s are of different
resource types. Check for this during move validation.
This commit is contained in:
Katy Moe 2021-11-15 10:43:06 +00:00
parent dc3ed6271c
commit 7162e79fdf
No known key found for this signature in database
GPG Key ID: 8C3780F6DCDDA885
1 changed files with 36 additions and 0 deletions

View File

@ -178,6 +178,16 @@ func ValidateMoves(stmts []MoveStatement, rootCfg *configs.Config, declaredInsts
}
}
// Resource types must match.
if resourceTypesDiffer(absFrom, absTo) {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Resource type mismatch",
Detail: fmt.Sprintf(
"This statement declares a move from %s to %s, which is a %s of a different type.", absFrom, absTo, noun,
),
})
}
}
}
@ -238,6 +248,32 @@ func moveableObjectExists(addr addrs.AbsMoveable, in instances.Set) bool {
}
}
func resourceTypesDiffer(absFrom, absTo addrs.AbsMoveable) bool {
switch absFrom.(type) {
case addrs.AbsResourceInstance, addrs.AbsResource:
// addrs.UnifyMoveEndpoints guarantees that both addresses are of the
// same kind, so at this point we can assume that absTo is an
// addrs.AbsResourceInstance or addrs.AbsResource.
return absMoveableResourceType(absFrom) != absMoveableResourceType(absTo)
default:
return false
}
}
// absMoveableResourceType returns the type of the supplied
// addrs.AbsResourceInstance or addrs.AbsResource, or "" for other AbsMoveable
// types.
func absMoveableResourceType(addr addrs.AbsMoveable) string {
switch addr := addr.(type) {
case addrs.AbsResourceInstance:
return addr.Resource.Resource.Type
case addrs.AbsResource:
return addr.Resource.Type
default:
return ""
}
}
func movableObjectDeclRange(addr addrs.AbsMoveable, cfg *configs.Config) (tfdiags.SourceRange, bool) {
switch addr := addr.(type) {
case addrs.ModuleInstance: