[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) connection = Fog::Connection.new(uri.to_s, false, connection_options)
@openstack_api_key = options[:openstack_api_key] @openstack_api_key = options[:openstack_api_key]
@openstack_username = options[:openstack_username] @openstack_username = options[:openstack_username]
@openstack_tenant = options[:openstack_tenant] @openstack_tenant = options[:openstack_tenant] || 'admin'
@compute_service_name = options[:openstack_compute_service_name] @compute_service_name = options[:openstack_compute_service_name]
@endpoint_type = options[:openstack_endpoint_type] || 'publicURL' @endpoint_type = options[:openstack_endpoint_type] || 'publicURL'

View File

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

View File

@ -19,6 +19,30 @@ module Fog
:tenant => self, :tenant => self,
:user => user) :user => user)
end 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 Tenant
end # class OpenStack end # class OpenStack
end # module Identity end # module Identity

View File

@ -10,6 +10,17 @@ module Fog
def all def all
load(connection.list_tenants.body['tenants']) load(connection.list_tenants.body['tenants'])
end 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 Tenants
end # class OpenStack end # class OpenStack
end # module Compute 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 module Identity
class OpenStack class OpenStack
class Real class Real
def list_tenants(options = {}) def list_tenants(limit = nil, marker = nil)
path = 'tenants' params = Hash.new
params['limit'] = limit if limit
params = options.map do |key, value| params['marker'] = marker if marker
next unless [
'limit', 'marker', 'name', 'id'
].include?(key.to_s)
"#{key}=#{value}"
end.compact.join('&')
path.concat("?#{params}") unless params.empty?
request( request(
:expects => [200, 204], :expects => [200, 204],
:method => 'GET', :method => 'GET',
:path => path :path => "tenants",
:query => params
) )
end end
end # class Real 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 def succeeds
test('succeeds') do test('succeeds') do
instance_eval(&Proc.new) !!instance_eval(&Proc.new)
true
end end
end end
end end
end end

View File

@ -1,9 +1,24 @@
Shindo.tests("Fog::Compute[:openstack] | tenant", ['openstack']) do Shindo.tests("Fog::Compute[:openstack] | tenant", ['openstack']) do
@instance = Fog::Identity[:openstack].tenants.first
tests('success') do tests('success') do
tests('#roles_for(0)').succeeds 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 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 = { @tenant_format = {
'id' => String, 'id' => String,
'name' => String, 'name' => String,
'enabled' => Fog::Boolean, 'enabled' => Fog::Nullable::Boolean,
'description' => String 'description' => Fog::Nullable::String,
'extra' => Fog::Nullable::Hash
} }
@role_format = { @role_format = {
@ -13,20 +14,40 @@ Shindo.tests('Fog::Identity[:openstack] | tenant requests', ['openstack']) do
} }
tests('success') 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 Fog::Identity[:openstack].list_tenants.body
end end
tests('#list_roles_for_user_on_tenant(0,1)'). tests('#list_roles_for_user_on_tenant(0,1)').
formats({'roles' => [@role_format]}) do formats({'roles' => [@role_format]}) do
pending unless Fog.mocking? openstack = Fog::Identity[:openstack]
Fog::Identity[:openstack].list_roles_for_user_on_tenant(0,1).body 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 end
tests('#get_tenant').formats({'tenant' => @tenant_format}) do tests('#get_tenant').formats({'tenant' => @tenant_format}) do
pending unless Fog.mocking? Fog::Identity[:openstack].get_tenant(@instance['tenant']['id']).body
Fog::Identity[:openstack].get_tenant(0).body
end 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
end end