summaryrefslogtreecommitdiff
path: root/opendc/models/rack.py
diff options
context:
space:
mode:
authorleonoverweel <l.overweel@gmail.com>2017-01-24 12:05:15 +0100
committerleonoverweel <l.overweel@gmail.com>2017-01-24 12:05:15 +0100
commit86a50a4f6df9ece982743a3b7ca510846d248909 (patch)
tree79edc0478908b7fee9e5dca2088e562c7a62038b /opendc/models/rack.py
Initial commit
Diffstat (limited to 'opendc/models/rack.py')
-rw-r--r--opendc/models/rack.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/opendc/models/rack.py b/opendc/models/rack.py
new file mode 100644
index 00000000..a9aeeff9
--- /dev/null
+++ b/opendc/models/rack.py
@@ -0,0 +1,61 @@
+from opendc.models.model import Model
+from opendc.models.tile import Tile
+from opendc.models.object import Object
+from opendc.util import database, exceptions
+
+class Rack(Model):
+
+ JSON_TO_PYTHON_DICT = {
+ 'rack': {
+ 'id': 'id',
+ 'name': 'name',
+ 'capacity': 'capacity',
+ 'powerCapacityW': 'power_capacity_w'
+ }
+ }
+
+ PATH = '/v1/simulations/{simulationId}/datacenters/{datacenterId}/rooms/{roomId}/tiles/{tileId}/rack'
+
+ TABLE_NAME = 'racks'
+ COLUMNS = ['id', 'name', 'capacity', 'power_capacity_w']
+ COLUMNS_PRIMARY_KEY = ['id']
+
+ @classmethod
+ def from_tile_id(cls, tile_id):
+ """Get a Rack from the ID of the Tile it's on."""
+
+ tile = Tile.from_primary_key((tile_id,))
+
+ if not tile.exists():
+ return Rack(id = -1)
+
+ return cls.from_primary_key((tile.object_id,))
+
+ def google_id_has_at_least(self, google_id, authorization_level):
+ """Return True if the user has at least the given auth level over this Rack."""
+
+ # Get the Tile
+
+ try:
+ tile = Tile.query('object_id', self.id)[0]
+ except:
+ return False
+
+ # Check the Tile's Authorization
+
+ return tile.google_id_has_at_least(google_id, authorization_level)
+
+ def insert(self):
+ """Insert a Rack by first inserting an object."""
+
+ obj = Object(type = 'RACK')
+ obj.insert()
+
+ self.id = obj.id
+ self.insert_with_id()
+
+ def delete(self):
+ """Delete a Rack by deleting its associated object."""
+
+ obj = Object.from_primary_key((self.id,))
+ obj.delete()