Module: AWS::IAM::PolicyCollection
- Includes:
- Collection
- Included in:
- GroupPolicyCollection, UserPolicyCollection
- Defined in:
- lib/aws/iam/policy_collection.rb
Overview
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.
Instance Method Summary collapse
-
#[](name) ⇒ Policy
Retrieves a policy document by name.
-
#[]=(name, document) ⇒ Object
Adds or replaces a policy document.
-
#clear ⇒ Object
Removes all policies from the collection.
-
#delete(name) ⇒ Object
Deletes a policy by name.
- #each(opts = {}) {|name, policy| ... } ⇒ Object
-
#has_key?(name) ⇒ Boolean
(also: #include?, #key?, #member?)
True if there is a policy with the given name.
-
#keys ⇒ Enumerator<String>
(also: #names)
An enumerator for retrieving all the policy names that are currently associated with the resource.
-
#to_h ⇒ Hash
The contents of the collection as a hash.
-
#values ⇒ Enumerator<Policy>
An enumerator for retrieving all the policy documents that are currently associated with the resource.
-
#values_at(*names) ⇒ Array<Policy>
Retrieves multiple policy documents by name.
Instance Method Details
#[](name) ⇒ Policy
Retrieves a policy document by name.
36 37 38 39 40 41 |
# File 'lib/aws/iam/policy_collection.rb', line 36 def [] name resp = get_policy(:policy_name => name) Policy.from_json(URI.unescape(resp.policy_document)) rescue Errors::NoSuchEntity => e nil end |
#[]=(name, document) ⇒ Object
Adds or replaces a policy document.
51 52 53 54 55 56 |
# File 'lib/aws/iam/policy_collection.rb', line 51 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 ⇒ Object
Removes all policies from the collection.
98 99 100 |
# File 'lib/aws/iam/policy_collection.rb', line 98 def clear keys.each { |k| delete(k) } end |
#delete(name) ⇒ Object
Deletes a policy by name. This method is idempotent; if no policy exists with the given name, the method does nothing.
62 63 64 65 66 67 |
# File 'lib/aws/iam/policy_collection.rb', line 62 def delete(name) delete_policy(:policy_name => name) nil rescue Errors::NoSuchEntity => e nil end |
#each(opts = {}) {|name, policy| ... } ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/aws/iam/policy_collection.rb', line 121 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) ⇒ Boolean Also known as: include?, key?, member?
Returns True if there is a policy with the given name.
105 106 107 108 109 110 |
# File 'lib/aws/iam/policy_collection.rb', line 105 def has_key? name get_policy(:policy_name => name) true rescue Errors::NoSuchEntity => e false end |
#keys ⇒ Enumerator<String> Also known as: names
Returns An enumerator for retrieving all the policy names that are currently associated with the resource.
85 86 87 |
# File 'lib/aws/iam/policy_collection.rb', line 85 def keys enumerator(:names_only => true) end |
#to_h ⇒ Hash
Returns The contents of the collection as a hash.
140 141 142 143 144 145 |
# File 'lib/aws/iam/policy_collection.rb', line 140 def to_h inject({}) do |hash, (name, policy)| hash[name] = policy hash end end |
#values ⇒ Enumerator<Policy>
Returns An enumerator for retrieving all the policy documents that are currently associated with the resource.
93 94 95 |
# File 'lib/aws/iam/policy_collection.rb', line 93 def values enumerator(:values_only => true) end |
#values_at(*names) ⇒ Array<Policy>
Retrieves multiple policy documents by name. This method makes one request to AWS IAM per argument.
78 79 80 |
# File 'lib/aws/iam/policy_collection.rb', line 78 def values_at(*names) names.map { |n| self[n] } end |