diff options
| author | Georgios Andreadis <info@gandreadis.com> | 2020-06-29 16:05:23 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2020-08-24 16:18:36 +0200 |
| commit | 4f9a40abdc7836345113c047f27fcc96800cb3f5 (patch) | |
| tree | e443d14e34a884b1a4d9c549f81d51202eddd5f7 /web-server/opendc/api/v2/simulations/simulationId | |
| parent | cd5f7bf3a72913e1602cb4c575e61ac7d5519be0 (diff) | |
Prepare web-server repository for monorepo
This change prepares the web-server Git repository for the monorepo residing at
https://github.com/atlarge-research.com/opendc. To accomodate for this, we
move all files into a web-server subdirectory.
Diffstat (limited to 'web-server/opendc/api/v2/simulations/simulationId')
12 files changed, 521 insertions, 0 deletions
diff --git a/web-server/opendc/api/v2/simulations/simulationId/__init__.py b/web-server/opendc/api/v2/simulations/simulationId/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/web-server/opendc/api/v2/simulations/simulationId/__init__.py diff --git a/web-server/opendc/api/v2/simulations/simulationId/authorizations/__init__.py b/web-server/opendc/api/v2/simulations/simulationId/authorizations/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/web-server/opendc/api/v2/simulations/simulationId/authorizations/__init__.py diff --git a/web-server/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py b/web-server/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py new file mode 100644 index 00000000..df2b5cfd --- /dev/null +++ b/web-server/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py @@ -0,0 +1,37 @@ +from opendc.models_old.authorization import Authorization +from opendc.models_old.simulation import Simulation +from opendc.util import exceptions +from opendc.util.rest import Response + + +def GET(request): + """Find all authorizations for a Simulation.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters(path={'simulationId': 'string'}) + + except exceptions.ParameterError as e: + return Response(400, str(e)) + + # Instantiate a Simulation and make sure it exists + + simulation = Simulation.from_primary_key((request.params_path['simulationId'], )) + + if not simulation.exists(): + return Response(404, '{} not found.'.format(simulation)) + + # Make sure this User is allowed to view this Simulation's Authorizations + + if not simulation.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from retrieving Authorizations for {}.'.format(simulation)) + + # Get the Authorizations + + authorizations = Authorization.query('simulation_id', request.params_path['simulationId']) + + # Return the Authorizations + + return Response(200, 'Successfully retrieved Authorizations for {}.'.format(simulation), + [x.to_JSON() for x in authorizations]) diff --git a/web-server/opendc/api/v2/simulations/simulationId/authorizations/userId/__init__.py b/web-server/opendc/api/v2/simulations/simulationId/authorizations/userId/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/web-server/opendc/api/v2/simulations/simulationId/authorizations/userId/__init__.py diff --git a/web-server/opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py b/web-server/opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py new file mode 100644 index 00000000..121530db --- /dev/null +++ b/web-server/opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py @@ -0,0 +1,178 @@ +from opendc.models_old.authorization import Authorization +from opendc.models_old.simulation import Simulation +from opendc.models_old.user import User +from opendc.util import exceptions +from opendc.util.rest import Response + + +def DELETE(request): + """Delete a user's authorization level over a simulation.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters(path={'simulationId': 'string', 'userId': 'string'}) + + except exceptions.ParameterError as e: + return Response(400, str(e)) + + # Instantiate an Authorization + + authorization = Authorization.from_primary_key((request.params_path['userId'], request.params_path['simulationId'])) + + # Make sure this Authorization exists in the database + + if not authorization.exists(): + return Response(404, '{} not found.'.format(authorization)) + + # Make sure this User is allowed to delete this Authorization + + if not authorization.google_id_has_at_least(request.google_id, 'OWN'): + return Response(403, 'Forbidden from deleting {}.'.format(authorization)) + + # Delete this Authorization + + authorization.delete() + + return Response(200, 'Successfully deleted {}.'.format(authorization), authorization.to_JSON()) + + +def GET(request): + """Get this User's Authorization over this Simulation.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters(path={'simulationId': 'string', 'userId': 'string'}) + + except exceptions.ParameterError as e: + return Response(400, str(e)) + + # Instantiate an Authorization + + authorization = Authorization.from_primary_key((request.params_path['userId'], request.params_path['simulationId'])) + + # Make sure this Authorization exists in the database + + if not authorization.exists(): + return Response(404, '{} not found.'.format(authorization)) + + # Read this Authorization from the database + + authorization.read() + + # Return this Authorization + + return Response(200, 'Successfully retrieved {}'.format(authorization), authorization.to_JSON()) + + +def POST(request): + """Add an authorization for a user's access to a simulation.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters(path={ + 'userId': 'string', + 'simulationId': 'string' + }, + body={'authorization': { + 'authorizationLevel': 'string' + }}) + + except exceptions.ParameterError as e: + return Response(400, str(e)) + + # Instantiate an Authorization + + authorization = Authorization.from_JSON({ + 'userId': + request.params_path['userId'], + 'simulationId': + request.params_path['simulationId'], + 'authorizationLevel': + request.params_body['authorization']['authorizationLevel'] + }) + + # Make sure the Simulation and User exist + + user = User.from_primary_key((authorization.user_id, )) + if not user.exists(): + return Response(404, '{} not found.'.format(user)) + + simulation = Simulation.from_primary_key((authorization.simulation_id, )) + if not simulation.exists(): + return Response(404, '{} not found.'.format(simulation)) + + # Make sure this User is allowed to add this Authorization + + if not simulation.google_id_has_at_least(request.google_id, 'OWN'): + return Response(403, 'Forbidden from creating {}.'.format(authorization)) + + # Make sure this Authorization does not already exist + + if authorization.exists(): + return Response(409, '{} already exists.'.format(authorization)) + + # Try to insert this Authorization into the database + + try: + authorization.insert() + + except exceptions.ForeignKeyError: + return Response(400, 'Invalid authorizationLevel') + + # Return this Authorization + + return Response(200, 'Successfully added {}'.format(authorization), authorization.to_JSON()) + + +def PUT(request): + """Change a user's authorization level over a simulation.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters(path={ + 'simulationId': 'string', + 'userId': 'string' + }, + body={'authorization': { + 'authorizationLevel': 'string' + }}) + + except exceptions.ParameterError as e: + return Response(400, str(e)) + + # Instantiate and Authorization + + authorization = Authorization.from_JSON({ + 'userId': + request.params_path['userId'], + 'simulationId': + request.params_path['simulationId'], + 'authorizationLevel': + request.params_body['authorization']['authorizationLevel'] + }) + + # Make sure this Authorization exists + + if not authorization.exists(): + return Response(404, '{} not found.'.format(authorization)) + + # Make sure this User is allowed to edit this Authorization + + if not authorization.google_id_has_at_least(request.google_id, 'OWN'): + return Response(403, 'Forbidden from updating {}.'.format(authorization)) + + # Try to update this Authorization + + try: + authorization.update() + + except exceptions.ForeignKeyError as e: + return Response(400, 'Invalid authorization level.') + + # Return this Authorization + + return Response(200, 'Successfully updated {}.'.format(authorization), authorization.to_JSON()) diff --git a/web-server/opendc/api/v2/simulations/simulationId/endpoint.py b/web-server/opendc/api/v2/simulations/simulationId/endpoint.py new file mode 100644 index 00000000..282e3291 --- /dev/null +++ b/web-server/opendc/api/v2/simulations/simulationId/endpoint.py @@ -0,0 +1,57 @@ +from datetime import datetime + +from opendc.models.simulation import Simulation +from opendc.models.topology import Topology +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() + + # TODO remove all experiments + + simulation.delete() + + return Response(200, f'Successfully deleted simulation.', simulation.obj) diff --git a/web-server/opendc/api/v2/simulations/simulationId/experiments/__init__.py b/web-server/opendc/api/v2/simulations/simulationId/experiments/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/web-server/opendc/api/v2/simulations/simulationId/experiments/__init__.py diff --git a/web-server/opendc/api/v2/simulations/simulationId/experiments/endpoint.py b/web-server/opendc/api/v2/simulations/simulationId/experiments/endpoint.py new file mode 100644 index 00000000..9df84838 --- /dev/null +++ b/web-server/opendc/api/v2/simulations/simulationId/experiments/endpoint.py @@ -0,0 +1,97 @@ +from opendc.models_old.experiment import Experiment +from opendc.models_old.simulation import Simulation +from opendc.util import exceptions +from opendc.util.rest import Response + + +def GET(request): + """Get this Simulation's Experiments.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters(path={'simulationId': 'string'}) + + except exceptions.ParameterError as e: + return Response(400, str(e)) + + # Instantiate a Simulation from the database + + simulation = Simulation.from_primary_key((request.params_path['simulationId'], )) + + # Make sure this Simulation exists + + if not simulation.exists(): + return Response(404, '{} not found.'.format(simulation)) + + # Make sure this user is authorized to view this Simulation's Experiments + + if not simulation.google_id_has_at_least(request.google_id, 'VIEW'): + return Reponse(403, 'Forbidden from viewing Experiments for {}.'.format(simulation)) + + # Get and return the Experiments + + experiments = Experiment.query('simulation_id', request.params_path['simulationId']) + + return Response(200, 'Successfully retrieved Experiments for {}.'.format(simulation), + [x.to_JSON() for x in experiments]) + + +def POST(request): + """Add a new Experiment for this Simulation.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters(path={'simulationId': 'string'}, + body={ + 'experiment': { + 'simulationId': 'string', + 'pathId': 'int', + 'traceId': 'int', + 'schedulerName': 'string', + 'name': 'string' + } + }) + + except exceptions.ParameterError as e: + return Response(400, str(e)) + + # Make sure the passed object's simulation id matches the path simulation id + + if request.params_path['simulationId'] != request.params_body['experiment']['simulationId']: + return Response(403, 'ID mismatch.') + + # Instantiate a Simulation from the database + + simulation = Simulation.from_primary_key((request.params_path['simulationId'], )) + + # Make sure this Simulation exists + + if not simulation.exists(): + return Response(404, '{} not found.'.format(simulation)) + + # Make sure this user is authorized to edit this Simulation's Experiments + + if not simulation.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from adding an experiment to {}.'.format(simulation)) + + # Instantiate an Experiment + + experiment = Experiment.from_JSON(request.params_body['experiment']) + experiment.state = 'QUEUED' + experiment.last_simulated_tick = 0 + + # Try to insert this Experiment + + try: + experiment.insert() + + except exceptions.ForeignKeyError as e: + return Response(400, 'Foreign key constraint not met.' + e) + + # Return this Experiment + + experiment.read() + + return Response(200, 'Successfully added {}.'.format(experiment), experiment.to_JSON()) diff --git a/web-server/opendc/api/v2/simulations/simulationId/test_endpoint.py b/web-server/opendc/api/v2/simulations/simulationId/test_endpoint.py new file mode 100644 index 00000000..a0586aab --- /dev/null +++ b/web-server/opendc/api/v2/simulations/simulationId/test_endpoint.py @@ -0,0 +1,97 @@ +from opendc.util.database import DB + + +def test_get_simulation_non_existing(client, mocker): + mocker.patch.object(DB, 'fetch_one', return_value=None) + assert '404' in client.get('/api/v2/simulations/1').status + + +def test_get_simulation_no_authorizations(client, mocker): + mocker.patch.object(DB, 'fetch_one', return_value={'authorizations': []}) + res = client.get('/api/v2/simulations/1') + assert '403' in res.status + + +def test_get_simulation_not_authorized(client, mocker): + mocker.patch.object(DB, + 'fetch_one', + return_value={ + '_id': '1', + 'authorizations': [{ + 'simulationId': '2', + 'authorizationLevel': 'OWN' + }] + }) + res = client.get('/api/v2/simulations/1') + assert '403' in res.status + + +def test_get_simulation(client, mocker): + mocker.patch.object(DB, + 'fetch_one', + return_value={ + '_id': '1', + 'authorizations': [{ + 'simulationId': '1', + 'authorizationLevel': 'EDIT' + }] + }) + res = client.get('/api/v2/simulations/1') + assert '200' in res.status + + +def test_update_simulation_missing_parameter(client): + assert '400' in client.put('/api/v2/simulations/1').status + + +def test_update_simulation_non_existing(client, mocker): + mocker.patch.object(DB, 'fetch_one', return_value=None) + assert '404' in client.put('/api/v2/simulations/1', json={'simulation': {'name': 'S'}}).status + + +def test_update_simulation_not_authorized(client, mocker): + mocker.patch.object(DB, + 'fetch_one', + return_value={ + '_id': '1', + 'authorizations': [{ + 'simulationId': '1', + 'authorizationLevel': 'VIEW' + }] + }) + mocker.patch.object(DB, 'update', return_value={}) + assert '403' in client.put('/api/v2/simulations/1', json={'simulation': {'name': 'S'}}).status + + +def test_update_simulation(client, mocker): + mocker.patch.object(DB, + 'fetch_one', + return_value={ + '_id': '1', + 'authorizations': [{ + 'simulationId': '1', + 'authorizationLevel': 'OWN' + }] + }) + mocker.patch.object(DB, 'update', return_value={}) + + res = client.put('/api/v2/simulations/1', json={'simulation': {'name': 'S'}}) + assert '200' in res.status + + +def test_delete_simulation_non_existing(client, mocker): + mocker.patch.object(DB, 'fetch_one', return_value=None) + assert '404' in client.delete('/api/v2/simulations/1').status + + +def test_delete_simulation_different_user(client, mocker): + mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1', 'googleId': 'other_test', 'authorizations': [{'simulationId': '1', 'authorizationLevel': 'VIEW'}], 'topologyIds': []}) + mocker.patch.object(DB, 'delete_one', return_value=None) + assert '403' in client.delete('/api/v2/simulations/1').status + + +def test_delete_simulation(client, mocker): + mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1', 'googleId': 'test', 'authorizations': [{'simulationId': '1', 'authorizationLevel': 'OWN'}], 'topologyIds': []}) + mocker.patch.object(DB, 'delete_one', return_value={'googleId': 'test'}) + res = client.delete('/api/v2/simulations/1') + assert '200' in res.status diff --git a/web-server/opendc/api/v2/simulations/simulationId/topologies/__init__.py b/web-server/opendc/api/v2/simulations/simulationId/topologies/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/web-server/opendc/api/v2/simulations/simulationId/topologies/__init__.py diff --git a/web-server/opendc/api/v2/simulations/simulationId/topologies/endpoint.py b/web-server/opendc/api/v2/simulations/simulationId/topologies/endpoint.py new file mode 100644 index 00000000..ab7b7006 --- /dev/null +++ b/web-server/opendc/api/v2/simulations/simulationId/topologies/endpoint.py @@ -0,0 +1,29 @@ +from datetime import datetime + +from opendc.models.simulation import Simulation +from opendc.models.topology import Topology +from opendc.util import exceptions +from opendc.util.rest import Response +from opendc.util.database import Database + + +def POST(request): + """Add a new Topology to the specified simulation and return it""" + + request.check_required_parameters(path={'simulationId': 'string'}, body={'topology': {'name': 'string'}}) + + simulation = Simulation.from_id(request.params_path['simulationId']) + + simulation.check_exists() + simulation.check_user_access(request.google_id, True) + + topology = Topology({'name': request.params_body['topology']['name']}) + topology.set_property('datetimeCreated', Database.datetime_to_string(datetime.now())) + topology.set_property('datetimeLastEdited', Database.datetime_to_string(datetime.now())) + topology.insert() + + simulation.obj['topologyIds'].append(topology.obj['_id']) + simulation.set_property('datetimeLastEdited', Database.datetime_to_string(datetime.now())) + simulation.update() + + return Response(200, 'Successfully inserted topology.', topology.obj) diff --git a/web-server/opendc/api/v2/simulations/simulationId/topologies/test_endpoint.py b/web-server/opendc/api/v2/simulations/simulationId/topologies/test_endpoint.py new file mode 100644 index 00000000..10b5e3c9 --- /dev/null +++ b/web-server/opendc/api/v2/simulations/simulationId/topologies/test_endpoint.py @@ -0,0 +1,26 @@ +from opendc.util.database import DB + + +def test_add_topology_missing_parameter(client): + assert '400' in client.post('/api/v2/simulations/1/topologies/').status + + +def test_add_topology(client, mocker): + mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1', 'authorizations': [{'simulationId': '1', 'authorizationLevel': 'OWN'}], 'topologyIds': []}) + mocker.patch.object(DB, + 'insert', + return_value={ + '_id': '1', + 'datetimeCreated': '000', + 'datetimeEdit': '000', + 'topologyIds': [] + }) + mocker.patch.object(DB, 'update', return_value={}) + res = client.post('/api/v2/simulations/1/topologies/', json={'topology': {'name': 'test simulation'}}) + assert 'datetimeCreated' in res.json['content'] + assert 'datetimeEdit' in res.json['content'] + assert 'topologyIds' in res.json['content'] + assert '200' in res.status + +def test_add_topology_no_authorizations(client, mocker): + pass
\ No newline at end of file |
