Class: Terracop::Cop::Style::ResourceTypeInName

Inherits:
Base
  • Object
show all
Defined in:
lib/terracop/cop/style/resource_type_in_name.rb

Overview

Note:

When you rename a resource terraform will destroy and recreate it. Use terraform mv on the state file to avoid this from happening.

This cop checks terraform resource names that include the type in them. This makes for very long and redundant names.

Examples:

# bad
resource "aws_security_group" "load_balancer_security_group" { }
resource "aws_security_group" "load_balancer_sg" { }
resource "aws_security_group_rule" "ingress_rule" { }

# good
resource "aws_security_group" "load_balancer" { }
resource "aws_security_group_rule" "ingress" { }

Constant Summary collapse

BLACKLIST =
{
  'aws_alb' => %w[alb lb load_balancer],
  'aws_alb_listener' => %w[listener],
  'aws_alb_target_group' => %w[tg],
  'aws_autoscaling_group' => %w[asg],
  'aws_cloudwatch_metric_alarm' => %w[metric alarm],
  'aws_iam_instance_profile' => %w[profile],
  'aws_iam_role' => %w[role],
  'aws_iam_role_policy' => %w[policy],
  'aws_route53_record' => %w[record],
  'aws_security_group' => %w[sg group security_group],
  'aws_security_group_rule' => %w[rule]
}.freeze

Instance Attribute Summary

Attributes inherited from Base

#attributes, #index, #name, #offenses, #type

Instance Method Summary collapse

Methods inherited from Base

config, cop_name, #human_name, #initialize, #offense, run

Constructor Details

This class inherits a constructor from Terracop::Cop::Base

Instance Method Details

#checkObject



41
42
43
44
45
46
47
48
49
# File 'lib/terracop/cop/style/resource_type_in_name.rb', line 41

def check
  blacklist = BLACKLIST[type]
  blacklist&.each do |word|
    if name.downcase.gsub('-', '_').include?(word)
      offense 'Do not include the resource type in its name.'
      return
    end
  end
end