From 299a874b73601859921e5e6ff8501fce8b69bb3d Mon Sep 17 00:00:00 2001 From: Charles Short Date: Thu, 11 Mar 2021 05:12:04 -0500 Subject: [PATCH] Refactor Token class There was not a oslo-incubator module so move openstack/common/keystone_object.py to sysinv/common/keystone.py and adjust imports where the class was used. Test: 1. Ran unit tests locally. 2. Built new sysing rpm package. 3. Built new ISO. 4. Installed simplex controller ran a couple of commands sucessfully. Story: 2006796 Task: 42036 Signed-off-by: Charles Short Change-Id: I95bc209259f677847cd65ed1fa3466fb99f10603 --- sysinv/sysinv/sysinv/sysinv/cert_mon/utils.py | 2 +- .../sysinv/sysinv/sysinv/common/keystone.py | 77 +++++++++++++++++++ .../sysinv/sysinv/sysinv/common/rest_api.py | 2 +- .../sysinv/tests/cert_mon/test_cert_mon.py | 2 +- .../sysinv/tests/openstack/common/__init__.py | 4 - ...t_keystone_objects.py => test_keystone.py} | 2 +- 6 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 sysinv/sysinv/sysinv/sysinv/common/keystone.py delete mode 100644 sysinv/sysinv/sysinv/sysinv/tests/openstack/common/__init__.py rename sysinv/sysinv/sysinv/sysinv/tests/{openstack/common/test_keystone_objects.py => test_keystone.py} (96%) diff --git a/sysinv/sysinv/sysinv/sysinv/cert_mon/utils.py b/sysinv/sysinv/sysinv/sysinv/cert_mon/utils.py index 279dd7da83..5bffc885fa 100644 --- a/sysinv/sysinv/sysinv/sysinv/cert_mon/utils.py +++ b/sysinv/sysinv/sysinv/sysinv/cert_mon/utils.py @@ -37,7 +37,7 @@ from six.moves.urllib.error import URLError from six.moves.urllib.request import urlopen from sysinv.common import constants -from sysinv.openstack.common.keystone_objects import Token +from sysinv.common.keystone import Token from sysinv.common import kubernetes as sys_kube # Subcloud sync status diff --git a/sysinv/sysinv/sysinv/sysinv/common/keystone.py b/sysinv/sysinv/sysinv/sysinv/common/keystone.py new file mode 100644 index 0000000000..f6d524288c --- /dev/null +++ b/sysinv/sysinv/sysinv/sysinv/common/keystone.py @@ -0,0 +1,77 @@ +# +# Copyright (c) 2015 Wind River Systems, Inc. +# +# SPDX-License-Identifier: Apache-2.0 +# +import datetime +import iso8601 + +from oslo_log import log + +LOG = log.getLogger(__name__) + + +class Token(object): + def __init__(self, token_data, token_id, region_name): + self.expired = False + self.data = token_data + self.token_id = token_id + self.region_name = region_name + + def set_expired(self): + self.expired = True + + def is_expired(self, within_seconds=300): + if not self.expired: + end = iso8601.parse_date(self.data['token']['expires_at']) + now = iso8601.parse_date(datetime.datetime.utcnow().isoformat()) + # don't use .seconds here since it will be only the 'seconds' part + # of the timedelta + delta = (end - now).total_seconds() + return delta <= within_seconds + return True + + def get_id(self): + """ + Get the identifier of the token. + """ + return self.token_id + + def _get_service_url(self, service_type, service_name, interface_type): + """ + Search the catalog of a service for the url based on the interface + Returns: url or None on failure + """ + for catalog in self.data['token']['catalog']: + if catalog['type'] == service_type: + if catalog['name'] == service_name: + if len(catalog['endpoints']) != 0: + for endpoint in catalog['endpoints']: + if ((endpoint['interface'] == interface_type) and + (endpoint['region'] == self.region_name)): + return endpoint['url'] + return None + + def get_service_admin_url(self, service_type, service_name): + """ + Search the catalog of a service for the administrative url + Returns: admin url or None on failure + """ + return self._get_service_url(service_type, service_name, 'admin') + + def get_service_internal_url(self, service_type, service_name): + """ + Search the catalog of a service for the administrative url + Returns: admin url or None on failure + """ + return self._get_service_url(service_type, service_name, 'internal') + + def get_service_public_url(self, service_type, service_name): + """ + Search the catalog of a service for the administrative url + Returns: admin url or None on failure + """ + return self._get_service_url(service_type, service_name, 'public') + + def get_service_url(self, service_type, service_name): + return self.get_service_admin_url(service_type, service_name) diff --git a/sysinv/sysinv/sysinv/sysinv/common/rest_api.py b/sysinv/sysinv/sysinv/sysinv/common/rest_api.py index 061c41cb0e..6fc2ad85a0 100644 --- a/sysinv/sysinv/sysinv/sysinv/common/rest_api.py +++ b/sysinv/sysinv/sysinv/sysinv/common/rest_api.py @@ -14,7 +14,7 @@ from six.moves.urllib.error import URLError from oslo_log import log from sysinv.common import configp from sysinv.common import exception as si_exception -from sysinv.openstack.common.keystone_objects import Token +from sysinv.common.keystone import Token from sysinv.common.exception import OpenStackException from sysinv.common.exception import OpenStackRestAPIException diff --git a/sysinv/sysinv/sysinv/sysinv/tests/cert_mon/test_cert_mon.py b/sysinv/sysinv/sysinv/sysinv/tests/cert_mon/test_cert_mon.py index 68e70e4724..236f95b26a 100644 --- a/sysinv/sysinv/sysinv/sysinv/tests/cert_mon/test_cert_mon.py +++ b/sysinv/sysinv/sysinv/sysinv/tests/cert_mon/test_cert_mon.py @@ -15,7 +15,7 @@ from sysinv.common import constants from sysinv.cert_mon import service as cert_mon from sysinv.cert_mon import utils as cert_mon_utils from sysinv.cert_mon import watcher as cert_mon_watcher -from sysinv.openstack.common.keystone_objects import Token +from sysinv.common.keystone import Token from sysinv.tests.db import base diff --git a/sysinv/sysinv/sysinv/sysinv/tests/openstack/common/__init__.py b/sysinv/sysinv/sysinv/sysinv/tests/openstack/common/__init__.py deleted file mode 100644 index c29dec0da5..0000000000 --- a/sysinv/sysinv/sysinv/sysinv/tests/openstack/common/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -# Copyright (c) 2021 Wind River Systems, Inc. -# -# SPDX-License-Identifier: Apache-2.0 -# diff --git a/sysinv/sysinv/sysinv/sysinv/tests/openstack/common/test_keystone_objects.py b/sysinv/sysinv/sysinv/sysinv/tests/test_keystone.py similarity index 96% rename from sysinv/sysinv/sysinv/sysinv/tests/openstack/common/test_keystone_objects.py rename to sysinv/sysinv/sysinv/sysinv/tests/test_keystone.py index 21fef21cb5..8ff9832283 100644 --- a/sysinv/sysinv/sysinv/sysinv/tests/openstack/common/test_keystone_objects.py +++ b/sysinv/sysinv/sysinv/sysinv/tests/test_keystone.py @@ -5,7 +5,7 @@ import datetime -from sysinv.openstack.common.keystone_objects import Token +from sysinv.common.keystone import Token from sysinv.tests.db import base TOKEN_EXPIRATION_WINDOW = 300