diff options
| author | Dean Troyer <dtroyer@gmail.com> | 2014-11-07 04:44:54 -0600 |
|---|---|---|
| committer | Dean Troyer <dtroyer@gmail.com> | 2014-11-07 12:28:42 -0600 |
| commit | 42d0b20ebc5fd7393d250fb72d30d5ce630dc54f (patch) | |
| tree | a127968897fee9a8eb268f07f4b3939b501b4682 /openstackclient/identity/v2_0 | |
| parent | 631ed3c8026e6b4539e7a8bf4adb8d2a7239b36a (diff) | |
| download | python-openstackclient-42d0b20ebc5fd7393d250fb72d30d5ce630dc54f.tar.gz | |
Add --or-show option to user create
The --or-show option is added to create commands for the common case
of needing to ensure an object exists and getting its properties if
it does or creating a new one if it does not exist.
Note that if the object exists, any additional options that would
set values in a newly created object are ignored if the object
exists.
FakeResource needs the __name__ attribute to fall through utils.find_resource.
Prove the concept on v2 user create then propogate once we're happy with it...
Change-Id: I6268566514840c284e6a1d44b409a81d6699ef99
Diffstat (limited to 'openstackclient/identity/v2_0')
| -rw-r--r-- | openstackclient/identity/v2_0/user.py | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/openstackclient/identity/v2_0/user.py b/openstackclient/identity/v2_0/user.py index 729c6ac8..2ebcba18 100644 --- a/openstackclient/identity/v2_0/user.py +++ b/openstackclient/identity/v2_0/user.py @@ -71,6 +71,11 @@ class CreateUser(show.ShowOne): action='store_true', help=_('Disable user'), ) + parser.add_argument( + '--or-show', + action='store_true', + help=_('Return existing user'), + ) return parser def take_action(self, parsed_args): @@ -91,13 +96,24 @@ class CreateUser(show.ShowOne): if parsed_args.password_prompt: parsed_args.password = utils.get_password(self.app.stdin) - user = identity_client.users.create( - parsed_args.name, - parsed_args.password, - parsed_args.email, - tenant_id=project_id, - enabled=enabled, - ) + try: + user = identity_client.users.create( + parsed_args.name, + parsed_args.password, + parsed_args.email, + tenant_id=project_id, + 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 # the returned resource has 'tenantId'. Sigh. # We're using project_id now inside OSC so there. |
