module AWS::IAM::PolicyCollection

Shared methods exposing a collection of policy documents associated with an IAM resource (a {User} or a {Group}). Policy collections can be constructed using {Group#policies} and {User#policies}.

Public Instance Methods

[](name) click to toggle source

Retrieves a policy document by name.

@param [String] name The name of the policy to retrieve.

@return [Policy] The policy with the given name. If no such

policy exists, this method returns `nil`.
# File lib/aws/iam/policy_collection.rb, line 33
def [] name
  resp = get_policy(:policy_name => name)
  Policy.from_json(URI.unescape(resp.policy_document))
rescue Errors::NoSuchEntity => e
  nil
end
[]=(name, document) click to toggle source

Adds or replaces a policy document.

@param [String] name The name of the policy document.

@param [Policy,String] document The policy document. This can

be a JSON string, or any object that responds to `to_json`.
The {Policy} class provides a convenient way to construct
policy documents that you can use with AWS IAM.
# File lib/aws/iam/policy_collection.rb, line 48
def []= name, document
  document = document.to_json if document.respond_to?(:to_json) and
    !document.kind_of?(String)
  put_policy(:policy_name => name,
             :policy_document => document)
end
clear() click to toggle source

Removes all policies from the collection.

# File lib/aws/iam/policy_collection.rb, line 95
def clear
  keys.each { |k| delete(k) }
end
delete(name) click to toggle source

Deletes a policy by name. This method is idempotent; if no policy exists with the given name, the method does nothing.

@param [String] name The name of the policy document.

# File lib/aws/iam/policy_collection.rb, line 59
def delete(name)
  delete_policy(:policy_name => name)
  nil
rescue Errors::NoSuchEntity => e
  nil
end
each(opts = {}) { |pn| ... } click to toggle source

@yield [name, policy] The name and document for each policy

that is associated with the resource.  Like `Hash#each`,
this method is sensitive to the arity of the provided block;
if the block takes two arguments, they will be the name and
document.  If it accepts only one argument, it will be an
array containing the name and document.
Calls superclass method
# File lib/aws/iam/policy_collection.rb, line 118
def each opts = {}, &block
  opts = opts.dup
  names_only = opts.delete(:names_only)
  values_only = opts.delete(:values_only)
  super(client_opts(opts)) do |pn|
    case
    when names_only
      yield pn
    when values_only
      yield self[pn]
    when block.arity == 2
      yield pn, self[pn]
    else
      yield [pn, self[pn]]
    end
  end
end
has_key?(name) click to toggle source

@param [String] name The name of the policy to check.

@return [Boolean] True if there is a policy with the given name.

# File lib/aws/iam/policy_collection.rb, line 102
def has_key? name
  get_policy(:policy_name => name)
  true
rescue Errors::NoSuchEntity => e
  false
end
Also aliased as: include?, key?, member?
include?(name)
Alias for: has_key?
key?(name)
Alias for: has_key?
keys() click to toggle source

@return [Enumerator<String>] An enumerator for retrieving all

the policy names that are currently associated with the
resource.
# File lib/aws/iam/policy_collection.rb, line 82
def keys
  enumerator(:names_only => true)
end
Also aliased as: names
member?(name)
Alias for: has_key?
names()
Alias for: keys
to_h() click to toggle source

@return [Hash] The contents of the collection as a hash.

# File lib/aws/iam/policy_collection.rb, line 137
def to_h
  inject({}) do |hash, (name, policy)|
    hash[name] = policy
    hash
  end
end
values() click to toggle source

@return [Enumerator<Policy>] An enumerator for retrieving all

the policy documents that are currently associated with the
resource.
# File lib/aws/iam/policy_collection.rb, line 90
def values
  enumerator(:values_only => true)
end
values_at(*names) click to toggle source

Retrieves multiple policy documents by name. This method makes one request to AWS IAM per argument.

@param names Each argument is the name of a policy to retrieve.

@return [Array<Policy>] An array containing the requested

policy documents, in the same order as the argument list.
If a requested policy does not exist, the array member
corresponding to that argument will be `nil`.
# File lib/aws/iam/policy_collection.rb, line 75
def values_at(*names)
  names.map { |n| self[n] }
end

Protected Instance Methods

client_opts(opts = {}) click to toggle source
# File lib/aws/iam/policy_collection.rb, line 168
def client_opts(opts = {})
  Hash[[[:"#{resource_name}_name",
         send(resource_name).name]]].merge(opts)
end
delete_policy(opts = {}) click to toggle source
# File lib/aws/iam/policy_collection.rb, line 162
def delete_policy(opts = {})
  client.send("delete_#{resource_name}_policy",
              client_opts(opts))
end
each_item(response, &block) click to toggle source
# File lib/aws/iam/policy_collection.rb, line 181
def each_item(response, &block)
  response.data[:policy_names].each(&block)
end
get_policy(opts = {}) click to toggle source
# File lib/aws/iam/policy_collection.rb, line 145
def get_policy(opts = {})
  client.send("get_#{resource_name}_policy",
              client_opts(opts))
end
put_policy(opts = {}) click to toggle source
# File lib/aws/iam/policy_collection.rb, line 151
def put_policy(opts = {})
  client.send("put_#{resource_name}_policy",
              client_opts(opts))
end
request_method() click to toggle source
# File lib/aws/iam/policy_collection.rb, line 157
def request_method
  :"list_#{resource_name}_policies"
end
resource_name() click to toggle source
# File lib/aws/iam/policy_collection.rb, line 174
def resource_name
  raise NotImplementedError unless
    self.class.name =~ /AWS::IAM::(.*)PolicyCollection$/
  $1.downcase
end