summaryrefslogtreecommitdiff
path: root/web-server/opendc/api/v2/simulations/simulationId/endpoint.py
blob: 0cee3e9c841457e7a0c47368fd900635ec57627b (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
from datetime import datetime

from opendc.models.experiment import Experiment
from opendc.models.simulation import Simulation
from opendc.models.topology import Topology
from opendc.models.user import User
from opendc.util.database import Database
from opendc.util.rest import Response


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

    request.check_required_parameters(path={'simulationId': 'string'})

    simulation = Simulation.from_id(request.params_path['simulationId'])

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

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


def PUT(request):
    """Update a simulation's name."""

    request.check_required_parameters(body={'simulation': {'name': 'name'}}, path={'simulationId': 'string'})

    simulation = Simulation.from_id(request.params_path['simulationId'])

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

    simulation.set_property('name', request.params_body['simulation']['name'])
    simulation.set_property('datetime_last_edited', Database.datetime_to_string(datetime.now()))
    simulation.update()

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


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

    request.check_required_parameters(path={'simulationId': 'string'})

    simulation = Simulation.from_id(request.params_path['simulationId'])

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

    for topology_id in simulation.obj['topologyIds']:
        topology = Topology.from_id(topology_id)
        topology.delete()

    for experiment_id in simulation.obj['experimentIds']:
        experiment = Experiment.from_id(experiment_id)
        experiment.delete()

    user = User.from_google_id(request.google_id)
    user.obj['authorizations'] = list(
        filter(lambda x: str(x['simulationId']) != request.params_path['simulationId'], user.obj['authorizations']))
    user.update()

    old_object = simulation.delete()

    return Response(200, 'Successfully deleted simulation.', old_object)