Sha256: 3294f12919b09b07a7f27a5546e6648503dec3ae730259d7f132ef585ea254a2

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

module Terraforming
  module Resource
    class IAMUserPolicy
      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_user_policy")
      end

      def tfstate(tfstate_base)
        resources = iam_user_policies.inject({}) do |result, policy|
          attributes = {
            "id" => iam_user_policy_id_of(policy),
            "name" => policy.policy_name,
            "policy" => prettify_policy(policy.policy_document, true),
            "user" => policy.user_name,
          }
          result["aws_iam_user_policy.#{policy.policy_name}"] = {
            "type" => "aws_iam_user_policy",
            "primary" => {
              "id" => iam_user_policy_id_of(policy),
              "attributes" => attributes
            }
          }

          result
        end

        generate_tfstate(resources, tfstate_base)
      end

      private

      def iam_users
        @client.list_users.users
      end

      def iam_user_policy_id_of(policy)
        "#{policy.user_name}:#{policy.policy_name}"
      end

      def iam_user_policy_names_in(user)
        @client.list_user_policies(user_name: user.user_name).policy_names
      end

      def iam_user_policy_of(user, policy_name)
        @client.get_user_policy(user_name: user.user_name, policy_name: policy_name)
      end

      def iam_user_policies
        iam_users.map do |user|
          iam_user_policy_names_in(user).map { |policy_name| iam_user_policy_of(user, 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_user_policy.rb
terraforming-0.1.2 lib/terraforming/resource/iam_user_policy.rb
terraforming-0.1.1 lib/terraforming/resource/iam_user_policy.rb
terraforming-0.1.0 lib/terraforming/resource/iam_user_policy.rb