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

module: Decouple installed and declared modules #109

Merged
merged 1 commit into from
May 9, 2022
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
2 changes: 1 addition & 1 deletion earlydecoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func LoadModule(path string, files map[string]*hcl.File) (*module.Meta, hcl.Diag
outputs[key] = *output
}

modulesCalls := make(map[string]module.ModuleCall)
modulesCalls := make(map[string]module.DeclaredModuleCall)
for key, moduleCall := range mod.ModuleCalls {
modulesCalls[key] = *moduleCall
}
Expand Down
70 changes: 35 additions & 35 deletions earlydecoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestLoadModule(t *testing.T) {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand All @@ -59,7 +59,7 @@ terraform {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand Down Expand Up @@ -100,7 +100,7 @@ provider "grafana" {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand Down Expand Up @@ -141,7 +141,7 @@ provider "grafana" {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand Down Expand Up @@ -186,7 +186,7 @@ provider "grafana" {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand Down Expand Up @@ -261,7 +261,7 @@ provider "grafana" {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand Down Expand Up @@ -326,7 +326,7 @@ resource "google_storage_bucket" "bucket" {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand Down Expand Up @@ -392,7 +392,7 @@ resource "google_storage_bucket" "bucket" {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand Down Expand Up @@ -450,7 +450,7 @@ provider "aws" {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand Down Expand Up @@ -514,7 +514,7 @@ provider "aws" {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand Down Expand Up @@ -553,7 +553,7 @@ resource "google_something" "test" {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand All @@ -578,7 +578,7 @@ variable "" {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand All @@ -594,7 +594,7 @@ variable {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
hcl.Diagnostics{
&hcl.Diagnostic{
Expand Down Expand Up @@ -642,7 +642,7 @@ variable "one" "two" {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
hcl.Diagnostics{
&hcl.Diagnostic{
Expand Down Expand Up @@ -694,7 +694,7 @@ variable "name" {
},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand All @@ -715,7 +715,7 @@ variable "name" {
},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand All @@ -737,7 +737,7 @@ variable "name" {
},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand All @@ -759,7 +759,7 @@ variable "name" {
},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand All @@ -784,7 +784,7 @@ variable "name" {
},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand All @@ -806,7 +806,7 @@ variable "name" {
},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand All @@ -825,7 +825,7 @@ output "name" {
"name": {Value: cty.NilVal},
},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand All @@ -852,7 +852,7 @@ terraform {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand All @@ -875,7 +875,7 @@ terraform {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand All @@ -896,7 +896,7 @@ terraform {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand All @@ -919,7 +919,7 @@ terraform {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand Down Expand Up @@ -947,7 +947,7 @@ terraform {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand All @@ -972,7 +972,7 @@ module "" {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
nil,
},
Expand All @@ -988,7 +988,7 @@ module {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
hcl.Diagnostics{
&hcl.Diagnostic{
Expand Down Expand Up @@ -1036,7 +1036,7 @@ module "one" "two" {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{},
ModuleCalls: map[string]module.DeclaredModuleCall{},
},
hcl.Diagnostics{
&hcl.Diagnostic{
Expand Down Expand Up @@ -1084,7 +1084,7 @@ module "name" {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{
ModuleCalls: map[string]module.DeclaredModuleCall{
"name": {
LocalName: "name",
},
Expand All @@ -1105,7 +1105,7 @@ module "name" {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{
ModuleCalls: map[string]module.DeclaredModuleCall{
"name": {
LocalName: "name",
SourceAddr: "registry.terraform.io/terraform-aws-modules/vpc/aws",
Expand All @@ -1127,10 +1127,10 @@ module "name" {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{
ModuleCalls: map[string]module.DeclaredModuleCall{
"name": {
LocalName: "name",
Version: "> 3.0.0, < 4.0.0",
Version: version.MustConstraints(version.NewConstraint("> 3.0.0, < 4.0.0")),
},
},
},
Expand All @@ -1150,11 +1150,11 @@ module "name" {
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
ModuleCalls: map[string]module.ModuleCall{
ModuleCalls: map[string]module.DeclaredModuleCall{
"name": {
LocalName: "name",
SourceAddr: "terraform-aws-modules/vpc/aws",
Version: "1.0.0",
Version: version.MustConstraints(version.NewConstraint("1.0.0")),
},
},
},
Expand Down
20 changes: 14 additions & 6 deletions earlydecoder/load_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package earlydecoder
import (
"fmt"

"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclsyntax"
Expand All @@ -23,7 +24,7 @@ type decodedModule struct {
DataSources map[string]*dataSource
Variables map[string]*module.Variable
Outputs map[string]*module.Output
ModuleCalls map[string]*module.ModuleCall
ModuleCalls map[string]*module.DeclaredModuleCall
}

func newDecodedModule() *decodedModule {
Expand All @@ -36,7 +37,7 @@ func newDecodedModule() *decodedModule {
DataSources: make(map[string]*dataSource),
Variables: make(map[string]*module.Variable),
Outputs: make(map[string]*module.Output),
ModuleCalls: make(map[string]*module.ModuleCall),
ModuleCalls: make(map[string]*module.DeclaredModuleCall),
}
}

Expand Down Expand Up @@ -285,22 +286,29 @@ func loadModuleFromFile(file *hcl.File, mod *decodedModule) hcl.Diagnostics {
}
name := block.Labels[0]
source := ""
version := ""
var versionCons version.Constraints

var valDiags hcl.Diagnostics
if attr, defined := content.Attributes["source"]; defined {
valDiags = gohcl.DecodeExpression(attr.Expr, nil, &source)
diags = append(diags, valDiags...)
}
if attr, defined := content.Attributes["version"]; defined {
valDiags = gohcl.DecodeExpression(attr.Expr, nil, &version)
var versionStr string
valDiags = gohcl.DecodeExpression(attr.Expr, nil, &versionStr)
diags = append(diags, valDiags...)
if versionStr != "" {
vc, err := version.NewConstraint(versionStr)
if err == nil {
versionCons = vc
}
}
}

mod.ModuleCalls[name] = &module.ModuleCall{
mod.ModuleCalls[name] = &module.DeclaredModuleCall{
LocalName: name,
SourceAddr: source,
Version: version,
Version: versionCons,
}
}

Expand Down
9 changes: 1 addition & 8 deletions module/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Meta struct {
CoreRequirements version.Constraints
Variables map[string]Variable
Outputs map[string]Output
ModuleCalls map[string]ModuleCall
ModuleCalls map[string]DeclaredModuleCall
}

type ProviderRequirements map[tfaddr.Provider]version.Constraints
Expand Down Expand Up @@ -67,10 +67,3 @@ type ProviderRef struct {
// configuration this address refers to.
Alias string
}

type ModuleCall struct {
LocalName string
SourceAddr string
Version string
Path string
}
Loading