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 ##############################################################################
12 from oslo_serialization import jsonutils
13 from docker import Client
15 from api import ApiResource
16 from api.database.v2.handlers import V2EnvironmentHandler
17 from api.database.v2.handlers import V2OpenrcHandler
18 from api.database.v2.handlers import V2PodHandler
19 from api.database.v2.handlers import V2ContainerHandler
20 from yardstick.common.utils import result_handler
21 from yardstick.common.utils import change_obj_to_dict
22 from yardstick.common import constants as consts
24 LOG = logging.getLogger(__name__)
25 LOG.setLevel(logging.DEBUG)
28 class V2Environments(ApiResource):
31 environment_handler = V2EnvironmentHandler()
32 environments = [change_obj_to_dict(e) for e in environment_handler.list_all()]
34 for e in environments:
35 container_info = e['container_id']
36 e['container_id'] = jsonutils.loads(container_info) if container_info else {}
39 'environments': environments
42 return result_handler(consts.API_SUCCESS, data)
45 return self._dispatch_post()
47 def create_environment(self, args):
51 return result_handler(consts.API_ERROR, 'name must be provided')
53 env_id = str(uuid.uuid4())
55 environment_handler = V2EnvironmentHandler()
61 environment_handler.insert(env_init_data)
63 return result_handler(consts.API_SUCCESS, {'uuid': env_id})
66 class V2Environment(ApiResource):
68 def get(self, environment_id):
70 uuid.UUID(environment_id)
72 return result_handler(consts.API_ERROR, 'invalid environment id')
74 environment_handler = V2EnvironmentHandler()
76 environment = environment_handler.get_by_uuid(environment_id)
78 return result_handler(consts.API_ERROR, 'no such environment id')
80 environment = change_obj_to_dict(environment)
81 container_id = environment['container_id']
82 environment['container_id'] = jsonutils.loads(container_id) if container_id else {}
83 return result_handler(consts.API_SUCCESS, {'environment': environment})
85 def delete(self, environment_id):
87 uuid.UUID(environment_id)
89 return result_handler(consts.API_ERROR, 'invalid environment id')
91 environment_handler = V2EnvironmentHandler()
93 environment = environment_handler.get_by_uuid(environment_id)
95 return result_handler(consts.API_ERROR, 'no such environment id')
97 if environment.openrc_id:
98 LOG.info('delete openrc: %s', environment.openrc_id)
99 openrc_handler = V2OpenrcHandler()
100 openrc_handler.delete_by_uuid(environment.openrc_id)
102 if environment.pod_id:
103 LOG.info('delete pod: %s', environment.pod_id)
104 pod_handler = V2PodHandler()
105 pod_handler.delete_by_uuid(environment.pod_id)
107 if environment.container_id:
108 LOG.info('delete containers')
109 container_info = jsonutils.loads(environment.container_id)
111 container_handler = V2ContainerHandler()
112 client = Client(base_url=consts.DOCKER_URL)
113 for k, v in container_info.items():
114 LOG.info('start delete: %s', k)
115 container = container_handler.get_by_uuid(v)
116 LOG.debug('container name: %s', container.name)
118 client.remove_container(container.name, force=True)
120 LOG.exception('remove container failed')
121 container_handler.delete_by_uuid(v)
123 environment_handler.delete_by_uuid(environment_id)
125 return result_handler(consts.API_SUCCESS, {'environment': environment_id})