86601b800c
W0403 relative-import is no longer suppressed by pylint.rc Note: six.moves.urllib.request was considered a relative import from six.moves import urllib is considered valid. The code is updated to satisfy pylint and pep8. nfv_scenario_tests main.py still needs to use a relative import based on how it is invoked. Story: 2004515 Task: 28575 Change-Id: I3f576f3990352e9456fe3ee5d3a80cb1307f31e0 Signed-off-by: Al Bailey <Al.Bailey@windriver.com>
136 lines
5.0 KiB
Python
Executable File
136 lines
5.0 KiB
Python
Executable File
#
|
|
# Copyright (c) 2015-2018 Wind River Systems, Inc.
|
|
#
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
#
|
|
import json
|
|
from six.moves import urllib
|
|
|
|
from nfv_common import debug
|
|
from nfv_plugins.nfvi_plugins.openstack.objects import Directory
|
|
from nfv_plugins.nfvi_plugins.openstack.objects import OPENSTACK_SERVICE
|
|
from nfv_plugins.nfvi_plugins.openstack.objects import PLATFORM_SERVICE
|
|
from nfv_plugins.nfvi_plugins.openstack.objects import SERVICE_CATEGORY
|
|
from nfv_plugins.nfvi_plugins.openstack.objects import Token
|
|
|
|
DLOG = debug.debug_get_logger('nfv_plugins.nfvi_plugins.openstack')
|
|
|
|
|
|
def get_token(directory):
|
|
"""
|
|
Ask OpenStack for a token
|
|
"""
|
|
try:
|
|
if directory.auth_uri is None:
|
|
url = ("%s://%s:%s/v3/auth/tokens" % (directory.auth_protocol,
|
|
directory.auth_host,
|
|
directory.auth_port))
|
|
else:
|
|
url = directory.auth_uri + "/v3/auth/tokens"
|
|
|
|
request_info = urllib.request.Request(url)
|
|
request_info.add_header("Content-Type", "application/json")
|
|
request_info.add_header("Accept", "application/json")
|
|
|
|
if directory.auth_password is None:
|
|
import keyring
|
|
password = keyring.get_password(directory.keyring_service,
|
|
directory.auth_username)
|
|
else:
|
|
password = directory.auth_password
|
|
|
|
payload = json.dumps(
|
|
{"auth": {
|
|
"identity": {
|
|
"methods": [
|
|
"password"
|
|
],
|
|
"password": {
|
|
"user": {
|
|
"name": directory.auth_username,
|
|
"password": password,
|
|
"domain": {"name": directory.auth_user_domain_name}
|
|
}
|
|
}
|
|
},
|
|
"scope": {
|
|
"project": {
|
|
"name": directory.auth_project,
|
|
"domain": {"name": directory.auth_project_domain_name}
|
|
}}}})
|
|
request_info.add_data(payload)
|
|
|
|
request = urllib.request.urlopen(request_info)
|
|
# Identity API v3 returns token id in X-Subject-Token
|
|
# response header.
|
|
token_id = request.info().getheader('X-Subject-Token')
|
|
response = json.loads(request.read())
|
|
request.close()
|
|
return Token(response, directory, token_id)
|
|
|
|
except urllib.error.HTTPError as e:
|
|
DLOG.error("%s" % e)
|
|
return None
|
|
|
|
except urllib.error.URLError as e:
|
|
DLOG.error("%s" % e)
|
|
return None
|
|
|
|
|
|
def get_directory(config, service_category):
|
|
"""
|
|
Get directory information from the given configuration for the given
|
|
service category.
|
|
"""
|
|
if SERVICE_CATEGORY.PLATFORM == service_category:
|
|
services = PLATFORM_SERVICE
|
|
elif SERVICE_CATEGORY.OPENSTACK == service_category:
|
|
services = OPENSTACK_SERVICE
|
|
else:
|
|
raise ValueError("service_category is invalid: %s" % service_category)
|
|
|
|
auth_info = config.CONF.get(service_category, None)
|
|
if auth_info is not None:
|
|
auth_uri = auth_info.get('authorization_uri', None)
|
|
else:
|
|
auth_uri = None
|
|
|
|
directory = Directory(
|
|
service_category,
|
|
config.CONF[service_category]['keyring_service'],
|
|
config.CONF[service_category]['authorization_protocol'],
|
|
config.CONF[service_category]['authorization_ip'],
|
|
config.CONF[service_category]['authorization_port'],
|
|
config.CONF[service_category]['tenant'],
|
|
config.CONF[service_category]['username'],
|
|
config.CONF[service_category].get('password', None),
|
|
config.CONF[service_category]['user_domain_name'],
|
|
config.CONF[service_category]['project_domain_name'],
|
|
auth_uri)
|
|
|
|
for service in services:
|
|
service_info = config.CONF.get(service, None)
|
|
if service_info is not None:
|
|
region_name = service_info.get('region_name', None)
|
|
service_name = service_info.get('service_name', None)
|
|
service_type = service_info.get('service_type', None)
|
|
endpoint_type = service_info.get('endpoint_type', None)
|
|
endpoint_override = service_info.get('endpoint_override', None)
|
|
endpoint_disabled = service_info.get('endpoint_disabled', False)
|
|
|
|
if endpoint_disabled in ['Yes', 'yes', 'Y', 'y', 'True', 'true',
|
|
'T', 't', '1']:
|
|
endpoint_disabled = True
|
|
else:
|
|
endpoint_disabled = False
|
|
|
|
if (((region_name is not None and service_name is not None and
|
|
service_type is not None and endpoint_type is not None) or
|
|
endpoint_override is not None) and not endpoint_disabled):
|
|
|
|
directory.set_service_info(service, region_name, service_name,
|
|
service_type, endpoint_type,
|
|
endpoint_override)
|
|
|
|
return directory
|