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:
parent
ac14390848
commit
d258e36c98
4 changed files with 18 additions and 64 deletions
|
@ -6,27 +6,19 @@ module Fog
|
||||||
class IAM
|
class IAM
|
||||||
|
|
||||||
class AccessKeys < Fog::Collection
|
class AccessKeys < Fog::Collection
|
||||||
attribute :user
|
|
||||||
attribute :filters
|
|
||||||
|
|
||||||
|
|
||||||
model Fog::AWS::IAM::AccessKey
|
model Fog::AWS::IAM::AccessKey
|
||||||
|
|
||||||
def initialize(attributes)
|
def initialize(attributes = {})
|
||||||
self.filters ||= {}
|
@username = attributes[:username]
|
||||||
if attributes[:user]
|
raise ArgumentError.new("Can't get an access_key's user without a username") unless @username
|
||||||
filters[:identifier] = attributes[:user].id
|
|
||||||
else
|
|
||||||
raise ArgumentError.new("Can't get a user's access_key without a user.id")
|
|
||||||
end
|
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def all
|
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
|
# 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)
|
load(data)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -35,11 +27,7 @@ module Fog
|
||||||
end
|
end
|
||||||
|
|
||||||
def new(attributes = {})
|
def new(attributes = {})
|
||||||
if user
|
super({ :username => @username }.merge!(attributes))
|
||||||
super({ :username => user.id }.merge!(attributes))
|
|
||||||
else
|
|
||||||
super
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,19 +6,12 @@ module Fog
|
||||||
class IAM
|
class IAM
|
||||||
|
|
||||||
class Policies < Fog::Collection
|
class Policies < Fog::Collection
|
||||||
attribute :user
|
|
||||||
attribute :filters
|
|
||||||
|
|
||||||
|
|
||||||
model Fog::AWS::IAM::Policy
|
model Fog::AWS::IAM::Policy
|
||||||
|
|
||||||
def initialize(attributes)
|
def initialize(attributes = {})
|
||||||
self.filters ||= {}
|
@username = attributes[:username]
|
||||||
if attributes[:user]
|
raise ArgumentError.new("Can't get a policy's user without a username") unless @username
|
||||||
filters[:identifier] = attributes[:user].id
|
|
||||||
else
|
|
||||||
raise ArgumentError.new("Can't get a policy's user without a user.id")
|
|
||||||
end
|
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -26,29 +19,25 @@ module Fog
|
||||||
def all
|
def all
|
||||||
# AWS method get_user_policy only returns an array of policy names, this is kind of useless,
|
# 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
|
# 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 = []
|
policies = []
|
||||||
policy_names.each do |policy_name|
|
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
|
end
|
||||||
load(policies) # data is an array of attribute hashes
|
load(policies) # data is an array of attribute hashes
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(identity)
|
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
|
new(data) # data is an attribute hash
|
||||||
rescue Fog::AWS::IAM::NotFound
|
rescue Fog::AWS::IAM::NotFound
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def new(attributes = {})
|
def new(attributes = {})
|
||||||
if user
|
super({ :username => @username }.merge!(attributes))
|
||||||
super({ :username => user.id }.merge!(attributes))
|
|
||||||
else
|
|
||||||
super
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -33,17 +33,6 @@ module Fog
|
||||||
connection.users.get(username)
|
connection.users.get(username)
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,7 +13,6 @@ module Fog
|
||||||
|
|
||||||
def save
|
def save
|
||||||
requires :id
|
requires :id
|
||||||
|
|
||||||
data = connection.create_user(id).body['User']
|
data = connection.create_user(id).body['User']
|
||||||
merge_attributes(data)
|
merge_attributes(data)
|
||||||
true
|
true
|
||||||
|
@ -27,24 +26,13 @@ module Fog
|
||||||
|
|
||||||
def policies
|
def policies
|
||||||
requires :id
|
requires :id
|
||||||
connection.policies(:user => self)
|
connection.policies(:username => id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def access_keys
|
def access_keys
|
||||||
requires :id
|
requires :id
|
||||||
connection.access_keys(:user => self)
|
connection.access_keys(:username => id)
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue