Sha256: ea8e63965807f8b2685642b4a771ff3a0cfe205890367992afcbd706b7400c47

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

module Terraforming
  module Resource
    class IAMGroupMembership
      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_group_membership")
      end

      def tfstate(tfstate_base)
        resources = iam_groups.inject({}) do |result, group|
          membership_name = membership_name_of(group)

          attributes = {
            "group"=> group.group_name,
            "id" => membership_name,
            "name" => membership_name,
            "users.#" => group_members_of(group).length.to_s,
          }
          result["aws_iam_group_membership.#{group.group_name}"] = {
            "type" => "aws_iam_group_membership",
            "primary" => {
              "id" => membership_name,
              "attributes" => attributes
            }
          }

          result
        end

        generate_tfstate(resources, tfstate_base)
      end

      private

      def group_members_of(group)
        @client.get_group(group_name: group.group_name).users.map { |user| user.user_name }
      end

      def iam_groups
        @client.list_groups.groups
      end

      def membership_name_of(group)
        "#{group.group_name}-group-membership"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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