1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/openstack/models/identity/user.rb
2012-04-30 10:34:59 +08:00

70 lines
1.9 KiB
Ruby

require 'fog/core/model'
module Fog
module Identity
class OpenStack
class User < Fog::Model
identity :id
attribute :email
attribute :enabled
attribute :name
attribute :tenantId, :aliases => 'tenant_id'
attribute :password
attr_accessor :email, :name, :tenant_id, :enabled, :password
def initialize(attributes)
@connection = attributes[:connection]
attributes[:enabled] ||= true
super
end
def save
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
requires :name, :tenant_id, :password
data = connection.create_user(name, password, email, tenant_id, enabled)
merge_attributes(data.body['user'])
true
end
def update(options = {})
requires :id
options.merge('id' => id)
response = connection.update_user(id, options)
true
end
def update_password(password)
update({'password' => password, 'url' => "/users/#{id}/OS-KSADM/password"})
end
def update_tenant(tenant)
tenant = tenant.id if tenant.class != String
update({:tenantId => tenant, 'url' => "/users/#{id}/OS-KSADM/tenant"})
end
def update_enabled(enabled)
update({:enabled => enabled, 'url' => "/users/#{id}/OS-KSADM/enabled"})
end
def destroy
requires :id
connection.delete_user(id)
true
end
def roles
return Array.new unless tenant_id
tenant = Fog::Identity::OpenStack::Tenant.
new(connection.get_tenant(tenant_id).body['tenant'])
connection.roles(
:tenant => tenant,
:user => self)
end
end # class User
end # class OpenStack
end # module Identity
end # module Fog