-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aim users model and nested model policy
- Loading branch information
Rodrigo Estebanez
committed
Jun 5, 2012
1 parent
5fd4f95
commit cd1ee8d
Showing
5 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |