diff --git a/doc/source/cli/command-objects/role.rst b/doc/source/cli/command-objects/role.rst index e2a2b457a0..752dc4e03c 100644 --- a/doc/source/cli/command-objects/role.rst +++ b/doc/source/cli/command-objects/role.rst @@ -115,6 +115,10 @@ Create new role New role name +.. option:: --description + + Add description about the role + role delete ----------- diff --git a/openstackclient/identity/v3/role.py b/openstackclient/identity/v3/role.py index 986f823fcf..36f3f9384b 100644 --- a/openstackclient/identity/v3/role.py +++ b/openstackclient/identity/v3/role.py @@ -176,6 +176,11 @@ class CreateRole(command.ShowOne): metavar='', help=_('New role name'), ) + parser.add_argument( + '--description', + metavar='', + help=_('Add description about the role'), + ) parser.add_argument( '--domain', metavar='', @@ -198,7 +203,8 @@ class CreateRole(command.ShowOne): try: role = identity_client.roles.create( - name=parsed_args.name, domain=domain_id) + name=parsed_args.name, domain=domain_id, + description=parsed_args.description) except ks_exc.Conflict: if parsed_args.or_show: @@ -345,6 +351,11 @@ class SetRole(command.Command): metavar='', help=_('Role to modify (name or ID)'), ) + parser.add_argument( + '--description', + metavar='', + help=_('Add description about the role'), + ) parser.add_argument( '--domain', metavar='', @@ -369,7 +380,8 @@ class SetRole(command.Command): parsed_args.role, domain_id=domain_id) - identity_client.roles.update(role.id, name=parsed_args.name) + identity_client.roles.update(role.id, name=parsed_args.name, + description=parsed_args.description) class ShowRole(command.ShowOne): diff --git a/openstackclient/tests/functional/identity/v3/common.py b/openstackclient/tests/functional/identity/v3/common.py index 86f090bcc0..a5edd9a549 100644 --- a/openstackclient/tests/functional/identity/v3/common.py +++ b/openstackclient/tests/functional/identity/v3/common.py @@ -33,7 +33,7 @@ class IdentityTests(base.TestCase): 'password_expires_at'] PROJECT_FIELDS = ['description', 'id', 'domain_id', 'is_domain', 'enabled', 'name', 'parent_id'] - ROLE_FIELDS = ['id', 'name', 'domain_id'] + ROLE_FIELDS = ['id', 'name', 'domain_id', 'description'] SERVICE_FIELDS = ['id', 'enabled', 'name', 'type', 'description'] REGION_FIELDS = ['description', 'enabled', 'parent_region', 'region'] ENDPOINT_FIELDS = ['id', 'region', 'region_id', 'service_id', diff --git a/openstackclient/tests/functional/identity/v3/test_role.py b/openstackclient/tests/functional/identity/v3/test_role.py index 38bfff7187..3954c4e301 100644 --- a/openstackclient/tests/functional/identity/v3/test_role.py +++ b/openstackclient/tests/functional/identity/v3/test_role.py @@ -20,6 +20,21 @@ class RoleTests(common.IdentityTests): def test_role_create(self): self._create_dummy_role() + def test_role_create_with_description(self): + role_name = data_utils.rand_name('TestRole') + description = data_utils.rand_name('description') + raw_output = self.openstack( + 'role create ' + '--description %(description)s ' + '%(name)s' % {'description': description, + 'name': role_name}) + role = self.parse_show_as_object(raw_output) + self.addCleanup(self.openstack, 'role delete %s' % role['id']) + items = self.parse_show(raw_output) + self.assert_show_fields(items, self.ROLE_FIELDS) + self.assertEqual(description, role['description']) + return role_name + def test_role_delete(self): role_name = self._create_dummy_role(add_clean_up=False) raw_output = self.openstack('role delete %s' % role_name) @@ -47,6 +62,16 @@ class RoleTests(common.IdentityTests): role = self.parse_show_as_object(raw_output) self.assertEqual(new_role_name, role['name']) + def test_role_set_description(self): + role_name = self._create_dummy_role() + description = data_utils.rand_name("NewDescription") + raw_output = self.openstack('role set --description %s %s' + % (description, role_name)) + self.assertEqual(0, len(raw_output)) + raw_output = self.openstack('role show %s' % role_name) + role = self.parse_show_as_object(raw_output) + self.assertEqual(description, role['description']) + def test_role_add(self): role_name = self._create_dummy_role() username = self._create_dummy_user() diff --git a/openstackclient/tests/unit/identity/v3/fakes.py b/openstackclient/tests/unit/identity/v3/fakes.py index fc4a48e37f..eb3ce2a356 100644 --- a/openstackclient/tests/unit/identity/v3/fakes.py +++ b/openstackclient/tests/unit/identity/v3/fakes.py @@ -176,6 +176,7 @@ ids_for_children = [PROJECT_WITH_GRANDPARENT['id']] role_id = 'r1' role_name = 'roller' +role_description = 'role description' ROLE = { 'id': role_id, diff --git a/openstackclient/tests/unit/identity/v3/test_role.py b/openstackclient/tests/unit/identity/v3/test_role.py index ead2cb58bb..4278ab1cc1 100644 --- a/openstackclient/tests/unit/identity/v3/test_role.py +++ b/openstackclient/tests/unit/identity/v3/test_role.py @@ -332,6 +332,7 @@ class TestRoleCreate(TestRole): kwargs = { 'domain': None, 'name': identity_fakes.role_name, + 'description': None, } # RoleManager.create(name=, domain=) @@ -375,6 +376,7 @@ class TestRoleCreate(TestRole): kwargs = { 'domain': identity_fakes.domain_id, 'name': identity_fakes.ROLE_2['name'], + 'description': None, } # RoleManager.create(name=, domain=) @@ -391,6 +393,49 @@ class TestRoleCreate(TestRole): ) self.assertEqual(datalist, data) + def test_role_create_with_description(self): + + self.roles_mock.create.return_value = fakes.FakeResource( + None, + copy.deepcopy(identity_fakes.ROLE_2), + loaded=True, + ) + arglist = [ + '--description', identity_fakes.role_description, + identity_fakes.ROLE_2['name'], + ] + verifylist = [ + ('description', identity_fakes.role_description), + ('name', identity_fakes.ROLE_2['name']), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + # In base command class ShowOne in cliff, abstract method take_action() + # returns a two-part tuple with a tuple of column names and a tuple of + # data to be shown. + columns, data = self.cmd.take_action(parsed_args) + + # Set expected values + kwargs = { + 'description': identity_fakes.role_description, + 'name': identity_fakes.ROLE_2['name'], + 'domain': None, + } + + # RoleManager.create(name=, domain=) + self.roles_mock.create.assert_called_with( + **kwargs + ) + + collist = ('domain', 'id', 'name') + self.assertEqual(collist, columns) + datalist = ( + 'd1', + identity_fakes.ROLE_2['id'], + identity_fakes.ROLE_2['name'], + ) + self.assertEqual(datalist, data) + class TestRoleDelete(TestRole): @@ -825,6 +870,7 @@ class TestRoleSet(TestRole): # Set expected values kwargs = { 'name': 'over', + 'description': None, } # RoleManager.update(role, name=) self.roles_mock.update.assert_called_with( @@ -856,6 +902,39 @@ class TestRoleSet(TestRole): # Set expected values kwargs = { 'name': 'over', + 'description': None, + } + # RoleManager.update(role, name=) + self.roles_mock.update.assert_called_with( + identity_fakes.ROLE_2['id'], + **kwargs + ) + self.assertIsNone(result) + + def test_role_set_description(self): + self.roles_mock.get.return_value = fakes.FakeResource( + None, + copy.deepcopy(identity_fakes.ROLE_2), + loaded=True, + ) + arglist = [ + '--name', 'over', + '--description', identity_fakes.role_description, + identity_fakes.ROLE_2['name'], + ] + verifylist = [ + ('name', 'over'), + ('description', identity_fakes.role_description), + ('role', identity_fakes.ROLE_2['name']), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + result = self.cmd.take_action(parsed_args) + + # Set expected values + kwargs = { + 'name': 'over', + 'description': identity_fakes.role_description, } # RoleManager.update(role, name=) self.roles_mock.update.assert_called_with( diff --git a/releasenotes/notes/add-description-to-role-afe7b6ff668df261.yaml b/releasenotes/notes/add-description-to-role-afe7b6ff668df261.yaml new file mode 100644 index 0000000000..31efb3778f --- /dev/null +++ b/releasenotes/notes/add-description-to-role-afe7b6ff668df261.yaml @@ -0,0 +1,8 @@ +--- +features: + - | + Now user can add the description when user create's + the role using OSC ``openstack role create`` command. + User can add the description by adding + `--description ` to OSC + ``openstack role create`` command.