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 GetModulePath method #171

Merged
merged 1 commit into from
Jul 31, 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
6 changes: 6 additions & 0 deletions helper/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/addrs"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
Expand Down Expand Up @@ -42,6 +43,11 @@ type RuleConfig struct {

var _ tflint.Runner = &Runner{}

// GetModulePath always returns the root module path address
func (r *Runner) GetModulePath() (addrs.Module, error) {
return []string{}, nil
}

// GetModuleContent gets a content of the current module
func (r *Runner) GetModuleContent(schema *hclext.BodySchema, opts *tflint.GetModuleContentOption) (*hclext.BodyContent, error) {
content := &hclext.BodyContent{}
Expand Down
4 changes: 4 additions & 0 deletions plugin/host2plugin/host2plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,10 @@ type mockServerImpl struct {
getFile func(string) (*hcl.File, error)
}

func (s *mockServer) GetModulePath() []string {
return []string{}
}

func (s *mockServer) GetModuleContent(schema *hclext.BodySchema, opts tflint.GetModuleContentOption) (*hclext.BodyContent, hcl.Diagnostics) {
return &hclext.BodyContent{}, hcl.Diagnostics{}
}
Expand Down
10 changes: 10 additions & 0 deletions plugin/plugin2host/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/terraform-linters/tflint-plugin-sdk/plugin/fromproto"
"github.com/terraform-linters/tflint-plugin-sdk/plugin/proto"
"github.com/terraform-linters/tflint-plugin-sdk/plugin/toproto"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/addrs"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
Expand All @@ -27,6 +28,15 @@ type GRPCClient struct {

var _ tflint.Runner = &GRPCClient{}

// GetModulePath gets the current module path address.
func (c *GRPCClient) GetModulePath() (addrs.Module, error) {
resp, err := c.Client.GetModulePath(context.Background(), &proto.GetModulePath_Request{})
if err != nil {
return nil, fromproto.Error(err)
}
return resp.Path, err
}

// GetResourceContent gets the contents of resources based on the schema.
// This is shorthand of GetModuleContent for resources
func (c *GRPCClient) GetResourceContent(name string, inner *hclext.BodySchema, opts *tflint.GetModuleContentOption) (*hclext.BodyContent, error) {
Expand Down
46 changes: 46 additions & 0 deletions plugin/plugin2host/plugin2host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/hashicorp/hcl/v2/json"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/plugin/proto"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/addrs"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/zclconf/go-cty/cty"
"google.golang.org/grpc"
Expand All @@ -34,6 +35,7 @@ type mockServer struct {
}

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

func (s *mockServer) GetModulePath() []string {
if s.impl.getModulePath != nil {
return s.impl.getModulePath()
}
return []string{}
}

func (s *mockServer) GetModuleContent(schema *hclext.BodySchema, opts tflint.GetModuleContentOption) (*hclext.BodyContent, hcl.Diagnostics) {
if s.impl.getModuleContent != nil {
return s.impl.getModuleContent(schema, opts)
Expand Down Expand Up @@ -91,6 +100,43 @@ func (s *mockServer) EmitIssue(rule tflint.Rule, message string, location hcl.Ra
// @see https://github.com/google/go-cmp/issues/40
var allowAllUnexported = cmp.Exporter(func(reflect.Type) bool { return true })

func TestGetModulePath(t *testing.T) {
tests := []struct {
Name string
ServerImpl func() []string
Want addrs.Module
}{
{
Name: "get root module path",
ServerImpl: func() []string {
return []string{}
},
Want: nil,
},
{
Name: "get child module path",
ServerImpl: func() []string {
return []string{"child1", "child2"}
},
Want: []string{"child1", "child2"},
},
}

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
client := startTestGRPCServer(t, newMockServer(mockServerImpl{getModulePath: test.ServerImpl}))

got, err := client.GetModulePath()
if err != nil {
t.Fatalf("failed to call GetModulePath: %s", err)
}
if diff := cmp.Diff(got, test.Want); diff != "" {
t.Errorf("diff: %s", diff)
}
})
}
}

func TestGetResourceContent(t *testing.T) {
// default error check helper
neverHappend := func(err error) bool { return err != nil }
Expand Down
6 changes: 6 additions & 0 deletions plugin/plugin2host/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var _ proto.RunnerServer = &GRPCServer{}

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

// GetModulePath gets the current module path address.
func (s *GRPCServer) GetModulePath(ctx context.Context, req *proto.GetModulePath_Request) (*proto.GetModulePath_Response, error) {
return &proto.GetModulePath_Response{Path: s.Impl.GetModulePath()}, nil
}

// GetModuleContent gets the contents of the module based on the schema.
func (s *GRPCServer) GetModuleContent(ctx context.Context, req *proto.GetModuleContent_Request) (*proto.GetModuleContent_Response, error) {
if req.Schema == nil {
Expand Down
Loading