introduce AbsMoveableResource

AbsMoveableResource is an AbsMoveable that is either a resource or a resource
instance. This interface represents a moveable that has a resource type.
This commit is contained in:
Katy Moe 2021-11-15 11:03:33 +00:00
parent 7162e79fdf
commit 11566b1d11
No known key found for this signature in database
GPG Key ID: 8C3780F6DCDDA885
3 changed files with 25 additions and 16 deletions

View File

@ -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.

View File

@ -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)
}

View File

@ -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: