Skip to content

Commit 653d52e

Browse files
committed
Add support to get glance endpoint
This patch will help to get glance endpoint form service catalog based on input task. Partially implements: blueprint glance-download-import Co-Authored-By: Victor Coutellier <[email protected]> Co-Authored-By: Pierre-Samuel Le Stang <[email protected]> Change-Id: Ib4f19351ca7985474d838998b402d3c5f9fb195f
1 parent cb60c1d commit 653d52e

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

glance/async_/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from oslo_utils import units
2020
from taskflow import task
2121

22+
from glance.common import exception as glance_exception
2223
from glance.i18n import _LW
2324

2425

@@ -77,3 +78,20 @@ def wrapper(*args, **kwargs):
7778
encodeutils.exception_to_unicode(exc))
7879
LOG.warning(msg)
7980
return wrapper
81+
82+
83+
def get_glance_endpoint(context, region, interface):
84+
"""Return glance endpoint depending the input tasks
85+
86+
"""
87+
# We use the current context to retrieve the image
88+
catalog = context.service_catalog
89+
90+
for service in catalog:
91+
if service['type'] == 'image':
92+
for endpoint in service['endpoints']:
93+
if endpoint['region'].lower() == region.lower():
94+
return endpoint.get('%sURL' % interface)
95+
96+
raise glance_exception.GlanceEndpointNotFound(region=region,
97+
interface=interface)

glance/common/exception.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,3 +453,8 @@ class InvalidDataMigrationScript(GlanceException):
453453
message = _("Invalid data migration script '%(script)s'. A valid data "
454454
"migration script must implement functions 'has_migrations' "
455455
"and 'migrate'.")
456+
457+
458+
class GlanceEndpointNotFound(NotFound):
459+
message = _("%(interface)s glance endpoint not "
460+
"found for region %(region)s")
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright 2022 OVHcloud
2+
# All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
# not use this file except in compliance with the License. You may obtain
6+
# a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations
14+
# under the License.
15+
16+
17+
from unittest import mock
18+
19+
from glance.async_ import utils
20+
import glance.common.exception
21+
from glance.tests.unit import base
22+
23+
24+
class TestGetGlanceEndpoint(base.IsolatedUnitTest):
25+
26+
def setUp(self):
27+
super(TestGetGlanceEndpoint, self).setUp()
28+
29+
self.service_catalog = [
30+
{
31+
'endpoints': [
32+
{
33+
'adminURL': 'http://localhost:8080/',
34+
'region': 'RegionOne',
35+
'internalURL': 'http://internalURL/',
36+
'publicURL': 'http://publicURL/',
37+
},
38+
],
39+
'type': 'object-store',
40+
},
41+
{
42+
'endpoints': [
43+
{
44+
'adminURL': 'http://localhost:8080/',
45+
'region': 'RegionOne',
46+
'internalURL': 'http://RegionOneInternal/',
47+
'publicURL': 'http://RegionOnePublic/',
48+
},
49+
],
50+
'type': 'image',
51+
},
52+
{
53+
'endpoints': [
54+
{
55+
'adminURL': 'http://localhost:8080/',
56+
'region': 'RegionTwo',
57+
'internalURL': 'http://RegionTwoInternal/',
58+
'publicURL': 'http://RegionTwoPublic/',
59+
},
60+
],
61+
'type': 'image',
62+
}
63+
]
64+
65+
self.context = mock.MagicMock(service_catalog=self.service_catalog)
66+
67+
def test_return_matching_glance_endpoint(self):
68+
self.assertEqual(utils.get_glance_endpoint(self.context,
69+
'RegionOne',
70+
'public'),
71+
'http://RegionOnePublic/')
72+
self.assertEqual(utils.get_glance_endpoint(self.context,
73+
'RegionTwo',
74+
'internal'),
75+
'http://RegionTwoInternal/')
76+
77+
def test_glance_endpoint_not_found(self):
78+
self.assertRaises(glance.common.exception.GlanceEndpointNotFound,
79+
utils.get_glance_endpoint, self.context,
80+
'RegionThree', 'public')

0 commit comments

Comments
 (0)