Skip to content

Commit

Permalink
Drop support for TFLint v0.40/v0.41
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Jun 15, 2023
1 parent b875e92 commit 812617f
Show file tree
Hide file tree
Showing 9 changed files with 354 additions and 409 deletions.
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
8 changes: 8 additions & 0 deletions plugin/host2plugin/host2plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,10 @@ func TestApplyGlobalConfig(t *testing.T) {
t.Run(test.Name, func(t *testing.T) {
client := startTestGRPCPluginServer(t, newMockRuleSet("test_ruleset", "0.1.0", mockRuleSetImpl{applyGlobalConfig: test.ServerImpl}))

// 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 +878,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

0 comments on commit 812617f

Please sign in to comment.