|
| 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 Snapshot 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 CreateSnapshot(show.ShowOne): |
| 29 | + """Create snapshot command""" |
| 30 | + |
| 31 | + api = 'volume' |
| 32 | + log = logging.getLogger(__name__ + '.CreateSnapshot') |
| 33 | + |
| 34 | + def get_parser(self, prog_name): |
| 35 | + parser = super(CreateSnapshot, self).get_parser(prog_name) |
| 36 | + parser.add_argument( |
| 37 | + 'volume', |
| 38 | + metavar='<volume>', |
| 39 | + help='The name or ID of the volume to snapshot', |
| 40 | + ) |
| 41 | + parser.add_argument( |
| 42 | + '--name', |
| 43 | + metavar='<name>', |
| 44 | + required=True, |
| 45 | + help='Name of the snapshot', |
| 46 | + ) |
| 47 | + parser.add_argument( |
| 48 | + '--description', |
| 49 | + metavar='<description>', |
| 50 | + help='Description of the snapshot', |
| 51 | + ) |
| 52 | + parser.add_argument( |
| 53 | + '--force', |
| 54 | + dest='force', |
| 55 | + action='store_true', |
| 56 | + default=False, |
| 57 | + help='Create a snapshot attached to an instance. Default is False', |
| 58 | + ) |
| 59 | + return parser |
| 60 | + |
| 61 | + def take_action(self, parsed_args): |
| 62 | + self.log.debug('take_action(%s)' % parsed_args) |
| 63 | + volume_client = self.app.client_manager.volume |
| 64 | + volume_id = utils.find_resource(volume_client.volumes, |
| 65 | + parsed_args.volume).id |
| 66 | + snapshot = volume_client.volume_snapshots.create( |
| 67 | + volume_id, |
| 68 | + parsed_args.force, |
| 69 | + parsed_args.name, |
| 70 | + parsed_args.description |
| 71 | + ) |
| 72 | + |
| 73 | + return zip(*sorted(snapshot._info.iteritems())) |
| 74 | + |
| 75 | + |
| 76 | +class DeleteSnapshot(command.Command): |
| 77 | + """Delete snapshot command""" |
| 78 | + |
| 79 | + api = 'volume' |
| 80 | + log = logging.getLogger(__name__ + '.DeleteSnapshot') |
| 81 | + |
| 82 | + def get_parser(self, prog_name): |
| 83 | + parser = super(DeleteSnapshot, self).get_parser(prog_name) |
| 84 | + parser.add_argument( |
| 85 | + 'snapshot', |
| 86 | + metavar='<snapshot>', |
| 87 | + help='Name or ID of snapshot to delete', |
| 88 | + ) |
| 89 | + return parser |
| 90 | + |
| 91 | + def take_action(self, parsed_args): |
| 92 | + self.log.debug('take_action(%s)' % parsed_args) |
| 93 | + volume_client = self.app.client_manager.volume |
| 94 | + snapshot_id = utils.find_resource(volume_client.volume_snapshots, |
| 95 | + parsed_args.snapshot).id |
| 96 | + volume_client.volume_snapshots.delete(snapshot_id) |
| 97 | + return |
| 98 | + |
| 99 | + |
| 100 | +class ListSnapshot(lister.Lister): |
| 101 | + """List snapshot command""" |
| 102 | + |
| 103 | + api = 'volume' |
| 104 | + log = logging.getLogger(__name__ + '.ListSnapshot') |
| 105 | + |
| 106 | + def take_action(self, parsed_args): |
| 107 | + self.log.debug('take_action(%s)' % parsed_args) |
| 108 | + columns = ( |
| 109 | + 'ID', |
| 110 | + 'Display Name', |
| 111 | + 'Display Description', |
| 112 | + 'Status', |
| 113 | + 'Size' |
| 114 | + ) |
| 115 | + data = self.app.client_manager.volume.volume_snapshots.list() |
| 116 | + return (columns, |
| 117 | + (utils.get_item_properties( |
| 118 | + s, columns, |
| 119 | + formatters={}, |
| 120 | + ) for s in data)) |
| 121 | + |
| 122 | + |
| 123 | +class SetSnapshot(command.Command): |
| 124 | + """Set snapshot command""" |
| 125 | + |
| 126 | + api = 'volume' |
| 127 | + log = logging.getLogger(__name__ + '.SetSnapshot') |
| 128 | + |
| 129 | + def get_parser(self, prog_name): |
| 130 | + parser = super(SetSnapshot, self).get_parser(prog_name) |
| 131 | + parser.add_argument( |
| 132 | + 'snapshot', |
| 133 | + metavar='<snapshot>', |
| 134 | + help='Name or ID of snapshot to change') |
| 135 | + parser.add_argument( |
| 136 | + '--name', |
| 137 | + metavar='<new-snapshot-name>', |
| 138 | + help='New snapshot name') |
| 139 | + parser.add_argument( |
| 140 | + '--description', |
| 141 | + metavar='<new-snapshot-description>', |
| 142 | + help='New snapshot description') |
| 143 | + return parser |
| 144 | + |
| 145 | + def take_action(self, parsed_args): |
| 146 | + self.log.debug('take_action(%s)' % parsed_args) |
| 147 | + volume_client = self.app.client_manager.volume |
| 148 | + snapshot = utils.find_resource(volume_client.volume_snapshots, |
| 149 | + parsed_args.snapshot) |
| 150 | + kwargs = {} |
| 151 | + if parsed_args.name: |
| 152 | + kwargs['display_name'] = parsed_args.name |
| 153 | + if parsed_args.description: |
| 154 | + kwargs['display_description'] = parsed_args.description |
| 155 | + |
| 156 | + if not kwargs: |
| 157 | + sys.stdout.write("Snapshot not updated, no arguments present") |
| 158 | + return |
| 159 | + snapshot.update(**kwargs) |
| 160 | + return |
| 161 | + |
| 162 | + |
| 163 | +class ShowSnapshot(show.ShowOne): |
| 164 | + """Show snapshot command""" |
| 165 | + |
| 166 | + api = 'volume' |
| 167 | + log = logging.getLogger(__name__ + '.ShowSnapshot') |
| 168 | + |
| 169 | + def get_parser(self, prog_name): |
| 170 | + parser = super(ShowSnapshot, self).get_parser(prog_name) |
| 171 | + parser.add_argument( |
| 172 | + 'snapshot', |
| 173 | + metavar='<snapshot>', |
| 174 | + help='Name or ID of snapshot to display') |
| 175 | + return parser |
| 176 | + |
| 177 | + def take_action(self, parsed_args): |
| 178 | + self.log.debug('take_action(%s)' % parsed_args) |
| 179 | + volume_client = self.app.client_manager.volume |
| 180 | + snapshot = utils.find_resource(volume_client.volume_snapshots, |
| 181 | + parsed_args.snapshot) |
| 182 | + |
| 183 | + return zip(*sorted(snapshot._info.iteritems())) |
0 commit comments