[openstack|identity] Update Tenants (Complete CRUD)

This commit is contained in:
Nelvin Driz 2012-02-26 16:09:22 +08:00
parent 3f08cbf399
commit 42d2cb94ec
12 changed files with 215 additions and 28 deletions

View File

@ -74,7 +74,7 @@ module Fog
connection = Fog::Connection.new(uri.to_s, false, connection_options)
@openstack_api_key = options[:openstack_api_key]
@openstack_username = options[:openstack_username]
@openstack_tenant = options[:openstack_tenant]
@openstack_tenant = options[:openstack_tenant] || 'admin'
@compute_service_name = options[:openstack_compute_service_name]
@endpoint_type = options[:openstack_endpoint_type] || 'publicURL'

View File

@ -23,9 +23,12 @@ module Fog
request :validate_token
request :list_tenants
request :create_tenant
request :get_tenant
request :get_tenants_by_id
request :get_tenants_by_name
request :update_tenant
request :delete_tenant
request :list_users
request :get_user_by_id
@ -112,7 +115,7 @@ module Fog
rescue Excon::Errors::HTTPStatusError => error
raise case error
when Excon::Errors::NotFound
Fog::Compute::OpenStack::NotFound.slurp(error)
Fog::Identity::OpenStack::NotFound.slurp(error)
else
error
end

View File

@ -19,6 +19,30 @@ module Fog
:tenant => self,
:user => user)
end
def destroy
requires :id
connection.delete_tenant(self.id)
true
end
def update(attr = nil)
requires :id
merge_attributes(
connection.update_tenant(self.id, attr || attributes).body['tenant'])
self
end
def save
requires :name
identity ? update : create
end
def create
merge_attributes(
connection.create_tenant(attributes).body['tenant'])
self
end
end # class Tenant
end # class OpenStack
end # module Identity

View File

@ -10,6 +10,17 @@ module Fog
def all
load(connection.list_tenants.body['tenants'])
end
def find_by_id(id)
self.find {|tenant| tenant.id == id} ||
Fog::Identity::OpenStack::Tenant.new(
connection.get_tenant(id).body['tenant'])
end
def destroy(id)
tenant = self.find_by_id(id)
tenant.destroy
end
end # class Tenants
end # class OpenStack
end # module Compute

View File

@ -0,0 +1,34 @@
module Fog
module Identity
class OpenStack
class Real
def create_tenant(attributes)
request(
:expects => [200],
:method => 'POST',
:path => "tenants",
:body => {
'tenant' => attributes
}.to_json
)
end # def create_tenant
end # class Real
class Mock
def create_tenant(attributes)
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 create_tenant
end # class Mock
end # class OpenStack
end # module Identity
end # module Fog

View File

@ -0,0 +1,31 @@
module Fog
module Identity
class OpenStack
class Real
def delete_tenant(id)
request(
:expects => [200],
:method => 'DELETE',
:path => "tenants/#{id}"
)
end # def create_tenant
end # class Real
class Mock
def delete_tenant(attributes)
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 create_tenant
end # class Mock
end # class OpenStack
end # module Identity
end # module Fog

View File

@ -2,23 +2,16 @@ 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?
def list_tenants(limit = nil, marker = nil)
params = Hash.new
params['limit'] = limit if limit
params['marker'] = marker if marker
request(
:expects => [200, 204],
:method => 'GET',
:path => path
:path => "tenants",
:query => params
)
end
end # class Real

View File

@ -0,0 +1,30 @@
module Fog
module Identity
class OpenStack
class Real
def update_tenant(id, attributes)
request(
:expects => [200],
:method => 'PUT',
:path => "tenants/#{id}",
:body => {
'tenant' => attributes
}.to_json
)
end # def create_tenant
end # class Real
class Mock
def update_tenant(id, attributes)
response = Excon::Response.new
response.status = [200, 204][rand(1)]
attributes = {'enabled' => true, 'id' => '1'}.merge(attributes)
response.body = {
'tenant' => attributes
}
response
end # def create_tenant
end # class Mock
end # class OpenStack
end # module Identity
end # module Fog

View File

@ -3,10 +3,9 @@ module Shindo
def succeeds
test('succeeds') do
instance_eval(&Proc.new)
true
!!instance_eval(&Proc.new)
end
end
end
end
end

View File

@ -1,9 +1,24 @@
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)
instance = Fog::Identity[:openstack].tenants.first
instance.roles_for(0)
end
end
tests('CRUD') do
tests('#create').succeeds do
@instance = Fog::Identity[:openstack].tenants.create(:name => 'test')
!@instance.id.nil?
end
tests('#update').succeeds do
@instance.update(:name => 'test2')
@instance.name == 'test2'
end
tests('#destroy').succeeds do
@instance.destroy == true
end
end
end

View File

@ -0,0 +1,26 @@
Shindo.tests("Fog::Compute[:openstack] | tenants", ['openstack']) do
@instance = Fog::Identity[:openstack].tenants.create(:name => 'test')
tests('success') do
tests('#find_by_id').succeeds do
tenant = Fog::Identity[:openstack].tenants.find_by_id(@instance.id)
tenant.id == @instance.id
end
tests('#destroy').succeeds do
Fog::Identity[:openstack].tenants.destroy(@instance.id)
end
end
tests('fails') do
pending if Fog.mocking?
tests('#find_by_id').raises(Fog::Identity::OpenStack::NotFound) do
Fog::Identity[:openstack].tenants.find_by_id('fake')
end
tests('#destroy').raises(Fog::Identity::OpenStack::NotFound) do
Fog::Identity[:openstack].tenants.destroy('fake')
end
end
end

View File

@ -3,8 +3,9 @@ Shindo.tests('Fog::Identity[:openstack] | tenant requests', ['openstack']) do
@tenant_format = {
'id' => String,
'name' => String,
'enabled' => Fog::Boolean,
'description' => String
'enabled' => Fog::Nullable::Boolean,
'description' => Fog::Nullable::String,
'extra' => Fog::Nullable::Hash
}
@role_format = {
@ -13,20 +14,40 @@ Shindo.tests('Fog::Identity[:openstack] | tenant requests', ['openstack']) do
}
tests('success') do
tests('#list_tenants').formats({'tenants' => [@tenant_format]}) do
tests('#list_tenants').formats({'tenants' => [@tenant_format], 'tenants_links' => []}) 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
openstack = Fog::Identity[:openstack]
openstack.list_roles_for_user_on_tenant(
openstack.tenants.first, openstack.users.first).body
end
tests('#create_tenant').formats({'tenant' => @tenant_format}) do
@instance = Fog::Identity[:openstack].create_tenant('name' => 'test').body
end
tests('#get_tenant').formats({'tenant' => @tenant_format}) do
pending unless Fog.mocking?
Fog::Identity[:openstack].get_tenant(0).body
Fog::Identity[:openstack].get_tenant(@instance['tenant']['id']).body
end
tests('#update_tenant check format').formats({'tenant' => @tenant_format}) do
@instance = Fog::Identity[:openstack].update_tenant(
@instance['tenant']['id'], 'name' => 'test2').body
end
tests('#update_tenant update name').succeeds do
@instance = Fog::Identity[:openstack].update_tenant(
@instance['tenant']['id'], 'name' => 'test3').body
@instance['tenant']['name'] == 'test3'
end
tests('#delete_tenant').succeeds do
Fog::Identity[:openstack].delete_tenant(@instance['tenant']['id'])
end
end
end