diff options
| author | Josh Kearney <josh@jk0.org> | 2012-12-26 14:22:23 -0600 |
|---|---|---|
| committer | Josh Kearney <josh@jk0.org> | 2013-01-16 11:29:34 -0600 |
| commit | eb1ae2e9f23d618069e5eee7abedbd5a049d2878 (patch) | |
| tree | c296242e1f91256be03157bdab688b28dac46a26 /openstackclient | |
| parent | 67e413a224d8bec7729ec1aa703c6e4c91ee243b (diff) | |
| download | python-openstackclient-eb1ae2e9f23d618069e5eee7abedbd5a049d2878.tar.gz | |
Adds Glance API v2 support.
Change-Id: Ib0325e62a7e50aa94e852a73f9a2cb95daa8d5f6
Diffstat (limited to 'openstackclient')
| -rw-r--r-- | openstackclient/common/clientmanager.py | 4 | ||||
| -rw-r--r-- | openstackclient/common/utils.py | 2 | ||||
| -rw-r--r-- | openstackclient/image/client.py | 41 | ||||
| -rw-r--r-- | openstackclient/image/v2/__init__.py | 0 | ||||
| -rw-r--r-- | openstackclient/image/v2/image.py | 109 |
5 files changed, 154 insertions, 2 deletions
diff --git a/openstackclient/common/clientmanager.py b/openstackclient/common/clientmanager.py index 0a9f9102..73c2e570 100644 --- a/openstackclient/common/clientmanager.py +++ b/openstackclient/common/clientmanager.py @@ -23,6 +23,7 @@ import logging from openstackclient.common import exceptions as exc from openstackclient.compute import client as compute_client from openstackclient.identity import client as identity_client +from openstackclient.image import client as image_client LOG = logging.getLogger(__name__) @@ -46,8 +47,9 @@ class ClientManager(object): """Manages access to API clients, including authentication. """ - identity = ClientCache(identity_client.make_client) compute = ClientCache(compute_client.make_client) + identity = ClientCache(identity_client.make_client) + image = ClientCache(image_client.make_client) def __init__(self, token=None, url=None, auth_url=None, diff --git a/openstackclient/common/utils.py b/openstackclient/common/utils.py index ea8170bc..70555be5 100644 --- a/openstackclient/common/utils.py +++ b/openstackclient/common/utils.py @@ -125,6 +125,6 @@ def get_client_class(api_name, version, version_map): except (KeyError, ValueError): msg = "Invalid %s client version '%s'. must be one of: %s" % ( (api_name, version, ', '.join(version_map.keys()))) - raise exc.UnsupportedVersion(msg) + raise exceptions.UnsupportedVersion(msg) return import_class(client_path) diff --git a/openstackclient/image/client.py b/openstackclient/image/client.py new file mode 100644 index 00000000..0a7c576e --- /dev/null +++ b/openstackclient/image/client.py @@ -0,0 +1,41 @@ +# Copyright 2012 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. +# + +import logging + +from openstackclient.common import utils + + +LOG = logging.getLogger(__name__) + +API_NAME = "image" +API_VERSIONS = { + # FIXME(jk0): Temporary 1.0 -> 2 mapping. + "1.0": "glanceclient.v2.client.Client" +} + + +def make_client(instance): + """Returns an image service client.""" + image_client = utils.get_client_class( + API_NAME, + instance._api_version[API_NAME], + API_VERSIONS + ) + + if not instance._url: + instance._url = instance.get_endpoint_for_service_type(API_NAME) + + return image_client(instance._url, token=instance._token) diff --git a/openstackclient/image/v2/__init__.py b/openstackclient/image/v2/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/openstackclient/image/v2/__init__.py diff --git a/openstackclient/image/v2/image.py b/openstackclient/image/v2/image.py new file mode 100644 index 00000000..41520612 --- /dev/null +++ b/openstackclient/image/v2/image.py @@ -0,0 +1,109 @@ +# Copyright 2012 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. +# + +"""Image Action Implementations""" + +import logging + +from cliff import command +from cliff import lister + +from glanceclient.common import utils as gc_utils +from openstackclient.common import utils + + +class ListImage(lister.Lister): + """List image command""" + + api = "image" + log = logging.getLogger(__name__ + ".ListImage") + + def get_parser(self, prog_name): + parser = super(ListImage, self).get_parser(prog_name) + parser.add_argument( + "--page-size", + metavar="<size>", + help="Number of images to request in each paginated request.", + ) + return parser + + def take_action(self, parsed_args): + self.log.debug("take_action(%s)" % parsed_args) + image_client = self.app.client_manager.image + + kwargs = {} + if parsed_args.page_size is not None: + kwargs["page_size"] = parsed_args.page_size + + data = image_client.images.list(**kwargs) + columns = ["ID", "Name"] + + return (columns, + (utils.get_item_properties( + s, columns, + ) for s in data), + ) + + +class SaveImage(command.Command): + """Save image command""" + + api = "image" + log = logging.getLogger(__name__ + ".SaveImage") + + def get_parser(self, prog_name): + parser = super(SaveImage, self).get_parser(prog_name) + parser.add_argument( + "--file", + metavar="<file>", + help="Local file to save downloaded image data to. " + "If this is not specified the image data will be " + "written to stdout.", + ) + parser.add_argument( + "id", + metavar="<image_id>", + help="ID of image to describe.", + ) + return parser + + def take_action(self, parsed_args): + self.log.debug("take_action(%s)" % parsed_args) + image_client = self.app.client_manager.image + + data = image_client.images.data(parsed_args.id) + gc_utils.save_image(data, parsed_args.file) + + +class ShowImage(command.Command): + """Show image command""" + + api = "image" + log = logging.getLogger(__name__ + ".ShowImage") + + def get_parser(self, prog_name): + parser = super(ShowImage, self).get_parser(prog_name) + parser.add_argument( + "id", + metavar="<image_id>", + help="ID of image to describe.", + ) + return parser + + def take_action(self, parsed_args): + self.log.debug("take_action(%s)" % parsed_args) + image_client = self.app.client_manager.image + + gc_utils.print_dict(image_client.images.get(parsed_args.id)) |
