Shared methods exposing a collection of policy documents associated with an IAM resource (a {User} or a {Group}). Policy collections can be constructed using {AWS::IAM::Group#policies} and {AWS::IAM::User#policies}.
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
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
Removes all policies from the collection.
# File lib/aws/iam/policy_collection.rb, line 95 def clear keys.each { |k| delete(k) } end
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
@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.
# 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
@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
@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
@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
@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
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
# File lib/aws/iam/policy_collection.rb, line 168 def client_opts(opts = {}) Hash[[[:"#{resource_name}_name", send(resource_name).name]]].merge(opts) end
# File lib/aws/iam/policy_collection.rb, line 162 def delete_policy(opts = {}) client.send("delete_#{resource_name}_policy", client_opts(opts)) end
# File lib/aws/iam/policy_collection.rb, line 181 def each_item(response, &block) response.policy_names.each(&block) end
# File lib/aws/iam/policy_collection.rb, line 145 def get_policy(opts = {}) client.send("get_#{resource_name}_policy", client_opts(opts)) end
# File lib/aws/iam/policy_collection.rb, line 151 def put_policy(opts = {}) client.send("put_#{resource_name}_policy", client_opts(opts)) end
# File lib/aws/iam/policy_collection.rb, line 157 def request_method :"list_#{resource_name}_policies" end
# File lib/aws/iam/policy_collection.rb, line 174 def resource_name raise NotImplementedError unless self.class.name =~ %rAWS::IAM::(.*)PolicyCollection$/ $1.downcase end