1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[openstack|identity] Update Tenants

This commit is contained in:
Nelvin Driz 2012-02-22 22:40:32 +08:00
parent 3e43a571e0
commit e1c5155a34
19 changed files with 358 additions and 45 deletions

View file

@ -5,6 +5,8 @@ class OpenStack < Fog::Bin
case key
when :compute
Fog::Compute::OpenStack
when :identity
Fog::Identity::OpenStack
else
raise ArgumentError, "Unrecognized service: #{key}"
end
@ -14,7 +16,10 @@ class OpenStack < Fog::Bin
@@connections ||= Hash.new do |hash, key|
hash[key] = case key
when :compute
Fog::Logger.warning("OpenStack[:compute] is not recommended, use Compute[:rackspace] for portability")
Fog::Logger.warning("OpenStack[:compute] is not recommended, use Compute[:openstack] for portability")
Fog::Compute.new(:provider => 'OpenStack')
when :identity
Fog::Logger.warning("OpenStack[:identity] is not recommended, use Compute[:openstack] for portability")
Fog::Compute.new(:provider => 'OpenStack')
else
raise ArgumentError, "Unrecognized service: #{key.inspect}"

View file

@ -76,6 +76,7 @@ module Fog
@openstack_username = options[:openstack_username]
@openstack_tenant = options[:openstack_tenant]
@compute_service_name = options[:openstack_compute_service_name]
@endpoint_type = options[:openstack_endpoint_type] || 'publicURL'
req_body= {
'auth' => {
@ -111,7 +112,7 @@ module Fog
detect{|x| @compute_service_name.include?(x['type']) }
end
mgmt_url = svc['endpoints'].detect{|x| x['publicURL']}['publicURL']
mgmt_url = svc['endpoints'].detect{|x| x[@endpoint_type]}[@endpoint_type]
token = body['access']['token']['id']
{ :token => token,

View file

@ -10,20 +10,24 @@ module Fog
:openstack_compute_service_name, :openstack_tenant
model_path 'fog/openstack/models/identity'
# model :tenant
# collection :tenants
# model :user
# collection :users
model :tenant
collection :tenants
model :user
collection :users
model :role
collection :roles
request_path 'fog/openstack/requests/identity'
request :check_token
request :validate_token
request :get_tenants
request :list_tenants
request :get_tenant
request :get_tenants_by_id
request :get_tenants_by_name
request :list_users
request :get_user_by_id
request :get_user_by_name
@ -32,9 +36,31 @@ module Fog
request :list_user_global_roles
class Mock
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {
:users => {},
:tenants => {}
}
end
end
def self.reset
@data = nil
end
def initialize(options={})
require 'multi_json'
@openstack_username = options[:openstack_username]
end
def data
self.class.data[@openstack_username]
end
def reset_data
self.class.data.delete(@openstack_username)
end
end
class Real
@ -71,8 +97,9 @@ module Fog
'X-Auth-Token' => @auth_token
}.merge!(params[:headers] || {}),
:host => @host,
:path => "#{@path}/#{params[:path]}",
:query => ('ignore_awful_caching' << Time.now.to_i.to_s)
:path => "#{@path}/#{params[:path]}"#,
# Causes errors for some requests like tenants?limit=1
# :query => ('ignore_awful_caching' << Time.now.to_i.to_s)
}))
rescue Excon::Errors::Unauthorized => error
if error.response.body != 'Bad username or password' # token expiration
@ -105,7 +132,8 @@ module Fog
:openstack_username => @openstack_username,
:openstack_auth_uri => @openstack_auth_uri,
:openstack_tenant => @openstack_tenant,
:openstack_compute_service_name => @openstack_compute_service_name
:openstack_compute_service_name => @openstack_compute_service_name,
:openstack_endpoint_type => 'adminURL'
}
credentials = Fog::OpenStack.authenticate_v2(options, @connection_options)

View file

@ -28,7 +28,6 @@ module Fog
true
end
def create_security_group_rule(min, max, ip_protocol = "tcp", cidr = "0.0.0.0/0", group_id = nil)
requires :id
connection.create_security_group_rule(id, ip_protocol, min, max, cidr, group_id)

View file

@ -0,0 +1,12 @@
require 'fog/core/model'
module Fog
module Identity
class OpenStack
class Role < Fog::Model
identity :id
attribute :name
end # class Role
end # class OpenStack
end # module Identity
end # module Fog

View file

@ -0,0 +1,21 @@
require 'fog/core/collection'
require 'fog/openstack/models/identity/role'
module Fog
module Identity
class OpenStack
class Roles < Fog::Collection
model Fog::Identity::OpenStack::Role
attribute :user
attribute :tenant
def all
requires :user, :tenant
load(connection.
list_roles_for_user_on_tenant(tenant.id, user.id).body['roles'])
end
end # class Tenants
end # class OpenStack
end # module Compute
end # module Fog

View file

@ -0,0 +1,25 @@
require 'fog/core/model'
module Fog
module Identity
class OpenStack
class Tenant < Fog::Model
identity :id
attribute :description
attribute :enabled
attribute :name
def to_s
self.name
end
def roles_for(user)
connection.roles(
:tenant => self,
:user => user)
end
end # class Tenant
end # class OpenStack
end # module Identity
end # module Fog

View file

@ -0,0 +1,16 @@
require 'fog/core/collection'
require 'fog/openstack/models/identity/tenant'
module Fog
module Identity
class OpenStack
class Tenants < Fog::Collection
model Fog::Identity::OpenStack::Tenant
def all
load(connection.list_tenants.body['tenants'])
end
end # class Tenants
end # class OpenStack
end # module Compute
end # module Fog

View file

@ -0,0 +1,27 @@
require 'fog/core/model'
module Fog
module Identity
class OpenStack
class User < Fog::Model
identity :id
attribute :email
attribute :enabled
attribute :name
attribute :tenantId
def roles
return Array.new unless tenantId
tenant = Fog::Identity::OpenStack::Tenant.
new(connection.get_tenant(tenantId).body['tenant'])
connection.roles(
:tenant => tenant,
:user => self)
end
end # class Tenant
end # class OpenStack
end # module Identity
end # module Fog

View file

@ -0,0 +1,16 @@
require 'fog/core/collection'
require 'fog/openstack/models/identity/user'
module Fog
module Identity
class OpenStack
class Users < Fog::Collection
model Fog::Identity::OpenStack::User
def all
load(connection.list_users.body['users'])
end
end # class Tenants
end # class OpenStack
end # module Compute
end # module Fog

View file

@ -0,0 +1,31 @@
module Fog
module Identity
class OpenStack
class Real
def get_tenant(id)
request(
:expects => [200, 204],
:method => 'GET',
:path => "tenants/#{id}"
)
end
end # class Real
class Mock
def get_tenant(id)
response = Excon::Response.new
response.status = [200, 204][rand(1)]
response.body = {
'tenant' => {
'id' => '1',
'description' => 'Has access to everything',
'enabled' => true,
'name' => 'admin'
}
}
response
end # def list_tenants
end # class Mock
end # class OpenStack
end # module Identity
end # module Fog

View file

@ -1,23 +0,0 @@
module Fog
module Identity
class OpenStack
class Real
def get_tenants
request(
:expects => [200],
:method => 'GET',
:path => "tenants"
)
end
end
class Mock
end
end
end
end

View file

@ -2,22 +2,28 @@ module Fog
module Identity
class OpenStack
class Real
def list_roles_for_user_on_tenant(tenant_id, user_id)
request(
:expects => [200],
:method => 'GET',
:path => "tenants/#{tenant_id}/users/#{user_id}/roles"
)
end
end
end # def list_roles_for_user_on_tenant
end # class Real
class Mock
end
end
end
end
def list_roles_for_user_on_tenant(tenant_id, user_id)
response = Excon::Response.new
response.status = 200
response.body = {
'roles' => [
{'id' => '1',
'name' => 'admin'}
]
}
response
end # def list_roles_for_user_on_tenant
end # class Mock
end # class OpenStack
end # module Identity
end # module Fog

View file

@ -0,0 +1,51 @@
module Fog
module Identity
class OpenStack
class Real
def list_tenants(options = {})
path = 'tenants'
params = options.map do |key, value|
next unless [
'limit', 'marker', 'name', 'id'
].include?(key.to_s)
"#{key}=#{value}"
end.compact.join('&')
path.concat("?#{params}") unless params.empty?
request(
:expects => [200, 204],
:method => 'GET',
:path => path
)
end
end # class Real
class Mock
def list_tenants
response = Excon::Response.new
response.status = [200, 204][rand(1)]
response.body = {
'tenants' => [
{'id' => '1',
'description' => 'Has access to everything',
'enabled' => true,
'name' => 'admin'},
{'id' => '2',
'description' => 'Normal tenant',
'enabled' => true,
'name' => 'default'},
{'id' => '3',
'description' => 'Disabled tenant',
'enabled' => false,
'name' => 'disabled'}
]
}
response
end # def list_tenants
end # class Mock
end # class OpenStack
end # module Identity
end # module Fog

View file

@ -0,0 +1,32 @@
module Fog
module Identity
class OpenStack
class Real
def list_users
request(
:expects => [200, 204],
:method => 'GET',
:path => 'users'
)
end
end # class Real
class Mock
def list_users
response = Excon::Response.new
response.status = [200, 204][rand(1)]
response.body = {
'users' => [
{'id' => '1',
'enabled' => true,
'name' => 'admin',
'email' => 'admin@example.com',
'tenantId' => nil}
]
}
response
end # def list_tenants
end # class Mock
end # class OpenStack
end # module Identity
end # module Fog

View file

@ -0,0 +1,9 @@
Shindo.tests("Fog::Compute[:openstack] | tenant", ['openstack']) do
@instance = Fog::Identity[:openstack].tenants.first
tests('success') do
tests('#roles_for(0)').succeeds do
@instance.roles_for(0)
end
end
end

View file

@ -0,0 +1,9 @@
Shindo.tests("Fog::Compute[:openstack] | user", ['openstack']) do
@instance = Fog::Identity[:openstack].users.first
tests('success') do
tests('#roles').succeeds do
@instance.roles
end
end
end

View file

@ -0,0 +1,32 @@
Shindo.tests('Fog::Identity[:openstack] | tenant requests', ['openstack']) do
@tenant_format = {
'id' => String,
'name' => String,
'enabled' => Fog::Boolean,
'description' => String
}
@role_format = {
'id' => String,
'name' => String
}
tests('success') do
tests('#list_tenants').formats({'tenants' => [@tenant_format]}) do
Fog::Identity[:openstack].list_tenants.body
end
tests('#list_roles_for_user_on_tenant(0,1)').
formats({'roles' => [@role_format]}) do
pending unless Fog.mocking?
Fog::Identity[:openstack].list_roles_for_user_on_tenant(0,1).body
end
tests('#get_tenant').formats({'tenant' => @tenant_format}) do
pending unless Fog.mocking?
Fog::Identity[:openstack].get_tenant(0).body
end
end
end

View file

@ -0,0 +1,16 @@
Shindo.tests('Fog::Identity[:openstack] | user requests', ['openstack']) do
@user_format = {
'id' => String,
'name' => String,
'enabled' => Fog::Boolean,
'email' => String,
'tenantId' => Fog::Nullable::String
}
tests('success') do
tests('#list_users').formats({'users' => [@user_format]}) do
Fog::Identity[:openstack].list_users.body
end
end
end