summaryrefslogtreecommitdiffstats
path: root/api/resources/v2/pods.py
blob: ebc1312da8e76e6477a06b903c206c99a8ea7693 (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
import uuid
import yaml
import logging

from oslo_serialization import jsonutils

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

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


class V2Pods(ApiResource):

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

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

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

        try:
            uuid.UUID(environment_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid environment id')

        LOG.info('writing pod file: %s', consts.POD_FILE)
        upload_file.save(consts.POD_FILE)

        with open(consts.POD_FILE) as f:
            data = yaml.safe_load(TaskTemplate.render(f.read()))
        LOG.debug('pod content is: %s', data)

        LOG.info('create pod in database')
        pod_id = str(uuid.uuid4())
        pod_handler = V2PodHandler()
        pod_init_data = {
            'uuid': pod_id,
            'environment_id': environment_id,
            'content': jsonutils.dumps(data)
        }
        pod_handler.insert(pod_init_data)

        LOG.info('update pod in environment')
        environment_handler = V2EnvironmentHandler()
        environment_handler.update_attr(environment_id, {'pod_id': pod_id})

        return result_handler(consts.API_SUCCESS, {'uuid': pod_id, 'pod': data})


class V2Pod(ApiResource):

    def get(self, pod_id):
        try:
            uuid.UUID(pod_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid pod id')

        pod_handler = V2PodHandler()
        try:
            pod = pod_handler.get_by_uuid(pod_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such pod')

        content = jsonutils.loads(pod.content)

        return result_handler(consts.API_SUCCESS, {'pod': content})

    def delete(self, pod_id):
        try:
            uuid.UUID(pod_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'invalid pod id')

        pod_handler = V2PodHandler()
        try:
            pod = pod_handler.get_by_uuid(pod_id)
        except ValueError:
            return result_handler(consts.API_ERROR, 'no such pod')

        LOG.info('update pod in environment')
        environment_handler = V2EnvironmentHandler()
        environment_handler.update_attr(pod.environment_id, {'pod_id': None})

        LOG.info('delete pod in database')
        pod_handler.delete_by_uuid(pod_id)

        return result_handler(consts.API_SUCCESS, {'pod': pod_id})