summaryrefslogtreecommitdiff
path: root/web-server/opendc/api/v2/topologies/topologyId/endpoint.py
blob: 32e2dc8ada3672918fe7ecd3909d407c100e9eda (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 datetime import datetime

from opendc.util.database import Database
from opendc.models.simulation import Simulation
from opendc.models.topology import Topology
from opendc.util.rest import Response



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

    request.check_required_parameters(path={'topologyId': 'int'})

    topology = Topology.from_id(request.params_path['topologyId'])

    topology.check_exists()
    topology.check_user_access(request.google_id, False)

    return Response(200, 'Successfully retrieved topology.', topology.obj)

def PUT(request):
    """Update this topology"""
    request.check_required_parameters(path={'topologyId': 'int'},
                                      body={
                                          'topology': {
                                                'name': 'string',
                                                'rooms': {}
                                           }
                                      })
    topology = Topology.from_id(request.params_path['topologyId'])

    topology.check_exists()
    topology.check_user_access(request.google_id, True)

    topology.set_property('name', request.params_body['topology']['name'])
    topology.set_property('rooms', request.params_body['topology']['rooms'])
    topology.set_property('datetimeLastEdited', Database.datetime_to_string(datetime.now()))

    topology.update()

    return Response(200, 'Successfully updated topology.', topology.obj)

def DELETE(request):
    """Delete this topology"""
    request.check_required_parameters(path={'topologyId': 'int'})

    topology = Topology.from_id(request.params_path['topologyId'])

    topology.check_exists()
    topology.check_user_access(request.google_id, True)

    simulation = Simulation.from_id(topology.obj['simulationId'])
    simulation.check_exists()
    if request.params_path['topologyId'] in simulation.obj['topologyIds']:
        simulation.obj['topologyIds'].remove(request.params_path['topologyId'])
    simulation.update()

    topology.delete()

    return Response(200, 'Successfully deleted topology.', topology.obj)