Skip to content

Commit

Permalink
Make use of the Targets body schema field for modules (#101)
Browse files Browse the repository at this point in the history
* update hcl-lang
* Add list of filenames to module meta

earlydecoder now collects a sorted list of filenames for each module

* Make use of the Targets body schema field for modules

The target will point to the first file of a module

* target main.tf in module if file exists
  • Loading branch information
dbanck authored Apr 6, 2022
1 parent 88c2916 commit d31af62
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 13 deletions.
8 changes: 7 additions & 1 deletion earlydecoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package earlydecoder

import (
"fmt"
"sort"

"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl/v2"
Expand All @@ -11,13 +12,17 @@ import (

func LoadModule(path string, files map[string]*hcl.File) (*module.Meta, hcl.Diagnostics) {
var diags hcl.Diagnostics
filenames := make([]string, 0)

mod := newDecodedModule()
for _, f := range files {
for filename, f := range files {
filenames = append(filenames, filename)
fDiags := loadModuleFromFile(f, mod)
diags = append(diags, fDiags...)
}

sort.Strings(filenames)

var coreRequirements version.Constraints
for _, rc := range mod.RequiredCore {
c, err := version.NewConstraint(rc)
Expand Down Expand Up @@ -166,5 +171,6 @@ func LoadModule(path string, files map[string]*hcl.File) (*module.Meta, hcl.Diag
CoreRequirements: coreRequirements,
Variables: variables,
Outputs: outputs,
Filenames: filenames,
}, diags
}
38 changes: 32 additions & 6 deletions earlydecoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestLoadModule(t *testing.T) {
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand All @@ -56,6 +57,7 @@ terraform {
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand Down Expand Up @@ -95,6 +97,7 @@ provider "grafana" {
},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand Down Expand Up @@ -134,6 +137,7 @@ provider "grafana" {
},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand Down Expand Up @@ -177,6 +181,7 @@ provider "grafana" {
},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand Down Expand Up @@ -250,6 +255,7 @@ provider "grafana" {
},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand Down Expand Up @@ -313,6 +319,7 @@ resource "google_storage_bucket" "bucket" {
},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand Down Expand Up @@ -377,6 +384,7 @@ resource "google_storage_bucket" "bucket" {
},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand Down Expand Up @@ -433,6 +441,7 @@ provider "aws" {
},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand Down Expand Up @@ -495,6 +504,7 @@ provider "aws" {
},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand Down Expand Up @@ -532,6 +542,7 @@ resource "google_something" "test" {
},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand All @@ -555,6 +566,7 @@ variable "" {
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand All @@ -569,6 +581,7 @@ variable {
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
hcl.Diagnostics{
&hcl.Diagnostic{
Expand Down Expand Up @@ -615,6 +628,7 @@ variable "one" "two" {
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
hcl.Diagnostics{
&hcl.Diagnostic{
Expand Down Expand Up @@ -664,7 +678,8 @@ variable "name" {
Type: cty.DynamicPseudoType,
},
},
Outputs: map[string]module.Output{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand All @@ -683,7 +698,8 @@ variable "name" {
Type: cty.String,
},
},
Outputs: map[string]module.Output{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand All @@ -703,7 +719,8 @@ variable "name" {
Description: "description",
},
},
Outputs: map[string]module.Output{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand All @@ -723,7 +740,8 @@ variable "name" {
IsSensitive: true,
},
},
Outputs: map[string]module.Output{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand All @@ -746,7 +764,8 @@ variable "name" {
IsSensitive: true,
},
},
Outputs: map[string]module.Output{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand All @@ -766,7 +785,8 @@ variable "name" {
DefaultValue: cty.EmptyObjectVal,
},
},
Outputs: map[string]module.Output{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand All @@ -784,6 +804,7 @@ output "name" {
Outputs: map[string]module.Output{
"name": {Value: cty.NilVal},
},
Filenames: []string{"test.tf"},
},
nil,
},
Expand All @@ -809,6 +830,7 @@ terraform {
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand All @@ -830,6 +852,7 @@ terraform {
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand All @@ -849,6 +872,7 @@ terraform {
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand All @@ -870,6 +894,7 @@ terraform {
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand All @@ -896,6 +921,7 @@ terraform {
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: []string{"test.tf"},
},
nil,
},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.14
require (
github.com/google/go-cmp v0.5.7
github.com/hashicorp/go-version v1.4.0
github.com/hashicorp/hcl-lang v0.0.0-20220322100058-bdead81fb0c9
github.com/hashicorp/hcl-lang v0.0.0-20220406121211-c20527a75592
github.com/hashicorp/hcl/v2 v2.11.1
github.com/hashicorp/terraform-json v0.13.0
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09
github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go-version v1.4.0 h1:aAQzgqIrRKRa7w75CKpbBxYsmUoPjzVm1W59ca1L0J4=
github.com/hashicorp/go-version v1.4.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/hcl-lang v0.0.0-20220322093838-7d146ea70bc9 h1:KOUGusoj5MubxgHn5VqDvZfTBHZKV7h0b216xyfqeuo=
github.com/hashicorp/hcl-lang v0.0.0-20220322093838-7d146ea70bc9/go.mod h1:oQgcOV8OizFyZfZh3FbQSsQtvtTv8hD23MLAxfn3E+E=
github.com/hashicorp/hcl-lang v0.0.0-20220322100058-bdead81fb0c9 h1:Oj1j/Fl1pFz+4+Th5/y2iUbKfF+UguMkqlF9NMD/s+Q=
github.com/hashicorp/hcl-lang v0.0.0-20220322100058-bdead81fb0c9/go.mod h1:oQgcOV8OizFyZfZh3FbQSsQtvtTv8hD23MLAxfn3E+E=
github.com/hashicorp/hcl-lang v0.0.0-20220406121211-c20527a75592 h1:pSTtkCAbU+SLxw6J59ihqFDX5lJ9xR/fhqaOng1kQXY=
github.com/hashicorp/hcl-lang v0.0.0-20220406121211-c20527a75592/go.mod h1:oQgcOV8OizFyZfZh3FbQSsQtvtTv8hD23MLAxfn3E+E=
github.com/hashicorp/hcl/v2 v2.11.1 h1:yTyWcXcm9XB0TEkyU/JCRU6rYy4K+mgLtzn2wlrJbcc=
github.com/hashicorp/hcl/v2 v2.11.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/terraform-json v0.13.0 h1:Li9L+lKD1FO5RVFRM1mMMIBDoUHslOniyEi5CM+FWGY=
Expand Down
3 changes: 2 additions & 1 deletion module/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
)

type Meta struct {
Path string
Path string
Filenames []string

Backend *Backend
ProviderReferences map[ProviderRef]tfaddr.Provider
Expand Down
32 changes: 32 additions & 0 deletions schema/module_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform-schema/internal/schema/refscope"
"github.com/hashicorp/terraform-schema/module"
"github.com/zclconf/go-cty/cty"
Expand Down Expand Up @@ -90,5 +91,36 @@ func schemaForDependentModuleBlock(localName string, modMeta *module.Meta) (*sch
NestedTargetables: targetableOutputs,
})

if len(modMeta.Filenames) > 0 {
filename := modMeta.Filenames[0]

// Prioritize main.tf based on best practices as documented at
// https://learn.hashicorp.com/tutorials/terraform/module-create
if sliceContains(modMeta.Filenames, "main.tf") {
filename = "main.tf"
}

bodySchema.Targets = &schema.Target{
Path: lang.Path{
Path: modMeta.Path,
LanguageID: "terraform",
},
Range: hcl.Range{
Filename: filename,
Start: hcl.InitialPos,
End: hcl.InitialPos,
},
}
}

return bodySchema, nil
}

func sliceContains(slice []string, value string) bool {
for _, val := range slice {
if val == value {
return true
}
}
return false
}
Loading

0 comments on commit d31af62

Please sign in to comment.