2012-06-02 11:04:17 -04:00
|
|
|
require 'fog/core/collection'
|
|
|
|
require 'fog/aws/models/iam/policy'
|
|
|
|
|
|
|
|
module Fog
|
|
|
|
module AWS
|
|
|
|
class IAM
|
|
|
|
|
|
|
|
class Policies < Fog::Collection
|
2012-12-22 18:30:34 -05:00
|
|
|
|
2012-06-02 11:04:17 -04:00
|
|
|
model Fog::AWS::IAM::Policy
|
2012-12-22 18:30:34 -05:00
|
|
|
|
2012-06-05 09:02:03 -04:00
|
|
|
def initialize(attributes = {})
|
|
|
|
@username = attributes[:username]
|
|
|
|
raise ArgumentError.new("Can't get a policy's user without a username") unless @username
|
2012-06-02 11:04:17 -04:00
|
|
|
super
|
|
|
|
end
|
2012-12-22 18:30:34 -05:00
|
|
|
|
2012-06-02 11:04:17 -04:00
|
|
|
|
|
|
|
def all
|
2012-12-22 18:30:34 -05:00
|
|
|
# AWS method get_user_policy only returns an array of policy names, this is kind of useless,
|
2012-06-02 11:04:17 -04:00
|
|
|
# that's why it has to loop through the list to get the details of each element. I don't like it because it makes this method slow
|
2012-12-22 18:30:34 -05:00
|
|
|
policy_names = service.list_user_policies(@username).body['PolicyNames'] # it returns an array
|
2012-06-02 11:04:17 -04:00
|
|
|
policies = []
|
|
|
|
policy_names.each do |policy_name|
|
2012-12-22 18:30:34 -05:00
|
|
|
policies << service.get_user_policy(policy_name,@username).body['Policy']
|
2012-06-02 11:04:17 -04:00
|
|
|
end
|
|
|
|
load(policies) # data is an array of attribute hashes
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(identity)
|
2012-12-22 18:30:34 -05:00
|
|
|
data = service.get_user_policy(identity,@username).body['Policy']
|
2012-06-02 11:04:17 -04:00
|
|
|
new(data) # data is an attribute hash
|
|
|
|
rescue Fog::AWS::IAM::NotFound
|
|
|
|
nil
|
|
|
|
end
|
2012-12-22 18:30:34 -05:00
|
|
|
|
2012-06-02 11:04:17 -04:00
|
|
|
def new(attributes = {})
|
2012-06-05 09:02:03 -04:00
|
|
|
super({ :username => @username }.merge!(attributes))
|
2012-06-02 11:04:17 -04:00
|
|
|
end
|
2012-12-22 18:30:34 -05:00
|
|
|
|
2012-06-02 11:04:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-12-22 18:30:34 -05:00
|
|
|
end
|