From 7e7d4c70df18d3400a6923130077e4867f5f2562 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 14 Apr 2017 17:33:18 -0700 Subject: [PATCH] config: allow version constraints on providers, but validate them We now accept syntactically-valid version constraints on provider blocks, though we still don't actually do anything with them. --- config/config.go | 16 ++++++++++------ config/config_test.go | 10 ++++++++-- .../provider-version-invalid/main.tf | 5 +++++ 3 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 config/test-fixtures/provider-version-invalid/main.tf diff --git a/config/config.go b/config/config.go index 83b4e197a..34ce8cf2d 100644 --- a/config/config.go +++ b/config/config.go @@ -8,6 +8,7 @@ import ( "strconv" "strings" + "github.com/blang/semver" "github.com/hashicorp/go-multierror" "github.com/hashicorp/hil" "github.com/hashicorp/hil/ast" @@ -350,8 +351,8 @@ func (c *Config) Validate() error { } } - // Check that providers aren't declared multiple times, and that versions - // aren't used yet since they aren't properly supported. + // Check that providers aren't declared multiple times and that their + // version constraints, where present, are syntactically valid. providerSet := make(map[string]struct{}) for _, p := range c.ProviderConfigs { name := p.FullName() @@ -363,10 +364,13 @@ func (c *Config) Validate() error { } if p.Version != "" { - errs = append(errs, fmt.Errorf( - "provider.%s: version constraints are not yet supported; remove the 'version' argument from configuration", - name, - )) + _, err := semver.ParseRange(p.Version) + if err != nil { + errs = append(errs, fmt.Errorf( + "provider.%s: invalid version constraint %q: %s", + name, p.Version, err, + )) + } } providerSet[name] = struct{}{} diff --git a/config/config_test.go b/config/config_test.go index a70b9831c..dea2ab87e 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -208,10 +208,16 @@ func TestConfigValidate_table(t *testing.T) { "", }, { - "provider with version constraint", + "provider with valid version constraint", "provider-version", + false, + "", + }, + { + "provider with invalid version constraint", + "provider-version-invalid", true, - "version constraints are not yet supported", + "invalid version constraint", }, } diff --git a/config/test-fixtures/provider-version-invalid/main.tf b/config/test-fixtures/provider-version-invalid/main.tf new file mode 100644 index 000000000..a3d4d9cab --- /dev/null +++ b/config/test-fixtures/provider-version-invalid/main.tf @@ -0,0 +1,5 @@ +provider "aws" { + version = "bananas" + a = "a" + b = "b" +}