Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform.required_providers provider source #37

Merged
merged 4 commits into from
Dec 12, 2019
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
4 changes: 2 additions & 2 deletions tfconfig/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ func (m *Module) init(diags Diagnostics) {
// case so callers can easily recognize it.
for _, r := range m.ManagedResources {
if _, exists := m.RequiredProviders[r.Provider.Name]; !exists {
m.RequiredProviders[r.Provider.Name] = []string{}
m.RequiredProviders[r.Provider.Name] = &ProviderRequirement{}
}
}
for _, r := range m.DataResources {
if _, exists := m.RequiredProviders[r.Provider.Name]; !exists {
m.RequiredProviders[r.Provider.Name] = []string{}
m.RequiredProviders[r.Provider.Name] = &ProviderRequirement{}
}
}

Expand Down
39 changes: 18 additions & 21 deletions tfconfig/load_hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/hashicorp/hcl/v2/hclsyntax"

"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclparse"
ctyjson "github.com/zclconf/go-cty/cty/json"
)
Expand Down Expand Up @@ -51,18 +51,17 @@ func loadModule(dir string) (*Module, Diagnostics) {
}
}

for _, block := range content.Blocks {
// Our schema only allows required_providers here, so we
// assume that we'll only get that block type.
attrs, attrDiags := block.Body.JustAttributes()
diags = append(diags, attrDiags...)

for name, attr := range attrs {
var version string
valDiags := gohcl.DecodeExpression(attr.Expr, nil, &version)
diags = append(diags, valDiags...)
if !valDiags.HasErrors() {
mod.RequiredProviders[name] = append(mod.RequiredProviders[name], version)
for _, innerBlock := range content.Blocks {
switch innerBlock.Type {
case "required_providers":
reqs, reqsDiags := decodeRequiredProvidersBlock(innerBlock)
diags = append(diags, reqsDiags...)
for name, req := range reqs {
if _, exists := mod.RequiredProviders[name]; !exists {
mod.RequiredProviders[name] = req
} else {
mod.RequiredProviders[name].VersionConstraints = append(mod.RequiredProviders[name].VersionConstraints, req.VersionConstraints...)
}
}
}
}
Expand Down Expand Up @@ -178,22 +177,20 @@ func loadModule(dir string) (*Module, Diagnostics) {
diags = append(diags, contentDiags...)

name := block.Labels[0]

// Even if there isn't an explicit version required, we still
// need an entry in our map to signal the unversioned dependency.
if _, exists := mod.RequiredProviders[name]; !exists {
mod.RequiredProviders[name] = &ProviderRequirement{}
}
if attr, defined := content.Attributes["version"]; defined {
var version string
valDiags := gohcl.DecodeExpression(attr.Expr, nil, &version)
diags = append(diags, valDiags...)
if !valDiags.HasErrors() {
mod.RequiredProviders[name] = append(mod.RequiredProviders[name], version)
mod.RequiredProviders[name].VersionConstraints = append(mod.RequiredProviders[name].VersionConstraints, version)
}
}

// Even if there wasn't an explicit version required, we still
// need an entry in our map to signal the unversioned dependency.
if _, exists := mod.RequiredProviders[name]; !exists {
mod.RequiredProviders[name] = []string{}
}

case "resource", "data":

content, _, contentDiags := block.Body.PartialContent(resourceSchema)
Expand Down
10 changes: 4 additions & 6 deletions tfconfig/load_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,15 @@ func loadModuleLegacyHCL(dir string) (*Module, Diagnostics) {
if err != nil {
return nil, diagnosticsErrorf("invalid provider block at %s: %s", item.Pos(), err)
}

if block.Version != "" {
mod.RequiredProviders[name] = append(mod.RequiredProviders[name], block.Version)
}

// Even if there wasn't an explicit version required, we still
// need an entry in our map to signal the unversioned dependency.
if _, exists := mod.RequiredProviders[name]; !exists {
mod.RequiredProviders[name] = []string{}
mod.RequiredProviders[name] = &ProviderRequirement{}
}

if block.Version != "" {
mod.RequiredProviders[name].VersionConstraints = append(mod.RequiredProviders[name].VersionConstraints, block.Version)
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions tfconfig/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ type Module struct {
Variables map[string]*Variable `json:"variables"`
Outputs map[string]*Output `json:"outputs"`

RequiredCore []string `json:"required_core,omitempty"`
RequiredProviders map[string][]string `json:"required_providers"`
RequiredCore []string `json:"required_core,omitempty"`
RequiredProviders map[string]*ProviderRequirement `json:"required_providers"`

ManagedResources map[string]*Resource `json:"managed_resources"`
DataResources map[string]*Resource `json:"data_resources"`
Expand All @@ -27,7 +27,7 @@ func newModule(path string) *Module {
Path: path,
Variables: make(map[string]*Variable),
Outputs: make(map[string]*Output),
RequiredProviders: make(map[string][]string),
RequiredProviders: make(map[string]*ProviderRequirement),
ManagedResources: make(map[string]*Resource),
DataResources: make(map[string]*Resource),
ModuleCalls: make(map[string]*ModuleCall),
Expand Down
76 changes: 76 additions & 0 deletions tfconfig/provider_ref.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,85 @@
package tfconfig

import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/zclconf/go-cty/cty/gocty"
)

// ProviderRef is a reference to a provider configuration within a module.
// It represents the contents of a "provider" argument in a resource, or
// a value in the "providers" map for a module call.
type ProviderRef struct {
Name string `json:"name"`
Alias string `json:"alias,omitempty"` // Empty if the default provider configuration is referenced
}

type ProviderRequirement struct {
Source string `json:"source,omitempty"`
VersionConstraints []string `json:"version_constraints,omitempty"`
}

func decodeRequiredProvidersBlock(block *hcl.Block) (map[string]*ProviderRequirement, hcl.Diagnostics) {
attrs, diags := block.Body.JustAttributes()
reqs := make(map[string]*ProviderRequirement)
for name, attr := range attrs {
expr, err := attr.Expr.Value(nil)
if err != nil {
diags = append(diags, err...)
}

switch {
case expr.Type().IsPrimitiveType():
var version string
valDiags := gohcl.DecodeExpression(attr.Expr, nil, &version)
diags = append(diags, valDiags...)
if !valDiags.HasErrors() {
reqs[name] = &ProviderRequirement{
VersionConstraints: []string{version},
}
}

case expr.Type().IsObjectType():
var pr ProviderRequirement
if expr.Type().HasAttribute("version") {
var version string
err := gocty.FromCtyValue(expr.GetAttr("version"), &version)
if err == nil {
pr.VersionConstraints = append(pr.VersionConstraints, version)
} else {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unsuitable value type",
Detail: "Unsuitable value: string required",
Subject: attr.Expr.Range().Ptr(),
})
}
}
if expr.Type().HasAttribute("source") {
var source string
err := gocty.FromCtyValue(expr.GetAttr("source"), &source)
if err == nil {
pr.Source = source
} else {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unsuitable value type",
Detail: "Unsuitable value: string required",
Subject: attr.Expr.Range().Ptr(),
})
}
}
reqs[name] = &pr

default:
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unsuitable value type",
Detail: "Unsuitable value: string required",
Subject: attr.Expr.Range().Ptr(),
})
}
}

return reqs, diags
}
5 changes: 1 addition & 4 deletions tfconfig/test-fixtures/basics-json/basics-json.out.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"path": "test-fixtures/basics-json",

"required_providers": {
"null": []
"null": {}
},

"variables": {
"A": {
"name": "A",
Expand Down Expand Up @@ -40,7 +38,6 @@
}
}
},

"managed_resources": {
"null_resource.A": {
"mode": "managed",
Expand Down
5 changes: 1 addition & 4 deletions tfconfig/test-fixtures/basics/basics.out.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"path": "test-fixtures/basics",

"required_providers": {
"null": []
"null": {}
},

"variables": {
"A": {
"name": "A",
Expand Down Expand Up @@ -40,7 +38,6 @@
}
}
},

"managed_resources": {
"null_resource.A": {
"mode": "managed",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
{
"path": "test-fixtures/data-resources",

"required_providers": {
"external": [],
"notexternal": []
"external": {},
"notexternal": {}
},

"variables": {},
"outputs": {},

"managed_resources": {},
"data_resources": {
"data.external.foo": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{
"path": "test-fixtures/legacy-block-labels",

"required_core": [">= 0.11.0"],
"required_core": [
">= 0.11.0"
],
"required_providers": {
"aws": ["1.0.0"],
"notnull": [],
"external": [],
"noversion": []
"aws": {
"version_constraints": [
"1.0.0"
]
},
"notnull": {},
"external": {},
"noversion": {}
},

"variables": {
"foo": {
"name": "foo",
Expand All @@ -30,7 +34,6 @@
}
}
},

"managed_resources": {
"null_resource.foo": {
"mode": "managed",
Expand Down
5 changes: 1 addition & 4 deletions tfconfig/test-fixtures/overrides/overrides.out.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"path": "test-fixtures/overrides",

"required_providers": {
"null": []
"null": {}
},

"variables": {
"A": {
"name": "A",
Expand Down Expand Up @@ -49,7 +47,6 @@
}
}
},

"managed_resources": {
"null_resource.A": {
"mode": "managed",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
{
"path": "test-fixtures/provider-configs",

"required_providers": {
"foo": [],
"bar": ["1.0.0", "1.1.0"],
"baz": ["2.0.0"]
"foo": {},
"bar": {
"version_constraints": [
"1.0.0",
"1.1.0"
]
},
"baz": {
"version_constraints": [
"2.0.0"
]
}
},

"variables": {},
"outputs": {},

"managed_resources": {
"bar_bar.bar": {
"mode": "managed",
Expand Down
21 changes: 21 additions & 0 deletions tfconfig/test-fixtures/provider-source/provider-source.out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"path": "test-fixtures/provider-source",
"required_providers": {
"foo": {
"version_constraints": [
"2.0.0"
]
},
"bat": {
"source": "baz/bat",
"version_constraints": [
"1.0.0"
]
}
},
"variables": {},
"outputs": {},
"managed_resources": {},
"data_resources": {},
"module_calls": {}
}
9 changes: 9 additions & 0 deletions tfconfig/test-fixtures/provider-source/provider-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_providers {
foo = "2.0.0"
bat = {
source = "baz/bat"
version = "1.0.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
{
"path": "test-fixtures/resource-provider-alias",

"required_providers": {
"aws": [],
"notaws": []
"aws": {},
"notaws": {}
},

"variables": {},
"outputs": {},

"managed_resources": {
"aws_instance.foo": {
"mode": "managed",
Expand Down
Loading