1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd.
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 ##############################################################################
13 from oslo_serialization import jsonutils
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
23 LOG = logging.getLogger(__name__)
24 LOG.setLevel(logging.DEBUG)
27 class V2Pods(ApiResource):
30 return self._dispatch_post()
32 def upload_pod_file(self, args):
34 upload_file = args['file']
36 return result_handler(consts.API_ERROR, 'file must be provided')
39 environment_id = args['environment_id']
41 return result_handler(consts.API_ERROR, 'environment_id must be provided')
44 uuid.UUID(environment_id)
46 return result_handler(consts.API_ERROR, 'invalid environment id')
48 LOG.info('writing pod file: %s', consts.POD_FILE)
49 upload_file.save(consts.POD_FILE)
51 with open(consts.POD_FILE) as f:
52 data = yaml_load(TaskTemplate.render(f.read()))
53 LOG.debug('pod content is: %s', data)
55 LOG.info('create pod in database')
56 pod_id = str(uuid.uuid4())
57 pod_handler = V2PodHandler()
60 'environment_id': environment_id,
61 'content': jsonutils.dumps(data)
63 pod_handler.insert(pod_init_data)
65 LOG.info('update pod in environment')
66 environment_handler = V2EnvironmentHandler()
67 environment_handler.update_attr(environment_id, {'pod_id': pod_id})
69 return result_handler(consts.API_SUCCESS, {'uuid': pod_id, 'pod': data})
72 class V2Pod(ApiResource):
74 def get(self, pod_id):
78 return result_handler(consts.API_ERROR, 'invalid pod id')
80 pod_handler = V2PodHandler()
82 pod = pod_handler.get_by_uuid(pod_id)
84 return result_handler(consts.API_ERROR, 'no such pod')
86 content = jsonutils.loads(pod.content)
88 return result_handler(consts.API_SUCCESS, {'pod': content})
90 def delete(self, pod_id):
94 return result_handler(consts.API_ERROR, 'invalid pod id')
96 pod_handler = V2PodHandler()
98 pod = pod_handler.get_by_uuid(pod_id)
100 return result_handler(consts.API_ERROR, 'no such pod')
102 LOG.info('update pod in environment')
103 environment_handler = V2EnvironmentHandler()
104 environment_handler.update_attr(pod.environment_id, {'pod_id': None})
106 LOG.info('delete pod in database')
107 pod_handler.delete_by_uuid(pod_id)
109 return result_handler(consts.API_SUCCESS, {'pod': pod_id})