Skip to content

Commit d31af62

Browse files
authored
Make use of the Targets body schema field for modules (#101)
* 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
1 parent 88c2916 commit d31af62

File tree

7 files changed

+184
-13
lines changed

7 files changed

+184
-13
lines changed

earlydecoder/decoder.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package earlydecoder
22

33
import (
44
"fmt"
5+
"sort"
56

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

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

1517
mod := newDecodedModule()
16-
for _, f := range files {
18+
for filename, f := range files {
19+
filenames = append(filenames, filename)
1720
fDiags := loadModuleFromFile(f, mod)
1821
diags = append(diags, fDiags...)
1922
}
2023

24+
sort.Strings(filenames)
25+
2126
var coreRequirements version.Constraints
2227
for _, rc := range mod.RequiredCore {
2328
c, err := version.NewConstraint(rc)
@@ -166,5 +171,6 @@ func LoadModule(path string, files map[string]*hcl.File) (*module.Meta, hcl.Diag
166171
CoreRequirements: coreRequirements,
167172
Variables: variables,
168173
Outputs: outputs,
174+
Filenames: filenames,
169175
}, diags
170176
}

earlydecoder/decoder_test.go

+32-6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func TestLoadModule(t *testing.T) {
4040
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
4141
Variables: map[string]module.Variable{},
4242
Outputs: map[string]module.Output{},
43+
Filenames: []string{"test.tf"},
4344
},
4445
nil,
4546
},
@@ -56,6 +57,7 @@ terraform {
5657
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
5758
Variables: map[string]module.Variable{},
5859
Outputs: map[string]module.Output{},
60+
Filenames: []string{"test.tf"},
5961
},
6062
nil,
6163
},
@@ -95,6 +97,7 @@ provider "grafana" {
9597
},
9698
Variables: map[string]module.Variable{},
9799
Outputs: map[string]module.Output{},
100+
Filenames: []string{"test.tf"},
98101
},
99102
nil,
100103
},
@@ -134,6 +137,7 @@ provider "grafana" {
134137
},
135138
Variables: map[string]module.Variable{},
136139
Outputs: map[string]module.Output{},
140+
Filenames: []string{"test.tf"},
137141
},
138142
nil,
139143
},
@@ -177,6 +181,7 @@ provider "grafana" {
177181
},
178182
Variables: map[string]module.Variable{},
179183
Outputs: map[string]module.Output{},
184+
Filenames: []string{"test.tf"},
180185
},
181186
nil,
182187
},
@@ -250,6 +255,7 @@ provider "grafana" {
250255
},
251256
Variables: map[string]module.Variable{},
252257
Outputs: map[string]module.Output{},
258+
Filenames: []string{"test.tf"},
253259
},
254260
nil,
255261
},
@@ -313,6 +319,7 @@ resource "google_storage_bucket" "bucket" {
313319
},
314320
Variables: map[string]module.Variable{},
315321
Outputs: map[string]module.Output{},
322+
Filenames: []string{"test.tf"},
316323
},
317324
nil,
318325
},
@@ -377,6 +384,7 @@ resource "google_storage_bucket" "bucket" {
377384
},
378385
Variables: map[string]module.Variable{},
379386
Outputs: map[string]module.Output{},
387+
Filenames: []string{"test.tf"},
380388
},
381389
nil,
382390
},
@@ -433,6 +441,7 @@ provider "aws" {
433441
},
434442
Variables: map[string]module.Variable{},
435443
Outputs: map[string]module.Output{},
444+
Filenames: []string{"test.tf"},
436445
},
437446
nil,
438447
},
@@ -495,6 +504,7 @@ provider "aws" {
495504
},
496505
Variables: map[string]module.Variable{},
497506
Outputs: map[string]module.Output{},
507+
Filenames: []string{"test.tf"},
498508
},
499509
nil,
500510
},
@@ -532,6 +542,7 @@ resource "google_something" "test" {
532542
},
533543
Variables: map[string]module.Variable{},
534544
Outputs: map[string]module.Output{},
545+
Filenames: []string{"test.tf"},
535546
},
536547
nil,
537548
},
@@ -555,6 +566,7 @@ variable "" {
555566
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
556567
Variables: map[string]module.Variable{},
557568
Outputs: map[string]module.Output{},
569+
Filenames: []string{"test.tf"},
558570
},
559571
nil,
560572
},
@@ -569,6 +581,7 @@ variable {
569581
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
570582
Variables: map[string]module.Variable{},
571583
Outputs: map[string]module.Output{},
584+
Filenames: []string{"test.tf"},
572585
},
573586
hcl.Diagnostics{
574587
&hcl.Diagnostic{
@@ -615,6 +628,7 @@ variable "one" "two" {
615628
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
616629
Variables: map[string]module.Variable{},
617630
Outputs: map[string]module.Output{},
631+
Filenames: []string{"test.tf"},
618632
},
619633
hcl.Diagnostics{
620634
&hcl.Diagnostic{
@@ -664,7 +678,8 @@ variable "name" {
664678
Type: cty.DynamicPseudoType,
665679
},
666680
},
667-
Outputs: map[string]module.Output{},
681+
Outputs: map[string]module.Output{},
682+
Filenames: []string{"test.tf"},
668683
},
669684
nil,
670685
},
@@ -683,7 +698,8 @@ variable "name" {
683698
Type: cty.String,
684699
},
685700
},
686-
Outputs: map[string]module.Output{},
701+
Outputs: map[string]module.Output{},
702+
Filenames: []string{"test.tf"},
687703
},
688704
nil,
689705
},
@@ -703,7 +719,8 @@ variable "name" {
703719
Description: "description",
704720
},
705721
},
706-
Outputs: map[string]module.Output{},
722+
Outputs: map[string]module.Output{},
723+
Filenames: []string{"test.tf"},
707724
},
708725
nil,
709726
},
@@ -723,7 +740,8 @@ variable "name" {
723740
IsSensitive: true,
724741
},
725742
},
726-
Outputs: map[string]module.Output{},
743+
Outputs: map[string]module.Output{},
744+
Filenames: []string{"test.tf"},
727745
},
728746
nil,
729747
},
@@ -746,7 +764,8 @@ variable "name" {
746764
IsSensitive: true,
747765
},
748766
},
749-
Outputs: map[string]module.Output{},
767+
Outputs: map[string]module.Output{},
768+
Filenames: []string{"test.tf"},
750769
},
751770
nil,
752771
},
@@ -766,7 +785,8 @@ variable "name" {
766785
DefaultValue: cty.EmptyObjectVal,
767786
},
768787
},
769-
Outputs: map[string]module.Output{},
788+
Outputs: map[string]module.Output{},
789+
Filenames: []string{"test.tf"},
770790
},
771791
nil,
772792
},
@@ -784,6 +804,7 @@ output "name" {
784804
Outputs: map[string]module.Output{
785805
"name": {Value: cty.NilVal},
786806
},
807+
Filenames: []string{"test.tf"},
787808
},
788809
nil,
789810
},
@@ -809,6 +830,7 @@ terraform {
809830
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
810831
Variables: map[string]module.Variable{},
811832
Outputs: map[string]module.Output{},
833+
Filenames: []string{"test.tf"},
812834
},
813835
nil,
814836
},
@@ -830,6 +852,7 @@ terraform {
830852
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
831853
Variables: map[string]module.Variable{},
832854
Outputs: map[string]module.Output{},
855+
Filenames: []string{"test.tf"},
833856
},
834857
nil,
835858
},
@@ -849,6 +872,7 @@ terraform {
849872
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
850873
Variables: map[string]module.Variable{},
851874
Outputs: map[string]module.Output{},
875+
Filenames: []string{"test.tf"},
852876
},
853877
nil,
854878
},
@@ -870,6 +894,7 @@ terraform {
870894
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
871895
Variables: map[string]module.Variable{},
872896
Outputs: map[string]module.Output{},
897+
Filenames: []string{"test.tf"},
873898
},
874899
nil,
875900
},
@@ -896,6 +921,7 @@ terraform {
896921
ProviderRequirements: map[tfaddr.Provider]version.Constraints{},
897922
Variables: map[string]module.Variable{},
898923
Outputs: map[string]module.Output{},
924+
Filenames: []string{"test.tf"},
899925
},
900926
nil,
901927
},

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.14
55
require (
66
github.com/google/go-cmp v0.5.7
77
github.com/hashicorp/go-version v1.4.0
8-
github.com/hashicorp/hcl-lang v0.0.0-20220322100058-bdead81fb0c9
8+
github.com/hashicorp/hcl-lang v0.0.0-20220406121211-c20527a75592
99
github.com/hashicorp/hcl/v2 v2.11.1
1010
github.com/hashicorp/terraform-json v0.13.0
1111
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896

go.sum

+2-4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09
2929
github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
3030
github.com/hashicorp/go-version v1.4.0 h1:aAQzgqIrRKRa7w75CKpbBxYsmUoPjzVm1W59ca1L0J4=
3131
github.com/hashicorp/go-version v1.4.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
32-
github.com/hashicorp/hcl-lang v0.0.0-20220322093838-7d146ea70bc9 h1:KOUGusoj5MubxgHn5VqDvZfTBHZKV7h0b216xyfqeuo=
33-
github.com/hashicorp/hcl-lang v0.0.0-20220322093838-7d146ea70bc9/go.mod h1:oQgcOV8OizFyZfZh3FbQSsQtvtTv8hD23MLAxfn3E+E=
34-
github.com/hashicorp/hcl-lang v0.0.0-20220322100058-bdead81fb0c9 h1:Oj1j/Fl1pFz+4+Th5/y2iUbKfF+UguMkqlF9NMD/s+Q=
35-
github.com/hashicorp/hcl-lang v0.0.0-20220322100058-bdead81fb0c9/go.mod h1:oQgcOV8OizFyZfZh3FbQSsQtvtTv8hD23MLAxfn3E+E=
32+
github.com/hashicorp/hcl-lang v0.0.0-20220406121211-c20527a75592 h1:pSTtkCAbU+SLxw6J59ihqFDX5lJ9xR/fhqaOng1kQXY=
33+
github.com/hashicorp/hcl-lang v0.0.0-20220406121211-c20527a75592/go.mod h1:oQgcOV8OizFyZfZh3FbQSsQtvtTv8hD23MLAxfn3E+E=
3634
github.com/hashicorp/hcl/v2 v2.11.1 h1:yTyWcXcm9XB0TEkyU/JCRU6rYy4K+mgLtzn2wlrJbcc=
3735
github.com/hashicorp/hcl/v2 v2.11.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
3836
github.com/hashicorp/terraform-json v0.13.0 h1:Li9L+lKD1FO5RVFRM1mMMIBDoUHslOniyEi5CM+FWGY=

module/meta.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
)
88

99
type Meta struct {
10-
Path string
10+
Path string
11+
Filenames []string
1112

1213
Backend *Backend
1314
ProviderReferences map[ProviderRef]tfaddr.Provider

schema/module_schema.go

+32
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/hashicorp/hcl-lang/lang"
77
"github.com/hashicorp/hcl-lang/schema"
8+
"github.com/hashicorp/hcl/v2"
89
"github.com/hashicorp/terraform-schema/internal/schema/refscope"
910
"github.com/hashicorp/terraform-schema/module"
1011
"github.com/zclconf/go-cty/cty"
@@ -90,5 +91,36 @@ func schemaForDependentModuleBlock(localName string, modMeta *module.Meta) (*sch
9091
NestedTargetables: targetableOutputs,
9192
})
9293

94+
if len(modMeta.Filenames) > 0 {
95+
filename := modMeta.Filenames[0]
96+
97+
// Prioritize main.tf based on best practices as documented at
98+
// https://learn.hashicorp.com/tutorials/terraform/module-create
99+
if sliceContains(modMeta.Filenames, "main.tf") {
100+
filename = "main.tf"
101+
}
102+
103+
bodySchema.Targets = &schema.Target{
104+
Path: lang.Path{
105+
Path: modMeta.Path,
106+
LanguageID: "terraform",
107+
},
108+
Range: hcl.Range{
109+
Filename: filename,
110+
Start: hcl.InitialPos,
111+
End: hcl.InitialPos,
112+
},
113+
}
114+
}
115+
93116
return bodySchema, nil
94117
}
118+
119+
func sliceContains(slice []string, value string) bool {
120+
for _, val := range slice {
121+
if val == value {
122+
return true
123+
}
124+
}
125+
return false
126+
}

0 commit comments

Comments
 (0)