1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Refactor aim modeling for nested models (policies and access keys)

This commit is contained in:
Rodrigo Estebanez 2012-06-05 15:02:03 +02:00
parent ac14390848
commit d258e36c98
4 changed files with 18 additions and 64 deletions

View file

@ -6,27 +6,19 @@ module Fog
class IAM
class AccessKeys < Fog::Collection
attribute :user
attribute :filters
model Fog::AWS::IAM::AccessKey
def initialize(attributes)
self.filters ||= {}
if attributes[:user]
filters[:identifier] = attributes[:user].id
else
raise ArgumentError.new("Can't get a user's access_key without a user.id")
end
def initialize(attributes = {})
@username = attributes[:username]
raise ArgumentError.new("Can't get an access_key's user without a username") unless @username
super
end
def all
data = connection.list_access_keys('UserName'=> filters[:identifier]).body['AccessKeys']
data = connection.list_access_keys('UserName'=> @username).body['AccessKeys']
# AWS response doesn't contain the UserName, this injects it
data.each {|access_key| access_key['UserName'] = filters[:identifier] }
data.each {|access_key| access_key['UserName'] = @username }
load(data)
end
@ -35,11 +27,7 @@ module Fog
end
def new(attributes = {})
if user
super({ :username => user.id }.merge!(attributes))
else
super
end
super({ :username => @username }.merge!(attributes))
end
end

View file

@ -6,19 +6,12 @@ module Fog
class IAM
class Policies < Fog::Collection
attribute :user
attribute :filters
model Fog::AWS::IAM::Policy
def initialize(attributes)
self.filters ||= {}
if attributes[:user]
filters[:identifier] = attributes[:user].id
else
raise ArgumentError.new("Can't get a policy's user without a user.id")
end
def initialize(attributes = {})
@username = attributes[:username]
raise ArgumentError.new("Can't get a policy's user without a username") unless @username
super
end
@ -26,29 +19,25 @@ module Fog
def all
# AWS method get_user_policy only returns an array of policy names, this is kind of useless,
# 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
policy_names = connection.list_user_policies(filters[:identifier]).body['PolicyNames'] # it returns an array
policy_names = connection.list_user_policies(@username).body['PolicyNames'] # it returns an array
policies = []
policy_names.each do |policy_name|
policies << connection.get_user_policy(policy_name,filters[:identifier]).body
policies << connection.get_user_policy(policy_name,@username).body
end
load(policies) # data is an array of attribute hashes
end
def get(identity)
data = connection.get_user_policy(identity,filters[:identifier]).body
data = connection.get_user_policy(identity,@username).body
new(data) # data is an attribute hash
rescue Fog::AWS::IAM::NotFound
nil
end
def new(attributes = {})
if user
super({ :username => user.id }.merge!(attributes))
else
super
end
super({ :username => @username }.merge!(attributes))
end
end
end
end

View file

@ -33,17 +33,6 @@ module Fog
connection.users.get(username)
end
# Converts attributes to a parameter hash suitable for requests
# def attributes_to_params
# options = {
# 'PolicyName' => id,
# 'UserName' => username,
# 'PolicyDocument' => document
# }
#
# options.delete_if {|key, value| value.nil?}
# end
end
end
end

View file

@ -13,7 +13,6 @@ module Fog
def save
requires :id
data = connection.create_user(id).body['User']
merge_attributes(data)
true
@ -27,24 +26,13 @@ module Fog
def policies
requires :id
connection.policies(:user => self)
connection.policies(:username => id)
end
def access_keys
requires :id
connection.access_keys(:user => self)
connection.access_keys(:username => id)
end
# # Converts attributes to a parameter hash suitable for requests
# def attributes_to_params
# options = {
# 'UserName' => id,
# 'Path' => path,
# 'Arn' => arn,
# 'UserId' => user_id
# }
#
# options.delete_if {|key, value| value.nil?}
# end
end
end