diff --git a/internal/addrs/moveable.go b/internal/addrs/moveable.go index dcbd8c03e..083fcfb27 100644 --- a/internal/addrs/moveable.go +++ b/internal/addrs/moveable.go @@ -24,6 +24,19 @@ var ( _ AbsMoveable = AbsModuleCall{} ) +// AbsMoveableResource is an AbsMoveable that is either a resource or a resource +// instance. +type AbsMoveableResource interface { + AbsMoveable + Type() string +} + +// The following are all of the possible AbsMoveableResource types: +var ( + _ AbsMoveableResource = AbsResource{} + _ AbsMoveableResource = AbsResourceInstance{} +) + // ConfigMoveable is similar to AbsMoveable but represents a static object in // the configuration, rather than an instance of that object created by // module expansion. diff --git a/internal/addrs/resource.go b/internal/addrs/resource.go index 0ebf89d99..9c81c0edd 100644 --- a/internal/addrs/resource.go +++ b/internal/addrs/resource.go @@ -186,6 +186,10 @@ func (r AbsResource) String() string { return fmt.Sprintf("%s.%s", r.Module.String(), r.Resource.String()) } +func (r AbsResource) Type() string { + return r.Resource.Type +} + func (r AbsResource) Equal(o AbsResource) bool { return r.Module.Equal(o.Module) && r.Resource.Equal(o.Resource) } @@ -267,6 +271,10 @@ func (r AbsResourceInstance) String() string { return fmt.Sprintf("%s.%s", r.Module.String(), r.Resource.String()) } +func (r AbsResourceInstance) Type() string { + return r.Resource.Resource.Type +} + func (r AbsResourceInstance) Equal(o AbsResourceInstance) bool { return r.Module.Equal(o.Module) && r.Resource.Equal(o.Resource) } diff --git a/internal/refactoring/move_validate.go b/internal/refactoring/move_validate.go index b59bc315e..b393c0a0b 100644 --- a/internal/refactoring/move_validate.go +++ b/internal/refactoring/move_validate.go @@ -251,29 +251,17 @@ func moveableObjectExists(addr addrs.AbsMoveable, in instances.Set) bool { func resourceTypesDiffer(absFrom, absTo addrs.AbsMoveable) bool { switch absFrom.(type) { case addrs.AbsResourceInstance, addrs.AbsResource: + absFrom := absFrom.(addrs.AbsMoveableResource) // addrs.UnifyMoveEndpoints guarantees that both addresses are of the - // same kind, so at this point we can assume that absTo is an + // same kind, so at this point we can assume that absTo is also an // addrs.AbsResourceInstance or addrs.AbsResource. - return absMoveableResourceType(absFrom) != absMoveableResourceType(absTo) + absTo := absTo.(addrs.AbsMoveableResource) + return absFrom.Type() != absTo.Type() 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: