From e89a0ab3c6282d6364402e923bc9a850d253a5fc Mon Sep 17 00:00:00 2001 From: Lingxian Kong Date: Mon, 17 May 2021 12:32:58 +1200 Subject: [PATCH] Fix nova client initialization in multi-region cloud The current nova client initialization doesn't take regions into consideration so that the flavor used for creating trove instance is coming from other regions. Change-Id: Ia5a93a220c9ec86a23d281dc3604eeaa9c2d6673 (cherry-picked from 28e02161b0db8dad9d71a17bcb334fb9d7741dec) --- trove_dashboard/api/trove.py | 66 ++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/trove_dashboard/api/trove.py b/trove_dashboard/api/trove.py index 83208de..c1d5501 100644 --- a/trove_dashboard/api/trove.py +++ b/trove_dashboard/api/trove.py @@ -13,17 +13,15 @@ # under the License. from django.conf import settings -from troveclient.v1 import client - -from openstack_dashboard.api import base -from oslo_log import log as logging - from horizon.utils import functions as utils from horizon.utils.memoized import memoized # noqa - -from keystoneauth1 import loading from keystoneauth1 import session +from keystoneclient.auth import token_endpoint from novaclient import client as nova_client +from openstack_auth import utils as auth_utils +from openstack_dashboard.api import base +from oslo_log import log as logging +from troveclient.v1 import client # Supported compute versions NOVA_VERSIONS = base.APIVersionManager("compute", preferred_version=2) @@ -39,17 +37,21 @@ LOG = logging.getLogger(__name__) def troveclient(request): insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False) cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None) - trove_url = base.url_for(request, 'database') - c = client.Client(request.user.username, - request.user.token.id, - project_id=request.user.project_id, - auth_url=trove_url, - insecure=insecure, - cacert=cacert, - http_log_debug=settings.DEBUG) - c.client.auth_token = request.user.token.id - c.client.management_url = trove_url - return c + endpoint_type = getattr(settings, 'OPENSTACK_ENDPOINT_TYPE', 'publicURL') + region = request.user.services_region + + endpoint = base.url_for(request, 'database') + auth_url, _ = auth_utils.fix_auth_url_version_prefix( + settings.OPENSTACK_KEYSTONE_URL) + auth = token_endpoint.Token(auth_url, request.user.token.id) + verify = not insecure and (cacert or True) + + t_client = client.Client(session=session.Session(auth=auth, verify=verify), + service_type='database', + endpoint_type=endpoint_type, + region_name=region, + endpoint_override=endpoint) + return t_client def cluster_list(request, marker=None): @@ -254,17 +256,23 @@ def backup_create(request, name, instance_id, description=None, def nova_client_client(request): insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False) cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None) - identity_url = base.url_for(request, 'identity') - loader = loading.get_plugin_loader('token') - auth = loader.load_from_options(auth_url=identity_url, - token=request.user.token.id, - tenant_id=request.user.project_id) - sess = session.Session(auth=auth) - nova = nova_client.Client(NOVA_VERSION, - session=sess, - insecure=insecure, - cacert=cacert, - http_log_debug=settings.DEBUG) + endpoint_type = getattr(settings, 'OPENSTACK_ENDPOINT_TYPE', 'publicURL') + region = request.user.services_region + + endpoint = base.url_for(request, 'compute') + auth_url, _ = auth_utils.fix_auth_url_version_prefix( + settings.OPENSTACK_KEYSTONE_URL) + auth = token_endpoint.Token(auth_url, request.user.token.id) + verify = not insecure and (cacert or True) + + nova = nova_client.Client( + NOVA_VERSION, + session=session.Session(auth=auth, verify=verify), + endpoint_type=endpoint_type, + service_type='compute', + region_name=region, + endpoint_override=endpoint) + return nova