Enhances Roles List

This patch enhances the ListRolesAction by adding a detail argument
that toggles whether the names or all role data will be returned.

Change-Id: Ib1755bcf5c1361a3e5616014bf063f69e49687c6
Implements: blueprint tripleo-common-enhance-list-roles-action
This commit is contained in:
Ryan Brady 2017-09-29 09:58:39 -04:00 committed by Dougal Matthews
parent c82bf420dc
commit 9bd54ab664
2 changed files with 36 additions and 4 deletions

View File

@ -185,12 +185,16 @@ class ListRolesAction(base.TripleOAction):
Parses roles_data.yaml and returns the names of all available roles.
:param container: name of the Swift container / plan name
:param detail: if false(default), displays role names only. if true,
returns all roles data
:return: list of roles in the container's deployment plan
"""
def __init__(self, container=constants.DEFAULT_CONTAINER_NAME):
def __init__(self, container=constants.DEFAULT_CONTAINER_NAME,
detail=False):
super(ListRolesAction, self).__init__()
self.container = container
self.detail = detail
def run(self, context):
try:
@ -203,7 +207,10 @@ class ListRolesAction(base.TripleOAction):
LOG.exception(err_msg)
return actions.Result(error=err_msg)
return [role['name'] for role in roles_data]
if self.detail:
return roles_data
else:
return [role['name'] for role in roles_data]
class ExportPlanAction(base.TripleOAction):

View File

@ -235,10 +235,10 @@ class DeletePlanActionTest(base.TestCase):
swift.delete_container.assert_called_with(self.container_name)
class RoleListActionTest(base.TestCase):
class ListRolesActionTest(base.TestCase):
def setUp(self):
super(RoleListActionTest, self).setUp()
super(ListRolesActionTest, self).setUp()
self.container = 'overcloud'
self.ctx = mock.MagicMock()
@ -257,6 +257,31 @@ class RoleListActionTest(base.TestCase):
expected = ['MyController', 'Compute', 'CustomRole']
self.assertEqual(expected, result)
@mock.patch('tripleo_common.actions.base.TripleOAction.get_object_client')
def test_run_show_detail(self, get_obj_client_mock):
# setup swift
swift = mock.MagicMock()
swift.get_object.return_value = ({}, ROLES_DATA_YAML_CONTENTS)
get_obj_client_mock.return_value = swift
# Test
action = plan.ListRolesAction(detail=True)
result = action.run(self.ctx)
# verify
expected = [
{u'CountDefault': 1,
u'ServicesDefault': [u'OS::TripleO::Services::CACerts'],
u'name': u'MyController'},
{u'HostnameFormatDefault': u'%stackname%-novacompute-%index%',
u'ServicesDefault': [u'OS::TripleO::Services::NovaCompute',
u'OS::TripleO::Services::DummyService'],
u'name': u'Compute'},
{u'ServicesDefault': [u'OS::TripleO::Services::Kernel'],
u'name': u'CustomRole'}]
self.assertEqual(expected, result)
@mock.patch('tripleo_common.actions.base.TripleOAction.get_object_client')
def test_no_roles_data_file(self, get_obj_client_mock):