Sha256: 99da89456d5267c2de6a66649b6b615d549269c0c1e6fe55e3ff487d7bd0c991

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

module Terraforming
  module Resource
    class IAMRolePolicy
      include Terraforming::Util

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

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

      def initialize(client)
        @client = client
      end

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

      def tfstate(tfstate_base)
        resources = iam_role_policies.inject({}) do |result, policy|
          attributes = {
            "id" => iam_role_policy_id_of(policy),
            "name" => policy.policy_name,
            "policy" => prettify_policy(policy.policy_document, true),
            "role" => policy.role_name,
          }
          result["aws_iam_role_policy.#{policy.policy_name}"] = {
            "type" => "aws_iam_role_policy",
            "primary" => {
              "id" => iam_role_policy_id_of(policy),
              "attributes" => attributes
            }
          }

          result
        end

        generate_tfstate(resources, tfstate_base)
      end

      private

      def iam_roles
        @client.list_roles.roles
      end

      def iam_role_policy_id_of(policy)
        "#{policy.role_name}:#{policy.policy_name}"
      end

      def iam_role_policy_names_in(role)
        @client.list_role_policies(role_name: role.role_name).policy_names
      end

      def iam_role_policy_of(role, policy_name)
        @client.get_role_policy(role_name: role.role_name, policy_name: policy_name)
      end

      def iam_role_policies
        iam_roles.map do |role|
          iam_role_policy_names_in(role).map { |policy_name| iam_role_policy_of(role, policy_name) }
        end.flatten
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
terraforming-0.1.3 lib/terraforming/resource/iam_role_policy.rb
terraforming-0.1.2 lib/terraforming/resource/iam_role_policy.rb
terraforming-0.1.1 lib/terraforming/resource/iam_role_policy.rb
terraforming-0.1.0 lib/terraforming/resource/iam_role_policy.rb