summaryrefslogtreecommitdiff
path: root/opendc-web/opendc-web-api/opendc/models/topology.py
blob: c6354ae69e1980e1fcf2b7b6b168d46c99766cc2 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from marshmallow import Schema, fields

from opendc.models.project import Project
from opendc.models.model import Model


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):
    """Model representing a Project."""

    collection_name = 'topologies'

    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 user_id: The User ID of the user.
        :param edit_access: True when edit access should be checked, otherwise view access.
        """
        project = Project.from_id(self.obj['projectId'])
        project.check_user_access(user_id, edit_access)