Sha256: af2bdc957ee6cd23472f7aa472d6d0b3fa45c289c3b838dcc453494da1d25d7d

Contents?: true

Size: 1.37 KB

Versions: 6

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module PracticeTerraforming
  module Resource
    class IAMRole
      include PracticeTerraforming::Util

      def self.tf(client: Aws::IAM::Client.new)
        self.new(client).tf
      end

      def self.tfstate(client: Aws::IAM::Client.new)
        self.new(client).tfstate
      end

      def initialize(client)
        @client = client
      end

      def tf
        apply_template(@client, "tf/iam_role")
      end

      def tfstate
        iam_roles.inject({}) do |resources, role|
          attributes = {
            "arn" => role.arn,
            "assume_role_policy" =>
              prettify_policy(role.assume_role_policy_document, breakline: true, unescape: true),
            "id" => role.role_name,
            "name" => role.role_name,
            "description" => role.description,
            "path" => role.path,
            "unique_id" => role.role_id
          }
          resources["aws_iam_role.#{module_name_of(role)}"] = {
            "type" => "aws_iam_role",
            "primary" => {
              "id" => role.role_name,
              "attributes" => attributes
            }
          }

          resources
        end
      end

      private

      def iam_roles
        @client.list_roles.map(&:roles).flatten
      end

      def module_name_of(role)
        normalize_module_name(role.role_name)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
practice_terraforming-0.1.8 lib/practice_terraforming/resource/iam_role.rb
practice_terraforming-0.1.7 lib/practice_terraforming/resource/iam_role.rb
practice_terraforming-0.1.6 lib/practice_terraforming/resource/iam_role.rb
practice_terraforming-0.1.5 lib/practice_terraforming/resource/iam_role.rb
practice_terraforming-0.1.4 lib/practice_terraforming/resource/iam_role.rb
practice_terraforming-0.1.3 lib/practice_terraforming/resource/iam_role.rb