aboutsummaryrefslogtreecommitdiffstats
path: root/moonv4/moon_interface/tools/get_keystone_token.py
blob: 63b0d0b6ca83d747d3ae9b9e243efb78867c9d8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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'])