|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | + |
| 13 | +import argparse |
| 14 | + |
| 15 | +from openstackclient.i18n import _ |
| 16 | + |
| 17 | + |
| 18 | +class _CommaListAction(argparse.Action): |
| 19 | + |
| 20 | + def __call__(self, parser, namespace, values, option_string=None): |
| 21 | + setattr(namespace, self.dest, values.split(',')) |
| 22 | + |
| 23 | + |
| 24 | +def add_tag_filtering_option_to_parser(parser, collection_name): |
| 25 | + parser.add_argument( |
| 26 | + '--tags', |
| 27 | + metavar='<tag>[,<tag>,...]', |
| 28 | + action=_CommaListAction, |
| 29 | + help=_('List %s which have all given tag(s) ' |
| 30 | + '(Comma-separated list of tags)') % collection_name |
| 31 | + ) |
| 32 | + parser.add_argument( |
| 33 | + '--tags-any', |
| 34 | + metavar='<tag>[,<tag>,...]', |
| 35 | + action=_CommaListAction, |
| 36 | + help=_('List %s which have any given tag(s) ' |
| 37 | + '(Comma-separated list of tags)') % collection_name |
| 38 | + ) |
| 39 | + parser.add_argument( |
| 40 | + '--not-tags', |
| 41 | + metavar='<tag>[,<tag>,...]', |
| 42 | + action=_CommaListAction, |
| 43 | + help=_('Exclude %s which have all given tag(s) ' |
| 44 | + '(Comma-separated list of tags)') % collection_name |
| 45 | + ) |
| 46 | + parser.add_argument( |
| 47 | + '--not-tags-any', |
| 48 | + metavar='<tag>[,<tag>,...]', |
| 49 | + action=_CommaListAction, |
| 50 | + help=_('Exclude %s which have any given tag(s) ' |
| 51 | + '(Comma-separated list of tags)') % collection_name |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +def get_tag_filtering_args(parsed_args, args): |
| 56 | + if parsed_args.tags: |
| 57 | + args['tags'] = ','.join(parsed_args.tags) |
| 58 | + if parsed_args.tags_any: |
| 59 | + args['tags-any'] = ','.join(parsed_args.tags_any) |
| 60 | + if parsed_args.not_tags: |
| 61 | + args['not-tags'] = ','.join(parsed_args.not_tags) |
| 62 | + if parsed_args.not_tags_any: |
| 63 | + args['not-tags-any'] = ','.join(parsed_args.not_tags_any) |
| 64 | + |
| 65 | + |
| 66 | +def add_tag_option_to_parser_for_create(parser, resource_name): |
| 67 | + tag_group = parser.add_mutually_exclusive_group() |
| 68 | + tag_group.add_argument( |
| 69 | + '--tag', |
| 70 | + action='append', |
| 71 | + dest='tags', |
| 72 | + metavar='<tag>', |
| 73 | + default=[], |
| 74 | + help=_('Tag to be added to the %s ' |
| 75 | + '(repeat option to set multiple tags)') % resource_name |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +def add_tag_option_to_parser_for_set(parser, resource_name): |
| 80 | + parser.add_argument( |
| 81 | + '--tag', |
| 82 | + action='append', |
| 83 | + dest='tags', |
| 84 | + metavar='<tag>', |
| 85 | + default=[], |
| 86 | + help=_('Tag to be added to the %s ' |
| 87 | + '(repeat option to set multiple tags)') % resource_name |
| 88 | + ) |
| 89 | + parser.add_argument( |
| 90 | + '--clear-tags', |
| 91 | + action='store_true', |
| 92 | + help=_('Clear tags associated with the %s. Specify ' |
| 93 | + 'both --tag and --clear-tags to overwrite ' |
| 94 | + 'current tags') % resource_name |
| 95 | + ) |
| 96 | + parser.add_argument( |
| 97 | + '--remove-tag', |
| 98 | + metavar='<tag>', |
| 99 | + default=[], |
| 100 | + help=_('Tag to be deleted from the %s ' |
| 101 | + '(repeat option to delete multiple tags)') % resource_name |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +def update_tags_in_args(parsed_args, obj, args): |
| 106 | + if parsed_args.clear_tags: |
| 107 | + args['tags'] = [] |
| 108 | + obj.tags = [] |
| 109 | + if parsed_args.remove_tag: |
| 110 | + if parsed_args.remove_tag in obj.tags: |
| 111 | + obj.tags.remove(parsed_args.remove_tag) |
| 112 | + args['tags'] = list(set(obj.tags)) |
| 113 | + return |
| 114 | + if parsed_args.tags: |
| 115 | + args['tags'] = list(set(obj.tags).union( |
| 116 | + set(parsed_args.tags))) |
0 commit comments