summaryrefslogtreecommitdiff
path: root/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/endpoint.py
blob: 44ed82d6895e3eb0643a101c8d6b4f4f82312ccf (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
from opendc.models.room import Room
from opendc.util import database, exceptions
from opendc.util.rest import Response

def GET(request):
    """Get this Room."""

    # Make sure required parameters are there

    try:
        request.check_required_parameters(
            path = {
                'simulationId': 'int',
                'datacenterId': 'int',
                'roomId': 'int'
            }
        )

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

    # Instantiate a Room from the database

    room = Room.from_primary_key((request.params_path['roomId'],))

    # Make sure this Room exists

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

    # Make sure this user is authorized to view this Room

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

    # Return this Room

    room.read()

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

def PUT(request):
    """Update this Room's name and type."""

    # Make sure required parameters are there
    
    try:
        request.check_required_parameters(
            path = {
                'simulationId': 'int',
                'datacenterId': 'int',
                'roomId': 'int'
            },
            body = {
                'room': {
                    'name': 'string',
                    'roomType': 'string'
                }
            }
        )
        
    except exceptions.ParameterError as e:
        return Response(400, e.message)

    # Instantiate a Room from the database

    room = Room.from_primary_key((request.params_path['roomId'],))

    # Make sure this Room exists

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

    # Make sure this user is authorized to edit this Room

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

    # Update this Room

    room.name = request.params_body['room']['name']
    room.type = request.params_body['room']['roomType']

    try:
        room.update()
    except exceptions.ForeignKeyError:
        return Response(400, 'Invalid `roomType` or existing `name`.')

    # Return this Room

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

def DELETE(request):
    """Delete this Room."""

    # Make sure required parameters are there

    try:
        request.check_required_parameters(
            path = {
                'simulationId': 'int',
                'datacenterId': 'int',
                'roomId': 'int'
            }
        )

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

    # Instantiate a Room and make sure it exists

    room = Room.from_primary_key((request.params_path['roomId'],))

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

    # Make sure this user is authorized to delete this Room

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

    # Delete this Room

    room.delete()

    # Return this Room

    return Response(
        200,
        'Sucessfully deleted {}.'.format(room),
        room.to_JSON()
    )