Add function to upload image from local/url in GUI
[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             image_id = e['image_id']
39             e['image_id'] = image_id.split(',') if image_id else []
40
41         data = {
42             'environments': environments
43         }
44
45         return result_handler(consts.API_SUCCESS, data)
46
47     def post(self):
48         return self._dispatch_post()
49
50     def create_environment(self, args):
51         try:
52             name = args['name']
53         except KeyError:
54             return result_handler(consts.API_ERROR, 'name must be provided')
55
56         env_id = str(uuid.uuid4())
57
58         environment_handler = V2EnvironmentHandler()
59
60         env_init_data = {
61             'name': name,
62             'uuid': env_id
63         }
64         environment_handler.insert(env_init_data)
65
66         return result_handler(consts.API_SUCCESS, {'uuid': env_id})
67
68
69 class V2Environment(ApiResource):
70
71     def get(self, environment_id):
72         try:
73             uuid.UUID(environment_id)
74         except ValueError:
75             return result_handler(consts.API_ERROR, 'invalid environment id')
76
77         environment_handler = V2EnvironmentHandler()
78         try:
79             environment = environment_handler.get_by_uuid(environment_id)
80         except ValueError:
81             return result_handler(consts.API_ERROR, 'no such environment id')
82
83         environment = change_obj_to_dict(environment)
84
85         container_id = environment['container_id']
86         environment['container_id'] = jsonutils.loads(container_id) if container_id else {}
87
88         image_id = environment['image_id']
89         environment['image_id'] = image_id.split(',') if image_id else []
90
91         return result_handler(consts.API_SUCCESS, {'environment': environment})
92
93     def delete(self, environment_id):
94         try:
95             uuid.UUID(environment_id)
96         except ValueError:
97             return result_handler(consts.API_ERROR, 'invalid environment id')
98
99         environment_handler = V2EnvironmentHandler()
100         try:
101             environment = environment_handler.get_by_uuid(environment_id)
102         except ValueError:
103             return result_handler(consts.API_ERROR, 'no such environment id')
104
105         if environment.openrc_id:
106             LOG.info('delete openrc: %s', environment.openrc_id)
107             openrc_handler = V2OpenrcHandler()
108             openrc_handler.delete_by_uuid(environment.openrc_id)
109
110         if environment.pod_id:
111             LOG.info('delete pod: %s', environment.pod_id)
112             pod_handler = V2PodHandler()
113             pod_handler.delete_by_uuid(environment.pod_id)
114
115         if environment.container_id:
116             LOG.info('delete containers')
117             container_info = jsonutils.loads(environment.container_id)
118
119             container_handler = V2ContainerHandler()
120             client = Client(base_url=consts.DOCKER_URL)
121             for k, v in container_info.items():
122                 LOG.info('start delete: %s', k)
123                 container = container_handler.get_by_uuid(v)
124                 LOG.debug('container name: %s', container.name)
125                 try:
126                     client.remove_container(container.name, force=True)
127                 except Exception:
128                     LOG.exception('remove container failed')
129                 container_handler.delete_by_uuid(v)
130
131         environment_handler.delete_by_uuid(environment_id)
132
133         return result_handler(consts.API_SUCCESS, {'environment': environment_id})