Skip to content

Commit

Permalink
fix:
Browse files Browse the repository at this point in the history
Or aws_iam_policy_attachment_exclusive_attachment may be better.

I prefer names that are descriptive of what issue we are warning about. Additionally, the prefix should preferably match the resource name. What do you think?
  • Loading branch information
kayman-mk committed Dec 12, 2024
1 parent 115097f commit ab27152
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ These rules enforce best practices and naming conventions:
|[aws_elasticache_replication_group_previous_type](aws_elasticache_replication_group_previous_type.md)|Disallow using previous node types||
|[aws_elasticache_replication_group_default_parameter_group](aws_elasticache_replication_group_default_parameter_group.md)|Disallow using default parameter group||
|[aws_instance_previous_type](aws_instance_previous_type.md)|Disallow using previous generation instance types||
|[aws_iam_policy_attachment_has_alternatives](aws_iam_policy_attachment_has_alternatives.md)|Consider alternative resources to `aws_iam_policy_attachment`||
|[aws_iam_policy_attachment_has_alternatives](aws_iam_policy_attachment_exclusive_attachment)|Consider alternative resources to `aws_iam_policy_attachment`||
|[aws_iam_policy_document_gov_friendly_arns](aws_iam_policy_document_gov_friendly_arns.md)|Ensure `iam_policy_document` data sources do not contain `arn:aws:` ARN's||
|[aws_iam_policy_gov_friendly_arns](aws_iam_policy_gov_friendly_arns.md)|Ensure `iam_policy` resources do not contain `arn:aws:` ARN's||
|[aws_iam_role_policy_gov_friendly_arns](aws_iam_role_policy_gov_friendly_arns.md)|Ensure `iam_role_policy` resources do not contain `arn:aws:` ARN's||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# aws_iam_policy_attachment_has_alternatives
# aws_iam_policy_attachment_exclusive_attachment

Consider alternative resources to `aws_iam_policy_attachment`.

## Configuration

```hcl
rule "aws_iam_policy_attachment_has_alternatives" {
rule "aws_iam_policy_attachment_exclusive_attachment" {
enabled = true
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,44 @@ import (
"github.com/terraform-linters/tflint-ruleset-aws/project"
)

// AwsIAMPolicyAttachmentHasAlternativesRule warns that the resource has alternatives recommended
type AwsIAMPolicyAttachmentHasAlternativesRule struct {
// AwsIAMPolicyAttachmentExclusiveAttachmentRule warns that the resource has alternatives recommended
type AwsIAMPolicyAttachmentExclusiveAttachmentRule struct {
tflint.DefaultRule

resourceType string
attributeName string
}

// AwsIAMPolicyAttachmentHasAlternativesRule returns new rule with default attributes
func NewAwsIAMPolicyAttachmentHasAlternativesRule() *AwsIAMPolicyAttachmentHasAlternativesRule {
return &AwsIAMPolicyAttachmentHasAlternativesRule{
// AwsIAMPolicyAttachmentExclusiveAttachmentRule returns new rule with default attributes
func NewAwsIAMPolicyAttachmentExclusiveAttachmentRule() *AwsIAMPolicyAttachmentExclusiveAttachmentRule {
return &AwsIAMPolicyAttachmentExclusiveAttachmentRule{
resourceType: "aws_iam_policy_attachment",
attributeName: "name",
}
}

// Name returns the rule name
func (r *AwsIAMPolicyAttachmentHasAlternativesRule) Name() string {
return "aws_iam_policy_attachment_has_alternatives"
func (r *AwsIAMPolicyAttachmentExclusiveAttachmentRule) Name() string {
return "aws_iam_policy_attachment_exclusive_attachment"
}

// Enabled returns whether the rule is enabled by default
func (r *AwsIAMPolicyAttachmentHasAlternativesRule) Enabled() bool {
func (r *AwsIAMPolicyAttachmentExclusiveAttachmentRule) Enabled() bool {
return false
}

// Severity returns the rule severity
func (r *AwsIAMPolicyAttachmentHasAlternativesRule) Severity() tflint.Severity {
func (r *AwsIAMPolicyAttachmentExclusiveAttachmentRule) Severity() tflint.Severity {
return tflint.WARNING
}

// Link returns the rule reference link
func (r *AwsIAMPolicyAttachmentHasAlternativesRule) Link() string {
func (r *AwsIAMPolicyAttachmentExclusiveAttachmentRule) Link() string {
return project.ReferenceLink(r.Name())
}

// Check checks the length of the policy
func (r *AwsIAMPolicyAttachmentHasAlternativesRule) Check(runner tflint.Runner) error {
func (r *AwsIAMPolicyAttachmentExclusiveAttachmentRule) Check(runner tflint.Runner) error {
resources, err := runner.GetResourceContent(r.resourceType, &hclext.BodySchema{
Attributes: []hclext.AttributeSchema{{Name: r.attributeName}},
}, nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/terraform-linters/tflint-plugin-sdk/helper"
)

func Test_AwsIAMPolicyAttachmentHasAlternativesRule(t *testing.T) {
func Test_AwsIAMPolicyAttachmentExclusiveAttachmentRule(t *testing.T) {
rand.Seed(time.Now().UnixNano())
cases := []struct {
Name string
Expand All @@ -25,7 +25,7 @@ resource "aws_iam_policy_attachment" "attachment" {
`,
Expected: helper.Issues{
{
Rule: NewAwsIAMPolicyAttachmentHasAlternativesRule(),
Rule: NewAwsIAMPolicyAttachmentExclusiveAttachmentRule(),
Message: "Consider aws_iam_role_policy_attachment, aws_iam_user_policy_attachment, or aws_iam_group_policy_attachment instead.",
Range: hcl.Range{
Filename: "resource.tf",
Expand All @@ -46,7 +46,7 @@ resource "aws_iam_role_policy_attachment" "attachment" {
},
}

rule := NewAwsIAMPolicyAttachmentHasAlternativesRule()
rule := NewAwsIAMPolicyAttachmentExclusiveAttachmentRule()

for _, tc := range cases {
runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content})
Expand Down
2 changes: 1 addition & 1 deletion rules/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var manualRules = []tflint.Rule{
NewAwsElastiCacheReplicationGroupDefaultParameterGroupRule(),
NewAwsElastiCacheReplicationGroupInvalidTypeRule(),
NewAwsElastiCacheReplicationGroupPreviousTypeRule(),
NewAwsIAMPolicyAttachmentHasAlternativesRule(),
NewAwsIAMPolicyAttachmentExclusiveAttachmentRule(),
NewAwsIAMPolicySidInvalidCharactersRule(),
NewAwsIAMPolicyTooLongPolicyRule(),
NewAwsLambdaFunctionDeprecatedRuntimeRule(),
Expand Down

0 comments on commit ab27152

Please sign in to comment.