/
simulations.py
340 lines (293 loc) · 13.1 KB
/
simulations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
# Copyright 2016 Kitware Inc.
#
# Licensed under the Apache License, Version 2.0 ( the "License" );
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###############################################################################
import cherrypy
import jsonschema
from girder.utility import ziputil
from girder.constants import AccessType
from girder.utility.model_importer import ModelImporter
from girder.api.rest import getCurrentUser, Resource
from girder.api.rest import RestException
from girder.api.describe import Description, autoDescribeRoute
from girder.api import access
from girder.api.docs import addModel
from .models import schema
from .utility import list_simulation_assets
class Simulations(Resource):
def __init__(self):
super(Simulations, self).__init__()
self.resourceName = 'simulations'
self.route('GET', (':id',), self.get)
self.route('DELETE', (':id',), self.delete)
self.route('PATCH', (':id',), self.update)
self.route('POST', (':id', 'clone'), self.clone)
self.route('GET', (':id', 'steps', ':stepName'), self.get_step)
self.route('PATCH', (':id', 'steps', ':stepName'), self.update_step)
self.route('GET', (':id', 'download'), self.download)
self.route('GET', (':id', 'access'), self.get_access)
self.route('PUT', (':id', 'access'), self.set_access)
self.route('PATCH', (':id', 'access'), self.patch_access)
self.route('PATCH', (':id', 'access', 'revoke'), self.revoke_access)
self._model = ModelImporter.model('simulation', 'hpccloud')
@autoDescribeRoute(
Description('Get a simulation')
.modelParam('id', 'The simulation to get.',
model='simulation', plugin='hpccloud', level=AccessType.READ)
)
@access.user
def get(self, simulation, params):
return simulation
@autoDescribeRoute(
Description('Delete a simulation')
.modelParam('id', 'The simulation to delete.',
model='simulation', plugin='hpccloud', level=AccessType.WRITE)
)
@access.user
def delete(self, simulation, params):
user = getCurrentUser()
self._model.delete(user, simulation)
addModel('Steps', schema.simulation['properties']['steps'], 'simulations')
addModel('UpdateProperties', {
'id': 'UpdateProperties',
'properties': {
'name': {
'type': 'string',
'description': 'The simulation name.'
},
'description': {
'type': 'string',
'description': 'The description of the simulation.'
},
'active': {
'type': 'string',
'items': {
'type': 'string'
},
'description': 'The name of the active step.'
},
'disabled': {
'type': 'array',
'items': {
'type': 'string'
},
'description': 'List of disabled steps.'
},
}
}, 'simulations')
@autoDescribeRoute(
Description('Update a simulation')
.modelParam('id', 'The simulation to update.',
model='simulation', plugin='hpccloud', level=AccessType.WRITE)
.jsonParam('updates', 'The properties of the simulation to update.',
dataType='UpdateProperties', required=True, paramType='body')
)
@access.user
def update(self, simulation, updates, params):
immutable = ['projectId', 'folderId', 'access', 'userId', '_id',
'updated', 'created']
for p in updates:
if p in immutable:
raise RestException('\'%s\' is an immutable property' % p, 400)
user = getCurrentUser()
name = updates.get('name')
description = updates.get('description')
active = updates.get('active')
disabled = updates.get('disabled')
status = updates.get('status')
metadata = updates.get('metadata')
steps = updates.get('steps')
return self._model.update_simulation(user, simulation, name,
metadata, description, active,
disabled, status, steps)
addModel('CloneParams', {
'id': 'CloneParams',
'properties': {
'name': {
'type': 'string',
'description': 'The new name for the simulation'
}
}
}, 'simulations')
@autoDescribeRoute(
Description('Clone a simulation')
.modelParam('id', 'The simulation to clone.',
model='simulation', plugin='hpccloud', level=AccessType.READ)
.jsonParam('props', 'The properies of the simulation to update.',
dataType='CloneParams', required=True, paramType='body')
)
@access.user
def clone(self, simulation, props, params):
self.requireParams(('name', ), props)
user = getCurrentUser()
cloned = self._model.clone(user, simulation, props['name'])
cherrypy.response.status = 201
cherrypy.response.headers['Location'] = '/simulations/%s' \
% cloned['_id']
return cloned
@autoDescribeRoute(
Description('Get a particular step in a simulation')
.modelParam('id', 'The simulation containing the step.',
model='simulation', plugin='hpccloud', level=AccessType.READ)
.param('stepName', 'The step name to gets.',
dataType='string', required=True, paramType='path')
)
@access.user
def get_step(self, simulation, stepName, params):
if stepName not in simulation.get('steps', {}):
raise RestException('Simulation %s doesn\'t contain step %s' %
(simulation['_id'], stepName), 400)
return simulation.get('steps', {}).get(stepName)
addModel('Step', schema.definitions['stepUpdate'], 'simulations')
@autoDescribeRoute(
Description('Update a particular step in a simulation')
.modelParam('id', 'The simulation containing the step.',
model='simulation', plugin='hpccloud', level=AccessType.WRITE)
.param('stepName', 'The step name to gets.',
dataType='string', required=True, paramType='path')
.jsonParam('updates', 'The properies of the step to update.',
dataType='Step', required=True, paramType='body')
)
@access.user
def update_step(self, simulation, stepName, updates, params):
user = getCurrentUser()
immutable = ['type', 'folderId']
if stepName not in simulation.get('steps', {}):
raise RestException('Simulation %s doesn\'t contain step %s' %
(simulation['_id'], stepName), 400)
for p in updates:
if p in immutable:
raise RestException('\'%s\' is an immutable property' % p, 400)
try:
ref_resolver = jsonschema.RefResolver.from_schema(
schema.definitions)
jsonschema.validate(
updates, schema.definitions['stepUpdate'],
resolver=ref_resolver)
except jsonschema.ValidationError as ve:
raise RestException(ve.message, 400)
status = updates.get('status')
metadata = updates.get('metadata')
export = updates.get('export')
view = updates.get('view')
return self._model.update_step(
user, simulation, stepName, status, metadata, export, view)
@autoDescribeRoute(
Description('Get access object of simulation')
.modelParam('id', 'id of the simulation', model='simulation',
plugin='hpccloud', level=AccessType.READ)
)
@access.user
def get_access(self, simulation, params):
return simulation.get('access', {'groups': [], 'users': []})
@autoDescribeRoute(
Description('Share a simulation with a set of users or groups')
.modelParam('id', 'The simulation to be shared.', model='simulation',
plugin='hpccloud', level=AccessType.WRITE)
.jsonParam('share', 'Array of users to share the project with.',
dataType='object', required=True, paramType='body')
)
@access.user
def set_access(self, simulation, share, params):
user = getCurrentUser()
# Validate we have been given a value body
try:
ref_resolver = jsonschema.RefResolver.from_schema(
schema.definitions)
jsonschema.validate(share, schema.simulation['definitions']['share'],
resolver=ref_resolver)
except jsonschema.ValidationError as ve:
raise RestException(ve.message, 400)
users = share.get('users', [])
groups = share.get('groups', [])
return self._model.set_access(user, simulation, users, groups)
@autoDescribeRoute(
Description('Share a simulation with a set of users or groups')
.modelParam('id', 'The simulation to be shared.', model='simulation',
plugin='hpccloud', level=AccessType.WRITE)
.jsonParam('share', 'Array of users to share the project with.',
dataType='object', required=True, paramType='body')
)
@access.user
def patch_access(self, simulation, share, params):
user = getCurrentUser()
# Validate we have been given a value body
try:
ref_resolver = jsonschema.RefResolver.from_schema(
schema.definitions)
jsonschema.validate(share, schema.simulation['definitions']['share'],
resolver=ref_resolver)
except jsonschema.ValidationError as ve:
raise RestException(ve.message, 400)
users = share.get('users', [])
groups = share.get('groups', [])
level = share.get('level', 0)
flags = share.get('flags', [])
return self._model.patch_access(user, simulation,
users, groups, level, flags)
@autoDescribeRoute(
Description('Revoke permissions for asimulation given a set of users \
or groups')
.modelParam('id', 'The simulation to be unshared.', model='simulation',
plugin='hpccloud', level=AccessType.WRITE)
.jsonParam('share', 'Array of users to share the project with.',
dataType='object', required=True, paramType='body')
)
@access.user
def revoke_access(self, simulation, share, params):
user = getCurrentUser()
# Validate we have been given a value body
try:
ref_resolver = jsonschema.RefResolver.from_schema(
schema.definitions)
jsonschema.validate(share, schema.simulation['definitions']['share'],
resolver=ref_resolver)
except jsonschema.ValidationError as ve:
raise RestException(ve.message, 400)
users = share.get('users', [])
groups = share.get('groups', [])
return self._model.revoke_access(user, simulation, users, groups)
@autoDescribeRoute(
Description('Download all the asset associated with a simulation')
.modelParam('id', 'The simulation to download.',
model='simulation', plugin='hpccloud', level=AccessType.READ)
)
@access.user
def download(self, simulation, params):
user = self.getCurrentUser()
cherrypy.response.headers['Content-Type'] = 'application/zip'
cherrypy.response.headers['Content-Disposition'] = \
u'attachment; filename="{}{}"'.format(simulation['name'], '.zip')
def stream():
zip = ziputil.ZipGenerator()
for (path, file) in list_simulation_assets(user, simulation):
for data in zip.addFile(file, path):
yield data
yield zip.footer()
return stream
@autoDescribeRoute(
Description('Set particular step in the simulation as the active step')
.modelParam('id', 'The simulation id.',
model='simulation', plugin='hpccloud', level=AccessType.WRITE)
.param('stepName', 'The simulation step name.',
dataType='string', required=True, paramType='path')
)
@access.user
def set_active(self, simulation, stepName, params):
if stepName not in simulation.get('steps', {}):
raise RestException('Invalid step name', 400)
simulation['active'] = stepName
return self._model.save(simulation)