aboutsummaryrefslogtreecommitdiffstats
path: root/moon_manager/moon_manager/plugins/moon_keystone_plugin.py
blob: 0fb9b3630097660d3d4ec6b65aa3978be86c529f (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
73
74
75
76
77
# Software Name: MOON

# Version: 5.4

# SPDX-FileCopyrightText: Copyright (c) 2018-2020 Orange and its contributors
# SPDX-License-Identifier: Apache-2.0

# This software is distributed under the 'Apache License 2.0',
# the text of which is available at 'http://www.apache.org/licenses/LICENSE-2.0.txt'
# or see the "LICENSE" file for more details.

"""
Plugin to request OpenStack infrastructure:
- Keystone
"""

from moon_manager.plugins.moon_openstack_plugin import *

LOGGER = logging.getLogger("moon.manager.plugins.moon_keystone_plugin")

PLUGIN_TYPE = "information"
_ = str


class KeystoneConnector(OpenStackConnector):

    def get_items(self, item_id=None, **kwargs):
        username = ""
        domain_id = ""
        if "username" in kwargs:
            username = kwargs['username']
        if "domain_id" in kwargs:
            domain_id = kwargs['domain_id']
        if username and domain_id:
            return self._get(endpoint="/users?name={}&domain_id={}".format(username, domain_id),
                             _exception=KeystoneUserError)
        elif username:
            return self._get(endpoint="/users?name={}".format(username),
                             _exception=KeystoneUserError)
        elif domain_id:
            return self._get(endpoint="/users?domain_id={}".format(domain_id),
                             _exception=KeystoneUserError)
        else:
            return self._get(endpoint="/users",
                             _exception=KeystoneUserError)

    def add_item(self, item_id=None, **kwargs):
        if 'name' not in kwargs:
            raise KeystoneError("Cannot find name in request")
        _user = {
            "user": {
                "enabled": True,
                "name": kwargs['name'],
            }
        }
        if 'project' in kwargs:
            _user['user']['default_project_id'] = kwargs['project']
        if 'domain' in kwargs:
            _user['user']['domain_id'] = kwargs['domain']
        if 'password' in kwargs:
            _user['user']['password'] = kwargs['password']
        try:
            return self._post(endpoint="/users/",
                               data=_user,
                               _exception=KeystoneUserError)
        except KeystoneUserConflict:
            return True

    def update_item(self, item_id, **kwargs):
        raise NotImplementedError()  # pragma: no cover

    def delete_item(self, item_id, **kwargs):
        raise NotImplementedError()  # pragma: no cover


class Connector(KeystoneConnector):
    pass