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 <charles.short@windriver.com>
Change-Id: I95bc209259f677847cd65ed1fa3466fb99f10603
This commit is contained in:
Charles Short 2021-03-11 05:12:04 -05:00 committed by Chuck Short
parent 9ef262a5a7
commit 299a874b73
6 changed files with 81 additions and 8 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -1,4 +0,0 @@
# Copyright (c) 2021 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#

View File

@ -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