|
| 1 | +# Copyright 2012-2013 OpenStack Foundation |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | +# |
| 15 | + |
| 16 | +"""Identity v3 Consumer action implementations""" |
| 17 | + |
| 18 | +import logging |
| 19 | +import six |
| 20 | +import sys |
| 21 | + |
| 22 | +from cliff import command |
| 23 | +from cliff import lister |
| 24 | +from cliff import show |
| 25 | + |
| 26 | +from openstackclient.common import utils |
| 27 | + |
| 28 | + |
| 29 | +class CreateConsumer(show.ShowOne): |
| 30 | + """Create consumer command""" |
| 31 | + |
| 32 | + log = logging.getLogger(__name__ + '.CreateConsumer') |
| 33 | + |
| 34 | + def get_parser(self, prog_name): |
| 35 | + parser = super(CreateConsumer, self).get_parser(prog_name) |
| 36 | + parser.add_argument( |
| 37 | + '--description', |
| 38 | + metavar='<consumer-description>', |
| 39 | + help='New consumer description', |
| 40 | + ) |
| 41 | + return parser |
| 42 | + |
| 43 | + def take_action(self, parsed_args): |
| 44 | + self.log.debug('take_action(%s)' % parsed_args) |
| 45 | + identity_client = self.app.client_manager.identity |
| 46 | + consumer = identity_client.consumers.create_consumer( |
| 47 | + parsed_args.description |
| 48 | + ) |
| 49 | + info = {} |
| 50 | + info.update(consumer._info) |
| 51 | + return zip(*sorted(six.iteritems(info))) |
| 52 | + |
| 53 | + |
| 54 | +class DeleteConsumer(command.Command): |
| 55 | + """Delete consumer command""" |
| 56 | + |
| 57 | + log = logging.getLogger(__name__ + '.DeleteConsumer') |
| 58 | + |
| 59 | + def get_parser(self, prog_name): |
| 60 | + parser = super(DeleteConsumer, self).get_parser(prog_name) |
| 61 | + parser.add_argument( |
| 62 | + 'consumer', |
| 63 | + metavar='<consumer>', |
| 64 | + help='ID of consumer to delete', |
| 65 | + ) |
| 66 | + return parser |
| 67 | + |
| 68 | + def take_action(self, parsed_args): |
| 69 | + self.log.debug('take_action(%s)' % parsed_args) |
| 70 | + identity_client = self.app.client_manager.identity |
| 71 | + consumer = utils.find_resource( |
| 72 | + identity_client.consumers, parsed_args.consumer) |
| 73 | + identity_client.consumers.delete_consumer(consumer.id) |
| 74 | + return |
| 75 | + |
| 76 | + |
| 77 | +class ListConsumer(lister.Lister): |
| 78 | + """List consumer command""" |
| 79 | + |
| 80 | + log = logging.getLogger(__name__ + '.ListConsumer') |
| 81 | + |
| 82 | + def take_action(self, parsed_args): |
| 83 | + self.log.debug('take_action(%s)' % parsed_args) |
| 84 | + columns = ('ID', 'Description') |
| 85 | + data = self.app.client_manager.identity.consumers.list_consumers() |
| 86 | + return (columns, |
| 87 | + (utils.get_item_properties( |
| 88 | + s, columns, |
| 89 | + formatters={}, |
| 90 | + ) for s in data)) |
| 91 | + |
| 92 | + |
| 93 | +class SetConsumer(command.Command): |
| 94 | + """Set consumer command""" |
| 95 | + |
| 96 | + log = logging.getLogger(__name__ + '.SetConsumer') |
| 97 | + |
| 98 | + def get_parser(self, prog_name): |
| 99 | + parser = super(SetConsumer, self).get_parser(prog_name) |
| 100 | + parser.add_argument( |
| 101 | + 'consumer', |
| 102 | + metavar='<consumer>', |
| 103 | + help='ID of consumer to change', |
| 104 | + ) |
| 105 | + parser.add_argument( |
| 106 | + '--description', |
| 107 | + metavar='<new-consumer-description>', |
| 108 | + help='New consumer description', |
| 109 | + ) |
| 110 | + return parser |
| 111 | + |
| 112 | + def take_action(self, parsed_args): |
| 113 | + self.log.debug('take_action(%s)' % parsed_args) |
| 114 | + identity_client = self.app.client_manager.identity |
| 115 | + consumer = utils.find_resource( |
| 116 | + identity_client.consumers, parsed_args.consumer) |
| 117 | + kwargs = {} |
| 118 | + if parsed_args.description: |
| 119 | + kwargs['description'] = parsed_args.description |
| 120 | + |
| 121 | + if not len(kwargs): |
| 122 | + sys.stdout.write("Consumer not updated, no arguments present") |
| 123 | + return |
| 124 | + |
| 125 | + consumer = identity_client.consumers.update_consumer( |
| 126 | + consumer.id, |
| 127 | + **kwargs |
| 128 | + ) |
| 129 | + |
| 130 | + info = {} |
| 131 | + info.update(consumer._info) |
| 132 | + return zip(*sorted(six.iteritems(info))) |
| 133 | + |
| 134 | + |
| 135 | +class ShowConsumer(show.ShowOne): |
| 136 | + """Show consumer command""" |
| 137 | + |
| 138 | + log = logging.getLogger(__name__ + '.ShowConsumer') |
| 139 | + |
| 140 | + def get_parser(self, prog_name): |
| 141 | + parser = super(ShowConsumer, self).get_parser(prog_name) |
| 142 | + parser.add_argument( |
| 143 | + 'consumer', |
| 144 | + metavar='<consumer>', |
| 145 | + help='ID of consumer to display', |
| 146 | + ) |
| 147 | + return parser |
| 148 | + |
| 149 | + def take_action(self, parsed_args): |
| 150 | + self.log.debug('take_action(%s)' % parsed_args) |
| 151 | + identity_client = self.app.client_manager.identity |
| 152 | + consumer = utils.find_resource( |
| 153 | + identity_client.consumers, parsed_args.consumer) |
| 154 | + |
| 155 | + info = {} |
| 156 | + info.update(consumer._info) |
| 157 | + return zip(*sorted(six.iteritems(info))) |
0 commit comments