From d258e36c98b61f2e0d1cc22e221cb6d7e3380fb1 Mon Sep 17 00:00:00 2001 From: Rodrigo Estebanez Date: Tue, 5 Jun 2012 15:02:03 +0200 Subject: [PATCH] Refactor aim modeling for nested models (policies and access keys) --- lib/fog/aws/models/iam/access_keys.rb | 26 +++++++----------------- lib/fog/aws/models/iam/policies.rb | 29 +++++++++------------------ lib/fog/aws/models/iam/policy.rb | 11 ---------- lib/fog/aws/models/iam/user.rb | 16 ++------------- 4 files changed, 18 insertions(+), 64 deletions(-) diff --git a/lib/fog/aws/models/iam/access_keys.rb b/lib/fog/aws/models/iam/access_keys.rb index 8cab07ae9..db0e93698 100644 --- a/lib/fog/aws/models/iam/access_keys.rb +++ b/lib/fog/aws/models/iam/access_keys.rb @@ -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 diff --git a/lib/fog/aws/models/iam/policies.rb b/lib/fog/aws/models/iam/policies.rb index b977704f3..eefd9d464 100644 --- a/lib/fog/aws/models/iam/policies.rb +++ b/lib/fog/aws/models/iam/policies.rb @@ -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 diff --git a/lib/fog/aws/models/iam/policy.rb b/lib/fog/aws/models/iam/policy.rb index 666d57629..c2f67b542 100644 --- a/lib/fog/aws/models/iam/policy.rb +++ b/lib/fog/aws/models/iam/policy.rb @@ -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 diff --git a/lib/fog/aws/models/iam/user.rb b/lib/fog/aws/models/iam/user.rb index 0fee23f22..380a36590 100644 --- a/lib/fog/aws/models/iam/user.rb +++ b/lib/fog/aws/models/iam/user.rb @@ -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