Skip to content

Commit

Permalink
aim users model and nested model policy
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigo Estebanez committed Jun 5, 2012
1 parent 5fd4f95 commit cd1ee8d
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/fog/aws/iam.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ class ValidationError < Fog::AWS::IAM::Error; end
request :update_user
request :upload_server_certificate
request :upload_signing_certificate

model_path 'fog/aws/models/iam'
model :user
collection :users
model :policy
collection :policies


class Mock
def self.data
Expand Down
55 changes: 55 additions & 0 deletions lib/fog/aws/models/iam/policies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'fog/core/collection'
require 'fog/aws/models/iam/policy'

module Fog
module AWS
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
super
end


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
policies = []
policy_names.each do |policy_name|
policies << connection.get_user_policy(policy_name,filters[:identifier]).body
end
load(policies) # data is an array of attribute hashes
end

def get(identity)
data = connection.get_user_policy(identity,filters[:identifier]).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
end

end
end
end
end
42 changes: 42 additions & 0 deletions lib/fog/aws/models/iam/policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'fog/core/model'

module Fog
module AWS
class IAM

class Policy < Fog::Model

identity :id, :aliases => 'PolicyName'
attribute :username, :aliases => 'UserName'
attribute :document, :aliases => 'PolicyDocument'

def save
requires :id
requires :username
requires :document

data = connection.put_user_policy(username, id, document).body
merge_attributes(data)
true
end

def user
requires :username
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
end
48 changes: 48 additions & 0 deletions lib/fog/aws/models/iam/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'fog/core/model'

module Fog
module AWS
class IAM

class User < Fog::Model

identity :id, :aliases => 'UserName'
attribute :path, :aliases => 'Path'
attribute :arn, :aliases => 'Arn'
attribute :user_id, :aliases => 'UserId'

def save
requires :id

data = connection.create_user(id).body['User']
merge_attributes(data)
true
end

def destroy
requires :id
connection.delete_user(id)
true
end

def policies
requires :id
connection.policies(:user => self)
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
27 changes: 27 additions & 0 deletions lib/fog/aws/models/iam/users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'fog/core/collection'
require 'fog/aws/models/iam/user'

module Fog
module AWS
class IAM

class Users < Fog::Collection

model Fog::AWS::IAM::User

def all
data = connection.list_users.body['Users']
load(data) # data is an array of attribute hashes
end

def get(identity)
data = connection.get_user('UserName' => identity).body['User']
new(data) # data is an attribute hash
rescue Fog::AWS::IAM::NotFound
nil
end

end
end
end
end

0 comments on commit cd1ee8d

Please sign in to comment.