From 072322336e2f896e6cedf3388136fe942ec08196 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Mon, 23 Apr 2018 17:19:55 -0700 Subject: [PATCH] addrs: AbsProviderConfig.Inherited method This helper deals with the address wrangling required to find the address that a provider configuration might inherit from if no explicit configuration is given and instead configuration is taken from the parent module. This method is not generally useful, and is here mainly just to help the provider-related graph transformations in the main terraform package. --- addrs/provider_config.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/addrs/provider_config.go b/addrs/provider_config.go index 7e5b5eec0..e10090569 100644 --- a/addrs/provider_config.go +++ b/addrs/provider_config.go @@ -174,6 +174,34 @@ func ParseAbsProviderConfig(traversal hcl.Traversal) (AbsProviderConfig, tfdiags return ret, diags } +// Inherited returns an address that the receiving configuration address might +// inherit from in a parent module. The second bool return value indicates if +// such inheritance is possible, and thus whether the returned address is valid. +// +// Inheritance is possible only for default (un-aliased) providers in modules +// other than the root module. Even if a valid address is returned, inheritence +// may not be performed for other reasons, such as if the calling module +// provided explicit provider configurations within the call for this module. +// The ProviderTransformer graph transform in the main terraform module has +// the authoritative logic for provider inheritance, and this method is here +// mainly just for its benefit. +func (pc AbsProviderConfig) Inherited() (AbsProviderConfig, bool) { + // Can't inherit if we're already in the root. + if len(pc.Module) == 0 { + return AbsProviderConfig{}, false + } + + // Can't inherit if we have an alias. + if pc.ProviderConfig.Alias != "" { + return AbsProviderConfig{}, false + } + + // Otherwise, we might inherit from a configuration with the same + // provider name in the parent module instance. + parentMod := pc.Module.Parent() + return pc.ProviderConfig.Absolute(parentMod), true +} + func (pc AbsProviderConfig) String() string { if len(pc.Module) == 0 { return pc.ProviderConfig.String()