diff --git a/config/config.go b/config/config.go index c47f62a54..05a25216d 100644 --- a/config/config.go +++ b/config/config.go @@ -430,6 +430,15 @@ func (c *Config) Validate() error { } } + // Verify provider points to a provider that is configured + if r.Provider != "" { + if _, ok := providerSet[r.Provider]; !ok { + errs = append(errs, fmt.Errorf( + "%s: resource depends on non-configured provider '%s'", + n, r.Provider)) + } + } + // Verify provisioners don't contain any splats for _, p := range r.Provisioners { // This validation checks that there are now splat variables diff --git a/config/config_test.go b/config/config_test.go index 7ed7f5cb4..64b6d6cef 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -217,6 +217,20 @@ func TestConfigValidate_providerMultiGood(t *testing.T) { } } +func TestConfigValidate_providerMultiRefGood(t *testing.T) { + c := testConfig(t, "validate-provider-multi-ref-good") + if err := c.Validate(); err != nil { + t.Fatalf("should be valid: %s", err) + } +} + +func TestConfigValidate_providerMultiRefBad(t *testing.T) { + c := testConfig(t, "validate-provider-multi-ref-bad") + if err := c.Validate(); err == nil { + t.Fatal("should not be valid") + } +} + func TestConfigValidate_provConnSplatOther(t *testing.T) { c := testConfig(t, "validate-prov-conn-splat-other") if err := c.Validate(); err != nil { diff --git a/config/test-fixtures/validate-provider-multi-ref-bad/main.tf b/config/test-fixtures/validate-provider-multi-ref-bad/main.tf new file mode 100644 index 000000000..dba674bc6 --- /dev/null +++ b/config/test-fixtures/validate-provider-multi-ref-bad/main.tf @@ -0,0 +1,7 @@ +provider "aws" { + alias = "bar" +} + +resource "aws_instance" "foo" { + provider = "aws.baz" +} diff --git a/config/test-fixtures/validate-provider-multi-ref-good/main.tf b/config/test-fixtures/validate-provider-multi-ref-good/main.tf new file mode 100644 index 000000000..bd814edf1 --- /dev/null +++ b/config/test-fixtures/validate-provider-multi-ref-good/main.tf @@ -0,0 +1,7 @@ +provider "aws" { + alias = "bar" +} + +resource "aws_instance" "foo" { + provider = "aws.bar" +}