aboutsummaryrefslogtreecommitdiffstats
path: root/api/resources/v2/environments.py
blob: 4b27c45b852f5a763053a9390f9f267d63f10973 (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
import uuid
import logging

from oslo_serialization import jsonutils

from api import ApiResource
from api.database.v2.handlers import V2EnvironmentHandler
from yardstick.common.utils import result_handler
from yardstick.common.utils import change_obj_to_dict
from yardstick.common import constants as consts

LOG = logging.getLogger(__name__)
LOG.setLevel(logging.DEBUG)


class V2Environments(ApiResource):

    def get(self):
        environment_handler = V2EnvironmentHandler()
        environments = [change_obj_to_dict(e) for e in environment_handler.list_all()]

        for e in environments:
            container_info = e['container_id']
            e['container_id'] = jsonutils.loads(container_info) if container_info else {}

        data = {
            'environments': environments
        }

        return result_handler(consts.API_SUCCESS, data)

    def post(self):
        return self._dispatch_post()

    def create_environment(self, args):
        try:
            name = args['name']
        except KeyError:
            return result_handler(consts.API_ERROR, 'name must be provided')

        env_id = str(uuid.uuid4())

        environment_handler = V2EnvironmentHandler()

        env_init_data = {
            'name': name,
            'uuid': env_id
        }
        environment_handler.insert(env_init_data)

        return result_handler(consts.API_SUCCESS, {'uuid': env_id})