Merge "Include capability to get token and endpoints for services"

This commit is contained in:
Zuul 2023-10-16 14:18:43 +00:00 committed by Gerrit Code Review
commit 36b39028b2
3 changed files with 97 additions and 0 deletions

View File

@ -81,6 +81,12 @@ class DuplicateDeployment(SoftwareError):
"""Duplicate Deployment Error.""" """Duplicate Deployment Error."""
pass pass
class ReleaseIsoDeleteFailure(SoftwareError): class ReleaseIsoDeleteFailure(SoftwareError):
"""Release iso delete error.""" """Release iso delete error."""
pass pass
class SysinvClientNotInitialized(SoftwareError):
"""Sysinv Client Not Initialized Error."""
pass

View File

@ -17,6 +17,7 @@ import subprocess
import sys import sys
import tarfile import tarfile
import tempfile import tempfile
from oslo_config import cfg as oslo_cfg
from lxml import etree as ElementTree from lxml import etree as ElementTree
from xml.dom import minidom from xml.dom import minidom
@ -28,6 +29,7 @@ from software.exceptions import ReleaseUploadFailure
from software.exceptions import ReleaseValidationFailure from software.exceptions import ReleaseValidationFailure
from software.exceptions import ReleaseMismatchFailure from software.exceptions import ReleaseMismatchFailure
from software.exceptions import SoftwareFail from software.exceptions import SoftwareFail
from software.exceptions import SysinvClientNotInitialized
import software.constants as constants import software.constants as constants
import software.utils as utils import software.utils as utils
@ -39,6 +41,7 @@ try:
except Exception: except Exception:
SW_VERSION = "unknown" SW_VERSION = "unknown"
CONF = oslo_cfg.CONF
# these next 4 variables may need to change to support ostree # these next 4 variables may need to change to support ostree
repo_root_dir = "/var/www/pages/updates" repo_root_dir = "/var/www/pages/updates"
@ -1035,3 +1038,43 @@ def read_upgrade_metadata(mounted_dir):
"required_patch": upgrade.findtext("required_patch"), "required_patch": upgrade.findtext("required_patch"),
}) })
return to_release, supported_from_releases return to_release, supported_from_releases
def get_endpoints_token(config=None, service_type="platform"):
try:
if not config:
keystone_conf = CONF.get('keystone_authtoken')
else:
keystone_conf = config
user = {
'auth_url': keystone_conf["auth_url"] + '/v3',
'username': keystone_conf["username"],
'password': keystone_conf["password"],
'project_name': keystone_conf["project_name"],
'user_domain_name': keystone_conf["user_domain_name"],
'project_domain_name': keystone_conf["project_domain_name"],
}
region_name = keystone_conf["region_name"]
token, endpoint = utils.get_auth_token_and_endpoint(user=user,
service_type=service_type,
region_name=region_name,
interface='public')
return token, endpoint
except Exception as e:
LOG.error("Failed to get '%s' endpoint. Error: %s", service_type, str(e))
return None, None
def get_sysinv_client(token, endpoint):
try:
from cgtsclient import client
sysinv_client = client.Client(version='1', endpoint=endpoint, token=token, timeout=600)
return sysinv_client
except ImportError:
msg = "Failed to import cgtsclient"
LOG.exception(msg)
raise ImportError(msg)
except Exception as e:
msg = "Failed to get sysinv client. Error: %s" % str(e)
LOG.exception(msg)
raise SysinvClientNotInitialized(msg)

View File

@ -202,3 +202,51 @@ def get_all_files(temp_dir=constants.SCRATCH_DIR):
except Exception: except Exception:
LOG.exception("Failed to get files from %s", temp_dir) LOG.exception("Failed to get files from %s", temp_dir)
return [] return []
def get_auth_token_and_endpoint(user: dict, service_type: str, region_name: str, interface: str):
"""Get the auth token and endpoint for a service
:param user: user dict
:param service_type: service type
:param region_name: region name
:param interface: interface type
:return: auth token and endpoint
"""
from keystoneauth1 import exceptions
from keystoneauth1 import identity
from keystoneauth1 import session
required_user_keys = ['auth_url',
'username',
'password',
'project_name',
'user_domain_name',
'project_domain_name']
if not all(key in user for key in required_user_keys):
raise Exception("Missing required key(s) to authenticate to Keystone")
try:
LOG.info("Authenticating for service type: %s, region name: %s, interface: %s",
service_type,
region_name,
interface)
auth = identity.Password(
auth_url=user['auth_url'],
username=user['username'],
password=user['password'],
project_name=user['project_name'],
user_domain_name=user['user_domain_name'],
project_domain_name=user['project_domain_name']
)
sess = session.Session(auth=auth)
return sess.get_token(), sess.get_endpoint(service_type=service_type,
region_name=region_name,
interface=interface)
except exceptions.http.Unauthorized:
LOG.error("Failed to authenticate to Keystone. Request unauthorized")
raise
except Exception as e:
LOG.exception("Failed to get token and endpoint. Error: %s", str(e))
raise