Skip to content

Commit 549898f

Browse files
authored
Add GetModulePath method (#171)
1 parent 28c5530 commit 549898f

File tree

10 files changed

+847
-526
lines changed

10 files changed

+847
-526
lines changed

helper/runner.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/hashicorp/hcl/v2"
88
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
9+
"github.com/terraform-linters/tflint-plugin-sdk/terraform/addrs"
910
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
1011
"github.com/zclconf/go-cty/cty"
1112
"github.com/zclconf/go-cty/cty/convert"
@@ -42,6 +43,11 @@ type RuleConfig struct {
4243

4344
var _ tflint.Runner = &Runner{}
4445

46+
// GetModulePath always returns the root module path address
47+
func (r *Runner) GetModulePath() (addrs.Module, error) {
48+
return []string{}, nil
49+
}
50+
4551
// GetModuleContent gets a content of the current module
4652
func (r *Runner) GetModuleContent(schema *hclext.BodySchema, opts *tflint.GetModuleContentOption) (*hclext.BodyContent, error) {
4753
content := &hclext.BodyContent{}

plugin/host2plugin/host2plugin_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,10 @@ type mockServerImpl struct {
463463
getFile func(string) (*hcl.File, error)
464464
}
465465

466+
func (s *mockServer) GetModulePath() []string {
467+
return []string{}
468+
}
469+
466470
func (s *mockServer) GetModuleContent(schema *hclext.BodySchema, opts tflint.GetModuleContentOption) (*hclext.BodyContent, hcl.Diagnostics) {
467471
return &hclext.BodyContent{}, hcl.Diagnostics{}
468472
}

plugin/plugin2host/client.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/terraform-linters/tflint-plugin-sdk/plugin/fromproto"
1414
"github.com/terraform-linters/tflint-plugin-sdk/plugin/proto"
1515
"github.com/terraform-linters/tflint-plugin-sdk/plugin/toproto"
16+
"github.com/terraform-linters/tflint-plugin-sdk/terraform/addrs"
1617
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
1718
"github.com/zclconf/go-cty/cty"
1819
"github.com/zclconf/go-cty/cty/gocty"
@@ -27,6 +28,15 @@ type GRPCClient struct {
2728

2829
var _ tflint.Runner = &GRPCClient{}
2930

31+
// GetModulePath gets the current module path address.
32+
func (c *GRPCClient) GetModulePath() (addrs.Module, error) {
33+
resp, err := c.Client.GetModulePath(context.Background(), &proto.GetModulePath_Request{})
34+
if err != nil {
35+
return nil, fromproto.Error(err)
36+
}
37+
return resp.Path, err
38+
}
39+
3040
// GetResourceContent gets the contents of resources based on the schema.
3141
// This is shorthand of GetModuleContent for resources
3242
func (c *GRPCClient) GetResourceContent(name string, inner *hclext.BodySchema, opts *tflint.GetModuleContentOption) (*hclext.BodyContent, error) {

plugin/plugin2host/plugin2host_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/hashicorp/hcl/v2/json"
1515
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
1616
"github.com/terraform-linters/tflint-plugin-sdk/plugin/proto"
17+
"github.com/terraform-linters/tflint-plugin-sdk/terraform/addrs"
1718
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
1819
"github.com/zclconf/go-cty/cty"
1920
"google.golang.org/grpc"
@@ -34,6 +35,7 @@ type mockServer struct {
3435
}
3536

3637
type mockServerImpl struct {
38+
getModulePath func() []string
3739
getModuleContent func(*hclext.BodySchema, tflint.GetModuleContentOption) (*hclext.BodyContent, hcl.Diagnostics)
3840
getFile func(string) (*hcl.File, error)
3941
getFiles func() map[string][]byte
@@ -46,6 +48,13 @@ func newMockServer(impl mockServerImpl) *mockServer {
4648
return &mockServer{impl: impl}
4749
}
4850

51+
func (s *mockServer) GetModulePath() []string {
52+
if s.impl.getModulePath != nil {
53+
return s.impl.getModulePath()
54+
}
55+
return []string{}
56+
}
57+
4958
func (s *mockServer) GetModuleContent(schema *hclext.BodySchema, opts tflint.GetModuleContentOption) (*hclext.BodyContent, hcl.Diagnostics) {
5059
if s.impl.getModuleContent != nil {
5160
return s.impl.getModuleContent(schema, opts)
@@ -91,6 +100,43 @@ func (s *mockServer) EmitIssue(rule tflint.Rule, message string, location hcl.Ra
91100
// @see https://github.com/google/go-cmp/issues/40
92101
var allowAllUnexported = cmp.Exporter(func(reflect.Type) bool { return true })
93102

103+
func TestGetModulePath(t *testing.T) {
104+
tests := []struct {
105+
Name string
106+
ServerImpl func() []string
107+
Want addrs.Module
108+
}{
109+
{
110+
Name: "get root module path",
111+
ServerImpl: func() []string {
112+
return []string{}
113+
},
114+
Want: nil,
115+
},
116+
{
117+
Name: "get child module path",
118+
ServerImpl: func() []string {
119+
return []string{"child1", "child2"}
120+
},
121+
Want: []string{"child1", "child2"},
122+
},
123+
}
124+
125+
for _, test := range tests {
126+
t.Run(test.Name, func(t *testing.T) {
127+
client := startTestGRPCServer(t, newMockServer(mockServerImpl{getModulePath: test.ServerImpl}))
128+
129+
got, err := client.GetModulePath()
130+
if err != nil {
131+
t.Fatalf("failed to call GetModulePath: %s", err)
132+
}
133+
if diff := cmp.Diff(got, test.Want); diff != "" {
134+
t.Errorf("diff: %s", diff)
135+
}
136+
})
137+
}
138+
}
139+
94140
func TestGetResourceContent(t *testing.T) {
95141
// default error check helper
96142
neverHappend := func(err error) bool { return err != nil }

plugin/plugin2host/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var _ proto.RunnerServer = &GRPCServer{}
2828

2929
// Server is the interface that the host should implement when a plugin communicates with the host.
3030
type Server interface {
31+
GetModulePath() []string
3132
GetModuleContent(*hclext.BodySchema, tflint.GetModuleContentOption) (*hclext.BodyContent, hcl.Diagnostics)
3233
GetFile(string) (*hcl.File, error)
3334
// For performance, GetFiles returns map[string][]bytes instead of map[string]*hcl.File.
@@ -37,6 +38,11 @@ type Server interface {
3738
EmitIssue(rule tflint.Rule, message string, location hcl.Range) error
3839
}
3940

41+
// GetModulePath gets the current module path address.
42+
func (s *GRPCServer) GetModulePath(ctx context.Context, req *proto.GetModulePath_Request) (*proto.GetModulePath_Response, error) {
43+
return &proto.GetModulePath_Response{Path: s.Impl.GetModulePath()}, nil
44+
}
45+
4046
// GetModuleContent gets the contents of the module based on the schema.
4147
func (s *GRPCServer) GetModuleContent(ctx context.Context, req *proto.GetModuleContent_Request) (*proto.GetModuleContent_Response, error) {
4248
if req.Schema == nil {

0 commit comments

Comments
 (0)