Merge "Gets 'flavor' data if the resource exists"
This commit is contained in:
commit
c9edb9792d
@ -190,6 +190,16 @@ class V1(api.Api):
|
||||
}
|
||||
},
|
||||
|
||||
'flavor_get': {
|
||||
'ref': 'flavors/{flavor_name}',
|
||||
'method': 'GET',
|
||||
'required': ['flavor_name'],
|
||||
'properties': {
|
||||
'flavor_name': {'type': 'string'},
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
'flavor_delete': {
|
||||
'ref': 'flavors/{flavor_name}',
|
||||
'method': 'DELETE',
|
||||
|
@ -491,6 +491,25 @@ def flavor_create(transport, request, name, flavor_data):
|
||||
transport.send(request)
|
||||
|
||||
|
||||
def flavor_get(transport, request, flavor_name, callback=None):
|
||||
"""Gets flavor 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 flavor_name: Flavor reference name.
|
||||
:type flavor_name: `six.text_type`
|
||||
|
||||
"""
|
||||
|
||||
request.operation = 'flavor_get'
|
||||
request.params['flavor_name'] = flavor_name
|
||||
|
||||
resp = transport.send(request)
|
||||
return resp.deserialized_content
|
||||
|
||||
|
||||
def flavor_delete(transport, request, name):
|
||||
"""Deletes the flavor `name`
|
||||
|
||||
|
@ -14,12 +14,14 @@
|
||||
# limitations under the License.
|
||||
|
||||
from zaqarclient.queues.v1 import core
|
||||
from zaqarclient.transport import errors
|
||||
|
||||
|
||||
class Flavor(object):
|
||||
|
||||
def __init__(self, client, name,
|
||||
pool, auto_create=True, **capabilities):
|
||||
pool=None, auto_create=True,
|
||||
**capabilities):
|
||||
self.client = client
|
||||
|
||||
self.name = name
|
||||
@ -38,10 +40,16 @@ class Flavor(object):
|
||||
"""
|
||||
req, trans = self.client._request_and_transport()
|
||||
|
||||
data = {'pool': self.pool,
|
||||
'capabilities': self.capabilities}
|
||||
try:
|
||||
flavor = core.flavor_get(trans, req, self.name)
|
||||
self.pool = flavor["pool"]
|
||||
self.capabilities = flavor.get("capabilities", {})
|
||||
|
||||
core.flavor_create(trans, req, self.name, data)
|
||||
except errors.ResourceNotFound:
|
||||
data = {'pool': self.pool,
|
||||
'capabilities': self.capabilities}
|
||||
|
||||
core.flavor_create(trans, req, self.name, data)
|
||||
|
||||
def delete(self):
|
||||
req, trans = self.client._request_and_transport()
|
||||
|
@ -21,12 +21,14 @@ class Pool(object):
|
||||
|
||||
def __init__(self, client, name,
|
||||
weight=None, uri=None,
|
||||
group=None,
|
||||
auto_create=True, **options):
|
||||
self.client = client
|
||||
|
||||
self.uri = uri
|
||||
self.name = name
|
||||
self.weight = weight
|
||||
self.group = group
|
||||
self.options = options
|
||||
|
||||
if auto_create:
|
||||
@ -45,6 +47,7 @@ class Pool(object):
|
||||
pool = core.pool_get(trans, req, self.name)
|
||||
self.uri = pool["uri"]
|
||||
self.weight = pool["weight"]
|
||||
self.group = pool.get("group", None)
|
||||
self.options = pool.get("options", {})
|
||||
|
||||
except errors.ResourceNotFound:
|
||||
@ -52,6 +55,9 @@ class Pool(object):
|
||||
'weight': self.weight,
|
||||
'options': self.options}
|
||||
|
||||
if self.client.api_version >= 1.1:
|
||||
data['group'] = self.group
|
||||
|
||||
core.pool_create(trans, req, self.name, data)
|
||||
|
||||
def delete(self):
|
||||
|
@ -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
|
||||
|
||||
|
||||
@ -31,7 +33,7 @@ class QueuesV1_1FlavorUnitTest(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
|
||||
@ -40,6 +42,22 @@ class QueuesV1_1FlavorUnitTest(base.QueuesTestBase):
|
||||
self.assertEqual(flavor.name, 'tasty')
|
||||
self.assertEqual(flavor.pool, 'stomach')
|
||||
|
||||
def test_flavor_get(self):
|
||||
flavor_data = {'pool': 'stomach'}
|
||||
|
||||
with mock.patch.object(self.transport, 'send',
|
||||
autospec=True) as send_method:
|
||||
|
||||
resp = response.Response(None, json.dumps(flavor_data))
|
||||
send_method.return_value = resp
|
||||
|
||||
# NOTE(flaper87): This will call
|
||||
# ensure exists in the client instance
|
||||
# since auto_create's default is True
|
||||
flavor = self.client.flavor('test')
|
||||
self.assertEqual(flavor.name, 'test')
|
||||
self.assertEqual(flavor.pool, 'stomach')
|
||||
|
||||
def test_flavor_delete(self):
|
||||
flavor_data = {'pool': 'stomach'}
|
||||
|
||||
@ -47,8 +65,8 @@ class QueuesV1_1FlavorUnitTest(base.QueuesTestBase):
|
||||
autospec=True) as send_method:
|
||||
|
||||
resp = response.Response(None, None)
|
||||
send_method.return_value = resp
|
||||
|
||||
resp_data = response.Response(None, json.dumps(flavor_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
|
||||
@ -67,19 +85,33 @@ class QueuesV1_1FlavorFunctionalTest(base.QueuesTestBase):
|
||||
|
||||
def test_flavor_create(self):
|
||||
pool_data = {'uri': 'sqlite://',
|
||||
'weight': 10}
|
||||
'weight': 10,
|
||||
'group': 'us'}
|
||||
self.client.pool('stomach', **pool_data)
|
||||
|
||||
flavor_data = {'pool': 'stomach'}
|
||||
flavor_data = {'pool': 'us'}
|
||||
flavor = self.client.flavor('tasty', **flavor_data)
|
||||
self.assertEqual(flavor.name, 'tasty')
|
||||
self.assertEqual(flavor.pool, 'stomach')
|
||||
self.assertEqual(flavor.pool, 'us')
|
||||
|
||||
def test_flavor_get(self):
|
||||
pool_data = {'weight': 10,
|
||||
'group': 'us',
|
||||
'uri': 'sqlite://'}
|
||||
self.client.pool('stomach', **pool_data)
|
||||
|
||||
flavor_data = {'pool': 'us'}
|
||||
self.client.flavor('tasty', **flavor_data)
|
||||
flavor = self.client.flavor('tasty')
|
||||
self.assertEqual(flavor.name, 'tasty')
|
||||
self.assertEqual(flavor.pool, 'us')
|
||||
|
||||
def test_flavor_delete(self):
|
||||
pool_data = {'uri': 'sqlite://',
|
||||
'weight': 10}
|
||||
'weight': 10,
|
||||
'group': 'us'}
|
||||
self.client.pool('stomach', **pool_data)
|
||||
|
||||
flavor_data = {'pool': 'stomach'}
|
||||
flavor_data = {'pool': 'us'}
|
||||
flavor = self.client.flavor('tasty', **flavor_data)
|
||||
flavor.delete()
|
||||
|
Loading…
x
Reference in New Issue
Block a user