diff options
| author | Gage Hugo <gagehugo@gmail.com> | 2017-07-06 16:21:03 -0500 |
|---|---|---|
| committer | Dean Troyer <dtroyer@gmail.com> | 2018-02-23 20:26:57 +0000 |
| commit | d32664150fbc00340f3ff4304c13abf9a191299a (patch) | |
| tree | 31731b192ed863514ce60ab1244d04d6edf910af /openstackclient/identity | |
| parent | 8c5f7555698491c3a0b44fe6c3fee50d0189f2d6 (diff) | |
| download | python-openstackclient-d32664150fbc00340f3ff4304c13abf9a191299a.tar.gz | |
Add project tags functionality
This change adds tags functionality for projects in keystone. A user
can add a single tag with "--tag", chain "--tag" to add multiple
tags, or clear tags with "--no-tag".
Change-Id: I31cfef3e76dcefe299dacb00c11bb1a10a252628
Partially-Implements: bp project-tags
Diffstat (limited to 'openstackclient/identity')
| -rw-r--r-- | openstackclient/identity/v3/project.py | 9 | ||||
| -rw-r--r-- | openstackclient/identity/v3/tag.py | 116 |
2 files changed, 124 insertions, 1 deletions
diff --git a/openstackclient/identity/v3/project.py b/openstackclient/identity/v3/project.py index 60efbac4..e819a0a8 100644 --- a/openstackclient/identity/v3/project.py +++ b/openstackclient/identity/v3/project.py @@ -26,7 +26,7 @@ import six from openstackclient.i18n import _ from openstackclient.identity import common - +from openstackclient.identity.v3 import tag LOG = logging.getLogger(__name__) @@ -79,6 +79,7 @@ class CreateProject(command.ShowOne): action='store_true', help=_('Return existing project'), ) + tag.add_tag_option_to_parser_for_create(parser, _('project')) return parser def take_action(self, parsed_args): @@ -102,6 +103,7 @@ class CreateProject(command.ShowOne): kwargs = {} if parsed_args.property: kwargs = parsed_args.property.copy() + kwargs['tags'] = list(set(parsed_args.tags)) try: project = identity_client.projects.create( @@ -207,6 +209,7 @@ class ListProject(command.Lister): '(default: asc), repeat this option to specify multiple ' 'keys and directions.'), ) + tag.add_tag_filtering_option_to_parser(parser, _('projects')) return parser def take_action(self, parsed_args): @@ -234,6 +237,8 @@ class ListProject(command.Lister): kwargs['user'] = user_id + tag.get_tag_filtering_args(parsed_args, kwargs) + if parsed_args.my_projects: # NOTE(adriant): my-projects supersedes all the other filters. kwargs = {'user': self.app.client_manager.auth_ref.user_id} @@ -303,6 +308,7 @@ class SetProject(command.Command): help=_('Set a property on <project> ' '(repeat option to set multiple properties)'), ) + tag.add_tag_option_to_parser_for_set(parser, _('project')) return parser def take_action(self, parsed_args): @@ -323,6 +329,7 @@ class SetProject(command.Command): kwargs['enabled'] = False if parsed_args.property: kwargs.update(parsed_args.property) + tag.update_tags_in_args(parsed_args, project, kwargs) identity_client.projects.update(project.id, **kwargs) diff --git a/openstackclient/identity/v3/tag.py b/openstackclient/identity/v3/tag.py new file mode 100644 index 00000000..abf022d4 --- /dev/null +++ b/openstackclient/identity/v3/tag.py @@ -0,0 +1,116 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import argparse + +from openstackclient.i18n import _ + + +class _CommaListAction(argparse.Action): + + def __call__(self, parser, namespace, values, option_string=None): + setattr(namespace, self.dest, values.split(',')) + + +def add_tag_filtering_option_to_parser(parser, collection_name): + parser.add_argument( + '--tags', + metavar='<tag>[,<tag>,...]', + action=_CommaListAction, + help=_('List %s which have all given tag(s) ' + '(Comma-separated list of tags)') % collection_name + ) + parser.add_argument( + '--tags-any', + metavar='<tag>[,<tag>,...]', + action=_CommaListAction, + help=_('List %s which have any given tag(s) ' + '(Comma-separated list of tags)') % collection_name + ) + parser.add_argument( + '--not-tags', + metavar='<tag>[,<tag>,...]', + action=_CommaListAction, + help=_('Exclude %s which have all given tag(s) ' + '(Comma-separated list of tags)') % collection_name + ) + parser.add_argument( + '--not-tags-any', + metavar='<tag>[,<tag>,...]', + action=_CommaListAction, + help=_('Exclude %s which have any given tag(s) ' + '(Comma-separated list of tags)') % collection_name + ) + + +def get_tag_filtering_args(parsed_args, args): + if parsed_args.tags: + args['tags'] = ','.join(parsed_args.tags) + if parsed_args.tags_any: + args['tags-any'] = ','.join(parsed_args.tags_any) + if parsed_args.not_tags: + args['not-tags'] = ','.join(parsed_args.not_tags) + if parsed_args.not_tags_any: + args['not-tags-any'] = ','.join(parsed_args.not_tags_any) + + +def add_tag_option_to_parser_for_create(parser, resource_name): + tag_group = parser.add_mutually_exclusive_group() + tag_group.add_argument( + '--tag', + action='append', + dest='tags', + metavar='<tag>', + default=[], + help=_('Tag to be added to the %s ' + '(repeat option to set multiple tags)') % resource_name + ) + + +def add_tag_option_to_parser_for_set(parser, resource_name): + parser.add_argument( + '--tag', + action='append', + dest='tags', + metavar='<tag>', + default=[], + help=_('Tag to be added to the %s ' + '(repeat option to set multiple tags)') % resource_name + ) + parser.add_argument( + '--clear-tags', + action='store_true', + help=_('Clear tags associated with the %s. Specify ' + 'both --tag and --clear-tags to overwrite ' + 'current tags') % resource_name + ) + parser.add_argument( + '--remove-tag', + metavar='<tag>', + default=[], + help=_('Tag to be deleted from the %s ' + '(repeat option to delete multiple tags)') % resource_name + ) + + +def update_tags_in_args(parsed_args, obj, args): + if parsed_args.clear_tags: + args['tags'] = [] + obj.tags = [] + if parsed_args.remove_tag: + if parsed_args.remove_tag in obj.tags: + obj.tags.remove(parsed_args.remove_tag) + args['tags'] = list(set(obj.tags)) + return + if parsed_args.tags: + args['tags'] = list(set(obj.tags).union( + set(parsed_args.tags))) |
