|
| 1 | +# Copyright 2012 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 | +"""Image Action Implementations""" |
| 17 | + |
| 18 | +import logging |
| 19 | + |
| 20 | +from cliff import command |
| 21 | +from cliff import lister |
| 22 | + |
| 23 | +from glanceclient.common import utils as gc_utils |
| 24 | +from openstackclient.common import utils |
| 25 | + |
| 26 | + |
| 27 | +class ListImage(lister.Lister): |
| 28 | + """List image command""" |
| 29 | + |
| 30 | + api = "image" |
| 31 | + log = logging.getLogger(__name__ + ".ListImage") |
| 32 | + |
| 33 | + def get_parser(self, prog_name): |
| 34 | + parser = super(ListImage, self).get_parser(prog_name) |
| 35 | + parser.add_argument( |
| 36 | + "--page-size", |
| 37 | + metavar="<size>", |
| 38 | + help="Number of images to request in each paginated request.", |
| 39 | + ) |
| 40 | + return parser |
| 41 | + |
| 42 | + def take_action(self, parsed_args): |
| 43 | + self.log.debug("take_action(%s)" % parsed_args) |
| 44 | + image_client = self.app.client_manager.image |
| 45 | + |
| 46 | + kwargs = {} |
| 47 | + if parsed_args.page_size is not None: |
| 48 | + kwargs["page_size"] = parsed_args.page_size |
| 49 | + |
| 50 | + data = image_client.images.list(**kwargs) |
| 51 | + columns = ["ID", "Name"] |
| 52 | + |
| 53 | + return (columns, |
| 54 | + (utils.get_item_properties( |
| 55 | + s, columns, |
| 56 | + ) for s in data), |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +class SaveImage(command.Command): |
| 61 | + """Save image command""" |
| 62 | + |
| 63 | + api = "image" |
| 64 | + log = logging.getLogger(__name__ + ".SaveImage") |
| 65 | + |
| 66 | + def get_parser(self, prog_name): |
| 67 | + parser = super(SaveImage, self).get_parser(prog_name) |
| 68 | + parser.add_argument( |
| 69 | + "--file", |
| 70 | + metavar="<file>", |
| 71 | + help="Local file to save downloaded image data to. " |
| 72 | + "If this is not specified the image data will be " |
| 73 | + "written to stdout.", |
| 74 | + ) |
| 75 | + parser.add_argument( |
| 76 | + "id", |
| 77 | + metavar="<image_id>", |
| 78 | + help="ID of image to describe.", |
| 79 | + ) |
| 80 | + return parser |
| 81 | + |
| 82 | + def take_action(self, parsed_args): |
| 83 | + self.log.debug("take_action(%s)" % parsed_args) |
| 84 | + image_client = self.app.client_manager.image |
| 85 | + |
| 86 | + data = image_client.images.data(parsed_args.id) |
| 87 | + gc_utils.save_image(data, parsed_args.file) |
| 88 | + |
| 89 | + |
| 90 | +class ShowImage(command.Command): |
| 91 | + """Show image command""" |
| 92 | + |
| 93 | + api = "image" |
| 94 | + log = logging.getLogger(__name__ + ".ShowImage") |
| 95 | + |
| 96 | + def get_parser(self, prog_name): |
| 97 | + parser = super(ShowImage, self).get_parser(prog_name) |
| 98 | + parser.add_argument( |
| 99 | + "id", |
| 100 | + metavar="<image_id>", |
| 101 | + help="ID of image to describe.", |
| 102 | + ) |
| 103 | + return parser |
| 104 | + |
| 105 | + def take_action(self, parsed_args): |
| 106 | + self.log.debug("take_action(%s)" % parsed_args) |
| 107 | + image_client = self.app.client_manager.image |
| 108 | + |
| 109 | + gc_utils.print_dict(image_client.images.get(parsed_args.id)) |
0 commit comments