From 7162e79fdf8fc24be62371eb1d800d24e837cf19 Mon Sep 17 00:00:00 2001 From: Katy Moe Date: Mon, 15 Nov 2021 10:43:06 +0000 Subject: [PATCH] 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. --- internal/refactoring/move_validate.go | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/internal/refactoring/move_validate.go b/internal/refactoring/move_validate.go index 2c38ba756d..b59bc315ef 100644 --- a/internal/refactoring/move_validate.go +++ b/internal/refactoring/move_validate.go @@ -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: