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
This commit is contained in:
dynarro
2014-11-26 17:17:37 +01:00
parent ee44ce6bff
commit f5f35d4bfd
6 changed files with 85 additions and 9 deletions

View File

@@ -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:

View File

@@ -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',

View File

@@ -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`

View File

@@ -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()

View File

@@ -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://'}

View File

@@ -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')