# Copyright 2012 OpenStack LLC. # All Rights Reserved. # # 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. import copy import sys from glanceclient.common import utils import glanceclient.v1.images def do_image_list(gc, args): """List images.""" images = gc.images.list() columns = ['ID', 'Name', 'Status'] utils.print_list(images, columns) def _image_show(image): # Flatten image properties dict for display info = copy.deepcopy(image._info) for (k, v) in info.pop('properties').iteritems(): info['Property \'%s\'' % k] = v utils.print_dict(info) @utils.arg('id', metavar='', help='ID of image to describe.') def do_image_show(gc, args): """Describe a specific image.""" image = gc.images.get(args.id) _image_show(image) @utils.arg('--id', metavar='', help='ID of image to reserve.') @utils.arg('--name', metavar='', help='Name of image.') @utils.arg('--disk_format', metavar='', help='Disk format of image.') @utils.arg('--container_format', metavar='', help='Container format of image.') @utils.arg('--owner', metavar='', help='Tenant who should own image.') @utils.arg('--size', metavar='', help=('Size of image data (in bytes). Only used with' ' \'--location\' and \'--copy_from\'.')) @utils.arg('--min_disk', metavar='', help='Minimum size of disk needed to boot image (in gigabytes).') @utils.arg('--min_ram', metavar='', help='Minimum amount of ram needed to boot image (in megabytes).') @utils.arg('--location', metavar='', help=('URL where the data for this image already resides.' ' For example, if the image data is stored in the filesystem' ' local to the glance server at \'/usr/share/image.tar.gz\',' ' you would specify \'file:///usr/share/image.tar.gz\'.')) @utils.arg('--checksum', metavar='', help='Hash of image data used Glance can use for verification.') @utils.arg('--copy_from', metavar='', help=('Similar to \'--location\' in usage, but this indicates that' ' the Glance server should immediately copy the data and' ' store it in its configured image store.')) @utils.arg('--public', action='store_true', default=False, help='Make image accessible to the public.') @utils.arg('--protected', action='store_true', default=False, help='Prevent image from being deleted.') @utils.arg('--property', metavar="", action='append', default=[], help=("Arbitrary property to associate with image. " "May be used multiple times.")) def do_image_create(gc, args): # Filter out None values fields = dict(filter(lambda x: x[1] is not None, vars(args).items())) fields['is_public'] = fields.pop('public') raw_properties = fields.pop('property') fields['properties'] = {} for datum in raw_properties: key, value = datum.split('=', 1) fields['properties'][key] = value # Filter out values we can't use CREATE_PARAMS = glanceclient.v1.images.CREATE_PARAMS fields = dict(filter(lambda x: x[0] in CREATE_PARAMS, fields.items())) if 'location' not in fields and 'copy_from' not in fields: fields['data'] = sys.stdin image = gc.images.create(**fields) _image_show(image) @utils.arg('id', metavar='', help='ID of image to modify.') @utils.arg('--name', metavar='', help='Name of image.') @utils.arg('--disk_format', metavar='', help='Disk format of image.') @utils.arg('--container_format', metavar='', help='Container format of image.') @utils.arg('--owner', metavar='', help='Tenant who should own image.') @utils.arg('--size', metavar='', help='Size of image data (in bytes).') @utils.arg('--min_disk', metavar='', help='Minimum size of disk needed to boot image (in gigabytes).') @utils.arg('--min_ram', metavar='', help='Minimum amount of ram needed to boot image (in megabytes).') @utils.arg('--location', metavar='', help=('URL where the data for this image already resides.' ' For example, if the image data is stored in the filesystem' ' local to the glance server at \'/usr/share/image.tar.gz\',' ' you would specify \'file:///usr/share/image.tar.gz\'.')) @utils.arg('--checksum', metavar='', help='Hash of image data used Glance can use for verification.') @utils.arg('--copy_from', metavar='', help=('Similar to \'--location\' in usage, but this indicates that' ' the Glance server should immediately copy the data and' ' store it in its configured image store.')) @utils.arg('--is_public', type=bool, help='Make image accessible to the public.') @utils.arg('--is_protected', type=bool, help='Prevent image from being deleted.') @utils.arg('--property', metavar="", action='append', default=[], help=("Arbitrary property to associate with image. " "May be used multiple times.")) def do_image_update(gc, args): # Filter out None values fields = dict(filter(lambda x: x[1] is not None, vars(args).items())) image_id = fields.pop('id') raw_properties = fields.pop('property') fields['properties'] = {} for datum in raw_properties: key, value = datum.split('=', 1) fields['properties'][key] = value # Filter out values we can't use UPDATE_PARAMS = glanceclient.v1.images.UPDATE_PARAMS fields = dict(filter(lambda x: x[0] in UPDATE_PARAMS, fields.items())) if 'location' not in fields and 'copy_from' not in fields: fields['data'] = sys.stdin image = gc.images.update(image_id, **fields) _image_show(image) @utils.arg('id', metavar='', help='ID of image to delete.') def do_image_delete(gc, args): """Delete a specific image.""" gc.images.delete(args.id)