summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-api/opendc/models/topology.py
diff options
context:
space:
mode:
Diffstat (limited to 'opendc-web/opendc-web-api/opendc/models/topology.py')
-rw-r--r--opendc-web/opendc-web-api/opendc/models/topology.py96
1 files changed, 82 insertions, 14 deletions
diff --git a/opendc-web/opendc-web-api/opendc/models/topology.py b/opendc-web/opendc-web-api/opendc/models/topology.py
index cb4c4bab..c6354ae6 100644
--- a/opendc-web/opendc-web-api/opendc/models/topology.py
+++ b/opendc-web/opendc-web-api/opendc/models/topology.py
@@ -1,7 +1,81 @@
+from marshmallow import Schema, fields
+
+from opendc.models.project import Project
from opendc.models.model import Model
-from opendc.models.user import User
-from opendc.util.exceptions import ClientError
-from opendc.util.rest import Response
+
+
+class MemorySchema(Schema):
+ """
+ Schema representing a memory unit.
+ """
+ _id = fields.String()
+ name = fields.String()
+ speedMbPerS = fields.Integer()
+ sizeMb = fields.Integer()
+ energyConsumptionW = fields.Integer()
+
+
+class PuSchema(Schema):
+ """
+ Schema representing a processing unit.
+ """
+ _id = fields.String()
+ name = fields.String()
+ clockRateMhz = fields.Integer()
+ numberOfCores = fields.Integer()
+ energyConsumptionW = fields.Integer()
+
+
+class MachineSchema(Schema):
+ """
+ Schema representing a machine.
+ """
+ _id = fields.String()
+ position = fields.Integer()
+ cpus = fields.List(fields.Nested(PuSchema))
+ gpus = fields.List(fields.Nested(PuSchema))
+ memories = fields.List(fields.Nested(MemorySchema))
+ storages = fields.List(fields.Nested(MemorySchema))
+
+
+class ObjectSchema(Schema):
+ """
+ Schema representing a room object.
+ """
+ _id = fields.String()
+ name = fields.String()
+ capacity = fields.Integer()
+ powerCapacityW = fields.Integer()
+ machines = fields.List(fields.Nested(MachineSchema))
+
+
+class TileSchema(Schema):
+ """
+ Schema representing a room tile.
+ """
+ _id = fields.String()
+ positionX = fields.Integer()
+ positionY = fields.Integer()
+ rack = fields.Nested(ObjectSchema)
+
+
+class RoomSchema(Schema):
+ """
+ Schema representing a room.
+ """
+ _id = fields.String()
+ name = fields.String(required=True)
+ tiles = fields.List(fields.Nested(TileSchema), required=True)
+
+
+class TopologySchema(Schema):
+ """
+ Schema representing a datacenter topology.
+ """
+ _id = fields.String()
+ projectId = fields.String()
+ name = fields.String(required=True)
+ rooms = fields.List(fields.Nested(RoomSchema), required=True)
class Topology(Model):
@@ -9,19 +83,13 @@ class Topology(Model):
collection_name = 'topologies'
- def check_user_access(self, google_id, edit_access):
- """Raises an error if the user with given [google_id] has insufficient access.
+ def check_user_access(self, user_id, edit_access):
+ """Raises an error if the user with given [user_id] has insufficient access.
Checks access on the parent project.
- :param google_id: The Google ID of the user.
+ :param user_id: The User ID of the user.
:param edit_access: True when edit access should be checked, otherwise view access.
"""
- user = User.from_google_id(google_id)
- if 'projectId' not in self.obj:
- raise ClientError(Response(400, 'Missing projectId in topology.'))
-
- authorizations = list(
- filter(lambda x: str(x['projectId']) == str(self.obj['projectId']), user.obj['authorizations']))
- if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'):
- raise ClientError(Response(403, 'Forbidden from retrieving topology.'))
+ project = Project.from_id(self.obj['projectId'])
+ project.check_user_access(user_id, edit_access)