mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
aim users model and nested model policy
This commit is contained in:
parent
5fd4f95ab1
commit
cd1ee8de3f
5 changed files with 179 additions and 0 deletions
|
@ -55,6 +55,13 @@ module Fog
|
||||||
request :update_user
|
request :update_user
|
||||||
request :upload_server_certificate
|
request :upload_server_certificate
|
||||||
request :upload_signing_certificate
|
request :upload_signing_certificate
|
||||||
|
|
||||||
|
model_path 'fog/aws/models/iam'
|
||||||
|
model :user
|
||||||
|
collection :users
|
||||||
|
model :policy
|
||||||
|
collection :policies
|
||||||
|
|
||||||
|
|
||||||
class Mock
|
class Mock
|
||||||
def self.data
|
def self.data
|
||||||
|
|
55
lib/fog/aws/models/iam/policies.rb
Normal file
55
lib/fog/aws/models/iam/policies.rb
Normal file
|
@ -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
lib/fog/aws/models/iam/policy.rb
Normal file
42
lib/fog/aws/models/iam/policy.rb
Normal file
|
@ -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
lib/fog/aws/models/iam/user.rb
Normal file
48
lib/fog/aws/models/iam/user.rb
Normal file
|
@ -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
lib/fog/aws/models/iam/users.rb
Normal file
27
lib/fog/aws/models/iam/users.rb
Normal file
|
@ -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
|
Loading…
Add table
Reference in a new issue