From f5f35d4bfda73833d58cc3156962e87182be2f51 Mon Sep 17 00:00:00 2001 From: dynarro Date: Wed, 26 Nov 2014 17:17:37 +0100 Subject: [PATCH] Gets 'pool' data if the resource exists Pool instances can be created by just passing the name. They both check at construction time whether the resource exist in the server or not. If the resource doesn't exist, it'll attempt to create one. Zaqarclient does not call anymore the 'pool_create' directly. It calls 'pool_get' first, which ensures the resource exists. if not, a 'pool' will be created. Change-Id: I8f18b5031c80cfd5744ba3b0b7058654934f44b4 Partial-Bug: #1396063 --- tests/unit/queues/v1/test_core.py | 10 ++++++++ zaqarclient/queues/v1/api.py | 9 +++++++ zaqarclient/queues/v1/core.py | 19 +++++++++++++++ zaqarclient/queues/v1/pool.py | 16 +++++++++---- zaqarclient/tests/queues/pool.py | 39 +++++++++++++++++++++++++++---- zaqarclient/transport/request.py | 1 - 6 files changed, 85 insertions(+), 9 deletions(-) diff --git a/tests/unit/queues/v1/test_core.py b/tests/unit/queues/v1/test_core.py index fff515ee..b7cfb852 100644 --- a/tests/unit/queues/v1/test_core.py +++ b/tests/unit/queues/v1/test_core.py @@ -221,6 +221,16 @@ class TestV1Core(base.TestBase): 'test_pool', {'uri': 'sqlite://', 'weight': 0}) + def test_pool_get(self): + with mock.patch.object(self.transport, 'send', + autospec=True) as send_method: + resp = response.Response(None, None) + send_method.return_value = resp + + req = request.Request() + core.pool_get(self.transport, req, + 'test_pool') + def test_pool_delete(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: diff --git a/zaqarclient/queues/v1/api.py b/zaqarclient/queues/v1/api.py index 47aff0f0..489a186a 100644 --- a/zaqarclient/queues/v1/api.py +++ b/zaqarclient/queues/v1/api.py @@ -163,6 +163,15 @@ class V1(api.Api): } }, + 'pool_get': { + 'ref': 'pools/{pool_name}', + 'method': 'GET', + 'required': ['pool_name'], + 'properties': { + 'pool_name': {'type': 'string'}, + } + }, + 'pool_delete': { 'ref': 'pools/{pool_name}', 'method': 'DELETE', diff --git a/zaqarclient/queues/v1/core.py b/zaqarclient/queues/v1/core.py index 05177965..2dba328b 100644 --- a/zaqarclient/queues/v1/core.py +++ b/zaqarclient/queues/v1/core.py @@ -418,6 +418,25 @@ def shard_delete(transport, request, pool_name): return pool_delete(transport, request, pool_name) +def pool_get(transport, request, pool_name, callback=None): + """Gets pool data + + :param transport: Transport instance to use + :type transport: `transport.base.Transport` + :param request: Request instance ready to be sent. + :type request: `transport.request.Request` + :param pool_name: Pool reference name. + :type pool_name: `six.text_type` + + """ + + request.operation = 'pool_get' + request.params['pool_name'] = pool_name + + resp = transport.send(request) + return resp.deserialized_content + + def pool_create(transport, request, pool_name, pool_data): """Creates a pool called `pool_name` diff --git a/zaqarclient/queues/v1/pool.py b/zaqarclient/queues/v1/pool.py index 9230fbdb..3d687362 100644 --- a/zaqarclient/queues/v1/pool.py +++ b/zaqarclient/queues/v1/pool.py @@ -14,6 +14,7 @@ # limitations under the License. from zaqarclient.queues.v1 import core +from zaqarclient.transport import errors class Pool(object): @@ -40,11 +41,18 @@ class Pool(object): """ req, trans = self.client._request_and_transport() - data = {'uri': self.uri, - 'weight': self.weight, - 'options': self.options} + try: + pool = core.pool_get(trans, req, self.name) + self.uri = pool["uri"] + self.weight = pool["weight"] + self.options = pool.get("options", {}) - core.pool_create(trans, req, self.name, data) + except errors.ResourceNotFound: + data = {'uri': self.uri, + 'weight': self.weight, + 'options': self.options} + + core.pool_create(trans, req, self.name, data) def delete(self): req, trans = self.client._request_and_transport() diff --git a/zaqarclient/tests/queues/pool.py b/zaqarclient/tests/queues/pool.py index 4e51baf0..263b45c6 100644 --- a/zaqarclient/tests/queues/pool.py +++ b/zaqarclient/tests/queues/pool.py @@ -13,9 +13,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json import mock from zaqarclient.tests.queues import base +from zaqarclient.transport import errors from zaqarclient.transport import response @@ -29,7 +31,7 @@ class QueuesV1PoolUnitTest(base.QueuesTestBase): autospec=True) as send_method: resp = response.Response(None, None) - send_method.return_value = resp + send_method.side_effect = iter([errors.ResourceNotFound, resp]) # NOTE(flaper87): This will call # ensure exists in the client instance @@ -38,16 +40,35 @@ class QueuesV1PoolUnitTest(base.QueuesTestBase): self.assertEqual(pool.name, 'test') self.assertEqual(pool.weight, 10) + def test_pool_get(self): + pool_data = {'weight': 10, + 'uri': 'sqlite://', + 'options': {}} + + with mock.patch.object(self.transport, 'send', + autospec=True) as send_method: + + resp = response.Response(None, json.dumps(pool_data)) + send_method.return_value = resp + + # NOTE(flaper87): This will call + # ensure exists in the client instance + # since auto_create's default is True + pool = self.client.pool('test') + self.assertEqual(pool.name, 'test') + self.assertEqual(pool.weight, 10) + def test_pool_delete(self): pool_data = {'weight': 10, - 'uri': 'sqlite://'} + 'uri': 'sqlite://', + 'options': {}} with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, None) - send_method.return_value = resp - + resp_data = response.Response(None, json.dumps(pool_data)) + send_method.side_effect = iter([resp_data, resp]) # NOTE(flaper87): This will call # ensure exists in the client instance # since auto_create's default is True @@ -61,6 +82,16 @@ class QueuesV1PoolUnitTest(base.QueuesTestBase): class QueuesV1PoolFunctionalTest(base.QueuesTestBase): + def test_pool_get(self): + pool_data = {'weight': 10, + 'uri': 'sqlite://'} + + self.client.pool('test', **pool_data) + pool = self.client.pool('test') + self.assertEqual(pool.name, 'test') + self.assertEqual(pool.weight, 10) + self.assertEqual(pool.uri, 'sqlite://') + def test_pool_create(self): pool_data = {'weight': 10, 'uri': 'sqlite://'} diff --git a/zaqarclient/transport/request.py b/zaqarclient/transport/request.py index 5c4f786c..c19ec800 100644 --- a/zaqarclient/transport/request.py +++ b/zaqarclient/transport/request.py @@ -46,7 +46,6 @@ def prepare_request(auth_opts=None, data=None, **kwargs): # TODO(flaper87): Do something smarter # to get the api_version. req = auth_backend.authenticate(1, req) - req.headers['X-Project-Id'] = auth_opts.get('options', {}).get('os_project_id')