summaryrefslogtreecommitdiff
path: root/opendc/api/v1/tiles/tileId/rack/machines/position/endpoint.py
blob: 99011fa420fe3cb6261c71f3be6ec3c7c8249cba (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from opendc.models.machine import Machine
from opendc.models.rack import Rack
from opendc.util import exceptions
from opendc.util.rest import Response


def GET(request):
    """Get the Machine at this location in this Rack."""

    # Make sure required parameters are there

    try:
        request.check_required_parameters(
            path={
                'tileId': 'int',
                'position': 'int'
            }
        )

    except exceptions.ParameterError as e:
        return Response(400, e.message)

    # Instantiate a Machine from the database

    machine = Machine.from_tile_id_and_rack_position(request.params_path['tileId'], request.params_path['position'])

    # Make sure this Machine exists

    if not machine.exists():
        return Response(404, '{} not found.'.format(machine))

    # Make sure this user is authorized to view this Machine

    if not machine.google_id_has_at_least(request.google_id, 'VIEW'):
        return Response(403, 'Forbidden from retrieving {}.'.format(machine))

    # Return this Machine

    machine.read()

    return Response(
        200,
        'Successfully retrieved {}.'.format(machine),
        machine.to_JSON()
    )


def PUT(request):
    """Update the Machine at this location in this Rack."""

    try:
        request.check_required_parameters(
            path={
                'tileId': 'int',
                'position': 'int'
            },
            body={
                'machine': {
                    'rackId': 'int',
                    'position': 'int',
                    'tags': 'list-string',
                    'cpuIds': 'list-int',
                    'gpuIds': 'list-int',
                    'memoryIds': 'list-int',
                    'storageIds': 'list-int'
                }
            }
        )

    except exceptions.ParameterError as e:
        return Response(400, e.message)

    # Instantiate a Machine from the database

    machine = Machine.from_tile_id_and_rack_position(request.params_path['tileId'], request.params_path['position'])

    # Make sure this Machine exists

    if not machine.exists():
        return Response(404, '{} not found.'.format(machine))

    # Make sure this Machine's rack ID is right

    rack = Rack.from_tile_id(request.params_path['tileId'])

    if not rack.exists() or rack.id != request.params_body['machine']['rackId']:
        return Response(400, 'Mismatch in Rack IDs.')

    # Make sure this user is authorized to edit this Machine

    if not machine.google_id_has_at_least(request.google_id, 'EDIT'):
        return Response(403, 'Forbidden from retrieving {}.'.format(machine))

    # Update this Machine

    machine.position = request.params_body['machine']['position']
    machine.tags = request.params_body['machine']['tags']
    machine.cpu_ids = request.params_body['machine']['cpuIds']
    machine.gpu_ids = request.params_body['machine']['gpuIds']
    machine.memory_ids = request.params_body['machine']['memoryIds']
    machine.storage_ids = request.params_body['machine']['storageIds']

    try:
        machine.update()

    except exceptions.ForeignKeyError:
        return Response(409, 'Rack position occupied.')

    except Exception as e:
        print e
        return Response(400, 'Invalid Machine.')

    # Return this Machine

    machine.read()

    return Response(
        200,
        'Successfully updated {}.'.format(machine),
        machine.to_JSON()
    )


def DELETE(request):
    """Delete the Machine at this location in this Rack."""

    # Make sure required parameters are there

    try:
        request.check_required_parameters(
            path={
                'tileId': 'int',
                'position': 'int'
            }
        )

    except exceptions.ParameterError as e:
        return Response(400, e.message)

    # Instantiate a Machine from the database

    machine = Machine.from_tile_id_and_rack_position(request.params_path['tileId'], request.params_path['position'])

    # Make sure this Machine exists

    if not machine.exists():
        return Response(404, '{} not found.'.format(machine))

    # Make sure this user is authorized to edit this Machine

    if not machine.google_id_has_at_least(request.google_id, 'EDIT'):
        return Response(403, 'Forbidden from retrieving {}.'.format(machine))

    # Delete this Machine

    machine.delete()

    # Return this Machine

    return Response(
        200,
        'Successfully deleted {}.'.format(machine),
        machine.to_JSON()
    )