From 0a5fb40fdfdd3a104112d96035d8c9c7119ffd2c Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Fri, 10 Apr 2020 12:56:55 -0400 Subject: [PATCH] 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. --- command/testdata/providers/{ => basic}/main.tf | 0 configs/config.go | 14 ++++++++++++++ configs/provider_requirements.go | 5 ++--- 3 files changed, 16 insertions(+), 3 deletions(-) rename command/testdata/providers/{ => basic}/main.tf (100%) diff --git a/command/testdata/providers/main.tf b/command/testdata/providers/basic/main.tf similarity index 100% rename from command/testdata/providers/main.tf rename to command/testdata/providers/basic/main.tf diff --git a/configs/config.go b/configs/config.go index c43f1f361..dd562910d 100644 --- a/configs/config.go +++ b/configs/config.go @@ -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 { diff --git a/configs/provider_requirements.go b/configs/provider_requirements.go index 1437df0f7..b535afe0c 100644 --- a/configs/provider_requirements.go +++ b/configs/provider_requirements.go @@ -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