From 42d0b20ebc5fd7393d250fb72d30d5ce630dc54f Mon Sep 17 00:00:00 2001 From: Dean Troyer Date: Fri, 7 Nov 2014 04:44:54 -0600 Subject: 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 --- openstackclient/identity/v2_0/user.py | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) (limited to 'openstackclient/identity') 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. -- cgit v1.2.1