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" +}