summaryrefslogtreecommitdiff
path: root/opendc/models/rack.py
blob: b547afa1837a8fdf59dff490ad85bcd6c615b6a9 (plain)
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
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/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()