Documents up-to-date
[doctor.git] / doctor_tests / identity_auth.py
1 ##############################################################################
2 # Copyright (c) 2017 NEC Corporation and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 import os
10
11 from keystoneauth1 import loading
12 from keystoneauth1 import session
13
14
15 def get_identity_auth(username=None, password=None, project=None):
16     auth_url = os.environ['OS_AUTH_URL']
17     username = username or os.environ['OS_USERNAME']
18     password = password or os.environ['OS_PASSWORD']
19     user_domain_name = os.environ.get('OS_USER_DOMAIN_NAME') or 'Default'
20     user_domain_id = os.environ.get('OS_USER_DOMAIN_ID') or 'default'
21     project_name = (project or os.environ.get('OS_PROJECT_NAME') or
22                     os.environ.get('OS_TENANT_NAME'))
23     project_domain_name = os.environ.get('OS_PROJECT_DOMAIN_NAME') or 'Default'
24     project_domain_id = os.environ.get('OS_PROJECT_DOMAIN_ID') or 'default'
25
26     loader = loading.get_plugin_loader('password')
27     return loader.load_from_options(
28         auth_url=auth_url,
29         username=username,
30         password=password,
31         user_domain_name=user_domain_name,
32         user_domain_id=user_domain_id,
33         project_name=project_name,
34         tenant_name=project_name,
35         project_domain_name=project_domain_name,
36         project_domain_id=project_domain_id)
37
38
39 def get_session(auth=None):
40     """Get a user credentials auth session."""
41     if auth is None:
42         auth = get_identity_auth()
43     return session.Session(auth=auth,
44                            verify=os.environ.get('OS_CACERT'))