Skip to content

Commit 9031434

Browse files
committed
Drop support for TFLint v0.40/v0.41
1 parent b875e92 commit 9031434

File tree

9 files changed

+368
-409
lines changed

9 files changed

+368
-409
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ NOTE: This plugin system is experimental. This means that API compatibility is f
1010

1111
## Requirements
1212

13-
- TFLint v0.40+
13+
- TFLint v0.42+
1414
- Go v1.20
1515

1616
## Usage

plugin/fromproto/fromproto.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,7 @@ func BodyContent(body *proto.BodyContent) (*hclext.BodyContent, hcl.Diagnostics)
6565

6666
attributes := hclext.Attributes{}
6767
for key, attr := range body.Attributes {
68-
var expr hcl.Expression
69-
var exprDiags hcl.Diagnostics
70-
if attr.Expression != nil {
71-
expr, exprDiags = Expression(attr.Expression)
72-
} else {
73-
expr, exprDiags = hclext.ParseExpression(attr.Expr, attr.ExprRange.Filename, Pos(attr.ExprRange.Start))
74-
}
68+
expr, exprDiags := Expression(attr.Expression)
7569
diags = diags.Extend(exprDiags)
7670

7771
attributes[key] = &hclext.Attribute{

plugin/host2plugin/host2plugin_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ func TestApplyGlobalConfig(t *testing.T) {
381381
Arg *tflint.Config
382382
ServerImpl func(*tflint.Config) error
383383
ErrCheck func(error) bool
384+
LegacyHost bool
384385
}{
385386
{
386387
Name: "nil config",
@@ -435,12 +436,29 @@ func TestApplyGlobalConfig(t *testing.T) {
435436
return err == nil || err.Error() != "unexpected error"
436437
},
437438
},
439+
{
440+
Name: "legacy host version (TFLint v0.41)",
441+
Arg: nil,
442+
ServerImpl: func(config *tflint.Config) error {
443+
return nil
444+
},
445+
LegacyHost: true,
446+
ErrCheck: func(err error) bool {
447+
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"
448+
},
449+
},
438450
}
439451

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

456+
if !test.LegacyHost {
457+
// call VersionConstraints to avoid SDK version incompatible error
458+
if _, err := client.VersionConstraints(); err != nil {
459+
t.Fatalf("failed to call VersionConstraints: %s", err)
460+
}
461+
}
444462
err := client.ApplyGlobalConfig(test.Arg)
445463
if test.ErrCheck(err) {
446464
t.Fatalf("failed to call ApplyGlobalConfig: %s", err)
@@ -874,6 +892,10 @@ foo = 1
874892
t.Run(test.Name, func(t *testing.T) {
875893
client := startTestGRPCPluginServer(t, newMockRuleSet("test_ruleset", "0.1.0", mockRuleSetImpl{check: test.ServerImpl, newRunner: test.NewRunnerImpl}))
876894

895+
// call VersionConstraints to avoid SDK version incompatible error
896+
if _, err := client.VersionConstraints(); err != nil {
897+
t.Fatalf("failed to call VersionConstraints: %s", err)
898+
}
877899
if err := client.ApplyGlobalConfig(&tflint.Config{Fix: true}); err != nil {
878900
t.Fatalf("failed to call ApplyGlobalConfig: %s", err)
879901
}

plugin/host2plugin/server.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ type GRPCServer struct {
2626
impl tflint.RuleSet
2727
broker *plugin.GRPCBroker
2828
config *tflint.Config
29+
30+
// TFLint v0.41 and earlier does not check version constraints,
31+
// so it returns an error in that case.
32+
constraintChecked bool
2933
}
3034

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

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

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

8489
// ApplyGlobalConfig applies a common config to the plugin.
8590
func (s *GRPCServer) ApplyGlobalConfig(ctx context.Context, req *proto.ApplyGlobalConfig_Request) (*proto.ApplyGlobalConfig_Response, error) {
91+
// TFLint v0.41 and earlier does not check version constraints.
92+
if !s.constraintChecked {
93+
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()))
94+
}
95+
8696
if req.Config == nil {
8797
return nil, status.Error(codes.InvalidArgument, "config should not be null")
8898
}

plugin/plugin2host/client.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,6 @@ func (c *GRPCClient) evaluateExpr(expr hcl.Expression, target interface{}, opts
374374
resp, err := c.Client.EvaluateExpr(
375375
context.Background(),
376376
&proto.EvaluateExpr_Request{
377-
Expr: expr.Range().SliceBytes(file.Bytes),
378-
ExprRange: toproto.Range(expr.Range()),
379377
Expression: toproto.Expression(expr, file.Bytes),
380378
Option: &proto.EvaluateExpr_Option{Type: tyby, ModuleCtx: toproto.ModuleCtxType(opts.ModuleCtx)},
381379
},

plugin/plugin2host/server.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -119,31 +119,19 @@ func (s *GRPCServer) GetRuleConfigContent(ctx context.Context, req *proto.GetRul
119119
// EvaluateExpr evals the passed expression based on the type.
120120
func (s *GRPCServer) EvaluateExpr(ctx context.Context, req *proto.EvaluateExpr_Request) (*proto.EvaluateExpr_Response, error) {
121121
if req.Expression == nil {
122-
if req.Expr == nil {
123-
return nil, status.Error(codes.InvalidArgument, "expr should not be null")
124-
}
125-
if req.ExprRange == nil {
126-
return nil, status.Error(codes.InvalidArgument, "expr_range should not be null")
127-
}
128-
} else {
129-
if req.Expression.Bytes == nil {
130-
return nil, status.Error(codes.InvalidArgument, "expression.bytes should not be null")
131-
}
132-
if req.Expression.Range == nil {
133-
return nil, status.Error(codes.InvalidArgument, "expression.range should not be null")
134-
}
122+
return nil, status.Error(codes.InvalidArgument, "expression should not be null")
123+
}
124+
if req.Expression.Bytes == nil {
125+
return nil, status.Error(codes.InvalidArgument, "expression.bytes should not be null")
126+
}
127+
if req.Expression.Range == nil {
128+
return nil, status.Error(codes.InvalidArgument, "expression.range should not be null")
135129
}
136130
if req.Option == nil {
137131
return nil, status.Error(codes.InvalidArgument, "option should not be null")
138132
}
139133

140-
var expr hcl.Expression
141-
var diags hcl.Diagnostics
142-
if req.Expression != nil {
143-
expr, diags = fromproto.Expression(req.Expression)
144-
} else {
145-
expr, diags = hclext.ParseExpression(req.Expr, req.ExprRange.Filename, fromproto.Pos(req.ExprRange.Start))
146-
}
134+
expr, diags := fromproto.Expression(req.Expression)
147135
if diags.HasErrors() {
148136
return nil, toproto.Error(codes.InvalidArgument, diags)
149137
}

0 commit comments

Comments
 (0)