Merge "support more parameters in iperf3 testcase"
[yardstick.git] / api / resources / v2 / pods.py
1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 import uuid
10 import yaml
11 import logging
12
13 from oslo_serialization import jsonutils
14
15 from api import ApiResource
16 from api.database.v2.handlers import V2PodHandler
17 from api.database.v2.handlers import V2EnvironmentHandler
18 from yardstick.common import constants as consts
19 from yardstick.common.utils import result_handler
20 from yardstick.common.task_template import TaskTemplate
21
22 LOG = logging.getLogger(__name__)
23 LOG.setLevel(logging.DEBUG)
24
25
26 class V2Pods(ApiResource):
27
28     def post(self):
29         return self._dispatch_post()
30
31     def upload_pod_file(self, args):
32         try:
33             upload_file = args['file']
34         except KeyError:
35             return result_handler(consts.API_ERROR, 'file must be provided')
36
37         try:
38             environment_id = args['environment_id']
39         except KeyError:
40             return result_handler(consts.API_ERROR, 'environment_id must be provided')
41
42         try:
43             uuid.UUID(environment_id)
44         except ValueError:
45             return result_handler(consts.API_ERROR, 'invalid environment id')
46
47         LOG.info('writing pod file: %s', consts.POD_FILE)
48         upload_file.save(consts.POD_FILE)
49
50         with open(consts.POD_FILE) as f:
51             data = yaml.safe_load(TaskTemplate.render(f.read()))
52         LOG.debug('pod content is: %s', data)
53
54         LOG.info('create pod in database')
55         pod_id = str(uuid.uuid4())
56         pod_handler = V2PodHandler()
57         pod_init_data = {
58             'uuid': pod_id,
59             'environment_id': environment_id,
60             'content': jsonutils.dumps(data)
61         }
62         pod_handler.insert(pod_init_data)
63
64         LOG.info('update pod in environment')
65         environment_handler = V2EnvironmentHandler()
66         environment_handler.update_attr(environment_id, {'pod_id': pod_id})
67
68         return result_handler(consts.API_SUCCESS, {'uuid': pod_id, 'pod': data})
69
70
71 class V2Pod(ApiResource):
72
73     def get(self, pod_id):
74         try:
75             uuid.UUID(pod_id)
76         except ValueError:
77             return result_handler(consts.API_ERROR, 'invalid pod id')
78
79         pod_handler = V2PodHandler()
80         try:
81             pod = pod_handler.get_by_uuid(pod_id)
82         except ValueError:
83             return result_handler(consts.API_ERROR, 'no such pod')
84
85         content = jsonutils.loads(pod.content)
86
87         return result_handler(consts.API_SUCCESS, {'pod': content})
88
89     def delete(self, pod_id):
90         try:
91             uuid.UUID(pod_id)
92         except ValueError:
93             return result_handler(consts.API_ERROR, 'invalid pod id')
94
95         pod_handler = V2PodHandler()
96         try:
97             pod = pod_handler.get_by_uuid(pod_id)
98         except ValueError:
99             return result_handler(consts.API_ERROR, 'no such pod')
100
101         LOG.info('update pod in environment')
102         environment_handler = V2EnvironmentHandler()
103         environment_handler.update_attr(pod.environment_id, {'pod_id': None})
104
105         LOG.info('delete pod in database')
106         pod_handler.delete_by_uuid(pod_id)
107
108         return result_handler(consts.API_SUCCESS, {'pod': pod_id})