Fix Service CLI to work with Object from Bay

Fix Service CLI to work with Object from Bay changes. Construct
the REST URL for each API since now with Object from Bay changes
its mandatory to pass a bay identifier. Also, in the list
API explicitly pass the bay_ident for which the query is
supposed to be made. The help is also updated so that user
can see provide positional arguments.

For example:

magnum coe-service-show <service> --bay <bay_identifier>

Change-Id: Iefc891dac4113d9b2fd76359f80010a61c194281
Partially-Implements: bp objects-from-bay
This commit is contained in:
Vilobh Meshram 2015-11-25 15:47:27 -08:00 committed by Hongbin Lu
parent d05129851f
commit dfe22bcf6b
4 changed files with 77 additions and 64 deletions

View File

@ -57,7 +57,19 @@ fake_responses = {
CREATE_SVC,
),
},
'/v1/services/%s/%s' % (SERVICE1['id'], SERVICE1['bay_uuid']):
'/v1/services/?bay_ident=%s' % (SERVICE1['bay_uuid']):
{
'GET': (
{},
{'services': [SERVICE1, SERVICE2]},
),
'POST': (
{},
CREATE_SVC,
),
},
'/v1/services/%s/?bay_ident=%s' % (SERVICE1['id'],
SERVICE1['bay_uuid']):
{
'GET': (
{},
@ -72,7 +84,8 @@ fake_responses = {
UPDATED_SVC,
),
},
'/v1/services/%s/%s' % (SERVICE1['name'], SERVICE1['bay_uuid']):
'/v1/services/%s/?bay_ident=%s' % (SERVICE1['name'],
SERVICE1['bay_uuid']):
{
'GET': (
{},
@ -100,7 +113,8 @@ class ServiceManagerTest(testtools.TestCase):
def test_coe_service_list(self):
services = self.mgr.list(SERVICE1['bay_uuid'])
expect = [
('GET', '/v1/services', {}, None),
('GET', '/v1/services/?bay_ident=%s' % (SERVICE1['bay_uuid']),
{}, None),
]
self.assertEqual(expect, self.api.calls)
self.assertThat(services, matchers.HasLength(2))
@ -108,8 +122,9 @@ class ServiceManagerTest(testtools.TestCase):
def test_coe_service_show_by_id(self):
service = self.mgr.get(SERVICE1['id'], SERVICE1['bay_uuid'])
expect = [
('GET', '/v1/services/%s/%s' % (SERVICE1['id'],
SERVICE1['bay_uuid']), {}, None)
('GET', '/v1/services/%s/?bay_ident=%s' % (SERVICE1['id'],
SERVICE1['bay_uuid']),
{}, None)
]
self.assertEqual(expect, self.api.calls)
self.assertEqual(SERVICE1['name'], service.name)
@ -119,8 +134,9 @@ class ServiceManagerTest(testtools.TestCase):
def test_coe_service_show_by_name(self):
service = self.mgr.get(SERVICE1['name'], SERVICE1['bay_uuid'])
expect = [
('GET', '/v1/services/%s/%s' % (SERVICE1['name'],
SERVICE1['bay_uuid']), {}, None)
('GET', '/v1/services/%s/?bay_ident=%s' % (SERVICE1['name'],
SERVICE1['bay_uuid']),
{}, None)
]
self.assertEqual(expect, self.api.calls)
self.assertEqual(SERVICE1['name'], service.name)
@ -147,10 +163,8 @@ class ServiceManagerTest(testtools.TestCase):
def test_coe_service_delete_by_id(self):
service = self.mgr.delete(SERVICE1['id'], SERVICE1['bay_uuid'])
expect = [
('DELETE', '/v1/services/%s/%s' % (SERVICE1['id'],
SERVICE1['bay_uuid']),
{},
None),
('DELETE', '/v1/services/%s/?bay_ident=%s' % (
SERVICE1['id'], SERVICE1['bay_uuid']), {}, None),
]
self.assertEqual(expect, self.api.calls)
self.assertIsNone(service)
@ -158,10 +172,8 @@ class ServiceManagerTest(testtools.TestCase):
def test_coe_service_delete_by_name(self):
service = self.mgr.delete(SERVICE1['name'], SERVICE1['bay_uuid'])
expect = [
('DELETE', '/v1/services/%s/%s' % (SERVICE1['name'],
SERVICE1['bay_uuid']),
{},
None),
('DELETE', '/v1/services/%s/?bay_ident=%s' % (
SERVICE1['name'], SERVICE1['bay_uuid']), {}, None),
]
self.assertEqual(expect, self.api.calls)
self.assertIsNone(service)
@ -171,13 +183,11 @@ class ServiceManagerTest(testtools.TestCase):
'value': NEW_SELECTOR,
'path': '/selector'}
service = self.mgr.update(service_id=SERVICE1['id'],
bay_uuid=SERVICE1['bay_uuid'],
bay_ident=SERVICE1['bay_uuid'],
patch=patch)
expect = [
('PATCH', '/v1/services/%s/%s' % (SERVICE1['id'],
SERVICE1['bay_uuid']),
{},
patch),
('PATCH', '/v1/services/%s/?bay_ident=%s' % (
SERVICE1['id'], SERVICE1['bay_uuid']), {}, patch),
]
self.assertEqual(expect, self.api.calls)
self.assertEqual(NEW_SELECTOR, service.selector)

View File

@ -21,12 +21,12 @@ class ShellTest(shell_test_base.TestCommandLineArgument):
@mock.patch('magnumclient.v1.services.ServiceManager.list')
def test_coe_service_list_success(self, mock_list):
self._test_arg_success('coe-service-list bay_ident')
self._test_arg_success('coe-service-list --bay bay_ident')
self.assertTrue(mock_list.called)
@mock.patch('magnumclient.v1.services.ServiceManager.list')
def test_coe_service_list_failure(self, mock_list):
self._test_arg_failure('coe-service-list bay_ident --wrong',
self._test_arg_failure('coe-service-list --bay bay_ident --wrong',
self._unrecognized_arg_error)
self.assertFalse(mock_list.called)
@ -52,38 +52,36 @@ class ShellTest(shell_test_base.TestCommandLineArgument):
@mock.patch('magnumclient.v1.services.ServiceManager.delete')
def test_coe_service_delete_success(self, mock_delete):
self._test_arg_success('coe-service-delete xxx zzz')
self._test_arg_success('coe-service-delete xxx --bay zzz')
self.assertTrue(mock_delete.called)
self.assertEqual(1, mock_delete.call_count)
@mock.patch('magnumclient.v1.services.ServiceManager.delete')
def test_coe_service_delete_multiple_id_success(self, mock_delete):
self._test_arg_success('coe-service-delete xxx xyz zzz')
self.assertTrue(mock_delete.called)
self.assertEqual(2, mock_delete.call_count)
@mock.patch('magnumclient.v1.services.ServiceManager.delete')
def test_coe_service_delete_failure_no_arg(self, mock_delete):
self._test_arg_failure('coe-service-delete', self._few_argument_error)
self._test_arg_failure('coe-service-delete xxx',
self._mandatory_arg_error)
self.assertFalse(mock_delete.called)
@mock.patch('magnumclient.v1.services.ServiceManager.get')
def test_coe_service_show_success(self, mock_show):
self._test_arg_success('coe-service-show xxx zzz')
self._test_arg_success('coe-service-show xxx --bay zzz')
self.assertTrue(mock_show.called)
self.assertEqual(1, mock_show.call_count)
@mock.patch('magnumclient.v1.services.ServiceManager.get')
def test_coe_service_show_failure_no_arg(self, mock_show):
self._test_arg_failure('coe-service-show', self._few_argument_error)
self._test_arg_failure('coe-service-show xxx',
self._mandatory_arg_error)
self.assertFalse(mock_show.called)
@mock.patch('magnumclient.v1.services.ServiceManager.update')
def test_coe_service_update_success(self, mock_update):
self._test_arg_success('coe-service-update xxx zzz replace xxx=xxx')
self._test_arg_success('coe-service-update xxx --bay zzz'
' replace xxx=xxx')
self.assertTrue(mock_update.called)
self.assertEqual(1, mock_update.call_count)
@mock.patch('magnumclient.v1.services.ServiceManager.update')
def test_coe_service_update_failure_no_arg(self, mock_update):
self._test_arg_failure('coe-service-update', self._few_argument_error)
self._test_arg_failure('coe-service-update xxx',
self._few_argument_error)
self.assertFalse(mock_update.called)

View File

@ -28,9 +28,11 @@ class ServiceManager(base.Manager):
resource_class = Service
@staticmethod
def _path(id=None, bay_uuid=None):
if id and bay_uuid:
return '/v1/services/%s/%s' % (id, bay_uuid)
def _path(id=None, bay_ident=None):
if id and bay_ident:
return '/v1/services/%s/?bay_ident=%s' % (id, bay_ident)
elif bay_ident:
return '/v1/services/?bay_ident=%s' % (bay_ident)
else:
return '/v1/services'
@ -56,8 +58,8 @@ class ServiceManager(base.Manager):
:param sort_dir: Optional, direction of sorting, either 'asc' (the
default) or 'desc'.
:param detail: Optional, boolean whether to return detailed information
about services.
:param detail: Optional, boolean whether to return detailed
information about services.
:returns: A list of services.
@ -75,14 +77,14 @@ class ServiceManager(base.Manager):
path += '?' + '&'.join(filters)
if limit is None:
return self._list(self._path(path), "services")
return self._list(self._path(bay_ident=bay_ident), "services")
else:
return self._list_pagination(self._path(path), "services",
limit=limit)
return self._list_pagination(self._path(bay_ident=bay_ident),
"services", limit=limit)
def get(self, service_id, bay_uuid):
def get(self, service_id, bay_ident):
try:
return self._list(self._path(service_id, bay_uuid))[0]
return self._list(self._path(service_id, bay_ident))[0]
except IndexError:
return None
@ -96,8 +98,8 @@ class ServiceManager(base.Manager):
"Key must be in %s" % ",".join(CREATION_ATTRIBUTES))
return self._create(self._path(), new)
def delete(self, service_id, bay_uuid):
return self._delete(self._path(service_id, bay_uuid))
def delete(self, service_id, bay_ident):
return self._delete(self._path(service_id, bay_ident))
def update(self, service_id, bay_uuid, patch):
return self._update(self._path(service_id, bay_uuid), patch)
def update(self, service_id, bay_ident, patch):
return self._update(self._path(service_id, bay_ident), patch)

View File

@ -22,7 +22,10 @@ def _show_coe_service(service):
utils.print_dict(service._info)
@utils.arg('bay', metavar='<bay>', help="UUID or Name of Bay")
@utils.arg('--bay',
required=True,
metavar='<bay>',
help="UUID or Name of Bay")
def do_coe_service_list(cs, args):
"""Print a list of coe services."""
services = cs.services.list(args.bay)
@ -33,7 +36,7 @@ def do_coe_service_list(cs, args):
@utils.arg('--manifest-url',
metavar='<manifest-url>',
help='Name/URL of the serivce file to use for creating services.')
help='Name/URL of the service file to use for creating services.')
@utils.arg('--manifest',
metavar='<manifest>',
help='File path of the service file to use for creating services.')
@ -62,8 +65,9 @@ def do_coe_service_create(cs, args):
_show_coe_service(service)
@utils.arg('service', metavar='<service>', help="UUID or name of service")
@utils.arg('bay', metavar='<bay>', help="UUID or Name of Bay")
@utils.arg('services', metavar='<services>', help="UUID or name of service")
@utils.arg('--bay', required=True,
metavar='<bay>', help="UUID or Name of Bay")
@utils.arg(
'op',
metavar='<op>',
@ -85,17 +89,16 @@ def do_coe_service_update(cs, args):
with open(p['value'], 'r') as f:
p['value'] = f.read()
service = cs.services.update(args.service, args.bay, patch)
service = cs.services.update(args.services, args.bay, patch)
_show_coe_service(service)
@utils.arg('services',
metavar='<services>',
nargs='+',
help='ID or name of the (service)s to delete.')
@utils.arg('bay', metavar='<bay>', help="UUID or Name of Bay")
@utils.arg('services', metavar='<services>', nargs='+',
help='ID or name of the service to delete.')
@utils.arg('--bay', required=True, metavar='<bay>',
help="UUID or Name of Bay")
def do_coe_service_delete(cs, args):
"""Delete specified coe service(s)."""
"""Delete specified coe service."""
for service in args.services:
try:
cs.services.delete(service, args.bay)
@ -104,11 +107,11 @@ def do_coe_service_delete(cs, args):
{'service': service, 'e': e})
@utils.arg('service',
metavar='<service>',
@utils.arg('services', metavar='<services>',
help='ID or name of the service to show.')
@utils.arg('bay', metavar='<bay>', help="UUID or Name of Bay")
@utils.arg('--bay', required=True,
metavar='<bay>', help="UUID or Name of Bay")
def do_coe_service_show(cs, args):
"""Show details about the given coe service."""
service = cs.services.get(args.service, args.bay)
service = cs.services.get(args.services, args.bay)
_show_coe_service(service)