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
22 LOG = logging.getLogger(__name__)
23 LOG.setLevel(logging.DEBUG)
26 class V2Pods(ApiResource):
29 return self._dispatch_post()
31 def upload_pod_file(self, args):
33 upload_file = args['file']
35 return result_handler(consts.API_ERROR, 'file must be provided')
38 environment_id = args['environment_id']
40 return result_handler(consts.API_ERROR, 'environment_id must be provided')
43 uuid.UUID(environment_id)
45 return result_handler(consts.API_ERROR, 'invalid environment id')
47 LOG.info('writing pod file: %s', consts.POD_FILE)
48 upload_file.save(consts.POD_FILE)
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)
54 LOG.info('create pod in database')
55 pod_id = str(uuid.uuid4())
56 pod_handler = V2PodHandler()
59 'environment_id': environment_id,
60 'content': jsonutils.dumps(data)
62 pod_handler.insert(pod_init_data)
64 LOG.info('update pod in environment')
65 environment_handler = V2EnvironmentHandler()
66 environment_handler.update_attr(environment_id, {'pod_id': pod_id})
68 return result_handler(consts.API_SUCCESS, {'uuid': pod_id, 'pod': data})
71 class V2Pod(ApiResource):
73 def get(self, pod_id):
77 return result_handler(consts.API_ERROR, 'invalid pod id')
79 pod_handler = V2PodHandler()
81 pod = pod_handler.get_by_uuid(pod_id)
83 return result_handler(consts.API_ERROR, 'no such pod')
85 content = jsonutils.loads(pod.content)
87 return result_handler(consts.API_SUCCESS, {'pod': content})
89 def delete(self, pod_id):
93 return result_handler(consts.API_ERROR, 'invalid pod id')
95 pod_handler = V2PodHandler()
97 pod = pod_handler.get_by_uuid(pod_id)
99 return result_handler(consts.API_ERROR, 'no such pod')
101 LOG.info('update pod in environment')
102 environment_handler = V2EnvironmentHandler()
103 environment_handler.update_attr(pod.environment_id, {'pod_id': None})
105 LOG.info('delete pod in database')
106 pod_handler.delete_by_uuid(pod_id)
108 return result_handler(consts.API_SUCCESS, {'pod': pod_id})