Skip to content

Commit

Permalink
configs: deprecate version argument inside provider configuration blo…
Browse files Browse the repository at this point in the history
…cks (#26135)

The version argument is deprecated in Terraform v0.14 in favor of
required_providers and will be removed in a future version of terraform
(expected to be v0.15). The provider configuration documentation already
discourages use of 'version' inside provider configuration blocks, so it
only needed an extra note that it is actively deprecated.
  • Loading branch information
mildwonkey committed Sep 8, 2020
1 parent 8f9671b commit 923e157
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 11 deletions.
14 changes: 12 additions & 2 deletions configs/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ func TestConfigResolveAbsProviderAddr(t *testing.T) {

func TestConfigProviderRequirements(t *testing.T) {
cfg, diags := testNestedModuleConfigFromDir(t, "testdata/provider-reqs")
assertNoDiagnostics(t, diags)
// TODO: Version Constraint Deprecation.
// Once we've removed the version argument from provider configuration
// blocks, this can go back to expected 0 diagnostics.
// assertNoDiagnostics(t, diags)
assertDiagnosticCount(t, diags, 1)
assertDiagnosticSummary(t, diags, "Version constraints inside provider configuration blocks are deprecated")

tlsProvider := addrs.NewProvider(
addrs.DefaultRegistryHost,
Expand Down Expand Up @@ -153,7 +158,12 @@ func TestConfigProviderRequirements(t *testing.T) {

func TestConfigProviderRequirementsByModule(t *testing.T) {
cfg, diags := testNestedModuleConfigFromDir(t, "testdata/provider-reqs")
assertNoDiagnostics(t, diags)
// TODO: Version Constraint Deprecation.
// Once we've removed the version argument from provider configuration
// blocks, this can go back to expected 0 diagnostics.
// assertNoDiagnostics(t, diags)
assertDiagnosticCount(t, diags, 1)
assertDiagnosticSummary(t, diags, "Version constraints inside provider configuration blocks are deprecated")

tlsProvider := addrs.NewProvider(
addrs.DefaultRegistryHost,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
provider "aws" {
version = "1.0"
region = "us-west-2"
}

output "my_output" {
value = "my output"
}

5 changes: 3 additions & 2 deletions configs/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,12 @@ func testNestedModuleConfigFromDir(t *testing.T, path string) (*Config, hcl.Diag

parser := NewParser(nil)
mod, diags := parser.LoadConfigDir(path)
assertNoDiagnostics(t, diags)
if mod == nil {
t.Fatal("got nil root module; want non-nil")
}

versionI := 0
cfg, diags := BuildConfig(mod, ModuleWalkerFunc(
cfg, nestedDiags := BuildConfig(mod, ModuleWalkerFunc(
func(req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics) {
// For the sake of this test we're going to just treat our
// SourceAddr as a path relative to the calling module.
Expand All @@ -111,6 +110,8 @@ func testNestedModuleConfigFromDir(t *testing.T, path string) (*Config, hcl.Diag
return mod, version, diags
},
))

diags = append(diags, nestedDiags...)
return cfg, diags
}

Expand Down
6 changes: 6 additions & 0 deletions configs/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ func decodeProviderBlock(block *hcl.Block) (*Provider, hcl.Diagnostics) {
}

if attr, exists := content.Attributes["version"]; exists {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Version constraints inside provider configuration blocks are deprecated",
Detail: "Terraform 0.13 and earlier allowed provider version constraints inside the provider configuration block, but that is now deprecated and will be removed in a future version of Terraform. To silence this warning, move the provider version constraint into the required_providers block.",
Subject: attr.Expr.Range().Ptr(),
})
var versionDiags hcl.Diagnostics
provider.Version, versionDiags = decodeVersionConstraint(attr)
diags = append(diags, versionDiags...)
Expand Down
2 changes: 2 additions & 0 deletions configs/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func TestProviderReservedNames(t *testing.T) {
_, diags := parser.LoadConfigFile("config.tf")

assertExactDiagnostics(t, diags, []string{
//TODO: This deprecation warning will be removed in terraform v0.15.
`config.tf:4,13-20: Version constraints inside provider configuration blocks are deprecated; Terraform 0.13 and earlier allowed provider version constraints inside the provider configuration block, but that is now deprecated and will be removed in a future version of Terraform. To silence this warning, move the provider version constraint into the required_providers block.`,
`config.tf:10,3-8: Reserved argument name in provider block; The provider argument name "count" is reserved for use by Terraform in a future version.`,
`config.tf:11,3-13: Reserved argument name in provider block; The provider argument name "depends_on" is reserved for use by Terraform in a future version.`,
`config.tf:12,3-11: Reserved argument name in provider block; The provider argument name "for_each" is reserved for use by Terraform in a future version.`,
Expand Down
2 changes: 0 additions & 2 deletions configs/testdata/valid-files/provider-configs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ provider "foo" {
}

provider "bar" {
version = ">= 1.0.2"

other = 12
}

Expand Down
6 changes: 3 additions & 3 deletions website/docs/configuration/providers.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ works the same way as the `version` argument in a
constraint in a provider configuration is only used if `required_providers`
does not include one for that provider.

**We do not recommend using the `version` argument in provider configurations.**
**The `version` argument in provider configurations is deprecated.**
In Terraform 0.13 and later, version constraints should always be declared in
[the `required_providers` block](./provider-requirements.html).
[the `required_providers` block](./provider-requirements.html). The `version`
argument will be removed in a future version of Terraform.

-> **Note:** The `version` meta-argument made sense before Terraform 0.13, since
Terraform could only install providers that were distributed by HashiCorp. Now
that Terraform can install providers from multiple sources, it makes more sense
to keep version constraints and provider source addresses together.

0 comments on commit 923e157

Please sign in to comment.