Merge "Add --or-show option to user create"
This commit is contained in:
commit
3001d602c5
@ -71,6 +71,11 @@ class CreateUser(show.ShowOne):
|
|||||||
action='store_true',
|
action='store_true',
|
||||||
help=_('Disable user'),
|
help=_('Disable user'),
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--or-show',
|
||||||
|
action='store_true',
|
||||||
|
help=_('Return existing user'),
|
||||||
|
)
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
@ -91,6 +96,7 @@ class CreateUser(show.ShowOne):
|
|||||||
if parsed_args.password_prompt:
|
if parsed_args.password_prompt:
|
||||||
parsed_args.password = utils.get_password(self.app.stdin)
|
parsed_args.password = utils.get_password(self.app.stdin)
|
||||||
|
|
||||||
|
try:
|
||||||
user = identity_client.users.create(
|
user = identity_client.users.create(
|
||||||
parsed_args.name,
|
parsed_args.name,
|
||||||
parsed_args.password,
|
parsed_args.password,
|
||||||
@ -98,6 +104,16 @@ class CreateUser(show.ShowOne):
|
|||||||
tenant_id=project_id,
|
tenant_id=project_id,
|
||||||
enabled=enabled,
|
enabled=enabled,
|
||||||
)
|
)
|
||||||
|
except ksc_exc.Conflict as e:
|
||||||
|
if parsed_args.or_show:
|
||||||
|
user = utils.find_resource(
|
||||||
|
identity_client.users,
|
||||||
|
parsed_args.name,
|
||||||
|
)
|
||||||
|
self.log.info('Returning existing user %s', user.name)
|
||||||
|
else:
|
||||||
|
raise e
|
||||||
|
|
||||||
# NOTE(dtroyer): The users.create() method wants 'tenant_id' but
|
# NOTE(dtroyer): The users.create() method wants 'tenant_id' but
|
||||||
# the returned resource has 'tenantId'. Sigh.
|
# the returned resource has 'tenantId'. Sigh.
|
||||||
# We're using project_id now inside OSC so there.
|
# We're using project_id now inside OSC so there.
|
||||||
|
@ -210,6 +210,7 @@ class FakeModule(object):
|
|||||||
|
|
||||||
class FakeResource(object):
|
class FakeResource(object):
|
||||||
def __init__(self, manager, info, loaded=False):
|
def __init__(self, manager, info, loaded=False):
|
||||||
|
self.__name__ = type(self).__name__
|
||||||
self.manager = manager
|
self.manager = manager
|
||||||
self._info = info
|
self._info = info
|
||||||
self._add_details(info)
|
self._add_details(info)
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
import copy
|
import copy
|
||||||
import mock
|
import mock
|
||||||
|
|
||||||
|
from keystoneclient.openstack.common.apiclient import exceptions as ksc_exc
|
||||||
from openstackclient.identity.v2_0 import user
|
from openstackclient.identity.v2_0 import user
|
||||||
from openstackclient.tests import fakes
|
from openstackclient.tests import fakes
|
||||||
from openstackclient.tests.identity.v2_0 import fakes as identity_fakes
|
from openstackclient.tests.identity.v2_0 import fakes as identity_fakes
|
||||||
@ -342,6 +343,84 @@ class TestUserCreate(TestUser):
|
|||||||
)
|
)
|
||||||
self.assertEqual(data, datalist)
|
self.assertEqual(data, datalist)
|
||||||
|
|
||||||
|
def test_user_create_or_show_exists(self):
|
||||||
|
def _raise_conflict(*args, **kwargs):
|
||||||
|
raise ksc_exc.Conflict(None)
|
||||||
|
|
||||||
|
# need to make this throw an exception...
|
||||||
|
self.users_mock.create.side_effect = _raise_conflict
|
||||||
|
|
||||||
|
self.users_mock.get.return_value = fakes.FakeResource(
|
||||||
|
None,
|
||||||
|
copy.deepcopy(identity_fakes.USER),
|
||||||
|
loaded=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
arglist = [
|
||||||
|
'--or-show',
|
||||||
|
identity_fakes.user_name,
|
||||||
|
]
|
||||||
|
verifylist = [
|
||||||
|
('name', identity_fakes.user_name),
|
||||||
|
('or_show', True),
|
||||||
|
]
|
||||||
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||||
|
|
||||||
|
# DisplayCommandBase.take_action() returns two tuples
|
||||||
|
columns, data = self.cmd.take_action(parsed_args)
|
||||||
|
|
||||||
|
# UserManager.create(name, password, email, tenant_id=, enabled=)
|
||||||
|
self.users_mock.get.assert_called_with(identity_fakes.user_name)
|
||||||
|
|
||||||
|
collist = ('email', 'enabled', 'id', 'name', 'project_id')
|
||||||
|
self.assertEqual(collist, columns)
|
||||||
|
datalist = (
|
||||||
|
identity_fakes.user_email,
|
||||||
|
True,
|
||||||
|
identity_fakes.user_id,
|
||||||
|
identity_fakes.user_name,
|
||||||
|
identity_fakes.project_id,
|
||||||
|
)
|
||||||
|
self.assertEqual(datalist, data)
|
||||||
|
|
||||||
|
def test_user_create_or_show_not_exists(self):
|
||||||
|
arglist = [
|
||||||
|
'--or-show',
|
||||||
|
identity_fakes.user_name,
|
||||||
|
]
|
||||||
|
verifylist = [
|
||||||
|
('name', identity_fakes.user_name),
|
||||||
|
('or_show', True),
|
||||||
|
]
|
||||||
|
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 = {
|
||||||
|
'enabled': True,
|
||||||
|
'tenant_id': None,
|
||||||
|
}
|
||||||
|
# UserManager.create(name, password, email, tenant_id=, enabled=)
|
||||||
|
self.users_mock.create.assert_called_with(
|
||||||
|
identity_fakes.user_name,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
**kwargs
|
||||||
|
)
|
||||||
|
|
||||||
|
collist = ('email', 'enabled', 'id', 'name', 'project_id')
|
||||||
|
self.assertEqual(collist, columns)
|
||||||
|
datalist = (
|
||||||
|
identity_fakes.user_email,
|
||||||
|
True,
|
||||||
|
identity_fakes.user_id,
|
||||||
|
identity_fakes.user_name,
|
||||||
|
identity_fakes.project_id,
|
||||||
|
)
|
||||||
|
self.assertEqual(datalist, data)
|
||||||
|
|
||||||
|
|
||||||
class TestUserDelete(TestUser):
|
class TestUserDelete(TestUser):
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user