From 2b0013c5c1afe6d2fee5f93cf6928f6f910048c1 Mon Sep 17 00:00:00 2001
From: Steve Martinelli <stevemar@ca.ibm.com>
Date: Tue, 9 Jun 2015 17:25:12 -0400
Subject: [PATCH] Refactor option handling for user|group|project domain
 scoping

put the common options in identity.common, this way the help is
consistent

Change-Id: I5b09cfb56fa0f8d16feb95150f216fccbe9f2b22
---
 .../command-objects/ec2-credentials.rst       |  9 ++-
 doc/source/command-objects/trust.rst          |  9 +--
 openstackclient/identity/common.py            | 30 ++++++++++
 openstackclient/identity/v3/ec2creds.py       | 50 ++---------------
 openstackclient/identity/v3/group.py          | 56 +++----------------
 openstackclient/identity/v3/role.py           | 24 +-------
 openstackclient/identity/v3/trust.py          | 10 +---
 7 files changed, 57 insertions(+), 131 deletions(-)

diff --git a/doc/source/command-objects/ec2-credentials.rst b/doc/source/command-objects/ec2-credentials.rst
index f8e3856485..6748422e14 100644
--- a/doc/source/command-objects/ec2-credentials.rst
+++ b/doc/source/command-objects/ec2-credentials.rst
@@ -28,19 +28,18 @@ Create EC2 credentials
 
 .. option:: --user-domain <user-domain>
 
-    Select user from a specific domain (name or ID)
-    This can be used in case collisions between user names exist.
+    Domain the user belongs to (name or ID). This can be
+    used in case collisions between user names exist.
 
     .. versionadded:: 3
 
 .. option:: --project-domain <project-domain>
 
-    Select project from a specific domain (name or ID)
-    This can be used in case collisions between project names exist.
+    Domain the project belongs to (name or ID). This can be
+    used in case collisions between user names exist.
 
     .. versionadded:: 3
 
-
 The :option:`--project` and :option:`--user`  options are typically only
 useful for admin users, but may be allowed for other users depending on
 the policy of the cloud and the roles granted to the user.
diff --git a/doc/source/command-objects/trust.rst b/doc/source/command-objects/trust.rst
index c5e16b464c..556edc54d4 100644
--- a/doc/source/command-objects/trust.rst
+++ b/doc/source/command-objects/trust.rst
@@ -39,15 +39,16 @@ Create new trust
 
     Sets an expiration date for the trust (format of YYYY-mm-ddTHH:MM:SS)
 
-.. option:: --project-domain <domain>
+.. option:: --project-domain <project-domain>
 
-    Domain that contains <project> (name or ID)
+    Domain the project belongs to (name or ID). This can be
+    used in case collisions between user names exist.
 
-.. option:: --trustor-domain <domain>
+.. option:: --trustor-domain <trustor-domain>
 
     Domain that contains <trustor> (name or ID)
 
-.. option:: --trustee-domain <domain>
+.. option:: --trustee-domain <trustee-domain>
 
     Domain that contains <trustee> (name or ID)
 
diff --git a/openstackclient/identity/common.py b/openstackclient/identity/common.py
index b97a17788a..6eca02bad8 100644
--- a/openstackclient/identity/common.py
+++ b/openstackclient/identity/common.py
@@ -109,3 +109,33 @@ def _find_identity_resource(identity_client_manager, name_or_id,
         pass
 
     return resource_type(None, {'id': name_or_id, 'name': name_or_id})
+
+
+def add_user_domain_option_to_parser(parser):
+    parser.add_argument(
+        '--user-domain',
+        metavar='<user-domain>',
+        help=('Domain the user belongs to (name or ID). '
+              'This can be used in case collisions between user names '
+              'exist.')
+    )
+
+
+def add_group_domain_option_to_parser(parser):
+    parser.add_argument(
+        '--group-domain',
+        metavar='<group-domain>',
+        help=('Domain the group belongs to (name or ID). '
+              'This can be used in case collisions between group names '
+              'exist.')
+    )
+
+
+def add_project_domain_option_to_parser(parser):
+    parser.add_argument(
+        '--project-domain',
+        metavar='<project-domain>',
+        help=('Domain the project belongs to (name or ID). '
+              'This can be used in case collisions between project names '
+              'exist.')
+    )
diff --git a/openstackclient/identity/v3/ec2creds.py b/openstackclient/identity/v3/ec2creds.py
index f995ae5553..b518b37078 100644
--- a/openstackclient/identity/v3/ec2creds.py
+++ b/openstackclient/identity/v3/ec2creds.py
@@ -75,24 +75,8 @@ class CreateEC2Creds(show.ShowOne):
                 '(name or ID; default: current authenticated user)'
             ),
         )
-        parser.add_argument(
-            '--user-domain',
-            metavar='<user-domain>',
-            help=(
-                'Select user from a specific domain (name or ID); '
-                'This can be used in case collisions between user names '
-                'exist.'
-            ),
-        )
-        parser.add_argument(
-            '--project-domain',
-            metavar='<project-domain>',
-            help=(
-                'Select project from a specific domain (name or ID); '
-                'This can be used in case collisions between project names '
-                'exist.'
-            ),
-        )
+        common.add_user_domain_option_to_parser(parser)
+        common.add_project_domain_option_to_parser(parser)
         return parser
 
     def take_action(self, parsed_args):
@@ -149,15 +133,7 @@ class DeleteEC2Creds(command.Command):
             metavar='<user>',
             help=_('Delete credentials for user (name or ID)'),
         )
-        parser.add_argument(
-            '--user-domain',
-            metavar='<user-domain>',
-            help=(
-                'Select user from a specific domain (name or ID); '
-                'This can be used in case collisions between user names '
-                'exist.'
-            ),
-        )
+        common.add_user_domain_option_to_parser(parser)
         return parser
 
     def take_action(self, parsed_args):
@@ -179,15 +155,7 @@ class ListEC2Creds(lister.Lister):
             metavar='<user>',
             help=_('Filter list by user (name or ID)'),
         )
-        parser.add_argument(
-            '--user-domain',
-            metavar='<user-domain>',
-            help=(
-                'Select user from a specific domain (name or ID); '
-                'This can be used in case collisions between user names '
-                'exist.'
-            ),
-        )
+        common.add_user_domain_option_to_parser(parser)
         return parser
 
     def take_action(self, parsed_args):
@@ -223,15 +191,7 @@ class ShowEC2Creds(show.ShowOne):
             metavar='<user>',
             help=_('Show credentials for user (name or ID)'),
         )
-        parser.add_argument(
-            '--user-domain',
-            metavar='<user-domain>',
-            help=(
-                'Select user from a specific domain (name or ID); '
-                'This can be used in case collisions between user names '
-                'exist.'
-            ),
-        )
+        common.add_user_domain_option_to_parser(parser)
         return parser
 
     def take_action(self, parsed_args):
diff --git a/openstackclient/identity/v3/group.py b/openstackclient/identity/v3/group.py
index b064eb777c..d659f71e4a 100644
--- a/openstackclient/identity/v3/group.py
+++ b/openstackclient/identity/v3/group.py
@@ -46,20 +46,8 @@ class AddUserToGroup(command.Command):
             metavar='<user>',
             help='User to add to <group> (name or ID)',
         )
-        parser.add_argument(
-            '--group-domain',
-            metavar='<group-domain>',
-            help=('Domain the group belongs to (name or ID). '
-                  'This can be used in case collisions between group names '
-                  'exist.')
-        )
-        parser.add_argument(
-            '--user-domain',
-            metavar='<user-domain>',
-            help=('Domain the user belongs to (name or ID). '
-                  'This can be used in case collisions between user names '
-                  'exist.')
-        )
+        common.add_group_domain_option_to_parser(parser)
+        common.add_user_domain_option_to_parser(parser)
         return parser
 
     def take_action(self, parsed_args):
@@ -100,20 +88,8 @@ class CheckUserInGroup(command.Command):
             metavar='<user>',
             help='User to check (name or ID)',
         )
-        parser.add_argument(
-            '--group-domain',
-            metavar='<group-domain>',
-            help=('Domain the group belongs to (name or ID). '
-                  'This can be used in case collisions between group names '
-                  'exist.')
-        )
-        parser.add_argument(
-            '--user-domain',
-            metavar='<user-domain>',
-            help=('Domain the user belongs to (name or ID). '
-                  'This can be used in case collisions between user names '
-                  'exist.')
-        )
+        common.add_group_domain_option_to_parser(parser)
+        common.add_user_domain_option_to_parser(parser)
         return parser
 
     def take_action(self, parsed_args):
@@ -241,13 +217,7 @@ class ListGroup(lister.Lister):
             metavar='<user>',
             help='Filter group list by <user> (name or ID)',
         )
-        parser.add_argument(
-            '--user-domain',
-            metavar='<user-domain>',
-            help=('Domain the user belongs to (name or ID). '
-                  'This can be used in case collisions between user names '
-                  'exist.')
-        )
+        common.add_user_domain_option_to_parser(parser)
         parser.add_argument(
             '--long',
             action='store_true',
@@ -310,20 +280,8 @@ class RemoveUserFromGroup(command.Command):
             metavar='<user>',
             help='User to remove from <group> (name or ID)',
         )
-        parser.add_argument(
-            '--group-domain',
-            metavar='<group-domain>',
-            help=('Domain the group belongs to (name or ID). '
-                  'This can be used in case collisions between group names '
-                  'exist.')
-        )
-        parser.add_argument(
-            '--user-domain',
-            metavar='<user-domain>',
-            help=('Domain the user belongs to (name or ID). '
-                  'This can be used in case collisions between user names '
-                  'exist.')
-        )
+        common.add_group_domain_option_to_parser(parser)
+        common.add_user_domain_option_to_parser(parser)
         return parser
 
     def take_action(self, parsed_args):
diff --git a/openstackclient/identity/v3/role.py b/openstackclient/identity/v3/role.py
index 4f1c04d5f0..17f47ffd63 100644
--- a/openstackclient/identity/v3/role.py
+++ b/openstackclient/identity/v3/role.py
@@ -52,27 +52,9 @@ def _add_identity_and_resource_options_to_parser(parser):
         metavar='<group>',
         help='Include <group> (name or ID)',
     )
-    parser.add_argument(
-        '--user-domain',
-        metavar='<user-domain>',
-        help=('Domain the user belongs to (name or ID). '
-              'This can be used in case collisions between user names '
-              'exist.')
-    )
-    parser.add_argument(
-        '--group-domain',
-        metavar='<group-domain>',
-        help=('Domain the group belongs to (name or ID). '
-              'This can be used in case collisions between group names '
-              'exist.')
-    )
-    parser.add_argument(
-        '--project-domain',
-        metavar='<project-domain>',
-        help=('Domain the project belongs to (name or ID). '
-              'This can be used in case collisions between project names '
-              'exist.')
-    )
+    common.add_group_domain_option_to_parser(parser)
+    common.add_project_domain_option_to_parser(parser)
+    common.add_user_domain_option_to_parser(parser)
 
 
 def _process_identity_and_resource_options(parsed_args,
diff --git a/openstackclient/identity/v3/trust.py b/openstackclient/identity/v3/trust.py
index ab6673d2f8..c8e5c4c729 100644
--- a/openstackclient/identity/v3/trust.py
+++ b/openstackclient/identity/v3/trust.py
@@ -71,19 +71,15 @@ class CreateTrust(show.ShowOne):
             help='Sets an expiration date for the trust'
                  ' (format of YYYY-mm-ddTHH:MM:SS)',
         )
-        parser.add_argument(
-            '--project-domain',
-            metavar='<domain>',
-            help='Domain that contains <project> (name or ID)',
-        )
+        common.add_project_domain_option_to_parser(parser)
         parser.add_argument(
             '--trustor-domain',
-            metavar='<domain>',
+            metavar='<trustor-domain>',
             help='Domain that contains <trustor> (name or ID)',
         )
         parser.add_argument(
             '--trustee-domain',
-            metavar='<domain>',
+            metavar='<trustee-domain>',
             help='Domain that contains <trustee> (name or ID)',
         )
         return parser