Sha256: d1ab02ef6c7d2aaff6b7219d4838e8680bad20bfb94f0f9869a5453e07b2503c

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require 'terracop/cop/base'

module Terracop
  module Cop
    module Aws
      # This cop makes sure that AWS resources that can be tagged, are indeed
      # tagged.
      # By default it just checks that resources have at least one tag, any tag.
      # It is configurable to enforce the existance of some specific tags.
      #
      # @example
      #   # bad
      #   resource "aws_alb" "lb" {
      #     name_prefix = "app"
      #   }
      #
      #   # good
      #   resource "aws_alb" "lb" {
      #     name_prefix = "app"
      #     tags = {
      #       environment = "staging"
      #     }
      #   }
      class EnsureTags < Base
        register

        def check
          return unless attributes['tags']

          if self.class.config['Required']
            check_required(self.class.config['Required'])
          elsif attributes['tags'].empty?
            offense 'Tag resources properly.'
          end
        end

        private

        def check_required(required_tags)
          required_tags.each do |key|
            unless attributes['tags'][key]
              offense "Required tag \"#{key}\" is missing on this resource."
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
terracop-0.1.1 lib/terracop/cop/aws/ensure_tags.rb
terracop-0.1.0 lib/terracop/cop/aws/ensure_tags.rb