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

Drop support for TFLint v0.40/v0.41 #263

Merged
merged 1 commit into from
Jun 18, 2023
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ NOTE: This plugin system is experimental. This means that API compatibility is f

## Requirements

- TFLint v0.40+
- TFLint v0.42+
- Go v1.20

## Usage
Expand Down
8 changes: 1 addition & 7 deletions plugin/fromproto/fromproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,7 @@ func BodyContent(body *proto.BodyContent) (*hclext.BodyContent, hcl.Diagnostics)

attributes := hclext.Attributes{}
for key, attr := range body.Attributes {
var expr hcl.Expression
var exprDiags hcl.Diagnostics
if attr.Expression != nil {
expr, exprDiags = Expression(attr.Expression)
} else {
expr, exprDiags = hclext.ParseExpression(attr.Expr, attr.ExprRange.Filename, Pos(attr.ExprRange.Start))
}
expr, exprDiags := Expression(attr.Expression)
diags = diags.Extend(exprDiags)

attributes[key] = &hclext.Attribute{
Expand Down
22 changes: 22 additions & 0 deletions plugin/host2plugin/host2plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ func TestApplyGlobalConfig(t *testing.T) {
Arg *tflint.Config
ServerImpl func(*tflint.Config) error
ErrCheck func(error) bool
LegacyHost bool
}{
{
Name: "nil config",
Expand Down Expand Up @@ -435,12 +436,29 @@ func TestApplyGlobalConfig(t *testing.T) {
return err == nil || err.Error() != "unexpected error"
},
},
{
Name: "legacy host version (TFLint v0.41)",
Arg: nil,
ServerImpl: func(config *tflint.Config) error {
return nil
},
LegacyHost: true,
ErrCheck: func(err error) bool {
return err == nil || err.Error() != "failed to satisfy version constraints; tflint-ruleset-test_ruleset requires >= 0.42, but TFLint version is 0.40 or 0.41"
},
},
}

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
client := startTestGRPCPluginServer(t, newMockRuleSet("test_ruleset", "0.1.0", mockRuleSetImpl{applyGlobalConfig: test.ServerImpl}))

if !test.LegacyHost {
// call VersionConstraints to avoid SDK version incompatible error
if _, err := client.VersionConstraints(); err != nil {
t.Fatalf("failed to call VersionConstraints: %s", err)
}
}
err := client.ApplyGlobalConfig(test.Arg)
if test.ErrCheck(err) {
t.Fatalf("failed to call ApplyGlobalConfig: %s", err)
Expand Down Expand Up @@ -874,6 +892,10 @@ foo = 1
t.Run(test.Name, func(t *testing.T) {
client := startTestGRPCPluginServer(t, newMockRuleSet("test_ruleset", "0.1.0", mockRuleSetImpl{check: test.ServerImpl, newRunner: test.NewRunnerImpl}))

// call VersionConstraints to avoid SDK version incompatible error
if _, err := client.VersionConstraints(); err != nil {
t.Fatalf("failed to call VersionConstraints: %s", err)
}
if err := client.ApplyGlobalConfig(&tflint.Config{Fix: true}); err != nil {
t.Fatalf("failed to call ApplyGlobalConfig: %s", err)
}
Expand Down
10 changes: 10 additions & 0 deletions plugin/host2plugin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ type GRPCServer struct {
impl tflint.RuleSet
broker *plugin.GRPCBroker
config *tflint.Config

// TFLint v0.41 and earlier does not check version constraints,
// so it returns an error in that case.
constraintChecked bool
}

var _ proto.RuleSetServer = &GRPCServer{}
Expand Down Expand Up @@ -68,6 +72,7 @@ func (s *GRPCServer) GetRuleNames(ctx context.Context, req *proto.GetRuleNames_R

// GetVersionConstraint returns a constraint of TFLint versions.
func (s *GRPCServer) GetVersionConstraint(ctx context.Context, req *proto.GetVersionConstraint_Request) (*proto.GetVersionConstraint_Response, error) {
s.constraintChecked = true
return &proto.GetVersionConstraint_Response{Constraint: s.impl.VersionConstraint()}, nil
}

Expand All @@ -83,6 +88,11 @@ func (s *GRPCServer) GetConfigSchema(ctx context.Context, req *proto.GetConfigSc

// ApplyGlobalConfig applies a common config to the plugin.
func (s *GRPCServer) ApplyGlobalConfig(ctx context.Context, req *proto.ApplyGlobalConfig_Request) (*proto.ApplyGlobalConfig_Response, error) {
// TFLint v0.41 and earlier does not check version constraints.
if !s.constraintChecked {
return nil, status.Error(codes.FailedPrecondition, fmt.Sprintf("failed to satisfy version constraints; tflint-ruleset-%s requires >= 0.42, but TFLint version is 0.40 or 0.41", s.impl.RuleSetName()))
}

if req.Config == nil {
return nil, status.Error(codes.InvalidArgument, "config should not be null")
}
Expand Down
2 changes: 0 additions & 2 deletions plugin/plugin2host/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,6 @@ func (c *GRPCClient) evaluateExpr(expr hcl.Expression, target interface{}, opts
resp, err := c.Client.EvaluateExpr(
context.Background(),
&proto.EvaluateExpr_Request{
Expr: expr.Range().SliceBytes(file.Bytes),
ExprRange: toproto.Range(expr.Range()),
Expression: toproto.Expression(expr, file.Bytes),
Option: &proto.EvaluateExpr_Option{Type: tyby, ModuleCtx: toproto.ModuleCtxType(opts.ModuleCtx)},
},
Expand Down
28 changes: 8 additions & 20 deletions plugin/plugin2host/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,31 +119,19 @@ func (s *GRPCServer) GetRuleConfigContent(ctx context.Context, req *proto.GetRul
// EvaluateExpr evals the passed expression based on the type.
func (s *GRPCServer) EvaluateExpr(ctx context.Context, req *proto.EvaluateExpr_Request) (*proto.EvaluateExpr_Response, error) {
if req.Expression == nil {
if req.Expr == nil {
return nil, status.Error(codes.InvalidArgument, "expr should not be null")
}
if req.ExprRange == nil {
return nil, status.Error(codes.InvalidArgument, "expr_range should not be null")
}
} else {
if req.Expression.Bytes == nil {
return nil, status.Error(codes.InvalidArgument, "expression.bytes should not be null")
}
if req.Expression.Range == nil {
return nil, status.Error(codes.InvalidArgument, "expression.range should not be null")
}
return nil, status.Error(codes.InvalidArgument, "expression should not be null")
}
if req.Expression.Bytes == nil {
return nil, status.Error(codes.InvalidArgument, "expression.bytes should not be null")
}
if req.Expression.Range == nil {
return nil, status.Error(codes.InvalidArgument, "expression.range should not be null")
}
if req.Option == nil {
return nil, status.Error(codes.InvalidArgument, "option should not be null")
}

var expr hcl.Expression
var diags hcl.Diagnostics
if req.Expression != nil {
expr, diags = fromproto.Expression(req.Expression)
} else {
expr, diags = hclext.ParseExpression(req.Expr, req.ExprRange.Filename, fromproto.Pos(req.ExprRange.Start))
}
expr, diags := fromproto.Expression(req.Expression)
if diags.HasErrors() {
return nil, toproto.Error(codes.InvalidArgument, diags)
}
Expand Down
Loading