diff --git a/openstackclient/volume/v1/volume.py b/openstackclient/volume/v1/volume.py new file mode 100644 index 0000000000..f2d0851078 --- /dev/null +++ b/openstackclient/volume/v1/volume.py @@ -0,0 +1,216 @@ +# Copyright 2012-2013 OpenStack, LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# + +"""Volume v1 Volume action implementations""" + +import logging +import sys + +from cliff import command +from cliff import lister +from cliff import show + +from openstackclient.common import utils + + +class CreateVolume(show.ShowOne): + """Create volume command""" + + api = 'volume' + log = logging.getLogger(__name__ + '.CreateVolume') + + def get_parser(self, prog_name): + parser = super(CreateVolume, self).get_parser(prog_name) + parser.add_argument( + 'name', + metavar='', + help='Name of the volume', + ) + parser.add_argument( + '--size', + metavar='', + required=True, + type=int, + help='New volume size', + ) + parser.add_argument( + '--snapshot-id', + metavar='', + help='ID of the snapshot', + ) + parser.add_argument( + '--description', + metavar='', + help='Description of the volume', + ) + parser.add_argument( + '--volume-type', + metavar='', + help='Type of volume', + ) + parser.add_argument( + '--user-id', + metavar='', + help='User id derived from context', + ) + parser.add_argument( + '--project-id', + metavar='', + help='Project id derived from context', + ) + parser.add_argument( + '--availability-zone', + metavar='', + help='Availability Zone to use', + ) + parser.add_argument( + '--metadata', + metavar='', + help='Optional metadata to set on volume creation', + ) + parser.add_argument( + '--image-ref', + metavar='', + help='reference to an image stored in glance', + ) + parser.add_argument( + '--source-volid', + metavar='', + help='ID of source volume to clone from', + ) + + return parser + + def take_action(self, parsed_args): + self.log.debug('take_action(%s)' % parsed_args) + + volume_client = self.app.client_manager.volume + volume = volume_client.volumes.create( + parsed_args.size, + parsed_args.snapshot_id, + parsed_args.source_volid, + parsed_args.name, + parsed_args.description, + parsed_args.volume_type, + parsed_args.user_id, + parsed_args.project_id, + parsed_args.availability_zone, + parsed_args.metadata, + parsed_args.image_ref + ) + + return zip(*sorted(volume._info.iteritems())) + + +class DeleteVolume(command.Command): + """Delete volume command""" + + api = 'volume' + log = logging.getLogger(__name__ + '.DeleteVolume') + + def get_parser(self, prog_name): + parser = super(DeleteVolume, self).get_parser(prog_name) + parser.add_argument( + 'volume', + metavar='', + help='ID of volume to delete', + ) + return parser + + def take_action(self, parsed_args): + self.log.debug('take_action(%s)' % parsed_args) + volume_client = self.app.client_manager.volume + volume = utils.find_resource( + volume_client.volumes, parsed_args.volume) + volume_client.volumes.delete(volume.id) + return + + +class ListVolume(lister.Lister): + """List volume command""" + + api = 'volume' + log = logging.getLogger(__name__ + '.ListVolume') + + def take_action(self, parsed_args): + self.log.debug('take_action(%s)' % parsed_args) + columns = ('ID', 'Status', 'Display Name', 'Size', 'Volume Type') + data = self.app.client_manager.volume.volumes.list() + return (columns, + (utils.get_item_properties( + s, columns, + formatters={}, + ) for s in data)) + + +class SetVolume(command.Command): + """Set volume command""" + + api = 'volume' + log = logging.getLogger(__name__ + '.SetVolume') + + def get_parser(self, prog_name): + parser = super(SetVolume, self).get_parser(prog_name) + parser.add_argument( + 'volume', + metavar='', + help='ID of volume to change') + parser.add_argument( + '--name', + metavar='', + help='New volume name') + parser.add_argument( + '--description', + metavar='', + help='New volume description') + return parser + + def take_action(self, parsed_args): + self.log.debug('take_action(%s)' % parsed_args) + volume_client = self.app.client_manager.volume + volume = utils.find_resource(volume_client.volumes, parsed_args.volume) + kwargs = {} + if parsed_args.name: + kwargs['display_name'] = parsed_args.name + if parsed_args.description: + kwargs['display_description'] = parsed_args.description + + if not kwargs: + sys.stdout.write("Volume not updated, no arguments present \n") + return + volume_client.volumes.update(volume.id, **kwargs) + return + + +class ShowVolume(show.ShowOne): + """Show volume command""" + + api = 'volume' + log = logging.getLogger(__name__ + '.ShowVolume') + + def get_parser(self, prog_name): + parser = super(ShowVolume, self).get_parser(prog_name) + parser.add_argument( + 'volume', + metavar='', + help='ID of volume to display') + return parser + + def take_action(self, parsed_args): + self.log.debug('take_action(%s)' % parsed_args) + volume_client = self.app.client_manager.volume + volume = utils.find_resource(volume_client.volumes, parsed_args.volume) + + return zip(*sorted(volume._info.iteritems())) diff --git a/setup.py b/setup.py index e4ea3f93d3..922d103bbd 100644 --- a/setup.py +++ b/setup.py @@ -156,6 +156,11 @@ setuptools.setup( 'show_quota=openstackclient.volume.v1.quota:ShowQuota', 'list_quota=openstackclient.volume.v1.quota:ListQuota', 'set_quota=openstackclient.volume.v1.quota:SetQuota', + 'create_volume=openstackclient.volume.v1.volume:CreateVolume', + 'delete_volume=openstackclient.volume.v1.volume:DeleteVolume', + 'list_volume=openstackclient.volume.v1.volume:ListVolume', + 'set_volume=openstackclient.volume.v1.volume:SetVolume', + 'show_volume=openstackclient.volume.v1.volume:ShowVolume', ] } )