Skip to content

Commit

Permalink
tfconfig: decode provider aliases (hashicorp#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Nov 2, 2020
1 parent b049036 commit 0c45ba3
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 3 deletions.
15 changes: 15 additions & 0 deletions tfconfig/load_hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,21 @@ func LoadModuleFromFile(file *hcl.File, mod *Module) hcl.Diagnostics {
}
}

providerKey := name
var alias string
if attr, defined := content.Attributes["alias"]; defined {
valDiags := gohcl.DecodeExpression(attr.Expr, nil, &alias)
diags = append(diags, valDiags...)
if !valDiags.HasErrors() && alias != "" {
providerKey = fmt.Sprintf("%s.%s", name, alias)
}
}

mod.ProviderConfigs[providerKey] = &ProviderConfig{
Name: name,
Alias: alias,
}

case "resource", "data":

content, _, contentDiags := block.Body.PartialContent(resourceSchema)
Expand Down
14 changes: 11 additions & 3 deletions tfconfig/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,31 @@ type Module struct {
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"`
ModuleCalls map[string]*ModuleCall `json:"module_calls"`
ProviderConfigs map[string]*ProviderConfig `json:"provider_configs,omitempty"`
ManagedResources map[string]*Resource `json:"managed_resources"`
DataResources map[string]*Resource `json:"data_resources"`
ModuleCalls map[string]*ModuleCall `json:"module_calls"`

// Diagnostics records any errors and warnings that were detected during
// loading, primarily for inclusion in serialized forms of the module
// since this slice is also returned as a second argument from LoadModule.
Diagnostics Diagnostics `json:"diagnostics,omitempty"`
}

// ProviderConfig represents a provider block in the configuration
type ProviderConfig struct {
Name string `json:"name"`
Alias string `json:"alias,omitempty"`
}

// NewModule creates new Module representing Terraform module at the given path
func NewModule(path string) *Module {
return &Module{
Path: path,
Variables: make(map[string]*Variable),
Outputs: make(map[string]*Output),
RequiredProviders: make(map[string]*ProviderRequirement),
ProviderConfigs: make(map[string]*ProviderConfig),
ManagedResources: make(map[string]*Resource),
DataResources: make(map[string]*Resource),
ModuleCalls: make(map[string]*ModuleCall),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
}
}
},
"provider_configs": {
"aws": {"name": "aws"},
"noversion": {"name": "noversion"}
},
"managed_resources": {
"null_resource.foo": {
"mode": "managed",
Expand Down
22 changes: 22 additions & 0 deletions tfconfig/testdata/provider-aliases/provider-aliases.out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"path": "testdata/provider-aliases",
"variables": {},
"outputs": {},
"required_providers": {
"bar": {},
"baz": {},
"empty": {},
"foo": {}
},
"provider_configs": {
"bar.yellow": {"name": "bar", "alias": "yellow"},
"baz": {"name": "baz"},
"empty": {"name": "empty"},
"bar": {"name": "bar"},
"foo.blue": {"name": "foo", "alias": "blue"},
"foo.red": {"name": "foo", "alias": "red"}
},
"managed_resources": {},
"data_resources": {},
"module_calls": {}
}
9 changes: 9 additions & 0 deletions tfconfig/testdata/provider-aliases/provider-aliases.out.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

# Module `testdata/provider-aliases`

Provider Requirements:
* **bar:** (any version)
* **baz:** (any version)
* **empty:** (any version)
* **foo:** (any version)

21 changes: 21 additions & 0 deletions tfconfig/testdata/provider-aliases/provider-aliases.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
provider "foo" {
alias = "blue"
}

provider "foo" {
alias = "red"
}

provider "bar" {
}

provider "bar" {
alias = "yellow"
}

provider "baz" {
}

provider "empty" {
alias = ""
}
4 changes: 4 additions & 0 deletions tfconfig/testdata/provider-configs/provider-configs.out.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
},
"variables": {},
"outputs": {},
"provider_configs": {
"foo": {"name": "foo"},
"bar": {"name": "bar"}
},
"managed_resources": {
"bar_bar.bar": {
"mode": "managed",
Expand Down
3 changes: 3 additions & 0 deletions tfconfig/testdata/type-conversions/type-conversions.out.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
}
}
},
"provider_configs": {
"foo": {"name": "foo"}
},
"managed_resources": {
"foo.foo": {
"mode": "managed",
Expand Down
3 changes: 3 additions & 0 deletions tfconfig/testdata/type-errors/type-errors.out.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@
}
}
},
"provider_configs": {
"foo": {"name": "foo"}
},
"managed_resources": {
"foo.foo": {
"mode": "managed",
Expand Down

0 comments on commit 0c45ba3

Please sign in to comment.