From 7b32ec003babec2c6d052146baf2951e465454f5 Mon Sep 17 00:00:00 2001 From: Guojian Shao Date: Thu, 16 Jul 2015 23:23:45 +0800 Subject: [PATCH] add --project-domain option for user v3 user v3 create/set only support --project option, we need --project-domain to prevent collisions between project names exist. Change-Id: I2d62e5b9bb6df4c5c5a9542514faf2e4365bb18b Closes-Bug: #1475357 --- doc/source/command-objects/user.rst | 14 ++++- openstackclient/identity/v3/user.py | 14 +++-- .../tests/identity/v3/test_user.py | 62 +++++++++++++++++++ 3 files changed, 82 insertions(+), 8 deletions(-) diff --git a/doc/source/command-objects/user.rst b/doc/source/command-objects/user.rst index 8aec76b40d..2e297bb360 100644 --- a/doc/source/command-objects/user.rst +++ b/doc/source/command-objects/user.rst @@ -14,7 +14,7 @@ Create new user os user create [--domain ] - [--project ] + [--project [--project-domain ]] [--password ] [--password-prompt] [--email ] @@ -33,6 +33,11 @@ Create new user Default project (name or ID) +.. option:: --project-domain + + Domain the project belongs to (name or ID). + This can be used in case collisions between project names exist. + .. option:: --password Set user password @@ -136,7 +141,7 @@ Set user properties os user set [--name ] - [--project ] + [--project [--project-domain ]] [--password ] [--email ] [--description ] @@ -151,6 +156,11 @@ Set user properties Set default project (name or ID) +.. option:: --project-domain + + Domain the project belongs to (name or ID). + This can be used in case collisions between project names exist. + .. option:: --password Set user password diff --git a/openstackclient/identity/v3/user.py b/openstackclient/identity/v3/user.py index b72e0d1580..459707d2af 100644 --- a/openstackclient/identity/v3/user.py +++ b/openstackclient/identity/v3/user.py @@ -51,6 +51,7 @@ class CreateUser(show.ShowOne): metavar='', help='Default project (name or ID)', ) + common.add_project_domain_option_to_parser(parser) parser.add_argument( '--password', metavar='', @@ -96,10 +97,9 @@ class CreateUser(show.ShowOne): project_id = None if parsed_args.project: - project_id = utils.find_resource( - identity_client.projects, - parsed_args.project, - ).id + project_id = common.find_project(identity_client, + parsed_args.project, + parsed_args.project_domain).id domain_id = None if parsed_args.domain: @@ -301,6 +301,7 @@ class SetUser(command.Command): metavar='', help='Set default project (name or ID)', ) + common.add_project_domain_option_to_parser(parser) parser.add_argument( '--password', metavar='', @@ -367,8 +368,9 @@ class SetUser(command.Command): if parsed_args.description: kwargs['description'] = parsed_args.description if parsed_args.project: - project_id = utils.find_resource( - identity_client.projects, parsed_args.project).id + project_id = common.find_project(identity_client, + parsed_args.project, + parsed_args.project_domain).id kwargs['default_project'] = project_id kwargs['enabled'] = user.enabled if parsed_args.enable: diff --git a/openstackclient/tests/identity/v3/test_user.py b/openstackclient/tests/identity/v3/test_user.py index 18fe9016d4..bdde25a2ce 100644 --- a/openstackclient/tests/identity/v3/test_user.py +++ b/openstackclient/tests/identity/v3/test_user.py @@ -320,6 +320,68 @@ class TestUserCreate(TestUser): ) self.assertEqual(datalist, data) + def test_user_create_project_domain(self): + # Return the new project + self.projects_mock.get.return_value = fakes.FakeResource( + None, + copy.deepcopy(identity_fakes.PROJECT_2), + loaded=True, + ) + # Set up to return an updated user + USER_2 = copy.deepcopy(identity_fakes.USER) + USER_2['default_project_id'] = identity_fakes.PROJECT_2['id'] + self.users_mock.create.return_value = fakes.FakeResource( + None, + USER_2, + loaded=True, + ) + + arglist = [ + '--project', identity_fakes.PROJECT_2['name'], + '--project-domain', identity_fakes.PROJECT_2['domain_id'], + identity_fakes.user_name, + ] + verifylist = [ + ('project', identity_fakes.PROJECT_2['name']), + ('project_domain', identity_fakes.PROJECT_2['domain_id']), + ('enable', False), + ('disable', False), + ('name', identity_fakes.user_name), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + # DisplayCommandBase.take_action() returns two tuples + columns, data = self.cmd.take_action(parsed_args) + + # Set expected values + kwargs = { + 'name': identity_fakes.user_name, + 'default_project': identity_fakes.PROJECT_2['id'], + 'description': None, + 'domain': None, + 'email': None, + 'enabled': True, + 'password': None, + } + # UserManager.create(name=, domain=, project=, password=, email=, + # description=, enabled=, default_project=) + self.users_mock.create.assert_called_with( + **kwargs + ) + + collist = ('default_project_id', 'domain_id', 'email', + 'enabled', 'id', 'name') + self.assertEqual(collist, columns) + datalist = ( + identity_fakes.PROJECT_2['id'], + identity_fakes.domain_id, + identity_fakes.user_email, + True, + identity_fakes.user_id, + identity_fakes.user_name, + ) + self.assertEqual(datalist, data) + def test_user_create_domain(self): arglist = [ '--domain', identity_fakes.domain_name,