Sha256: be1980d8ac642a2eb8a5e0cf05059a0f004624b69fb1358376bc4725ce33da66

Contents?: true

Size: 1.37 KB

Versions: 16

Compression:

Stored size: 1.37 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)
        self.new(client).tfstate
      end

      def initialize(client)
        @client = client
      end

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

      def tfstate
        iam_groups.inject({}) do |resources, 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,
          }
          resources["aws_iam_group_membership.#{group.group_name}"] = {
            "type" => "aws_iam_group_membership",
            "primary" => {
              "id" => membership_name,
              "attributes" => attributes
            }
          }

          resources
        end
      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

16 entries across 16 versions & 1 rubygems

Version Path
terraforming-0.9.1 lib/terraforming/resource/iam_group_membership.rb
terraforming-0.9.0 lib/terraforming/resource/iam_group_membership.rb
terraforming-0.8.0 lib/terraforming/resource/iam_group_membership.rb
terraforming-0.7.0 lib/terraforming/resource/iam_group_membership.rb
terraforming-0.6.2 lib/terraforming/resource/iam_group_membership.rb
terraforming-0.6.1 lib/terraforming/resource/iam_group_membership.rb
terraforming-0.6.0 lib/terraforming/resource/iam_group_membership.rb
terraforming-0.5.0 lib/terraforming/resource/iam_group_membership.rb
terraforming-0.4.0 lib/terraforming/resource/iam_group_membership.rb
terraforming-0.3.2 lib/terraforming/resource/iam_group_membership.rb
terraforming-0.3.1 lib/terraforming/resource/iam_group_membership.rb
terraforming-0.3.0 lib/terraforming/resource/iam_group_membership.rb
terraforming-0.2.0 lib/terraforming/resource/iam_group_membership.rb
terraforming-0.1.6 lib/terraforming/resource/iam_group_membership.rb
terraforming-0.1.5 lib/terraforming/resource/iam_group_membership.rb
terraforming-0.1.4 lib/terraforming/resource/iam_group_membership.rb