Merge "Implemented the host subcommands masakari CLI"

This commit is contained in:
Jenkins 2017-02-14 01:35:59 +00:00 committed by Gerrit Code Review
commit da67205b52
3 changed files with 147 additions and 4 deletions

View File

@ -67,12 +67,12 @@ class Host(resource2.Resource):
"sort_key", "sort_dir", failover_segment_id="failover_segment_id",
type="type", on_maintenance="on_maintenance", reserved="reserved")
def update(self, session, prepend_key=False, has_body=False):
def update(self, session, prepend_key=False, has_body=True):
"""Update a host."""
request = self._prepare_request(prepend_key=prepend_key)
del request.body['id']
request_body = {"host": request.body}
session.put(request.uri, endpoint_filter=self.service,
json=request_body, headers=request.headers)
res = session.put(request.uri, endpoint_filter=self.service,
json=request_body, headers=request.headers)
self._translate_response(res, has_body=has_body)
return self

View File

@ -48,6 +48,31 @@ class TestV1Shell(base.TestCase):
'id': '14',
'description': 'UPDATE Discription'}
self.hosts_vals = {
'reserved': False,
'uuid': '0951e72c-49f5-46aa-8465-2d61ed3b46d9',
'deleted': False,
'on_maintenance': False,
'created_at': '2016-11-29T11:10:51.000000',
'control_attributes': 'control-attributesX',
'updated_at': '2016-11-29T11:30:18.000000',
'name': 'new_host-3',
'failover_segment': {
'uuid': '6b985a8a-f8c0-42e4-beaa-d2fcd8dabbb6',
'deleted': False,
'created_at': '2016-11-16T04:46:38.000000',
'description': None,
'recovery_method': 'auto',
'updated_at': None,
'service_type': 'testsegment01_auto',
'deleted_at': None,
'id': 3,
'name': 'testsegment01'},
'deleted_at': None,
'type': 'typeX',
'id': 10,
'failover_segment_id': '6b985a8a-f8c0-42e4-beaa-d2fcd8dabbb6'}
@mock.patch.object(utils, 'print_list')
def test_do_notification_list(self, mock_print_list):
service = mock.Mock()
@ -83,3 +108,22 @@ class TestV1Shell(base.TestCase):
mock_print_list.assert_called_once_with(
self.segment_vals,
columns)
@mock.patch.object(utils, 'print_list')
def test_do_host_list(self, mock_print_list):
service = mock.Mock()
service.hosts.return_value = self.hosts_vals
args = mock.Mock()
columns = [
'control_attributes',
'failover_segment_id',
'name',
'on_maintenance',
'type',
'uuid']
ms.do_host_list(service, args)
mock_print_list.assert_called_once_with(
self.hosts_vals,
columns)

View File

@ -153,3 +153,102 @@ def do_segment_delete(service, args):
utils.print_dict(segment.to_dict())
except Exception as e:
print(e)
@utils.arg('--segment-id', metavar='<SEGMENT_ID>', required=True,
help='Segment to display (name or ID)')
def do_host_list(service, args):
"""List hosts."""
try:
hosts = service.hosts(args.segment_id)
fields = [
'control_attributes', 'failover_segment_id', 'name',
'on_maintenance', 'type', 'uuid']
utils.print_list(hosts, fields)
except Exception as e:
print(e)
@utils.arg('--segment-id', metavar='<SEGMENT_ID>', required=True,
help='Segment to display (name or ID)')
@utils.arg('--id', metavar='<HOST_ID>', required=True,
help='Host to display (name or ID)')
def do_host_show(service, args):
"""Show a host details."""
try:
host = service.get_host(args.segment_id, args.id)
utils.print_dict(host.to_dict())
except Exception as e:
print(e)
@utils.arg('--segment-id', metavar='<SEGMENT_ID>', required=True,
help='Name or ID of segment.')
@utils.arg('--name', metavar='<HOST_NAME>', required=True,
help='Name of host.')
@utils.arg('--type', metavar='<TYPE>', required=True,
help='Type of host.')
@utils.arg('--control-attributes', metavar='<CONTROL_ATTRIBUTES>',
required=True, help='Control attributes of host.')
@utils.arg('--reserved', metavar='<RESERVED>', required=False,
help='')
@utils.arg('--on-maintenance', metavar='<ON_MAINTENANCE>', required=False,
help='')
def do_host_create(service, args):
"""Create a host."""
try:
attrs = {
'name': args.name,
'type': args.type,
'control_attributes': args.control_attributes,
'reserved': args.reserved,
'on_maintenance': args.on_maintenance,
}
host = service.create_host(args.segment_id, **attrs)
utils.print_dict(host.to_dict())
except Exception as e:
print(e)
@utils.arg('--segment-id', metavar='<SEGMENT_ID>', required=True,
help='Name or ID of segment.')
@utils.arg('--id', metavar='<HOST_ID>', required=True,
help='Name or ID of host.')
@utils.arg('--name', metavar='<HOST_NAME>', required=False,
help='Name of host.')
@utils.arg('--type', metavar='<TYPE>', required=False,
help='Type of host.')
@utils.arg('--control-attributes', metavar='<CONTROL_ATTRIBUTES>',
required=False, help='Control attributes of host.')
@utils.arg('--reserved', metavar='<RESERVED>', required=False, help='')
@utils.arg('--on-maintenance', metavar='<ON_MAINTENANCE>',
required=False, help='')
def do_host_update(service, args):
"""Update a host."""
try:
attrs = {
'name': args.name,
'type': args.type,
'control_attributes': args.control_attributes,
'reserved': args.reserved,
'on_maintenance': args.on_maintenance,
}
attrs = utils.remove_unspecified_items(attrs)
host = service.update_host(args.segment_id, args.id, **attrs)
utils.print_dict(host.to_dict())
except Exception as e:
print(e)
@utils.arg('--segment-id', metavar='<SEGMENT_ID>', required=True,
help='Segment ID of the host to delete.')
@utils.arg('--id', metavar='<HOST_ID>', required=True,
help='Name or ID of the host to delete.')
def do_host_delete(service, args):
"""Delete a host."""
try:
host = service.delete_host(args.segment_id, args.id)
if host:
utils.print_dict(host.to_dict())
except Exception as e:
print(e)