mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[rackspace|identity] Add user model and collection with tests.
This commit is contained in:
parent
2ad93f330f
commit
21c0fd079f
14 changed files with 314 additions and 0 deletions
|
@ -10,6 +10,14 @@ module Fog
|
|||
recognizes :rackspace_auth_url
|
||||
|
||||
model_path 'fog/rackspace/models/identity'
|
||||
model :user
|
||||
collection :users
|
||||
model :role
|
||||
collection :roles
|
||||
model :credential
|
||||
collection :credentials
|
||||
model :tenant
|
||||
collection :tenants
|
||||
|
||||
request_path 'fog/rackspace/requests/identity'
|
||||
request :create_token
|
||||
|
|
13
lib/fog/rackspace/models/identity/credential.rb
Normal file
13
lib/fog/rackspace/models/identity/credential.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Identity
|
||||
class Credential < Fog::Model
|
||||
identity :apiKey
|
||||
|
||||
attribute :username
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
32
lib/fog/rackspace/models/identity/credentials.rb
Normal file
32
lib/fog/rackspace/models/identity/credentials.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/rackspace/models/identity/credential'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Identity
|
||||
class Credentials < Fog::Collection
|
||||
|
||||
model Fog::Rackspace::Identity::Credential
|
||||
|
||||
attr_accessor :user
|
||||
|
||||
def all
|
||||
requires :user
|
||||
load(retrieve_credentials)
|
||||
end
|
||||
|
||||
def get(id)
|
||||
requires :user
|
||||
data = retrieve_credentials.find{ |credential| credential['id'] == id }
|
||||
data && new(data)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def retrieve_credentials
|
||||
data = connection.list_credentials(user.identity).body['credentials']
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
14
lib/fog/rackspace/models/identity/role.rb
Normal file
14
lib/fog/rackspace/models/identity/role.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Identity
|
||||
class Role < Fog::Model
|
||||
identity :id
|
||||
|
||||
attribute :name
|
||||
attribute :description
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
32
lib/fog/rackspace/models/identity/roles.rb
Normal file
32
lib/fog/rackspace/models/identity/roles.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/rackspace/models/identity/role'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Identity
|
||||
class Roles < Fog::Collection
|
||||
|
||||
model Fog::Rackspace::Identity::Role
|
||||
|
||||
attr_accessor :user
|
||||
|
||||
def all
|
||||
requires :user
|
||||
load(retrieve_roles)
|
||||
end
|
||||
|
||||
def get(id)
|
||||
requires :user
|
||||
data = retrieve_roles.find{ |role| role['id'] == id }
|
||||
data && new(data)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def retrieve_roles
|
||||
data = connection.list_user_roles(user.identity).body['roles']
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
15
lib/fog/rackspace/models/identity/tenant.rb
Normal file
15
lib/fog/rackspace/models/identity/tenant.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Identity
|
||||
class Tenant < Fog::Model
|
||||
identity :id
|
||||
|
||||
attribute :name
|
||||
attribute :description
|
||||
attribute :enabled
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
28
lib/fog/rackspace/models/identity/tenants.rb
Normal file
28
lib/fog/rackspace/models/identity/tenants.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/rackspace/models/identity/tenant'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Identity
|
||||
class Tenants < Fog::Collection
|
||||
|
||||
model Fog::Rackspace::Identity::Tenant
|
||||
|
||||
def all
|
||||
load(retrieve_tenants)
|
||||
end
|
||||
|
||||
def get(id)
|
||||
data = retrieve_tenants.find{ |tenant| tenant['id'] == id }
|
||||
data && new(data)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def retrieve_tenants
|
||||
data = connection.list_tenants.body['tenants']
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
53
lib/fog/rackspace/models/identity/user.rb
Normal file
53
lib/fog/rackspace/models/identity/user.rb
Normal file
|
@ -0,0 +1,53 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Identity
|
||||
class User < Fog::Model
|
||||
identity :id
|
||||
|
||||
attribute :username
|
||||
attribute :password, :alias => 'OS-KSADM:password'
|
||||
attribute :email
|
||||
attribute :enabled
|
||||
attribute :created
|
||||
attribute :updated
|
||||
|
||||
def save
|
||||
requires :username, :email, :enabled
|
||||
if identity.nil?
|
||||
data = connection.create_user(username, email, enabled, :password => password)
|
||||
else
|
||||
data = connection.update_user(identity, username, email, enabled, :password => password)
|
||||
end
|
||||
merge_attributes(data.body['user'])
|
||||
true
|
||||
end
|
||||
|
||||
def destroy
|
||||
requires :identity
|
||||
connection.delete_user(identity)
|
||||
true
|
||||
end
|
||||
|
||||
def roles
|
||||
@roles ||= begin
|
||||
Fog::Rackspace::Identity::Roles.new({
|
||||
:connection => connection,
|
||||
:user => self
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
def credentials
|
||||
@credentials ||= begin
|
||||
Fog::Rackspace::Identity::Credentials.new({
|
||||
:connection => connection,
|
||||
:user => self
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
32
lib/fog/rackspace/models/identity/users.rb
Normal file
32
lib/fog/rackspace/models/identity/users.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/rackspace/models/identity/user'
|
||||
|
||||
module Fog
|
||||
module Rackspace
|
||||
class Identity
|
||||
class Users < Fog::Collection
|
||||
|
||||
model Fog::Rackspace::Identity::User
|
||||
|
||||
def all
|
||||
data = connection.list_users.body['users']
|
||||
load(data)
|
||||
end
|
||||
|
||||
def get(user_id)
|
||||
data = connection.get_user_by_id(user_id).body['user']
|
||||
new(data)
|
||||
rescue Excon::Errors::NotFound
|
||||
nil
|
||||
end
|
||||
|
||||
def get_by_name(user_name)
|
||||
data = connection.get_user_by_name(user_name).body['user']
|
||||
new(data)
|
||||
rescue Excon::Errors::NotFound
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
16
tests/rackspace/models/identity/credentials_tests.rb
Normal file
16
tests/rackspace/models/identity/credentials_tests.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
Shindo.tests('Fog::Rackspace::Identity | credentials', ['rackspace']) do
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
service = Fog::Rackspace::Identity.new
|
||||
user = service.users.all.first
|
||||
|
||||
tests("#all").succeeds do
|
||||
user.credentials.all
|
||||
end
|
||||
|
||||
tests("#get").succeeds do
|
||||
credential = user.credentials.all.first
|
||||
user.credentials.get(credential.identity)
|
||||
end
|
||||
end
|
16
tests/rackspace/models/identity/roles_tests.rb
Normal file
16
tests/rackspace/models/identity/roles_tests.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
Shindo.tests('Fog::Rackspace::Identity | roles', ['rackspace']) do
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
service = Fog::Rackspace::Identity.new
|
||||
user = service.users.all.first
|
||||
|
||||
tests("#all").succeeds do
|
||||
user.roles.all
|
||||
end
|
||||
|
||||
tests("#get").succeeds do
|
||||
role = user.roles.all.first
|
||||
user.roles.get(role.identity)
|
||||
end
|
||||
end
|
21
tests/rackspace/models/identity/tenants_tests.rb
Normal file
21
tests/rackspace/models/identity/tenants_tests.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
Shindo.tests('Fog::Rackspace::Identity | tenants', ['rackspace']) do
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
service = Fog::Rackspace::Identity.new
|
||||
username = "fog_user_#{Time.now.to_i.to_s}"
|
||||
options = {
|
||||
:username => username,
|
||||
:email => 'email@example.com',
|
||||
:enabled => true
|
||||
}
|
||||
|
||||
tests("#all").succeeds do
|
||||
service.tenants.all
|
||||
end
|
||||
|
||||
tests("#get").succeeds do
|
||||
tenant = service.tenants.all.first
|
||||
service.tenants.get(tenant.identity)
|
||||
end
|
||||
end
|
17
tests/rackspace/models/identity/user_tests.rb
Normal file
17
tests/rackspace/models/identity/user_tests.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
Shindo.tests('Fog::Rackspace::Identity | user', ['rackspace']) do
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
service = Fog::Rackspace::Identity.new
|
||||
options = {
|
||||
:username => "fog_user_#{Time.now.to_i.to_s}",
|
||||
:email => 'email@example.com',
|
||||
:enabled => true
|
||||
}
|
||||
|
||||
model_tests(service.users, options, false) do
|
||||
tests('#save with existing user').succeeds do
|
||||
@instance.save
|
||||
end
|
||||
end
|
||||
end
|
17
tests/rackspace/models/identity/users_tests.rb
Normal file
17
tests/rackspace/models/identity/users_tests.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
Shindo.tests('Fog::Rackspace::Identity | users', ['rackspace']) do
|
||||
|
||||
pending if Fog.mocking?
|
||||
|
||||
service = Fog::Rackspace::Identity.new
|
||||
username = "fog_user_#{Time.now.to_i.to_s}"
|
||||
options = {
|
||||
:username => username,
|
||||
:email => 'email@example.com',
|
||||
:enabled => true
|
||||
}
|
||||
collection_tests(service.users, options, false) do
|
||||
tests('#get_by_name').succeeds do
|
||||
service.users.get_by_name(username)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue