Skip to content

Commit 60e7c51

Browse files
gtemaemonty
authored andcommitted
Switch image to use SDK
This is a work to switch OSC from using glanceclient to OpenStackSDK. With this change only v2 is using OpenStackSDK. V1 is still using glanceclient and will be switched in a separate change. Remove the direct depend on keystoneauth- let that flow through openstacksdk. Depends-on: https://review.opendev.org/#/c/698972 Change-Id: I36f292fb70c98f6e558f58be55d533d979c47ca7
1 parent fc12033 commit 60e7c51

16 files changed

Lines changed: 660 additions & 558 deletions

File tree

lower-constraints.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jmespath==0.9.0
3838
jsonpatch==1.16
3939
jsonpointer==1.13
4040
jsonschema==2.6.0
41-
keystoneauth1==3.14.0
41+
keystoneauth1==3.16.0
4242
kombu==4.0.0
4343
linecache2==1.0.0
4444
MarkupSafe==1.0
@@ -50,9 +50,9 @@ msgpack-python==0.4.0
5050
munch==2.1.0
5151
netaddr==0.7.18
5252
netifaces==0.10.4
53-
openstacksdk==0.17.0
53+
openstacksdk==0.36.0
5454
os-client-config==1.28.0
55-
os-service-types==1.2.0
55+
os-service-types==1.7.0
5656
os-testr==1.0.0
5757
osc-lib==2.0.0
5858
osc-placement==1.7.0
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
import six
14+
15+
16+
def get_osc_show_columns_for_sdk_resource(
17+
sdk_resource,
18+
osc_column_map,
19+
invisible_columns=None
20+
):
21+
"""Get and filter the display and attribute columns for an SDK resource.
22+
23+
Common utility function for preparing the output of an OSC show command.
24+
Some of the columns may need to get renamed, others made invisible.
25+
26+
:param sdk_resource: An SDK resource
27+
:param osc_column_map: A hash of mappings for display column names
28+
:param invisible_columns: A list of invisible column names
29+
30+
:returns: Two tuples containing the names of the display and attribute
31+
columns
32+
"""
33+
34+
if getattr(sdk_resource, 'allow_get', None) is not None:
35+
resource_dict = sdk_resource.to_dict(
36+
body=True, headers=False, ignore_none=False)
37+
else:
38+
resource_dict = sdk_resource
39+
40+
# Build the OSC column names to display for the SDK resource.
41+
attr_map = {}
42+
display_columns = list(resource_dict.keys())
43+
invisible_columns = [] if invisible_columns is None else invisible_columns
44+
for col_name in invisible_columns:
45+
if col_name in display_columns:
46+
display_columns.remove(col_name)
47+
for sdk_attr, osc_attr in six.iteritems(osc_column_map):
48+
if sdk_attr in display_columns:
49+
attr_map[osc_attr] = sdk_attr
50+
display_columns.remove(sdk_attr)
51+
if osc_attr not in display_columns:
52+
display_columns.append(osc_attr)
53+
sorted_display_columns = sorted(display_columns)
54+
55+
# Build the SDK attribute names for the OSC column names.
56+
attr_columns = []
57+
for column in sorted_display_columns:
58+
new_column = attr_map[column] if column in attr_map else column
59+
attr_columns.append(new_column)
60+
return tuple(sorted_display_columns), tuple(attr_columns)

openstackclient/compute/v2/server.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def _prep_server_detail(compute_client, image_client, server, refresh=True):
143143
if image_info:
144144
image_id = image_info.get('id', '')
145145
try:
146-
image = utils.find_resource(image_client.images, image_id)
146+
image = image_client.get_image(image_id)
147147
info['image'] = "%s (%s)" % (image.name, image_id)
148148
except Exception:
149149
info['image'] = image_id
@@ -735,10 +735,8 @@ def _show_progress(progress):
735735
# Lookup parsed_args.image
736736
image = None
737737
if parsed_args.image:
738-
image = utils.find_resource(
739-
image_client.images,
740-
parsed_args.image,
741-
)
738+
image = image_client.find_image(
739+
parsed_args.image, ignore_missing=False)
742740

743741
if not image and parsed_args.image_property:
744742
def emit_duplicated_warning(img, image_property):
@@ -749,7 +747,7 @@ def emit_duplicated_warning(img, image_property):
749747
'chosen_one': img_uuid_list[0]})
750748

751749
def _match_image(image_api, wanted_properties):
752-
image_list = image_api.image_list()
750+
image_list = image_api.images()
753751
images_matched = []
754752
for img in image_list:
755753
img_dict = {}
@@ -768,7 +766,7 @@ def _match_image(image_api, wanted_properties):
768766
return []
769767
return images_matched
770768

771-
images = _match_image(image_client.api, parsed_args.image_property)
769+
images = _match_image(image_client, parsed_args.image_property)
772770
if len(images) > 1:
773771
emit_duplicated_warning(images,
774772
parsed_args.image_property)
@@ -890,8 +888,8 @@ def _match_image(image_api, wanted_properties):
890888
# one specified by --image, then the compute service will
891889
# create a volume from the image and attach it to the
892890
# server as a non-root volume.
893-
image_id = utils.find_resource(
894-
image_client.images, dev_map[0]).id
891+
image_id = image_client.find_image(dev_map[0],
892+
ignore_missing=False).id
895893
mapping['uuid'] = image_id
896894
# 3. append size and delete_on_termination if exist
897895
if len(dev_map) > 2 and dev_map[2]:
@@ -1324,8 +1322,8 @@ def take_action(self, parsed_args):
13241322
# image name is given, map it to ID.
13251323
image_id = None
13261324
if parsed_args.image:
1327-
image_id = utils.find_resource(image_client.images,
1328-
parsed_args.image).id
1325+
image_id = image_client.find_image(parsed_args.image,
1326+
ignore_missing=False).id
13291327

13301328
search_opts = {
13311329
'reservation_id': parsed_args.reservation_id,
@@ -1476,12 +1474,12 @@ def take_action(self, parsed_args):
14761474
(s.image.get('id') for s in data
14771475
if s.image))):
14781476
try:
1479-
images[i_id] = image_client.images.get(i_id)
1477+
images[i_id] = image_client.get_image(i_id)
14801478
except Exception:
14811479
pass
14821480
else:
14831481
try:
1484-
images_list = image_client.images.list()
1482+
images_list = image_client.images()
14851483
for i in images_list:
14861484
images[i.id] = i
14871485
except Exception:
@@ -1925,7 +1923,7 @@ def _show_progress(progress):
19251923
# If parsed_args.image is not set, default to the currently used one.
19261924
image_id = parsed_args.image or server.to_dict().get(
19271925
'image', {}).get('id')
1928-
image = utils.find_resource(image_client.images, image_id)
1926+
image = image_client.get_image(image_id)
19291927

19301928
kwargs = {}
19311929
if parsed_args.property:
@@ -2195,10 +2193,7 @@ def take_action(self, parsed_args):
21952193

21962194
image = None
21972195
if parsed_args.image:
2198-
image = utils.find_resource(
2199-
image_client.images,
2200-
parsed_args.image,
2201-
)
2196+
image = image_client.find_image(parsed_args.image)
22022197

22032198
utils.find_resource(
22042199
compute_client.servers,

openstackclient/compute/v2/server_backup.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,11 @@ def _show_progress(progress):
100100
)
101101

102102
image_client = self.app.client_manager.image
103-
image = utils.find_resource(
104-
image_client.images,
105-
backup_name,
106-
)
103+
image = image_client.find_image(backup_name, ignore_missing=False)
107104

108105
if parsed_args.wait:
109106
if utils.wait_for_status(
110-
image_client.images.get,
107+
image_client.get_image,
111108
image.id,
112109
callback=_show_progress,
113110
):

openstackclient/compute/v2/server_image.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,11 @@ def _show_progress(progress):
7979
)
8080

8181
image_client = self.app.client_manager.image
82-
image = utils.find_resource(
83-
image_client.images,
84-
image_id,
85-
)
82+
image = image_client.find_image(image_id)
8683

8784
if parsed_args.wait:
8885
if utils.wait_for_status(
89-
image_client.images.get,
86+
image_client.get_image,
9087
image_id,
9188
callback=_show_progress,
9289
):

openstackclient/image/client.py

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
API_NAME = "image"
2828
API_VERSIONS = {
2929
"1": "glanceclient.v1.client.Client",
30-
"2": "glanceclient.v2.client.Client",
30+
"2": "openstack.connection.Connection",
3131
}
3232

3333
IMAGE_API_TYPE = 'image'
@@ -38,44 +38,52 @@
3838

3939

4040
def make_client(instance):
41-
"""Returns an image service client"""
42-
image_client = utils.get_client_class(
43-
API_NAME,
44-
instance._api_version[API_NAME],
45-
API_VERSIONS)
46-
LOG.debug('Instantiating image client: %s', image_client)
47-
48-
endpoint = instance.get_endpoint_for_service_type(
49-
API_NAME,
50-
region_name=instance.region_name,
51-
interface=instance.interface,
52-
)
53-
54-
client = image_client(
55-
endpoint,
56-
token=instance.auth.get_token(instance.session),
57-
cacert=instance.cacert,
58-
insecure=not instance.verify,
59-
)
6041

61-
# Create the low-level API
62-
63-
image_api = utils.get_client_class(
64-
API_NAME,
65-
instance._api_version[API_NAME],
66-
IMAGE_API_VERSIONS)
67-
LOG.debug('Instantiating image api: %s', image_api)
68-
69-
client.api = image_api(
70-
session=instance.session,
71-
endpoint=instance.get_endpoint_for_service_type(
72-
IMAGE_API_TYPE,
42+
if instance._api_version[API_NAME] != '1':
43+
LOG.debug(
44+
'Image client initialized using OpenStack SDK: %s',
45+
instance.sdk_connection.image,
46+
)
47+
return instance.sdk_connection.image
48+
else:
49+
"""Returns an image service client"""
50+
image_client = utils.get_client_class(
51+
API_NAME,
52+
instance._api_version[API_NAME],
53+
API_VERSIONS)
54+
LOG.debug('Instantiating image client: %s', image_client)
55+
56+
endpoint = instance.get_endpoint_for_service_type(
57+
API_NAME,
7358
region_name=instance.region_name,
7459
interface=instance.interface,
7560
)
76-
)
7761

78-
return client
62+
client = image_client(
63+
endpoint,
64+
token=instance.auth.get_token(instance.session),
65+
cacert=instance.cacert,
66+
insecure=not instance.verify,
67+
)
68+
69+
# Create the low-level API
70+
71+
image_api = utils.get_client_class(
72+
API_NAME,
73+
instance._api_version[API_NAME],
74+
IMAGE_API_VERSIONS)
75+
LOG.debug('Instantiating image api: %s', image_api)
76+
77+
client.api = image_api(
78+
session=instance.session,
79+
endpoint=instance.get_endpoint_for_service_type(
80+
IMAGE_API_TYPE,
81+
region_name=instance.region_name,
82+
interface=instance.interface,
83+
)
84+
)
85+
86+
return client
7987

8088

8189
def build_option_parser(parser):

0 commit comments

Comments
 (0)