summaryrefslogtreecommitdiff
path: root/web-server/opendc/models_old/machine.py
diff options
context:
space:
mode:
authorGeorgios Andreadis <info@gandreadis.com>2020-06-29 16:05:23 +0200
committerFabian Mastenbroek <mail.fabianm@gmail.com>2020-08-24 16:18:36 +0200
commit4f9a40abdc7836345113c047f27fcc96800cb3f5 (patch)
treee443d14e34a884b1a4d9c549f81d51202eddd5f7 /web-server/opendc/models_old/machine.py
parentcd5f7bf3a72913e1602cb4c575e61ac7d5519be0 (diff)
Prepare web-server repository for monorepo
This change prepares the web-server Git repository for the monorepo residing at https://github.com/atlarge-research.com/opendc. To accomodate for this, we move all files into a web-server subdirectory.
Diffstat (limited to 'web-server/opendc/models_old/machine.py')
-rw-r--r--web-server/opendc/models_old/machine.py122
1 files changed, 122 insertions, 0 deletions
diff --git a/web-server/opendc/models_old/machine.py b/web-server/opendc/models_old/machine.py
new file mode 100644
index 00000000..8e5ccb44
--- /dev/null
+++ b/web-server/opendc/models_old/machine.py
@@ -0,0 +1,122 @@
+import copy
+
+from opendc.models_old.model import Model
+from opendc.models_old.rack import Rack
+from opendc.util import database, exceptions
+
+
+class Machine(Model):
+ JSON_TO_PYTHON_DICT = {
+ 'machine': {
+ 'id': 'id',
+ 'rackId': 'rack_id',
+ 'position': 'position',
+ 'tags': 'tags',
+ 'cpuIds': 'cpu_ids',
+ 'gpuIds': 'gpu_ids',
+ 'memoryIds': 'memory_ids',
+ 'storageIds': 'storage_ids',
+ 'topologyId': 'topology_id'
+ }
+ }
+
+ PATH = '/v1/tiles/{tileId}/rack/machines'
+
+ COLLECTION_NAME = 'machines'
+ COLUMNS = ['id', 'rack_id', 'position', 'topology_id']
+ COLUMNS_PRIMARY_KEY = ['id']
+
+ device_table_to_attribute = {
+ 'cpus': 'cpu_ids',
+ 'gpus': 'gpu_ids',
+ 'memories': 'memory_ids',
+ 'storages': 'storage_ids'
+ }
+
+ def _update_devices(self, before_insert):
+ """Update this Machine's devices in the database."""
+
+ for device_table in self.device_table_to_attribute.keys():
+
+ # First, delete current machine-device links
+
+ statement = 'DELETE FROM machine_{} WHERE machine_id = %s'.format(device_table)
+ database.execute(statement, (before_insert.id, ))
+
+ # Then, add current ones
+
+ for device_id in getattr(before_insert, before_insert.device_table_to_attribute[device_table]):
+ statement = 'INSERT INTO machine_{} (machine_id, {}) VALUES (%s, %s)'.format(
+ device_table, before_insert.device_table_to_attribute[device_table][:-1])
+
+ database.execute(statement, (before_insert.id, device_id))
+
+ @classmethod
+ def from_tile_id_and_rack_position(cls, tile_id, position):
+ """Get a Rack from the ID of the tile its Rack is on, and its position in the Rack."""
+
+ try:
+ rack = Rack.from_tile_id(tile_id)
+ except:
+ return cls(id=-1)
+
+ try:
+ statement = 'SELECT id FROM machines WHERE rack_id = %s AND position = %s'
+ machine_id = database.fetch_one(statement, (rack.id, position))[0]
+ except:
+ return cls(id=-1)
+
+ return cls.from_primary_key((machine_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 Machine."""
+
+ # Get the Rack
+
+ try:
+ rack = Rack.from_primary_key((self.rack_id, ))
+ except exceptions.RowNotFoundError:
+ return False
+
+ # Check the Rack's Authorization
+
+ return rack.google_id_has_at_least(google_id, authorization_level)
+
+ def insert(self):
+ """Insert this Machine by also updating its devices."""
+
+ before_insert = copy.deepcopy(self)
+
+ super(Machine, self).insert()
+
+ before_insert.id = self.id
+ self._update_devices(before_insert)
+
+ def read(self):
+ """Read this Machine by also getting its CPU, GPU, Memory and Storage IDs."""
+
+ super(Machine, self).read()
+
+ for device_table in self.device_table_to_attribute.keys():
+
+ statement = 'SELECT * FROM machine_{} WHERE machine_id = %s'.format(device_table)
+ results = database.fetch_all(statement, (self.id, ))
+
+ device_ids = []
+
+ for row in results:
+ device_ids.append(row[2])
+
+ setattr(self, self.device_table_to_attribute[device_table], device_ids)
+
+ setattr(self, 'tags', [])
+
+ def update(self):
+ """Update this Machine by also updating its devices."""
+
+ before_update = copy.deepcopy(self)
+
+ super(Machine, self).update()
+
+ before_update.id = self.id
+ self._update_devices(before_update)