diff options
| -rw-r--r-- | opendc-api-spec.yml | 115 | ||||
| -rw-r--r-- | web-server/opendc/api/v2/paths.json | 4 | ||||
| -rw-r--r-- | web-server/opendc/models/prefab.py | 31 |
3 files changed, 149 insertions, 1 deletions
diff --git a/opendc-api-spec.yml b/opendc-api-spec.yml index 1e879b49..df8f8e94 100644 --- a/opendc-api-spec.yml +++ b/opendc-api-spec.yml @@ -660,6 +660,107 @@ paths: description: Unauthorized. '404': description: Trace not found. + /prefabs: + post: + tags: + - prefabs + description: Add a Prefab. + parameters: + - name: prefab + in: body + description: The new Prefab. + required: true + schema: + properties: + name: + type: string + responses: + '200': + description: Successfully added Prefab. + schema: + $ref: '#/definitions/Prefab' + '400': + description: Missing or incorrectly typed parameter. + '401': + description: Unauthorized. + '/prefabs/{prefabId}': + get: + tags: + - prefabs + description: Get this Prefab. + parameters: + - name: prefabId + in: path + description: Prefab's ID. + required: true + type: string + responses: + '200': + description: Successfully retrieved Prefab. + schema: + $ref: '#/definitions/Prefab' + '400': + description: Missing or incorrectly typed parameter. + '401': + description: Unauthorized. + '403': + description: Forbidden from retrieving Prefab. + '404': + description: Prefab not found + put: + tags: + - prefabs + description: Update this Prefab. + parameters: + - name: prefabId + in: path + description: Prefab's ID. + required: true + type: string + - name: prefab + in: body + description: Prefab's new properties. + required: true + schema: + properties: + simulation: + $ref: '#/definitions/Prefab' + responses: + '200': + description: Successfully updated Prefab. + schema: + $ref: '#/definitions/Prefab' + '400': + description: Missing or incorrectly typed parameter. + '401': + description: Unauthorized. + '403': + description: Forbidden from updating Prefab. + '404': + description: Prefab not found. + delete: + tags: + - prefabs + description: Delete this prefab. + parameters: + - name: prefabId + in: path + description: Prefab's ID. + required: true + type: string + responses: + '200': + description: Successfully deleted Prefab. + schema: + $ref: '#/definitions/Prefab' + '400': + description: Missing or incorrectly typed parameter. + '401': + description: Unauthorized. + '403': + description: Forbidden from deleting Prefab. + '404': + description: Prefab not found. definitions: Experiment: @@ -697,6 +798,20 @@ definitions: loadFraction: type: number format: float + Prefab: + type: object + properties: + _id: + type: string + name: + type: string + datetimeCreated: + type: string + format: dateTime + datetimeLastEdited: + type: string + format: dateTime + RackState: type: object properties: diff --git a/web-server/opendc/api/v2/paths.json b/web-server/opendc/api/v2/paths.json index c441170e..720e34cb 100644 --- a/web-server/opendc/api/v2/paths.json +++ b/web-server/opendc/api/v2/paths.json @@ -10,5 +10,7 @@ "/experiments/{experimentId}", "/schedulers", "/traces", - "/traces/{traceId}" + "/traces/{traceId}", + "/prefabs", + "/prefabs/{prefabId}", ] diff --git a/web-server/opendc/models/prefab.py b/web-server/opendc/models/prefab.py new file mode 100644 index 00000000..a3611a11 --- /dev/null +++ b/web-server/opendc/models/prefab.py @@ -0,0 +1,31 @@ +from opendc.models.model import Model +from opendc.models.user import User +from opendc.util.database import DB +from opendc.util.exceptions import ClientError +from opendc.util.rest import Response + + +class Prefab(Model): + """Model representing a Simulation.""" + + collection_name = 'prefabs' + + def check_user_access(self, google_id, edit_access): + """Raises an error if the user with given [google_id] has insufficient access. + + :param google_id: The Google ID of the user. + :param edit_access: True when edit access should be checked, otherwise view access. + """ + user = User.from_google_id(google_id) + authorizations = list(filter(lambda x: str(x['prefabId']) == str(self.get_id()), + user.obj['authorizations'])) + if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'): + raise ClientError(Response(403, "Forbidden from retrieving prefab.")) + + def get_all_authorizations(self): + """Get all user IDs having access to this simulation.""" + return [ + str(user['_id']) for user in DB.fetch_all({'authorizations': { + 'prefabId': self.obj['_id'] + }}, User.collection_name) + ] |
