configs: include provider configs in ProviderRequirements()

This PR adds iteration through any provider configuration blocks in the
config in addProviderRequirements().

A stale comment (of mine!) would leave one expecting the
module.ProviderRequirements to include any requirements from provider
configs. The comment was inaccurate and has been updated.
This commit is contained in:
Kristin Laemmert 2020-04-10 12:56:55 -04:00
parent 5ddb1a5808
commit 0a5fb40fdf
3 changed files with 16 additions and 3 deletions

View File

@ -223,6 +223,20 @@ func (c *Config) addProviderRequirements(reqs getproviders.Requirements) hcl.Dia
reqs[fqn] = nil
}
// "provider" block can also contain version constraints
for name, provider := range c.Module.ProviderConfigs {
fqn := c.Module.ProviderForLocalConfig(addrs.LocalProviderConfig{LocalName: name})
if _, ok := reqs[fqn]; !ok {
// We'll at least have an unconstrained dependency then, but might
// add to this in the loop below.
reqs[fqn] = nil
}
if provider.Version.Required != nil {
constraints := getproviders.MustParseVersionConstraints(provider.Version.Required.String())
reqs[fqn] = append(reqs[fqn], constraints...)
}
}
// ...and now we'll recursively visit all of the child modules to merge
// in their requirements too.
for _, childConfig := range c.Children {

View File

@ -21,9 +21,8 @@ type Source struct {
DeclRange hcl.Range
}
// ProviderRequirements represents merged provider version constraints.
// VersionConstraints come from terraform.require_providers blocks and provider
// blocks.
// ProviderRequirements represents provider version constraints from
// required_providers blocks.
type ProviderRequirements struct {
Type addrs.Provider
VersionConstraints []VersionConstraint