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
14 from docker.errors import APIError
16 from api import ApiResource
17 from api.database.v2.handlers import V2EnvironmentHandler
18 from api.database.v2.handlers import V2OpenrcHandler
19 from api.database.v2.handlers import V2PodHandler
20 from api.database.v2.handlers import V2ContainerHandler
21 from yardstick.common.utils import result_handler
22 from yardstick.common.utils import change_obj_to_dict
23 from yardstick.common import constants as consts
24 from yardstick.service.environment import Environment
26 LOG = logging.getLogger(__name__)
27 LOG.setLevel(logging.DEBUG)
30 class V2Environments(ApiResource):
33 environment_handler = V2EnvironmentHandler()
34 environments = [change_obj_to_dict(e) for e in environment_handler.list_all()]
36 for e in environments:
37 container_info = e['container_id']
38 e['container_id'] = jsonutils.loads(container_info) if container_info else {}
40 image_id = e['image_id']
41 e['image_id'] = image_id.split(',') if image_id else []
44 'environments': environments
47 return result_handler(consts.API_SUCCESS, data)
50 return self._dispatch_post()
52 def create_environment(self, args):
56 return result_handler(consts.API_ERROR, 'name must be provided')
58 env_id = str(uuid.uuid4())
60 environment_handler = V2EnvironmentHandler()
66 environment_handler.insert(env_init_data)
68 return result_handler(consts.API_SUCCESS, {'uuid': env_id})
71 class V2Environment(ApiResource):
73 def get(self, environment_id):
75 uuid.UUID(environment_id)
77 return result_handler(consts.API_ERROR, 'invalid environment id')
79 environment_handler = V2EnvironmentHandler()
81 environment = environment_handler.get_by_uuid(environment_id)
83 return result_handler(consts.API_ERROR, 'no such environment id')
85 environment = change_obj_to_dict(environment)
87 container_id = environment['container_id']
88 environment['container_id'] = jsonutils.loads(container_id) if container_id else {}
90 image_id = environment['image_id']
91 environment['image_id'] = image_id.split(',') if image_id else []
93 return result_handler(consts.API_SUCCESS, {'environment': environment})
95 def delete(self, environment_id):
97 uuid.UUID(environment_id)
99 return result_handler(consts.API_ERROR, 'invalid environment id')
101 environment_handler = V2EnvironmentHandler()
103 environment = environment_handler.get_by_uuid(environment_id)
105 return result_handler(consts.API_ERROR, 'no such environment id')
107 if environment.openrc_id:
108 LOG.info('delete openrc: %s', environment.openrc_id)
109 openrc_handler = V2OpenrcHandler()
110 openrc_handler.delete_by_uuid(environment.openrc_id)
112 if environment.pod_id:
113 LOG.info('delete pod: %s', environment.pod_id)
114 pod_handler = V2PodHandler()
115 pod_handler.delete_by_uuid(environment.pod_id)
117 if environment.container_id:
118 LOG.info('delete containers')
119 container_info = jsonutils.loads(environment.container_id)
121 container_handler = V2ContainerHandler()
122 client = Client(base_url=consts.DOCKER_URL)
123 for k, v in container_info.items():
124 LOG.info('start delete: %s', k)
125 container = container_handler.get_by_uuid(v)
126 LOG.debug('container name: %s', container.name)
128 client.remove_container(container.name, force=True)
130 LOG.exception('remove container failed')
131 container_handler.delete_by_uuid(v)
133 environment_handler.delete_by_uuid(environment_id)
135 return result_handler(consts.API_SUCCESS, {'environment': environment_id})
138 class V2SUT(ApiResource):
140 def get(self, environment_id):
142 uuid.UUID(environment_id)
144 return result_handler(consts.API_ERROR, 'invalid environment id')
146 environment_handler = V2EnvironmentHandler()
148 environment = environment_handler.get_by_uuid(environment_id)
150 return result_handler(consts.API_ERROR, 'no such environment id')
152 if not environment.pod_id:
153 return result_handler(consts.API_SUCCESS, {'sut': {}})
155 pod_handler = V2PodHandler()
157 pod = pod_handler.get_by_uuid(environment.pod_id)
159 return result_handler(consts.API_ERROR, 'no such pod id')
161 pod_content = pod.content
163 env = Environment(pod=pod_content)
164 sut_info = env.get_sut_info()
166 return result_handler(consts.API_SUCCESS, {'sut': sut_info})