Merge "Enable vnf/tg instantiate as blocking call."
[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 from yardstick.common.yaml_loader import yaml_load
22
23 LOG = logging.getLogger(__name__)
24 LOG.setLevel(logging.DEBUG)
25
26
27 class V2Pods(ApiResource):
28
29     def post(self):
30         return self._dispatch_post()
31
32     def upload_pod_file(self, args):
33         try:
34             upload_file = args['file']
35         except KeyError:
36             return result_handler(consts.API_ERROR, 'file must be provided')
37
38         try:
39             environment_id = args['environment_id']
40         except KeyError:
41             return result_handler(consts.API_ERROR, 'environment_id must be provided')
42
43         try:
44             uuid.UUID(environment_id)
45         except ValueError:
46             return result_handler(consts.API_ERROR, 'invalid environment id')
47
48         LOG.info('writing pod file: %s', consts.POD_FILE)
49         upload_file.save(consts.POD_FILE)
50
51         with open(consts.POD_FILE) as f:
52             data = yaml_load(TaskTemplate.render(f.read()))
53         LOG.debug('pod content is: %s', data)
54
55         LOG.info('create pod in database')
56         pod_id = str(uuid.uuid4())
57         pod_handler = V2PodHandler()
58         pod_init_data = {
59             'uuid': pod_id,
60             'environment_id': environment_id,
61             'content': jsonutils.dumps(data)
62         }
63         pod_handler.insert(pod_init_data)
64
65         LOG.info('update pod in environment')
66         environment_handler = V2EnvironmentHandler()
67         environment_handler.update_attr(environment_id, {'pod_id': pod_id})
68
69         return result_handler(consts.API_SUCCESS, {'uuid': pod_id, 'pod': data})
70
71
72 class V2Pod(ApiResource):
73
74     def get(self, pod_id):
75         try:
76             uuid.UUID(pod_id)
77         except ValueError:
78             return result_handler(consts.API_ERROR, 'invalid pod id')
79
80         pod_handler = V2PodHandler()
81         try:
82             pod = pod_handler.get_by_uuid(pod_id)
83         except ValueError:
84             return result_handler(consts.API_ERROR, 'no such pod')
85
86         content = jsonutils.loads(pod.content)
87
88         return result_handler(consts.API_SUCCESS, {'pod': content})
89
90     def delete(self, pod_id):
91         try:
92             uuid.UUID(pod_id)
93         except ValueError:
94             return result_handler(consts.API_ERROR, 'invalid pod id')
95
96         pod_handler = V2PodHandler()
97         try:
98             pod = pod_handler.get_by_uuid(pod_id)
99         except ValueError:
100             return result_handler(consts.API_ERROR, 'no such pod')
101
102         LOG.info('update pod in environment')
103         environment_handler = V2EnvironmentHandler()
104         environment_handler.update_attr(pod.environment_id, {'pod_id': None})
105
106         LOG.info('delete pod in database')
107         pod_handler.delete_by_uuid(pod_id)
108
109         return result_handler(consts.API_SUCCESS, {'pod': pod_id})