Last active
September 23, 2017 22:18
-
-
Save mohclips/da4ca1ec48f328f9494d303dba0caeb1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import os | |
import json | |
import argparse | |
import sys | |
import os_client_config | |
class Opts(object): | |
def __init__(self, cloud_name='devstack', debug=False): | |
self.cloud = cloud_name | |
self.debug = debug | |
# Use identity v3 API for examples. | |
self.identity_api_version = '3' | |
def create_connection_from_config(): | |
opts = Opts(cloud_name='devstack') | |
occ = os_client_config.OpenStackConfig() | |
try: | |
cloud = occ.get_one_cloud(opts.cloud) | |
except Exception as e: | |
print e | |
sys.exit(1) | |
return cloud | |
def create_connection_from_args(): | |
parser = argparse.ArgumentParser() | |
config = os_client_config.OpenStackConfig() | |
config.register_argparse_arguments(parser, sys.argv[1:]) | |
options = parser.parse_args() | |
try: | |
cloud = config.get_one_cloud(argparse=options) | |
except Exception as e: | |
print e | |
sys.exit(1) | |
return cloud | |
def get_regional_token(cloud): | |
session = requests.Session() | |
headers = {'Content-Type': 'application/json', 'Accept': 'application/json'} | |
os_user_domain = cloud.config['auth']['user_domain_name'] | |
os_username = cloud.config['auth']['username'] | |
os_password = cloud.config['auth']['password'] | |
# In the V3 identity API a project_name is only unique within a domain | |
# so you must also provide either a project_domain_id or project_domain_name. | |
os_project_id = cloud.config['auth']['project_id'] | |
url = cloud.config['auth']['auth_url'] + '/auth/tokens' | |
query_json = {'auth': { | |
'identity': { | |
'methods': ['password'], | |
'password': { | |
'user': { | |
'domain': { | |
'name': os_user_domain | |
}, | |
'name': os_username, | |
'password': os_password | |
} | |
} | |
}, | |
"scope": { | |
"project": { | |
"id": os_project_id | |
} | |
} | |
} | |
} | |
try: | |
response = session.request('POST', url, headers=headers, json=query_json) | |
except requests.exceptions.RequestException as e: | |
print e | |
sys.exit(1) | |
# we failed to authenticate | |
if response.status_code not in (201,): | |
print "RESP: HTTP Code:" + str(response.status_code) + " " + str(response.content) | |
sys.exit(1) | |
# we authenticated, now check the token is present | |
if 'X-Subject-Token' in response.headers.keys(): | |
auth_token = response.headers['X-Subject-Token'] | |
elif 'x-subject-token' in response.headers.keys(): # fix for issue #1 | |
auth_token = response.headers['x-subject-token'] | |
else: | |
print "Token not found" | |
sys.exit(1) | |
return auth_token | |
OS_CLIENT_CONFIG_FILE = os.environ.get('OS_CLIENT_CONFIG_FILE', None) | |
if OS_CLIENT_CONFIG_FILE == None: | |
cloud = create_connection_from_args() | |
else: | |
cloud = create_connection_from_config() | |
token = get_regional_token(cloud) | |
print token | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment