|
| 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 | +} |
0 commit comments