Skip to content

Commit eb1ae2e

Browse files
committed
Adds Glance API v2 support.
Change-Id: Ib0325e62a7e50aa94e852a73f9a2cb95daa8d5f6
1 parent 67e413a commit eb1ae2e

6 files changed

Lines changed: 157 additions & 2 deletions

File tree

openstackclient/common/clientmanager.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from openstackclient.common import exceptions as exc
2424
from openstackclient.compute import client as compute_client
2525
from openstackclient.identity import client as identity_client
26+
from openstackclient.image import client as image_client
2627

2728
LOG = logging.getLogger(__name__)
2829

@@ -46,8 +47,9 @@ class ClientManager(object):
4647
"""Manages access to API clients, including authentication.
4748
"""
4849

49-
identity = ClientCache(identity_client.make_client)
5050
compute = ClientCache(compute_client.make_client)
51+
identity = ClientCache(identity_client.make_client)
52+
image = ClientCache(image_client.make_client)
5153

5254
def __init__(self, token=None, url=None,
5355
auth_url=None,

openstackclient/common/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,6 @@ def get_client_class(api_name, version, version_map):
125125
except (KeyError, ValueError):
126126
msg = "Invalid %s client version '%s'. must be one of: %s" % (
127127
(api_name, version, ', '.join(version_map.keys())))
128-
raise exc.UnsupportedVersion(msg)
128+
raise exceptions.UnsupportedVersion(msg)
129129

130130
return import_class(client_path)

openstackclient/image/client.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
import logging
17+
18+
from openstackclient.common import utils
19+
20+
21+
LOG = logging.getLogger(__name__)
22+
23+
API_NAME = "image"
24+
API_VERSIONS = {
25+
# FIXME(jk0): Temporary 1.0 -> 2 mapping.
26+
"1.0": "glanceclient.v2.client.Client"
27+
}
28+
29+
30+
def make_client(instance):
31+
"""Returns an image service client."""
32+
image_client = utils.get_client_class(
33+
API_NAME,
34+
instance._api_version[API_NAME],
35+
API_VERSIONS
36+
)
37+
38+
if not instance._url:
39+
instance._url = instance.get_endpoint_for_service_type(API_NAME)
40+
41+
return image_client(instance._url, token=instance._token)

openstackclient/image/v2/__init__.py

Whitespace-only changes.

openstackclient/image/v2/image.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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))

setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ def read(fname):
110110
'set_user=openstackclient.identity.v2_0.user:SetUser',
111111
'show_user=openstackclient.identity.v2_0.user:ShowUser',
112112
'list_user-role=openstackclient.identity.v2_0.role:ListUserRole',
113+
'list_image=openstackclient.image.v2.image:ListImage',
114+
'show_image=openstackclient.image.v2.image:ShowImage',
115+
'save_image=openstackclient.image.v2.image:SaveImage',
113116
]
114117
}
115118
)

0 commit comments

Comments
 (0)