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

Add DocsLink for remote module source attributes #103

Merged
merged 4 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ 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-20220406121211-c20527a75592
github.com/hashicorp/hcl-lang v0.0.0-20220421093840-480fdfd2ecb5
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
github.com/hashicorp/terraform-registry-address v0.0.0-20220422093245-eb7bcc2ff473
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5
github.com/zclconf/go-cty v1.10.0
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b
Expand Down
9 changes: 4 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
Expand All @@ -29,14 +28,14 @@ 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-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-lang v0.0.0-20220421093840-480fdfd2ecb5 h1:A18R+0Emk5YuXo22u44wxwHAnNHLDK6NHxXl2CIQGlU=
github.com/hashicorp/hcl-lang v0.0.0-20220421093840-480fdfd2ecb5/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=
github.com/hashicorp/terraform-json v0.13.0/go.mod h1:y5OdLBCT+rxbwnpxZs9kGL7R9ExU76+cpdY8zHwoazk=
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896 h1:1FGtlkJw87UsTMg5s8jrekrHmUPUJaMcu6ELiVhQrNw=
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896/go.mod h1:bzBPnUIkI0RxauU8Dqo+2KrZZ28Cf48s8V6IHt3p4co=
github.com/hashicorp/terraform-registry-address v0.0.0-20220422093245-eb7bcc2ff473 h1:Vp3YMcnM+TvVMV5TplAhGeuzz3A0562AywL32y71y3Y=
github.com/hashicorp/terraform-registry-address v0.0.0-20220422093245-eb7bcc2ff473/go.mod h1:bdLC+qQlJIBHKbCMA6GipcuaKjmjcvZlnVdpU583z3Y=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand Down
1 change: 1 addition & 0 deletions module/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ type ProviderRef struct {
type ModuleCall struct {
LocalName string
SourceAddr string
Version string
Path string
}
26 changes: 22 additions & 4 deletions schema/module_schema.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package schema

import (
"fmt"
"sort"

"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
tfaddr "github.com/hashicorp/terraform-registry-address"
"github.com/hashicorp/terraform-schema/internal/schema/refscope"
"github.com/hashicorp/terraform-schema/module"
"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,22 @@ func schemaForDependentModuleBlock(localName string, modMeta *module.Meta) (*sch
}
}

moduleSourceRegistry, err := tfaddr.ParseRawModuleSourceRegistry(module.SourceAddr)
if err == nil {
version := module.Version
if version == "" {
version = "latest"
}
bodySchema.DocsLink = &schema.DocsLink{
URL: fmt.Sprintf(
`https://%s/modules/%s/%s`,
moduleSourceRegistry.PackageAddr.Host.ForDisplay(),
moduleSourceRegistry.PackageAddr.ForRegistryProtocol(),
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