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

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.



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.

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.



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

#clearObject

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.

Parameters:

  • name (String)

    The name of the policy document.



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

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.



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.

Parameters:

  • name (String)

    The name of the policy to check.

Returns:

  • (Boolean)

    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

#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.



85
86
87
# File 'lib/aws/iam/policy_collection.rb', line 85

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.



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

#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.



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.

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.



78
79
80
# File 'lib/aws/iam/policy_collection.rb', line 78

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