aboutsummaryrefslogtreecommitdiffstats
path: root/moonv4/moon_consul/moon_consul/api/system.py
blob: e21d9de2a3017ee862c16eef81229566800c32e6 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
# This software is distributed under the terms and conditions of the 'Apache-2.0'
# license which can be found in the file 'LICENSE' in this package distribution
# or at 'http://www.apache.org/licenses/LICENSE-2.0'.
"""
Assignments allow to connect data with elements of perimeter

"""

from flask import request
from flask_restful import Resource
# from oslo_config import cfg
from oslo_log import log as logging
# from moon_interface.tools import check_auth

__version__ = "0.1.0"

LOG = logging.getLogger(__name__)
# CONF = cfg.CONF


class Docker(Resource):
    """
    Endpoint for system requests
    """

    __urls__ = (
        "/configuration/docker",
    )

    def __init__(self, *args, **kwargs):
        self.conf = kwargs.get('conf', {})

    # @check_auth
    def get(self):
        """Retrieve docker configuration

        :return: {
            "docker": {
                "url": "hostname for the docker server (eg. /var/run/docker.sock)",
                "port": "port of the server",
                "user": "user of the server",
                "password": "password of the server",
                "protocol": "protocol to use (eg. unix)"
            }
        }
        """
        url = self.conf.DOCKER_URL
        # LOG.info(url)
        # hostname = url.split("@")[-1].split(":")[0].split("/")[0]
        # try:
        #     port = int(url.split("@")[-1].split(":")[1].split("/")[0])
        # except ValueError:
        #     port = None
        # user = url.split("//")[1].split(":")[0]
        # # TODO: password must be encrypted
        # try:
        #     password = url.split(":")[2].split("@")[0]
        # except IndexError:
        #     password = ""
        # protocol = url.split(":")[0]
        return {
            "docker": {
                "url": self.conf.DOCKER_URL,
                # "port": port,
                # "user": user,
                # "password": password,
                # "protocol": protocol,
            }
        }


class Components(Resource):
    """
    Endpoint for requests on components
    """

    __urls__ = (
        "/configuration/components",
        "/configuration/components/",
        "/configuration/components/<string:id_or_name>",
    )

    def __init__(self, *args, **kwargs):
        self.conf = kwargs.get('conf', {})

    # @check_auth
    def get(self, id_or_name=None):
        """Retrieve component list
        
        :param id_or_name: ID or name of the component

        :return: {
            "components": [ 
                {
                    "hostname": "hostname of the component",
                    "port": "port of the server in this component",
                    "id": "id of the component",
                    "keystone_id": "Keystone project ID served by this component if needed"
                },
            ]
        }
        """
        if id_or_name:
            for _component in self.conf.COMPONENTS:
                if id_or_name in (_component["hostname"], _component["id"]):
                    return {
                        "components": [_component, ]
                    }
            return {"components": []}
        return {"components": self.conf.COMPONENTS}

    # @check_auth
    def put(self, id_or_name=None):
        """Ask for adding a new component
        The response gives the TCP port to be used
        
        :param id_or_name: ID or name of the component
        :request body: {
            "hostname": "hostname of the new component",
            "keystone_id": "Keystone ID mapped to that component (if needed)"
        }
        :return: {
            "components": [ 
                {
                    "hostname": "hostname of the component",
                    "port": "port of the server in this component",
                    "id": "id of the component",
                    "keystone_id": "Keystone project ID served by this component"
                }
            ]
        }
        """
        if not id_or_name:
            return "Need a name for that component", 400
        for _component in self.conf.COMPONENTS:
            if id_or_name in (_component["hostname"], _component["id"]):
                return "ID already used", 409
        self.conf.COMPONENTS_PORT_START += 1
        port = self.conf.COMPONENTS_PORT_START
        data = request.json
        new_component = {
            "hostname": data.get("hostname", id_or_name),
            "port": port,
            "id": id_or_name,
            "keystone_id": data.get("keystone_id", "")
        }
        self.conf.COMPONENTS.append(new_component)
        return {
            "components": [new_component, ]
        }

    # @check_auth
    def delete(self, id_or_name=None):
        """Delete a component
        
        :param id_or_name: ID or name of the component
        :return: {
            "result": true
        }
        """
        if not id_or_name:
            return "Need a name for that component", 400
        for index, _component in enumerate(self.conf.COMPONENTS):
            if id_or_name in (_component["hostname"], _component["id"]):
                self.conf.COMPONENTS.pop(index)
                return {"result": True}
        return "Cannot find component named {}".format(id_or_name), 403