class PracticeTerraforming::Resource::IAMUserPolicyAttachment

Public Class Methods

new(client) click to toggle source
# File lib/practice_terraforming/resource/iam_user_policy_attachment.rb, line 20
def initialize(client)
  @client = client
end
tf(client: Aws::IAM::Client.new) click to toggle source

TODO: Select appropriate Client class from here: docs.aws.amazon.com/sdkforruby/api/index.html

# File lib/practice_terraforming/resource/iam_user_policy_attachment.rb, line 10
def self.tf(client: Aws::IAM::Client.new)
  self.new(client).tf
end
tfstate(client: Aws::IAM::Client.new) click to toggle source

TODO: Select appropriate Client class from here: docs.aws.amazon.com/sdkforruby/api/index.html

# File lib/practice_terraforming/resource/iam_user_policy_attachment.rb, line 16
def self.tfstate(client: Aws::IAM::Client.new)
  self.new(client).tfstate
end

Public Instance Methods

tf() click to toggle source
# File lib/practice_terraforming/resource/iam_user_policy_attachment.rb, line 24
def tf
  apply_template(@client, "tf/iam_user_policy_attachment")
end
tfstate() click to toggle source
# File lib/practice_terraforming/resource/iam_user_policy_attachment.rb, line 28
def tfstate
  iam_user_policy_attachments.inject({}) do |resources, user_policy_attachment|
    attributes = {
      "id" => user_policy_attachment[:name],
      "policy_arn" => user_policy_attachment[:policy_arn],
      "user" => user_policy_attachment[:user]
    }
    resources["aws_iam_user_policy_attachment.#{module_name_of(user_policy_attachment)}"] = {
      "type" => "aws_iam_user_policy_attachment",
      "primary" => {
        "id" => user_policy_attachment[:name],
        "attributes" => attributes
      }
    }

    resources
  end
end

Private Instance Methods

attachment_name_from(user, policy) click to toggle source
# File lib/practice_terraforming/resource/iam_user_policy_attachment.rb, line 49
def attachment_name_from(user, policy)
  "#{user.user_name}-#{policy.policy_name}-attachment"
end
iam_user_policy_attachments() click to toggle source
# File lib/practice_terraforming/resource/iam_user_policy_attachment.rb, line 61
def iam_user_policy_attachments
  iam_users.map do |user|
    policies_attached_to(user).map do |policy|
      {
        user: user.user_name,
        policy_arn: policy.policy_arn,
        name: attachment_name_from(user, policy)
      }
    end
  end.flatten
end
iam_users() click to toggle source
# File lib/practice_terraforming/resource/iam_user_policy_attachment.rb, line 53
def iam_users
  @client.list_users.map(&:users).flatten
end
module_name_of(user_policy_attachment) click to toggle source
# File lib/practice_terraforming/resource/iam_user_policy_attachment.rb, line 73
def module_name_of(user_policy_attachment)
  normalize_module_name(user_policy_attachment[:name])
end
policies_attached_to(user) click to toggle source
# File lib/practice_terraforming/resource/iam_user_policy_attachment.rb, line 57
def policies_attached_to(user)
  @client.list_attached_user_policies(user_name: user.user_name).attached_policies
end