|
| 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 Volume action implementations""" |
| 17 | + |
| 18 | +import logging |
| 19 | +import sys |
| 20 | + |
| 21 | +from cliff import command |
| 22 | +from cliff import lister |
| 23 | +from cliff import show |
| 24 | + |
| 25 | +from openstackclient.common import utils |
| 26 | + |
| 27 | + |
| 28 | +class CreateVolume(show.ShowOne): |
| 29 | + """Create volume command""" |
| 30 | + |
| 31 | + api = 'volume' |
| 32 | + log = logging.getLogger(__name__ + '.CreateVolume') |
| 33 | + |
| 34 | + def get_parser(self, prog_name): |
| 35 | + parser = super(CreateVolume, self).get_parser(prog_name) |
| 36 | + parser.add_argument( |
| 37 | + 'name', |
| 38 | + metavar='<name>', |
| 39 | + help='Name of the volume', |
| 40 | + ) |
| 41 | + parser.add_argument( |
| 42 | + '--size', |
| 43 | + metavar='<size>', |
| 44 | + required=True, |
| 45 | + type=int, |
| 46 | + help='New volume size', |
| 47 | + ) |
| 48 | + parser.add_argument( |
| 49 | + '--snapshot-id', |
| 50 | + metavar='<snapshot-id>', |
| 51 | + help='ID of the snapshot', |
| 52 | + ) |
| 53 | + parser.add_argument( |
| 54 | + '--description', |
| 55 | + metavar='<description>', |
| 56 | + help='Description of the volume', |
| 57 | + ) |
| 58 | + parser.add_argument( |
| 59 | + '--volume-type', |
| 60 | + metavar='<volume-type>', |
| 61 | + help='Type of volume', |
| 62 | + ) |
| 63 | + parser.add_argument( |
| 64 | + '--user-id', |
| 65 | + metavar='<user-id>', |
| 66 | + help='User id derived from context', |
| 67 | + ) |
| 68 | + parser.add_argument( |
| 69 | + '--project-id', |
| 70 | + metavar='<project-id>', |
| 71 | + help='Project id derived from context', |
| 72 | + ) |
| 73 | + parser.add_argument( |
| 74 | + '--availability-zone', |
| 75 | + metavar='<availability-zone>', |
| 76 | + help='Availability Zone to use', |
| 77 | + ) |
| 78 | + parser.add_argument( |
| 79 | + '--metadata', |
| 80 | + metavar='<metadata>', |
| 81 | + help='Optional metadata to set on volume creation', |
| 82 | + ) |
| 83 | + parser.add_argument( |
| 84 | + '--image-ref', |
| 85 | + metavar='<image-ref>', |
| 86 | + help='reference to an image stored in glance', |
| 87 | + ) |
| 88 | + parser.add_argument( |
| 89 | + '--source-volid', |
| 90 | + metavar='<source-volid>', |
| 91 | + help='ID of source volume to clone from', |
| 92 | + ) |
| 93 | + |
| 94 | + return parser |
| 95 | + |
| 96 | + def take_action(self, parsed_args): |
| 97 | + self.log.debug('take_action(%s)' % parsed_args) |
| 98 | + |
| 99 | + volume_client = self.app.client_manager.volume |
| 100 | + volume = volume_client.volumes.create( |
| 101 | + parsed_args.size, |
| 102 | + parsed_args.snapshot_id, |
| 103 | + parsed_args.source_volid, |
| 104 | + parsed_args.name, |
| 105 | + parsed_args.description, |
| 106 | + parsed_args.volume_type, |
| 107 | + parsed_args.user_id, |
| 108 | + parsed_args.project_id, |
| 109 | + parsed_args.availability_zone, |
| 110 | + parsed_args.metadata, |
| 111 | + parsed_args.image_ref |
| 112 | + ) |
| 113 | + |
| 114 | + return zip(*sorted(volume._info.iteritems())) |
| 115 | + |
| 116 | + |
| 117 | +class DeleteVolume(command.Command): |
| 118 | + """Delete volume command""" |
| 119 | + |
| 120 | + api = 'volume' |
| 121 | + log = logging.getLogger(__name__ + '.DeleteVolume') |
| 122 | + |
| 123 | + def get_parser(self, prog_name): |
| 124 | + parser = super(DeleteVolume, self).get_parser(prog_name) |
| 125 | + parser.add_argument( |
| 126 | + 'volume', |
| 127 | + metavar='<volume>', |
| 128 | + help='ID of volume to delete', |
| 129 | + ) |
| 130 | + return parser |
| 131 | + |
| 132 | + def take_action(self, parsed_args): |
| 133 | + self.log.debug('take_action(%s)' % parsed_args) |
| 134 | + volume_client = self.app.client_manager.volume |
| 135 | + volume = utils.find_resource( |
| 136 | + volume_client.volumes, parsed_args.volume) |
| 137 | + volume_client.volumes.delete(volume.id) |
| 138 | + return |
| 139 | + |
| 140 | + |
| 141 | +class ListVolume(lister.Lister): |
| 142 | + """List volume command""" |
| 143 | + |
| 144 | + api = 'volume' |
| 145 | + log = logging.getLogger(__name__ + '.ListVolume') |
| 146 | + |
| 147 | + def take_action(self, parsed_args): |
| 148 | + self.log.debug('take_action(%s)' % parsed_args) |
| 149 | + columns = ('ID', 'Status', 'Display Name', 'Size', 'Volume Type') |
| 150 | + data = self.app.client_manager.volume.volumes.list() |
| 151 | + return (columns, |
| 152 | + (utils.get_item_properties( |
| 153 | + s, columns, |
| 154 | + formatters={}, |
| 155 | + ) for s in data)) |
| 156 | + |
| 157 | + |
| 158 | +class SetVolume(command.Command): |
| 159 | + """Set volume command""" |
| 160 | + |
| 161 | + api = 'volume' |
| 162 | + log = logging.getLogger(__name__ + '.SetVolume') |
| 163 | + |
| 164 | + def get_parser(self, prog_name): |
| 165 | + parser = super(SetVolume, self).get_parser(prog_name) |
| 166 | + parser.add_argument( |
| 167 | + 'volume', |
| 168 | + metavar='<volume>', |
| 169 | + help='ID of volume to change') |
| 170 | + parser.add_argument( |
| 171 | + '--name', |
| 172 | + metavar='<new-volume-name>', |
| 173 | + help='New volume name') |
| 174 | + parser.add_argument( |
| 175 | + '--description', |
| 176 | + metavar='<volume-description>', |
| 177 | + help='New volume description') |
| 178 | + return parser |
| 179 | + |
| 180 | + def take_action(self, parsed_args): |
| 181 | + self.log.debug('take_action(%s)' % parsed_args) |
| 182 | + volume_client = self.app.client_manager.volume |
| 183 | + volume = utils.find_resource(volume_client.volumes, parsed_args.volume) |
| 184 | + kwargs = {} |
| 185 | + if parsed_args.name: |
| 186 | + kwargs['display_name'] = parsed_args.name |
| 187 | + if parsed_args.description: |
| 188 | + kwargs['display_description'] = parsed_args.description |
| 189 | + |
| 190 | + if not kwargs: |
| 191 | + sys.stdout.write("Volume not updated, no arguments present \n") |
| 192 | + return |
| 193 | + volume_client.volumes.update(volume.id, **kwargs) |
| 194 | + return |
| 195 | + |
| 196 | + |
| 197 | +class ShowVolume(show.ShowOne): |
| 198 | + """Show volume command""" |
| 199 | + |
| 200 | + api = 'volume' |
| 201 | + log = logging.getLogger(__name__ + '.ShowVolume') |
| 202 | + |
| 203 | + def get_parser(self, prog_name): |
| 204 | + parser = super(ShowVolume, self).get_parser(prog_name) |
| 205 | + parser.add_argument( |
| 206 | + 'volume', |
| 207 | + metavar='<volume>', |
| 208 | + help='ID of volume to display') |
| 209 | + return parser |
| 210 | + |
| 211 | + def take_action(self, parsed_args): |
| 212 | + self.log.debug('take_action(%s)' % parsed_args) |
| 213 | + volume_client = self.app.client_manager.volume |
| 214 | + volume = utils.find_resource(volume_client.volumes, parsed_args.volume) |
| 215 | + |
| 216 | + return zip(*sorted(volume._info.iteritems())) |
0 commit comments