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

Bump tflint-plugin-sdk to v0.10.0 #48

Merged
merged 1 commit into from
Mar 27, 2022
Merged

Bump tflint-plugin-sdk to v0.10.0 #48

merged 1 commit into from
Mar 27, 2022

Conversation

wata727
Copy link
Member

@wata727 wata727 commented Mar 21, 2022

This PR is an example of upgrading tflint-plugin-sdk for a ruleset plugin. v0.10.0 is a major update and all ruleset plugins will be affected. This PR has an aspect as a migration guide from v0.9.0.

All rules must embed tflint.DefaultRule

A new tflint.DefaultRule has been introduced. All rules must embed this structure to satisfy the interface.

- type AwsInstanceExampleTypeRule struct{}
+ type AwsInstanceExampleTypeRule struct {
+	tflint.DefaultRule
+ }

This feature acts as the default for each rule and is intended to mitigate incompatibilities in future updates.

Severity() must return tflint.Severity

For historical reasons, the Severity() method returned a string instead of an enum. However, it could return an arbitrary string, so it was replaced by an enum.

- func (r *AwsInstanceExampleTypeRule) Severity() string {
+ func (r *AwsInstanceExampleTypeRule) Severity() tflint.Severity {
	return tflint.ERROR
  }

WalkResourceAttributes is obsolete and replaced by GetResourceContent

For more flexible access to attributes, the WalkResourceAttributes API has been deprecated and a schema-based GetResourceContent has been introduced.

before

runner.WalkResourceAttributes("aws_instance", "instance_type", func (attribute *hcl.Attribute) error {
    attr.Expr // => An expression of `instance_type`
})

after

resources, _ = runner.GetResourceContent("aws_instance", &hclext.BodySchema{
    Attributes: []hclext.AttributeSchema{{Name: "instance_type"}},
}, nil)

for _, resource := range resources.Blocks {
    resource.Body.Attributes["instance_type"].Expr // => An expression of `instance_type`
}

The advantage of this way is that it can handle multiple attributes at the same time. It makes easier to implement processing such as emitting an issue only for resources with an invalid combination of instance_type and ami.

See also the tflint-plugin-sdk documentation for how to specify the schema with the hclext package.

WalkResourceBlocks is obsolete and replaced by GetResourceContent

Similarly, the WalkResourceBlocks API has been obsoleted.

before

runner.WalkResourceBlocks("aws_s3_bucket", "lifecycle_rule", func(block *hcl.Block) error {
	content, _, _ := block.Body.partialContent(&hcl.BodySchema{
		Attributes: []hcl.AttributeSchema{
			{Name: "enabled"},
		},
	})
	content.Attributes["enabled"].Expr //  => An expression of `enabled`
})

after

resources, _ = runner.GetResourceContent("aws_s3_bucket", &hclext.BodySchema{
	Blocks: []hclext.BodySchema{
		{
			Type: "lifecycle_rule",
			Body: &hclext.BodySchema{
				Attributes: []hclext.AttributeSchema{{Name: "enabled"}},
			},
		},
	},
}, nil)

for _, resource := range resources.Blocks {
	for _, rule := range resource.Body.Blocks {
		rule.Body.Attributes["enabled"].Expr // => An expression of `enabled`
	}
}

Note that hcl.Body cannot be handled in rules. The schema inside the nested block must be specified at once.

WalkResources is obsolete and replaced by GetResourceContent

Similar to the above, WalkResources has also been obsoleted. As far as I know, the purpose of this API is to inspect multiple attribute combinations and should be able to be replaced by the WalkResourceAttributes example.

WalkModuleCalls, Backend, and Config are obsolete and replace by GetModuleContent

Similar to GetResourceContent, schema-based acquisition is possible for structures other than resources.

-	backend, err := runner.Backend()
+	content, err := runner.GetModuleContent(&hclext.BodySchema{
+		Blocks: []hclext.BlockSchema{
+			{
+				Type: "terraform",
+				Body: &hclext.BodySchema{
+					Blocks: []hclext.BlockSchema{
+						{
+							Type:       "backend",
+							LabelNames: []string{"type"},
+						},
+					},
+				},
+			},
+		},
+	}, nil)

EmitIssueOnExpr() is obsolete, use EmitIssue()

For the module inspection, it was necessary to use different APIs to distinguish between issues for expressions and issues for ranges. However, this limitation has been lifted in the new API and EmitIssue() is now available for expression issues as well.

- runner.EmitIssueOnExpr(
+ runner.EmitIssue(
	rule,
	fmt.Sprintf("instance type is %s", instanceType),
-	attribute.Expr,
+	attribute.Expr.Range(),
  )

There are some other changes, but see terraform-linters/tflint-plugin-sdk#135 for more information. If you have any questions about the migration, you can open an issue in the tflint-plugin-sdk repository.

@wata727 wata727 force-pushed the grpc_plugin branch 2 times, most recently from 96b72ba to 2dc198d Compare March 27, 2022 07:31
@wata727 wata727 merged commit 4aee222 into main Mar 27, 2022
@wata727 wata727 deleted the grpc_plugin branch March 27, 2022 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant