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 Attribute Summary

Attributes included from Core::Model

#config

Instance Method Summary collapse

Methods included from Core::Collection::Limitable

#each_batch

Methods included from Core::Collection

#each_batch, #enum, #first, #in_groups_of, #page

Methods included from Core::Model

#client, #config_prefix, #initialize, #inspect

Instance Method Details

#[](name) ⇒ Policy

Retrieves a policy document by name.

Parameters:

  • name (String)

    The name of the policy to retrieve.

Returns:

  • (Policy)

    The policy with the given name. If no such policy exists, this method returns nil.



33
34
35
36
37
38
# 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) ⇒ Object

Adds or replaces a policy document.

Parameters:

  • name (String)

    The name of the policy document.

  • document (Policy, String)

    The policy document. This can be a JSON string, or any object that responds to to_json. The AWS::IAM::Policy class provides a convenient way to construct policy documents that you can use with AWS IAM.



48
49
50
51
52
53
# 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

#clearObject

Removes all policies from the collection.



95
96
97
# File 'lib/aws/iam/policy_collection.rb', line 95

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.

Parameters:

  • name (String)

    The name of the policy document.



59
60
61
62
63
64
# 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 = {}) {|name, policy| ... } ⇒ Object

Yields:

  • (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.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 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) ⇒ Boolean Also known as: include?, key?, member?

Returns True if there is a policy with the given name.

Parameters:

  • name (String)

    The name of the policy to check.

Returns:

  • (Boolean)

    True if there is a policy with the given name.



102
103
104
105
106
107
# 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

#keysEnumerator<String> Also known as: names

Returns An enumerator for retrieving all the policy names that are currently associated with the resource.

Returns:

  • (Enumerator<String>)

    An enumerator for retrieving all the policy names that are currently associated with the resource.



82
83
84
# File 'lib/aws/iam/policy_collection.rb', line 82

def keys
  enumerator(:names_only => true)
end

#to_hHash

Returns The contents of the collection as a hash.

Returns:

  • (Hash)

    The contents of the collection as a hash.



137
138
139
140
141
142
# File 'lib/aws/iam/policy_collection.rb', line 137

def to_h
  inject({}) do |hash, (name, policy)|
    hash[name] = policy
    hash
  end
end

#valuesEnumerator<Policy>

Returns An enumerator for retrieving all the policy documents that are currently associated with the resource.

Returns:

  • (Enumerator<Policy>)

    An enumerator for retrieving all the policy documents that are currently associated with the resource.



90
91
92
# File 'lib/aws/iam/policy_collection.rb', line 90

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.

Parameters:

  • names

    Each argument is the name of a policy to retrieve.

Returns:

  • (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.



75
76
77
# File 'lib/aws/iam/policy_collection.rb', line 75

def values_at(*names)
  names.map { |n| self[n] }
end