Skip to content

Commit 76ab2cb

Browse files
committed
generate rule
1 parent 158193b commit 76ab2cb

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# aws_security_group_rule_deprecated
2+
3+
// TODO: Write the rule's description here
4+
5+
## Example
6+
7+
```hcl
8+
resource "null_resource" "foo" {
9+
// TODO: Write the example Terraform code which violates the rule
10+
}
11+
```
12+
13+
```
14+
$ tflint
15+
16+
// TODO: Write the output when inspects the above code
17+
18+
```
19+
20+
## Why
21+
22+
// TODO: Write why you should follow the rule. This section is also a place to explain the value of the rule
23+
24+
## How To Fix
25+
26+
// TODO: Write how to fix it to avoid the problem
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package rules
2+
3+
import (
4+
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
5+
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
6+
"github.com/terraform-linters/tflint-ruleset-aws/project"
7+
)
8+
9+
// TODO: Write the rule's description here
10+
// AwsSecurityGroupRuleDeprecatedRule checks ...
11+
type AwsSecurityGroupRuleDeprecatedRule struct {
12+
tflint.DefaultRule
13+
14+
resourceType string
15+
attributeName string
16+
}
17+
18+
// NewAwsSecurityGroupRuleDeprecatedRule returns new rule with default attributes
19+
func NewAwsSecurityGroupRuleDeprecatedRule() *AwsSecurityGroupRuleDeprecatedRule {
20+
return &AwsSecurityGroupRuleDeprecatedRule{
21+
// TODO: Write resource type and attribute name here
22+
resourceType: "...",
23+
attributeName: "...",
24+
}
25+
}
26+
27+
// Name returns the rule name
28+
func (r *AwsSecurityGroupRuleDeprecatedRule) Name() string {
29+
return "aws_security_group_rule_deprecated"
30+
}
31+
32+
// Enabled returns whether the rule is enabled by default
33+
func (r *AwsSecurityGroupRuleDeprecatedRule) Enabled() bool {
34+
// TODO: Determine whether the rule is enabled by default
35+
return true
36+
}
37+
38+
// Severity returns the rule severity
39+
func (r *AwsSecurityGroupRuleDeprecatedRule) Severity() tflint.Severity {
40+
// TODO: Determine the rule's severiry
41+
return tflint.ERROR
42+
}
43+
44+
// Link returns the rule reference link
45+
func (r *AwsSecurityGroupRuleDeprecatedRule) Link() string {
46+
// TODO: If the rule is so trivial that no documentation is needed, return "" instead.
47+
return project.ReferenceLink(r.Name())
48+
}
49+
50+
// TODO: Write the details of the inspection
51+
// Check checks ...
52+
func (r *AwsSecurityGroupRuleDeprecatedRule) Check(runner tflint.Runner) error {
53+
// TODO: Write the implementation here. See this documentation for what tflint.Runner can do.
54+
// https://pkg.go.dev/github.com/terraform-linters/tflint-plugin-sdk/tflint#Runner
55+
56+
resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{
57+
Attributes: []hclext.AttributeSchema{
58+
{Name: r.attributeName},
59+
},
60+
}, nil)
61+
if err != nil {
62+
return err
63+
}
64+
65+
for _, resource := range resources.Blocks {
66+
attribute, exists := resource.Body.Attributes[r.attributeName]
67+
if !exists {
68+
continue
69+
}
70+
71+
runner.EmitIssue(
72+
r,
73+
"Consider using aws_vpc_security_group_egress_rule or aws_vpc_security_group_ingress_rule instead.",
74+
attribute.Expr.Range(),
75+
)
76+
}
77+
78+
return nil
79+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package rules
2+
3+
import (
4+
"testing"
5+
6+
hcl "github.com/hashicorp/hcl/v2"
7+
"github.com/terraform-linters/tflint-plugin-sdk/helper"
8+
)
9+
10+
func Test_AwsSecurityGroupRuleDeprecated(t *testing.T) {
11+
cases := []struct {
12+
Name string
13+
Content string
14+
Expected helper.Issues
15+
}{
16+
{
17+
Name: "basic",
18+
Content: `
19+
resource "null_resource" "null" {
20+
}
21+
`,
22+
Expected: helper.Issues{
23+
{
24+
Rule: NewAwsSecurityGroupRuleDeprecatedRule(),
25+
Message: "TODO",
26+
Range: hcl.Range{
27+
Filename: "resource.tf",
28+
Start: hcl.Pos{Line: 0, Column: 0},
29+
End: hcl.Pos{Line: 0, Column: 0},
30+
},
31+
},
32+
},
33+
},
34+
}
35+
36+
rule := NewAwsSecurityGroupRuleDeprecatedRule()
37+
38+
for _, tc := range cases {
39+
runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content})
40+
41+
if err := rule.Check(runner); err != nil {
42+
t.Fatalf("Unexpected error occurred: %s", err)
43+
}
44+
45+
helper.AssertIssues(t, tc.Expected, runner.Issues)
46+
}
47+
}

rules/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ var manualRules = []tflint.Rule{
4040
NewAwsSecurityGroupInvalidProtocolRule(),
4141
NewAwsSecurityGroupRuleInvalidProtocolRule(),
4242
NewAwsProviderMissingDefaultTagsRule(),
43+
NewAwsSecurityGroupRuleDeprecatedRule(),
4344
}
4445

4546
// Rules is a list of all rules

0 commit comments

Comments
 (0)