Merge "run_traffic: capture and exit gracefully if crash in trex run_traffic"
[yardstick.git] / api / resources / v2 / environments.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 logging
11
12 from oslo_serialization import jsonutils
13 from docker import Client
14
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
23
24 LOG = logging.getLogger(__name__)
25 LOG.setLevel(logging.DEBUG)
26
27
28 class V2Environments(ApiResource):
29
30     def get(self):
31         environment_handler = V2EnvironmentHandler()
32         environments = [change_obj_to_dict(e) for e in environment_handler.list_all()]
33
34         for e in environments:
35             container_info = e['container_id']
36             e['container_id'] = jsonutils.loads(container_info) if container_info else {}
37
38         data = {
39             'environments': environments
40         }
41
42         return result_handler(consts.API_SUCCESS, data)
43
44     def post(self):
45         return self._dispatch_post()
46
47     def create_environment(self, args):
48         try:
49             name = args['name']
50         except KeyError:
51             return result_handler(consts.API_ERROR, 'name must be provided')
52
53         env_id = str(uuid.uuid4())
54
55         environment_handler = V2EnvironmentHandler()
56
57         env_init_data = {
58             'name': name,
59             'uuid': env_id
60         }
61         environment_handler.insert(env_init_data)
62
63         return result_handler(consts.API_SUCCESS, {'uuid': env_id})
64
65
66 class V2Environment(ApiResource):
67
68     def get(self, environment_id):
69         try:
70             uuid.UUID(environment_id)
71         except ValueError:
72             return result_handler(consts.API_ERROR, 'invalid environment id')
73
74         environment_handler = V2EnvironmentHandler()
75         try:
76             environment = environment_handler.get_by_uuid(environment_id)
77         except ValueError:
78             return result_handler(consts.API_ERROR, 'no such environment id')
79
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})
84
85     def delete(self, environment_id):
86         try:
87             uuid.UUID(environment_id)
88         except ValueError:
89             return result_handler(consts.API_ERROR, 'invalid environment id')
90
91         environment_handler = V2EnvironmentHandler()
92         try:
93             environment = environment_handler.get_by_uuid(environment_id)
94         except ValueError:
95             return result_handler(consts.API_ERROR, 'no such environment id')
96
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)
101
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)
106
107         if environment.container_id:
108             LOG.info('delete containers')
109             container_info = jsonutils.loads(environment.container_id)
110
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)
117                 try:
118                     client.remove_container(container.name, force=True)
119                 except Exception:
120                     LOG.exception('remove container failed')
121                 container_handler.delete_by_uuid(v)
122
123         environment_handler.delete_by_uuid(environment_id)
124
125         return result_handler(consts.API_SUCCESS, {'environment': environment_id})