Merge "Test case: Fio volume benchmark testcase using job file"
[yardstick.git] / api / database / v2 / handlers.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 from api.database import db_session
10 from api.database.v2.models import V2Environment
11 from api.database.v2.models import V2Openrc
12 from api.database.v2.models import V2Image
13 from api.database.v2.models import V2Pod
14 from api.database.v2.models import V2Container
15 from api.database.v2.models import V2Project
16 from api.database.v2.models import V2Task
17
18
19 class V2EnvironmentHandler(object):
20
21     def insert(self, kwargs):
22         environment = V2Environment(**kwargs)
23         db_session.add(environment)
24         db_session.commit()
25         return environment
26
27     def list_all(self):
28         return V2Environment.query.all()
29
30     def get_by_uuid(self, uuid):
31         environment = V2Environment.query.filter_by(uuid=uuid).first()
32         if not environment:
33             raise ValueError
34         return environment
35
36     def update_attr(self, uuid, attr):
37         environment = self.get_by_uuid(uuid)
38         for k, v in attr.items():
39             setattr(environment, k, v)
40         db_session.commit()
41
42     def append_attr(self, uuid, attr):
43         environment = self.get_by_uuid(uuid)
44         for k, v in attr.items():
45             value = getattr(environment, k)
46             new = '{},{}'.format(value, v) if value else v
47             setattr(environment, k, new)
48         db_session.commit()
49
50     def delete_by_uuid(self, uuid):
51         environment = self.get_by_uuid(uuid)
52         db_session.delete(environment)
53         db_session.commit()
54
55
56 class V2OpenrcHandler(object):
57
58     def insert(self, kwargs):
59         openrc = V2Openrc(**kwargs)
60         db_session.add(openrc)
61         db_session.commit()
62         return openrc
63
64     def get_by_uuid(self, uuid):
65         openrc = V2Openrc.query.filter_by(uuid=uuid).first()
66         if not openrc:
67             raise ValueError
68         return openrc
69
70     def delete_by_uuid(self, uuid):
71         openrc = self.get_by_uuid(uuid)
72         db_session.delete(openrc)
73         db_session.commit()
74
75
76 class V2ImageHandler(object):
77
78     def insert(self, kwargs):
79         image = V2Image(**kwargs)
80         db_session.add(image)
81         db_session.commit()
82         return image
83
84     def get_by_uuid(self, uuid):
85         image = V2Image.query.filter_by(uuid=uuid).first()
86         if not image:
87             raise ValueError
88         return image
89
90     def delete_by_uuid(self, uuid):
91         image = self.get_by_uuid(uuid)
92         db_session.delete(image)
93         db_session.commit()
94
95
96 class V2PodHandler(object):
97
98     def insert(self, kwargs):
99         pod = V2Pod(**kwargs)
100         db_session.add(pod)
101         db_session.commit()
102         return pod
103
104     def get_by_uuid(self, uuid):
105         pod = V2Pod.query.filter_by(uuid=uuid).first()
106         if not pod:
107             raise ValueError
108         return pod
109
110     def delete_by_uuid(self, uuid):
111         pod = self.get_by_uuid(uuid)
112         db_session.delete(pod)
113         db_session.commit()
114
115
116 class V2ContainerHandler(object):
117
118     def insert(self, kwargs):
119         container = V2Container(**kwargs)
120         db_session.add(container)
121         db_session.commit()
122         return container
123
124     def get_by_uuid(self, uuid):
125         container = V2Container.query.filter_by(uuid=uuid).first()
126         if not container:
127             raise ValueError
128         return container
129
130     def update_attr(self, uuid, attr):
131         container = self.get_by_uuid(uuid)
132         for k, v in attr.items():
133             setattr(container, k, v)
134         db_session.commit()
135
136     def delete_by_uuid(self, uuid):
137         container = self.get_by_uuid(uuid)
138         db_session.delete(container)
139         db_session.commit()
140
141
142 class V2ProjectHandler(object):
143
144     def list_all(self):
145         return V2Project.query.all()
146
147     def insert(self, kwargs):
148         project = V2Project(**kwargs)
149         db_session.add(project)
150         db_session.commit()
151         return project
152
153     def get_by_uuid(self, uuid):
154         project = V2Project.query.filter_by(uuid=uuid).first()
155         if not project:
156             raise ValueError
157         return project
158
159     def update_attr(self, uuid, attr):
160         project = self.get_by_uuid(uuid)
161         for k, v in attr.items():
162             setattr(project, k, v)
163         db_session.commit()
164
165     def append_attr(self, uuid, attr):
166         project = self.get_by_uuid(uuid)
167         for k, v in attr.items():
168             value = getattr(project, k)
169             new = '{},{}'.format(value, v) if value else v
170             setattr(project, k, new)
171         db_session.commit()
172
173     def delete_by_uuid(self, uuid):
174         project = self.get_by_uuid(uuid)
175         db_session.delete(project)
176         db_session.commit()
177
178
179 class V2TaskHandler(object):
180
181     def list_all(self):
182         return V2Task.query.all()
183
184     def insert(self, kwargs):
185         task = V2Task(**kwargs)
186         db_session.add(task)
187         db_session.commit()
188         return task
189
190     def get_by_uuid(self, uuid):
191         task = V2Task.query.filter_by(uuid=uuid).first()
192         if not task:
193             raise ValueError
194         return task
195
196     def update_attr(self, uuid, attr):
197         task = self.get_by_uuid(uuid)
198         for k, v in attr.items():
199             setattr(task, k, v)
200         db_session.commit()
201
202     def delete_by_uuid(self, uuid):
203         task = self.get_by_uuid(uuid)
204         db_session.delete(task)
205         db_session.commit()