|
| 1 | +# Copyright 2012-2013 OpenStack, LLC. |
| 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 | +"""Volume v1 Type action implementations""" |
| 17 | + |
| 18 | +import logging |
| 19 | + |
| 20 | +from cliff import command |
| 21 | +from cliff import lister |
| 22 | +from cliff import show |
| 23 | + |
| 24 | +from openstackclient.common import utils |
| 25 | + |
| 26 | + |
| 27 | +class CreateType(show.ShowOne): |
| 28 | + """Create type command""" |
| 29 | + |
| 30 | + api = 'volume' |
| 31 | + log = logging.getLogger(__name__ + '.CreateType') |
| 32 | + |
| 33 | + def get_parser(self, prog_name): |
| 34 | + parser = super(CreateType, self).get_parser(prog_name) |
| 35 | + parser.add_argument( |
| 36 | + 'name', |
| 37 | + metavar='<name>', |
| 38 | + help='New type name', |
| 39 | + ) |
| 40 | + return parser |
| 41 | + |
| 42 | + def take_action(self, parsed_args): |
| 43 | + self.log.debug('take_action(%s)' % parsed_args) |
| 44 | + volume_client = self.app.client_manager.volume |
| 45 | + volume_type = volume_client.volume_types.create( |
| 46 | + parsed_args.name |
| 47 | + ) |
| 48 | + |
| 49 | + info = {} |
| 50 | + info.update(volume_type._info) |
| 51 | + return zip(*sorted(info.iteritems())) |
| 52 | + |
| 53 | + |
| 54 | +class DeleteType(command.Command): |
| 55 | + """Delete type command""" |
| 56 | + |
| 57 | + api = 'volume' |
| 58 | + log = logging.getLogger(__name__ + '.DeleteType') |
| 59 | + |
| 60 | + def get_parser(self, prog_name): |
| 61 | + parser = super(DeleteType, self).get_parser(prog_name) |
| 62 | + parser.add_argument( |
| 63 | + 'type', |
| 64 | + metavar='<type>', |
| 65 | + help='Name or ID of type to delete', |
| 66 | + ) |
| 67 | + return parser |
| 68 | + |
| 69 | + def take_action(self, parsed_args): |
| 70 | + self.log.debug('take_action(%s)' % parsed_args) |
| 71 | + volume_client = self.app.client_manager.volume |
| 72 | + volume_type = utils.find_resource( |
| 73 | + volume_client.volume_types, parsed_args.type) |
| 74 | + volume_client.volume_types.delete(volume_type.id) |
| 75 | + return |
| 76 | + |
| 77 | + |
| 78 | +class ListType(lister.Lister): |
| 79 | + """List type command""" |
| 80 | + |
| 81 | + api = 'volume' |
| 82 | + log = logging.getLogger(__name__ + '.ListType') |
| 83 | + |
| 84 | + def take_action(self, parsed_args): |
| 85 | + self.log.debug('take_action(%s)' % parsed_args) |
| 86 | + columns = ('ID', 'Name') |
| 87 | + data = self.app.client_manager.volume.volume_types.list() |
| 88 | + return (columns, |
| 89 | + (utils.get_item_properties( |
| 90 | + s, columns, |
| 91 | + formatters={}, |
| 92 | + ) for s in data)) |
0 commit comments