Sha256: 978b834b07c13ea2b7302d3134a906b16be39762a51b1cfd7dbe7ca79b051e1d

Contents?: true

Size: 1.57 KB

Versions: 3

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module PracticeTerraforming
  module Resource
    class IAMUser
      include PracticeTerraforming::Util

      # TODO: Select appropriate Client class from here:
      # http://docs.aws.amazon.com/sdkforruby/api/index.html
      def self.tf(client: Aws::IAM::Client.new)
        self.new(client).tf
      end

      # TODO: Select appropriate Client class from here:
      # http://docs.aws.amazon.com/sdkforruby/api/index.html
      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_user")
      end

      def tfstate
        iam_users.inject({}) do |resources, user|
          attributes = {
            "arn" => user.arn,
            "id" => user.user_name,
            "name" => user.user_name,
            "path" => user.path,
            "unique_id" => user.user_id,
            "force_destroy" => "false"
          }
          resources["aws_iam_user.#{module_name_of(user)}"] = {
            "type" => "aws_iam_user",
            "primary" => {
              "id" => user.user_name,
              "attributes" => attributes
            }
          }

          resources
        end
      end

      private

      def iam_users
        @client.list_users.map(&:users).flatten
      end

      def iam_tags_of(user)
        @client.list_user_tags(user_name: user.user_name).map(&:tags).flatten
      end

      def module_name_of(user)
        normalize_module_name(user.user_name)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
practice_terraforming-0.1.10 lib/practice_terraforming/resource/iam_user.rb
practice_terraforming-0.1.9 lib/practice_terraforming/resource/iam_user.rb
practice_terraforming-0.1.8 lib/practice_terraforming/resource/iam_user.rb