diff options
author | 2017-04-22 13:25:07 +0200 | |
---|---|---|
committer | 2017-04-22 13:25:07 +0200 | |
commit | d182202fc6001983541504ed323d68479086317e (patch) | |
tree | 11d4c10cdd3e995f519c3e0e324968fdaf175114 /moonv4/moon_interface/tools/get_keystone_token.py | |
parent | 83c1c966baf73329fab8ddcfad19ad7fe0c41c2a (diff) |
add moonv4
Change-Id: I247af788d0b0fb961fbc85416486b241eb1d807c
Signed-off-by: WuKong <rebirthmonkey@gmail.com>
Diffstat (limited to 'moonv4/moon_interface/tools/get_keystone_token.py')
-rw-r--r-- | moonv4/moon_interface/tools/get_keystone_token.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/moonv4/moon_interface/tools/get_keystone_token.py b/moonv4/moon_interface/tools/get_keystone_token.py new file mode 100644 index 00000000..63b0d0b6 --- /dev/null +++ b/moonv4/moon_interface/tools/get_keystone_token.py @@ -0,0 +1,72 @@ +import requests +from oslo_config import cfg +from oslo_log import log as logging +from moon_utilities import options # noqa +from moon_utilities import exceptions + +CONF = cfg.CONF +LOG = logging.getLogger(__name__) + + +def login(user=None, password=None, domain=None, project=None, url=None): + print("""Configuration: + user: {user} + domain: {domain} + project: {project} + url: {url}""".format( + user=CONF.keystone.user, + domain=CONF.keystone.domain, + project=CONF.keystone.project, + url=CONF.keystone.url, + )) + if not user: + user = CONF.keystone.user + if not password: + password = CONF.keystone.password + if not domain: + domain = CONF.keystone.domain + if not project: + project = CONF.keystone.project + if not url: + url = CONF.keystone.url + headers = { + "Content-Type": "application/json" + } + data_auth = { + "auth": { + "identity": { + "methods": [ + "password" + ], + "password": { + "user": { + "domain": { + "id": domain + }, + "name": user, + "password": password + } + } + }, + "scope": { + "project": { + "domain": { + "id": domain + }, + "name": project + } + } + } + } + + req = requests.post("{}/auth/tokens".format(url), + json=data_auth, headers=headers, + verify=False) + + if req.status_code not in (200, 201): + LOG.error(req.text) + raise exceptions.KeystoneError + headers['X-Auth-Token'] = req.headers['X-Subject-Token'] + return headers + +print(login()['X-Auth-Token']) |