Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions command/testdata/providers-schema/required/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"format_version": "0.1",
"provider_schemas": {
"registry.terraform.io/hashicorp/test": {
"provider": {
"version": 0,
"block": {
"attributes": {
"region": {
"description_kind": "plain",
"optional": true,
"type": "string"
}
},
"description_kind": "plain"
}
},
"resource_schemas": {
"test_instance": {
"version": 0,
"block": {
"attributes": {
"ami": {
"type": "string",
"optional": true,
"description_kind": "plain"
},
"id": {
"type": "string",
"optional": true,
"computed": true,
"description_kind": "plain"
}
},
"description_kind": "plain"
}
}
}
}
}
}
7 changes: 7 additions & 0 deletions command/testdata/providers-schema/required/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
terraform {
required_providers {
test = {
source = "hashicorp/test"
}
}
}
37 changes: 7 additions & 30 deletions configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ func (c *Config) addProviderRequirements(reqs getproviders.Requirements, recurse
var diags hcl.Diagnostics

// First we'll deal with the requirements directly in _our_ module...
if c.Module.ProviderRequirements != nil {
for _, providerReqs := range c.Module.ProviderRequirements.RequiredProviders {
fqn := providerReqs.Type
if _, ok := reqs[fqn]; !ok {
Expand Down Expand Up @@ -253,6 +254,8 @@ func (c *Config) addProviderRequirements(reqs getproviders.Requirements, recurse
}
reqs[fqn] = append(reqs[fqn], constraints...)
}
}

// Each resource in the configuration creates an *implicit* provider
// dependency, though we'll only record it if there isn't already
// an explicit dependency on the same provider.
Expand Down Expand Up @@ -323,11 +326,11 @@ func (c *Config) addProviderRequirements(reqs getproviders.Requirements, recurse
// provider version selection in an earlier step and have identified suitable
// versions for each provider.
func (c *Config) ProviderTypes() []addrs.Provider {
m := make(map[addrs.Provider]struct{})
c.gatherProviderTypes(m)
// Ignore diagnostics here because they relate to version constraints
reqs, _ := c.ProviderRequirements()

ret := make([]addrs.Provider, 0, len(m))
for k := range m {
ret := make([]addrs.Provider, 0, len(reqs))
for k := range reqs {
ret = append(ret, k)
}
sort.Slice(ret, func(i, j int) bool {
Expand All @@ -336,32 +339,6 @@ func (c *Config) ProviderTypes() []addrs.Provider {
return ret
}

func (c *Config) gatherProviderTypes(m map[addrs.Provider]struct{}) {
if c == nil {
return
}

for _, pc := range c.Module.ProviderConfigs {
fqn := c.Module.ProviderForLocalConfig(addrs.LocalProviderConfig{LocalName: pc.Name})
m[fqn] = struct{}{}
}
for _, rc := range c.Module.ManagedResources {
providerAddr := rc.ProviderConfigAddr()
fqn := c.Module.ProviderForLocalConfig(providerAddr)
m[fqn] = struct{}{}
}
for _, rc := range c.Module.DataResources {
providerAddr := rc.ProviderConfigAddr()
fqn := c.Module.ProviderForLocalConfig(providerAddr)
m[fqn] = struct{}{}
}

// Must also visit our child modules, recursively.
for _, cc := range c.Children {
cc.gatherProviderTypes(m)
}
}

// ResolveAbsProviderAddr returns the AbsProviderConfig represented by the given
// ProviderConfig address, which must not be nil or this method will panic.
//
Expand Down
1 change: 1 addition & 0 deletions configs/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestConfigProviderTypes(t *testing.T) {
addrs.NewDefaultProvider("aws"),
addrs.NewDefaultProvider("null"),
addrs.NewDefaultProvider("template"),
addrs.NewDefaultProvider("test"),
}
for _, problem := range deep.Equal(got, want) {
t.Error(problem)
Expand Down
8 changes: 8 additions & 0 deletions configs/testdata/valid-files/providers-explicit-implied.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ resource "aws_instance" "foo" {
resource "null_resource" "foo" {

}

terraform {
required_providers {
test = {
source = "hashicorp/test"
}
}
}