Skip to content

Commit

Permalink
add DocsLink for remote module source
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Apr 13, 2022
1 parent e912e34 commit f3db2dd
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 8 deletions.
21 changes: 17 additions & 4 deletions schema/module_schema.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package schema

import (
"fmt"
"sort"
"strings"

"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
Expand All @@ -11,7 +13,7 @@ import (
"github.com/zclconf/go-cty/cty"
)

func schemaForDependentModuleBlock(localName string, modMeta *module.Meta) (*schema.BodySchema, error) {
func schemaForDependentModuleBlock(module module.ModuleCall, modMeta *module.Meta) (*schema.BodySchema, error) {
attributes := make(map[string]*schema.AttributeSchema, 0)

for name, modVar := range modMeta.Variables {
Expand Down Expand Up @@ -40,7 +42,7 @@ func schemaForDependentModuleBlock(localName string, modMeta *module.Meta) (*sch
Attributes: attributes,
}

if localName == "" {
if module.LocalName == "" {
// avoid creating output refs if we don't have reference name
return bodySchema, nil
}
Expand All @@ -52,7 +54,7 @@ func schemaForDependentModuleBlock(localName string, modMeta *module.Meta) (*sch
for name, output := range modMeta.Outputs {
addr := lang.Address{
lang.RootStep{Name: "module"},
lang.AttrStep{Name: localName},
lang.AttrStep{Name: module.LocalName},
lang.AttrStep{Name: name},
}

Expand Down Expand Up @@ -82,7 +84,7 @@ func schemaForDependentModuleBlock(localName string, modMeta *module.Meta) (*sch

addr := lang.Address{
lang.RootStep{Name: "module"},
lang.AttrStep{Name: localName},
lang.AttrStep{Name: module.LocalName},
}
bodySchema.TargetableAs = append(bodySchema.TargetableAs, &schema.Targetable{
Address: addr,
Expand Down Expand Up @@ -113,6 +115,17 @@ func schemaForDependentModuleBlock(localName string, modMeta *module.Meta) (*sch
}
}

if strings.HasPrefix(module.SourceAddr, "registry.terraform.io/") {
shortName := strings.TrimPrefix(module.SourceAddr, "registry.terraform.io/")
version := module.Version
if version == "" {
version = "latest"
}
bodySchema.DocsLink = &schema.DocsLink{
URL: fmt.Sprintf(`https://registry.terraform.io/modules/%s/%s`, shortName, version),
}
}

return bodySchema, nil
}

Expand Down
126 changes: 123 additions & 3 deletions schema/module_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import (

func TestSchemaForDependentModuleBlock_emptyMeta(t *testing.T) {
meta := &module.Meta{}
depSchema, err := schemaForDependentModuleBlock("refname", meta)
module := module.ModuleCall{
LocalName: "refname",
}
depSchema, err := schemaForDependentModuleBlock(module, meta)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -63,7 +66,10 @@ func TestSchemaForDependentModuleBlock_basic(t *testing.T) {
},
},
}
depSchema, err := schemaForDependentModuleBlock("refname", meta)
module := module.ModuleCall{
LocalName: "refname",
}
depSchema, err := schemaForDependentModuleBlock(module, meta)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -263,9 +269,123 @@ func TestSchemaForDependentModuleBlock_Target(t *testing.T) {
},
},
}
module := module.ModuleCall{
LocalName: "refname",
}

for _, tc := range testCases {
depSchema, err := schemaForDependentModuleBlock(module, tc.meta)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(tc.expectedSchema, depSchema, ctydebug.CmpOptions); diff != "" {
t.Fatalf("schema mismatch: %s", diff)
}
}
}

func TestSchemaForDependentModuleBlock_DocsLink(t *testing.T) {
type testCase struct {
name string
meta *module.Meta
module module.ModuleCall
expectedSchema *schema.BodySchema
}

testCases := []testCase{
{
"local module",
&module.Meta{
Path: "./local",
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: nil,
},
module.ModuleCall{
LocalName: "refname",
SourceAddr: "./local",
},
&schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{},
TargetableAs: []*schema.Targetable{
{
Address: lang.Address{
lang.RootStep{Name: "module"},
lang.AttrStep{Name: "refname"},
},
ScopeId: refscope.ModuleScope,
AsType: cty.Object(map[string]cty.Type{}),
NestedTargetables: []*schema.Targetable{},
},
},
Targets: nil,
},
},
{
"remote module",
&module.Meta{
Path: "registry.terraform.io/terraform-aws-modules/vpc/aws",
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: nil,
},
module.ModuleCall{
LocalName: "vpc",
SourceAddr: "registry.terraform.io/terraform-aws-modules/vpc/aws",
},
&schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{},
TargetableAs: []*schema.Targetable{
{
Address: lang.Address{
lang.RootStep{Name: "module"},
lang.AttrStep{Name: "vpc"},
},
ScopeId: refscope.ModuleScope,
AsType: cty.Object(map[string]cty.Type{}),
NestedTargetables: []*schema.Targetable{},
},
},
DocsLink: &schema.DocsLink{
URL: "https://registry.terraform.io/modules/terraform-aws-modules/vpc/aws/latest",
},
},
},
{
"remote module with version",
&module.Meta{
Path: "registry.terraform.io/terraform-aws-modules/vpc/aws",
Variables: map[string]module.Variable{},
Outputs: map[string]module.Output{},
Filenames: nil,
},
module.ModuleCall{
LocalName: "vpc",
SourceAddr: "registry.terraform.io/terraform-aws-modules/vpc/aws",
Version: "1.33.7",
},
&schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{},
TargetableAs: []*schema.Targetable{
{
Address: lang.Address{
lang.RootStep{Name: "module"},
lang.AttrStep{Name: "vpc"},
},
ScopeId: refscope.ModuleScope,
AsType: cty.Object(map[string]cty.Type{}),
NestedTargetables: []*schema.Targetable{},
},
},
DocsLink: &schema.DocsLink{
URL: "https://registry.terraform.io/modules/terraform-aws-modules/vpc/aws/1.33.7",
},
},
},
}

for _, tc := range testCases {
depSchema, err := schemaForDependentModuleBlock("refname", tc.meta)
depSchema, err := schemaForDependentModuleBlock(tc.module, tc.meta)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion schema/schema_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (m *SchemaMerger) SchemaForModule(meta *module.Meta) (*schema.BodySchema, e
},
}

depSchema, err := schemaForDependentModuleBlock(module.LocalName, modMeta)
depSchema, err := schemaForDependentModuleBlock(module, modMeta)
if err == nil {
mergedSchema.Blocks["module"].DependentBody[schema.NewSchemaKey(depKeys)] = depSchema
}
Expand Down
2 changes: 2 additions & 0 deletions schema/schema_merge_v015_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ var expectedRemoteModuleSchema = &schema.BlockSchema{
},
},
},
DocsLink: &schema.DocsLink{URL: "https://registry.terraform.io/modules/namespace/foobar/latest"},
},
schema.NewSchemaKey(schema.DependencyKeys{
Attributes: []schema.AttributeDependent{
Expand Down Expand Up @@ -705,6 +706,7 @@ var expectedRemoteModuleSchema = &schema.BlockSchema{
},
},
},
DocsLink: &schema.DocsLink{URL: "https://registry.terraform.io/modules/namespace/foobar/latest"},
},
},
}

0 comments on commit f3db2dd

Please sign in to comment.