core: Fix schema loading for deleted resources

Resource instances removed from the configuration would previously use
the implied provider address. This is correct for default providers, but
incorrect for those from other namespaces or hosts. The fix here is to
use the stored provider config if it is present.
This commit is contained in:
Alisdair McDiarmid 2021-11-24 15:23:20 -05:00
parent a6b56ad76f
commit 5ddf5e1f7d
2 changed files with 30 additions and 3 deletions

View File

@ -136,6 +136,9 @@ func (n *NodeAbstractResourceInstance) Provider() addrs.Provider {
if n.Config != nil {
return n.Config.Provider
}
if n.storedProviderConfig.Provider.Type != "" {
return n.storedProviderConfig.Provider
}
return addrs.ImpliedProviderForUnqualifiedType(n.Addr.Resource.ContainingResource().ImpliedProvider())
}

View File

@ -15,6 +15,7 @@ func TestNodeAbstractResourceInstanceProvider(t *testing.T) {
tests := []struct {
Addr addrs.AbsResourceInstance
Config *configs.Resource
StoredProviderConfig addrs.AbsProviderConfig
Want addrs.Provider
}{
{
@ -87,6 +88,28 @@ func TestNodeAbstractResourceInstanceProvider(t *testing.T) {
Type: "happycloud",
},
},
{
Addr: addrs.Resource{
Mode: addrs.DataResourceMode,
Type: "null_resource",
Name: "baz",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
Config: nil,
StoredProviderConfig: addrs.AbsProviderConfig{
Module: addrs.RootModule,
Provider: addrs.Provider{
Hostname: addrs.DefaultProviderRegistryHost,
Namespace: "awesomecorp",
Type: "null",
},
},
// The stored provider config overrides the default behavior.
Want: addrs.Provider{
Hostname: addrs.DefaultProviderRegistryHost,
Namespace: "awesomecorp",
Type: "null",
},
},
}
for _, test := range tests {
@ -104,6 +127,7 @@ func TestNodeAbstractResourceInstanceProvider(t *testing.T) {
NodeAbstractResource: NodeAbstractResource{
Config: test.Config,
},
storedProviderConfig: test.StoredProviderConfig,
}
got := node.Provider()
if got != test.Want {