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

Make use of the Targets body schema field for modules #101

Merged
merged 6 commits into from
Apr 6, 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
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