diff options
| author | jc0b <j@jc0b.computer> | 2020-07-07 16:55:22 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2020-08-24 19:47:51 +0200 |
| commit | 223e916997eb641a1662110b6de630a4cdfdf479 (patch) | |
| tree | 90ca4364461f676db45f25e03d8f22fc32f9fdd8 /web-server | |
| parent | 9ff1e3c6bae253372a468dbdc9b8369ab8dd2c6f (diff) | |
| parent | b810c4413079bf5aeb5374f1cd20e151a83530d0 (diff) | |
Merge branch 'feature/mongodb-migration' of github.com:atlarge-research/opendc-dev into feature/mongodb-migration
Diffstat (limited to 'web-server')
31 files changed, 297 insertions, 319 deletions
diff --git a/web-server/README.md b/web-server/README.md index e5a46016..84fd09cc 100644 --- a/web-server/README.md +++ b/web-server/README.md @@ -32,7 +32,7 @@ The `Util` package handles several miscellaneous tasks: ### API Package -The `API` package contains the logic for the HTTP methods in each API endpoint. Packages are structured to mirror the API: the code for the endpoint `GET api/simulations`, for example, would be located at the `endpoint.py` inside the `simulations` package (so at `api/simulations/endpoint.py`). +The `API` package contains the logic for the HTTP methods in each API endpoint. Packages are structured to mirror the API: the code for the endpoint `GET api/projects`, for example, would be located at the `endpoint.py` inside the `projects` package (so at `api/projects/endpoint.py`). An `endpoint.py` file contains methods for each HTTP method it supports, which takes a request as input (such as `def GET(request):`). Typically, such a method checks whether the parameters were passed correctly (using the `Parameter Checker`); fetches some model from the database; checks whether the data exists and is accessible by the user who made the request; possibly modifies this data and writes it back to the database; and returns a JSON representation of the model. @@ -40,7 +40,7 @@ The `REST` component dynamically imports the appropriate method from the appropr ### Models Package -The `models` package contains the logic for mapping Python objects to their database representations. This involves an abstract `model` which has generic CRUD operations. Extensions of `model`, such as a `User` or `Simulation`, specify some more specific operations and their collection metadata. +The `models` package contains the logic for mapping Python objects to their database representations. This involves an abstract `model` which has generic CRUD operations. Extensions of `model`, such as a `User` or `Project`, specify some more specific operations and their collection metadata. `Endpoint`s import these `models` and use them to execute requests. @@ -58,7 +58,7 @@ Make sure you have Python 3.7+ installed (if not, get it [here](https://www.pyth pip install -r requirements.txt ``` -The web server also requires a running MongoDB instance. We recommend setting this up through docker, by running `docker-compose build` and `docker-compose up` in the [`mongodb` directory](../mongodb) of the main OpenDC repository. +The web server also requires a running MongoDB instance. We recommend setting this up through docker, by running `docker-compose build` and `docker-compose up` in the [`mongodb` directory](../database) of the main OpenDC repository. #### Get and configure the code diff --git a/web-server/main.py b/web-server/main.py index 7f499b34..af3e95b9 100644 --- a/web-server/main.py +++ b/web-server/main.py @@ -138,12 +138,12 @@ def serve_web_server_test(): @FLASK_CORE_APP.route('/') -@FLASK_CORE_APP.route('/simulations') -@FLASK_CORE_APP.route('/simulations/<path:simulation_id>') -@FLASK_CORE_APP.route('/simulations/<path:simulation_id>/experiments') -@FLASK_CORE_APP.route('/simulations/<path:simulation_id>/experiments/<path:experiment_id>') +@FLASK_CORE_APP.route('/projects') +@FLASK_CORE_APP.route('/projects/<path:project_id>') +@FLASK_CORE_APP.route('/projects/<path:project_id>/experiments') +@FLASK_CORE_APP.route('/projects/<path:project_id>/experiments/<path:experiment_id>') @FLASK_CORE_APP.route('/profile') -def serve_index(simulation_id=None, experiment_id=None): +def serve_index(project_id=None, experiment_id=None): return send_from_directory(STATIC_ROOT, 'index.html') diff --git a/web-server/opendc/api/v2/experiments/experimentId/endpoint.py b/web-server/opendc/api/v2/experiments/experimentId/endpoint.py index 1611f889..6706dc57 100644 --- a/web-server/opendc/api/v2/experiments/experimentId/endpoint.py +++ b/web-server/opendc/api/v2/experiments/experimentId/endpoint.py @@ -1,5 +1,5 @@ from opendc.models.experiment import Experiment -from opendc.models.simulation import Simulation +from opendc.models.project import Project from opendc.util.rest import Response @@ -19,12 +19,9 @@ def GET(request): def PUT(request): """Update this Experiments name.""" - request.check_required_parameters(path={'experimentId': 'string'}, - body={ - 'experiment': { - 'name': 'string', - } - }) + request.check_required_parameters(path={'experimentId': 'string'}, body={'experiment': { + 'name': 'string', + }}) experiment = Experiment.from_id(request.params_path['experimentId']) @@ -48,11 +45,11 @@ def DELETE(request): experiment.check_exists() experiment.check_user_access(request.google_id, True) - simulation = Simulation.from_id(experiment.obj['simulationId']) - simulation.check_exists() - if request.params_path['experimentId'] in simulation.obj['experimentIds']: - simulation.obj['experimentIds'].remove(request.params_path['experimentId']) - simulation.update() + project = Project.from_id(experiment.obj['projectId']) + project.check_exists() + if request.params_path['experimentId'] in project.obj['experimentIds']: + project.obj['experimentIds'].remove(request.params_path['experimentId']) + project.update() old_object = experiment.delete() diff --git a/web-server/opendc/api/v2/experiments/experimentId/test_endpoint.py b/web-server/opendc/api/v2/experiments/experimentId/test_endpoint.py index f3aa111c..a284cf32 100644 --- a/web-server/opendc/api/v2/experiments/experimentId/test_endpoint.py +++ b/web-server/opendc/api/v2/experiments/experimentId/test_endpoint.py @@ -7,7 +7,7 @@ def test_get_experiment_non_existing(client, mocker): def test_get_experiment_no_authorizations(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value={'simulationId': '1', 'authorizations': []}) + mocker.patch.object(DB, 'fetch_one', return_value={'projectId': '1', 'authorizations': []}) res = client.get('/api/v2/experiments/1') assert '403' in res.status @@ -16,10 +16,10 @@ def test_get_experiment_not_authorized(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value={ - 'simulationId': '1', + 'projectId': '1', '_id': '1', 'authorizations': [{ - 'simulationId': '2', + 'projectId': '2', 'authorizationLevel': 'OWN' }] }) @@ -31,10 +31,10 @@ def test_get_experiment(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value={ - 'simulationId': '1', + 'projectId': '1', '_id': '1', 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'EDIT' }] }) @@ -48,12 +48,11 @@ def test_update_experiment_missing_parameter(client): def test_update_experiment_non_existing(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value=None) - assert '404' in client.put('/api/v2/experiments/1', - json={ - 'experiment': { - 'name': 'test', - } - }).status + assert '404' in client.put('/api/v2/experiments/1', json={ + 'experiment': { + 'name': 'test', + } + }).status def test_update_experiment_not_authorized(client, mocker): @@ -61,19 +60,18 @@ def test_update_experiment_not_authorized(client, mocker): 'fetch_one', return_value={ '_id': '1', - 'simulationId': '1', + 'projectId': '1', 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'VIEW' }] }) mocker.patch.object(DB, 'update', return_value={}) - assert '403' in client.put('/api/v2/experiments/1', - json={ - 'experiment': { - 'name': 'test', - } - }).status + assert '403' in client.put('/api/v2/experiments/1', json={ + 'experiment': { + 'name': 'test', + } + }).status def test_update_experiment(client, mocker): @@ -81,36 +79,34 @@ def test_update_experiment(client, mocker): 'fetch_one', return_value={ '_id': '1', - 'simulationId': '1', + 'projectId': '1', 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'OWN' }] }) mocker.patch.object(DB, 'update', return_value={}) - res = client.put( - '/api/v2/experiments/1', - json={'experiment': { - 'name': 'test', - }}) + res = client.put('/api/v2/experiments/1', json={'experiment': { + 'name': 'test', + }}) assert '200' in res.status -def test_delete_simulation_non_existing(client, mocker): +def test_delete_project_non_existing(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value=None) assert '404' in client.delete('/api/v2/experiments/1').status -def test_delete_simulation_different_user(client, mocker): +def test_delete_project_different_user(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value={ '_id': '1', - 'simulationId': '1', + 'projectId': '1', 'googleId': 'other_test', 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'VIEW' }] }) @@ -118,16 +114,16 @@ def test_delete_simulation_different_user(client, mocker): assert '403' in client.delete('/api/v2/experiments/1').status -def test_delete_simulation(client, mocker): +def test_delete_project(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value={ '_id': '1', - 'simulationId': '1', + 'projectId': '1', 'googleId': 'test', 'experimentIds': ['1'], 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'OWN' }] }) diff --git a/web-server/opendc/api/v2/paths.json b/web-server/opendc/api/v2/paths.json index 720e34cb..01a7dcff 100644 --- a/web-server/opendc/api/v2/paths.json +++ b/web-server/opendc/api/v2/paths.json @@ -1,16 +1,16 @@ [ "/users", "/users/{userId}", - "/simulations", - "/simulations/{simulationId}", - "/simulations/{simulationId}/authorizations", - "/simulations/{simulationId}/topologies", + "/projects", + "/projects/{projectId}", + "/projects/{projectId}/authorizations", + "/projects/{projectId}/topologies", "/topologies/{topologyId}", - "/simulations/{simulationId}/experiments", + "/projects/{projectId}/experiments", "/experiments/{experimentId}", "/schedulers", "/traces", "/traces/{traceId}", "/prefabs", - "/prefabs/{prefabId}", + "/prefabs/{prefabId}" ] diff --git a/web-server/opendc/api/v2/simulations/__init__.py b/web-server/opendc/api/v2/projects/__init__.py index e69de29b..e69de29b 100644 --- a/web-server/opendc/api/v2/simulations/__init__.py +++ b/web-server/opendc/api/v2/projects/__init__.py diff --git a/web-server/opendc/api/v2/projects/endpoint.py b/web-server/opendc/api/v2/projects/endpoint.py new file mode 100644 index 00000000..10954cdd --- /dev/null +++ b/web-server/opendc/api/v2/projects/endpoint.py @@ -0,0 +1,32 @@ +from datetime import datetime + +from opendc.models.project import Project +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 POST(request): + """Create a new project, and return that new project.""" + + request.check_required_parameters(body={'project': {'name': 'string'}}) + + topology = Topology({'name': 'Default topology', 'rooms': []}) + topology.insert() + + project = Project(request.params_body['project']) + project.set_property('datetimeCreated', Database.datetime_to_string(datetime.now())) + project.set_property('datetimeLastEdited', Database.datetime_to_string(datetime.now())) + project.set_property('topologyIds', [topology.get_id()]) + project.set_property('experimentIds', []) + project.insert() + + topology.set_property('projectId', project.get_id()) + topology.update() + + user = User.from_google_id(request.google_id) + user.obj['authorizations'].append({'projectId': project.get_id(), 'authorizationLevel': 'OWN'}) + user.update() + + return Response(200, 'Successfully created project.', project.obj) diff --git a/web-server/opendc/api/v2/simulations/simulationId/__init__.py b/web-server/opendc/api/v2/projects/projectId/__init__.py index e69de29b..e69de29b 100644 --- a/web-server/opendc/api/v2/simulations/simulationId/__init__.py +++ b/web-server/opendc/api/v2/projects/projectId/__init__.py diff --git a/web-server/opendc/api/v2/simulations/simulationId/authorizations/__init__.py b/web-server/opendc/api/v2/projects/projectId/authorizations/__init__.py index e69de29b..e69de29b 100644 --- a/web-server/opendc/api/v2/simulations/simulationId/authorizations/__init__.py +++ b/web-server/opendc/api/v2/projects/projectId/authorizations/__init__.py diff --git a/web-server/opendc/api/v2/projects/projectId/authorizations/endpoint.py b/web-server/opendc/api/v2/projects/projectId/authorizations/endpoint.py new file mode 100644 index 00000000..9f6a60ec --- /dev/null +++ b/web-server/opendc/api/v2/projects/projectId/authorizations/endpoint.py @@ -0,0 +1,17 @@ +from opendc.models.project import Project +from opendc.util.rest import Response + + +def GET(request): + """Find all authorizations for a Project.""" + + request.check_required_parameters(path={'projectId': 'string'}) + + project = Project.from_id(request.params_path['projectId']) + + project.check_exists() + project.check_user_access(request.google_id, False) + + authorizations = project.get_all_authorizations() + + return Response(200, 'Successfully retrieved project authorizations', authorizations) diff --git a/web-server/opendc/api/v2/simulations/simulationId/authorizations/test_endpoint.py b/web-server/opendc/api/v2/projects/projectId/authorizations/test_endpoint.py index 4369d807..c3bbc093 100644 --- a/web-server/opendc/api/v2/simulations/simulationId/authorizations/test_endpoint.py +++ b/web-server/opendc/api/v2/projects/projectId/authorizations/test_endpoint.py @@ -4,7 +4,7 @@ from opendc.util.database import DB def test_get_authorizations_non_existing(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value=None) mocker.patch.object(DB, 'fetch_all', return_value=None) - assert '404' in client.get('/api/v2/simulations/1/authorizations').status + assert '404' in client.get('/api/v2/projects/1/authorizations').status def test_get_authorizations_not_authorized(client, mocker): @@ -14,12 +14,12 @@ def test_get_authorizations_not_authorized(client, mocker): '_id': '1', 'name': 'test trace', 'authorizations': [{ - 'simulationId': '2', + 'projectId': '2', 'authorizationLevel': 'OWN' }] }) mocker.patch.object(DB, 'fetch_all', return_value=[]) - res = client.get('/api/v2/simulations/1/authorizations') + res = client.get('/api/v2/projects/1/authorizations') assert '403' in res.status @@ -30,11 +30,11 @@ def test_get_authorizations(client, mocker): '_id': '1', 'name': 'test trace', 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'OWN' }] }) mocker.patch.object(DB, 'fetch_all', return_value=[]) - res = client.get('/api/v2/simulations/1/authorizations') + res = client.get('/api/v2/projects/1/authorizations') assert len(res.json['content']) == 0 assert '200' in res.status diff --git a/web-server/opendc/api/v2/projects/projectId/endpoint.py b/web-server/opendc/api/v2/projects/projectId/endpoint.py new file mode 100644 index 00000000..6f80f827 --- /dev/null +++ b/web-server/opendc/api/v2/projects/projectId/endpoint.py @@ -0,0 +1,66 @@ +from datetime import datetime + +from opendc.models.experiment import Experiment +from opendc.models.project import Project +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 Project.""" + + request.check_required_parameters(path={'projectId': 'string'}) + + project = Project.from_id(request.params_path['projectId']) + + project.check_exists() + project.check_user_access(request.google_id, False) + + return Response(200, 'Successfully retrieved project', project.obj) + + +def PUT(request): + """Update a project's name.""" + + request.check_required_parameters(body={'project': {'name': 'name'}}, path={'projectId': 'string'}) + + project = Project.from_id(request.params_path['projectId']) + + project.check_exists() + project.check_user_access(request.google_id, True) + + project.set_property('name', request.params_body['project']['name']) + project.set_property('datetime_last_edited', Database.datetime_to_string(datetime.now())) + project.update() + + return Response(200, 'Successfully updated project.', project.obj) + + +def DELETE(request): + """Delete this Project.""" + + request.check_required_parameters(path={'projectId': 'string'}) + + project = Project.from_id(request.params_path['projectId']) + + project.check_exists() + project.check_user_access(request.google_id, True) + + for topology_id in project.obj['topologyIds']: + topology = Topology.from_id(topology_id) + topology.delete() + + for experiment_id in project.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['projectId']) != request.params_path['projectId'], user.obj['authorizations'])) + user.update() + + old_object = project.delete() + + return Response(200, 'Successfully deleted project.', old_object) diff --git a/web-server/opendc/api/v2/simulations/simulationId/experiments/__init__.py b/web-server/opendc/api/v2/projects/projectId/experiments/__init__.py index e69de29b..e69de29b 100644 --- a/web-server/opendc/api/v2/simulations/simulationId/experiments/__init__.py +++ b/web-server/opendc/api/v2/projects/projectId/experiments/__init__.py diff --git a/web-server/opendc/api/v2/simulations/simulationId/experiments/endpoint.py b/web-server/opendc/api/v2/projects/projectId/experiments/endpoint.py index 0d7c208d..2e5b93df 100644 --- a/web-server/opendc/api/v2/simulations/simulationId/experiments/endpoint.py +++ b/web-server/opendc/api/v2/projects/projectId/experiments/endpoint.py @@ -1,12 +1,12 @@ from opendc.models.experiment import Experiment -from opendc.models.simulation import Simulation +from opendc.models.project import Project from opendc.util.rest import Response def POST(request): - """Add a new Experiment for this Simulation.""" + """Add a new Experiment for this Project.""" - request.check_required_parameters(path={'simulationId': 'string'}, + request.check_required_parameters(path={'projectId': 'string'}, body={ 'experiment': { 'topologyId': 'string', @@ -16,20 +16,20 @@ def POST(request): } }) - simulation = Simulation.from_id(request.params_path['simulationId']) + project = Project.from_id(request.params_path['projectId']) - simulation.check_exists() - simulation.check_user_access(request.google_id, True) + project.check_exists() + project.check_user_access(request.google_id, True) experiment = Experiment(request.params_body['experiment']) - experiment.set_property('simulationId', request.params_path['simulationId']) + experiment.set_property('projectId', request.params_path['projectId']) experiment.set_property('state', 'QUEUED') experiment.set_property('lastSimulatedTick', 0) experiment.insert() - simulation.obj['experimentIds'].append(experiment.get_id()) - simulation.update() + project.obj['experimentIds'].append(experiment.get_id()) + project.update() return Response(200, 'Successfully added Experiment.', experiment.obj) diff --git a/web-server/opendc/api/v2/simulations/simulationId/experiments/test_endpoint.py b/web-server/opendc/api/v2/projects/projectId/experiments/test_endpoint.py index 1fe09b10..11b79154 100644 --- a/web-server/opendc/api/v2/simulations/simulationId/experiments/test_endpoint.py +++ b/web-server/opendc/api/v2/projects/projectId/experiments/test_endpoint.py @@ -2,12 +2,12 @@ from opendc.util.database import DB def test_add_experiment_missing_parameter(client): - assert '400' in client.post('/api/v2/simulations/1/experiments').status + assert '400' in client.post('/api/v2/projects/1/experiments').status -def test_add_experiment_non_existing_simulation(client, mocker): +def test_add_experiment_non_existing_project(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value=None) - assert '404' in client.post('/api/v2/simulations/1/experiments', + assert '404' in client.post('/api/v2/projects/1/experiments', json={ 'experiment': { 'topologyId': '1', @@ -23,13 +23,13 @@ def test_add_experiment_not_authorized(client, mocker): 'fetch_one', return_value={ '_id': '1', - 'simulationId': '1', + 'projectId': '1', 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'VIEW' }] }) - assert '403' in client.post('/api/v2/simulations/1/experiments', + assert '403' in client.post('/api/v2/projects/1/experiments', json={ 'experiment': { 'topologyId': '1', @@ -45,10 +45,10 @@ def test_add_experiment(client, mocker): 'fetch_one', return_value={ '_id': '1', - 'simulationId': '1', + 'projectId': '1', 'experimentIds': ['1'], 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'EDIT' }] }) @@ -65,7 +65,7 @@ def test_add_experiment(client, mocker): }) mocker.patch.object(DB, 'update', return_value=None) res = client.post( - '/api/v2/simulations/1/experiments', + '/api/v2/projects/1/experiments', json={'experiment': { 'topologyId': '1', 'traceId': '1', diff --git a/web-server/opendc/api/v2/simulations/simulationId/test_endpoint.py b/web-server/opendc/api/v2/projects/projectId/test_endpoint.py index 3ab0161d..c7450b5a 100644 --- a/web-server/opendc/api/v2/simulations/simulationId/test_endpoint.py +++ b/web-server/opendc/api/v2/projects/projectId/test_endpoint.py @@ -1,113 +1,113 @@ from opendc.util.database import DB -def test_get_simulation_non_existing(client, mocker): +def test_get_project_non_existing(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value=None) - assert '404' in client.get('/api/v2/simulations/1').status + assert '404' in client.get('/api/v2/projects/1').status -def test_get_simulation_no_authorizations(client, mocker): +def test_get_project_no_authorizations(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value={'authorizations': []}) - res = client.get('/api/v2/simulations/1') + res = client.get('/api/v2/projects/1') assert '403' in res.status -def test_get_simulation_not_authorized(client, mocker): +def test_get_project_not_authorized(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value={ '_id': '1', 'authorizations': [{ - 'simulationId': '2', + 'projectId': '2', 'authorizationLevel': 'OWN' }] }) - res = client.get('/api/v2/simulations/1') + res = client.get('/api/v2/projects/1') assert '403' in res.status -def test_get_simulation(client, mocker): +def test_get_project(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value={ '_id': '1', 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'EDIT' }] }) - res = client.get('/api/v2/simulations/1') + res = client.get('/api/v2/projects/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_project_missing_parameter(client): + assert '400' in client.put('/api/v2/projects/1').status -def test_update_simulation_non_existing(client, mocker): +def test_update_project_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 + assert '404' in client.put('/api/v2/projects/1', json={'project': {'name': 'S'}}).status -def test_update_simulation_not_authorized(client, mocker): +def test_update_project_not_authorized(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value={ '_id': '1', 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'VIEW' }] }) mocker.patch.object(DB, 'update', return_value={}) - assert '403' in client.put('/api/v2/simulations/1', json={'simulation': {'name': 'S'}}).status + assert '403' in client.put('/api/v2/projects/1', json={'project': {'name': 'S'}}).status -def test_update_simulation(client, mocker): +def test_update_project(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value={ '_id': '1', 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'OWN' }] }) mocker.patch.object(DB, 'update', return_value={}) - res = client.put('/api/v2/simulations/1', json={'simulation': {'name': 'S'}}) + res = client.put('/api/v2/projects/1', json={'project': {'name': 'S'}}) assert '200' in res.status -def test_delete_simulation_non_existing(client, mocker): +def test_delete_project_non_existing(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value=None) - assert '404' in client.delete('/api/v2/simulations/1').status + assert '404' in client.delete('/api/v2/projects/1').status -def test_delete_simulation_different_user(client, mocker): +def test_delete_project_different_user(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value={ '_id': '1', 'googleId': 'other_test', 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'VIEW' }], 'topologyIds': [] }) mocker.patch.object(DB, 'delete_one', return_value=None) - assert '403' in client.delete('/api/v2/simulations/1').status + assert '403' in client.delete('/api/v2/projects/1').status -def test_delete_simulation(client, mocker): +def test_delete_project(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value={ '_id': '1', 'googleId': 'test', 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'OWN' }], 'topologyIds': [], @@ -115,5 +115,5 @@ def test_delete_simulation(client, mocker): }) mocker.patch.object(DB, 'update', return_value=None) mocker.patch.object(DB, 'delete_one', return_value={'googleId': 'test'}) - res = client.delete('/api/v2/simulations/1') + res = client.delete('/api/v2/projects/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/projects/projectId/topologies/__init__.py index e69de29b..e69de29b 100644 --- a/web-server/opendc/api/v2/simulations/simulationId/topologies/__init__.py +++ b/web-server/opendc/api/v2/projects/projectId/topologies/__init__.py diff --git a/web-server/opendc/api/v2/projects/projectId/topologies/endpoint.py b/web-server/opendc/api/v2/projects/projectId/topologies/endpoint.py new file mode 100644 index 00000000..211dc15d --- /dev/null +++ b/web-server/opendc/api/v2/projects/projectId/topologies/endpoint.py @@ -0,0 +1,31 @@ +from datetime import datetime + +from opendc.models.project import Project +from opendc.models.topology import Topology +from opendc.util.rest import Response +from opendc.util.database import Database + + +def POST(request): + """Add a new Topology to the specified project and return it""" + + request.check_required_parameters(path={'projectId': 'string'}, body={'topology': {'name': 'string'}}) + + project = Project.from_id(request.params_path['projectId']) + + project.check_exists() + project.check_user_access(request.google_id, True) + + topology = Topology({ + 'projectId': request.params_path['projectId'], + 'name': request.params_body['topology']['name'], + 'rooms': request.params_body['topology']['rooms'], + }) + + topology.insert() + + project.obj['topologyIds'].append(topology.get_id()) + project.set_property('datetimeLastEdited', Database.datetime_to_string(datetime.now())) + project.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/projects/projectId/topologies/test_endpoint.py index 74a1e398..ca123a73 100644 --- a/web-server/opendc/api/v2/simulations/simulationId/topologies/test_endpoint.py +++ b/web-server/opendc/api/v2/projects/projectId/topologies/test_endpoint.py @@ -2,7 +2,7 @@ from opendc.util.database import DB def test_add_topology_missing_parameter(client): - assert '400' in client.post('/api/v2/simulations/1/topologies').status + assert '400' in client.post('/api/v2/projects/1/topologies').status def test_add_topology(client, mocker): @@ -11,7 +11,7 @@ def test_add_topology(client, mocker): return_value={ '_id': '1', 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'OWN' }], 'topologyIds': [] @@ -25,9 +25,8 @@ def test_add_topology(client, mocker): '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 'datetimeLastEdited' in res.json['content'] + res = client.post('/api/v2/projects/1/topologies', json={'topology': {'name': 'test project', 'rooms': []}}) + assert 'rooms' in res.json['content'] assert '200' in res.status @@ -36,13 +35,13 @@ def test_add_topology_not_authorized(client, mocker): 'fetch_one', return_value={ '_id': '1', - 'simulationId': '1', + 'projectId': '1', 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'VIEW' }] }) - assert '403' in client.post('/api/v2/simulations/1/topologies', + assert '403' in client.post('/api/v2/projects/1/topologies', json={ 'topology': { 'name': 'test_topology', diff --git a/web-server/opendc/api/v2/simulations/test_endpoint.py b/web-server/opendc/api/v2/projects/test_endpoint.py index ae1b5a7c..a50735b0 100644 --- a/web-server/opendc/api/v2/simulations/test_endpoint.py +++ b/web-server/opendc/api/v2/projects/test_endpoint.py @@ -1,11 +1,11 @@ from opendc.util.database import DB -def test_add_simulation_missing_parameter(client): - assert '400' in client.post('/api/v2/simulations').status +def test_add_project_missing_parameter(client): + assert '400' in client.post('/api/v2/projects').status -def test_add_simulation(client, mocker): +def test_add_project(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1', 'authorizations': []}) mocker.patch.object(DB, 'insert', @@ -16,7 +16,7 @@ def test_add_simulation(client, mocker): 'topologyIds': [] }) mocker.patch.object(DB, 'update', return_value={}) - res = client.post('/api/v2/simulations', json={'simulation': {'name': 'test simulation'}}) + res = client.post('/api/v2/projects', json={'project': {'name': 'test project'}}) assert 'datetimeCreated' in res.json['content'] assert 'datetimeLastEdited' in res.json['content'] assert 'topologyIds' in res.json['content'] diff --git a/web-server/opendc/api/v2/simulations/endpoint.py b/web-server/opendc/api/v2/simulations/endpoint.py deleted file mode 100644 index 993e317a..00000000 --- a/web-server/opendc/api/v2/simulations/endpoint.py +++ /dev/null @@ -1,32 +0,0 @@ -from datetime import datetime - -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 POST(request): - """Create a new simulation, and return that new simulation.""" - - request.check_required_parameters(body={'simulation': {'name': 'string'}}) - - topology = Topology({'name': 'Default topology', 'rooms': []}) - topology.insert() - - simulation = Simulation(request.params_body['simulation']) - simulation.set_property('datetimeCreated', Database.datetime_to_string(datetime.now())) - simulation.set_property('datetimeLastEdited', Database.datetime_to_string(datetime.now())) - simulation.set_property('topologyIds', [topology.get_id()]) - simulation.set_property('experimentIds', []) - simulation.insert() - - topology.set_property('simulationId', simulation.get_id()) - topology.update() - - user = User.from_google_id(request.google_id) - user.obj['authorizations'].append({'simulationId': simulation.get_id(), 'authorizationLevel': 'OWN'}) - user.update() - - return Response(200, 'Successfully created simulation.', simulation.obj) diff --git a/web-server/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py b/web-server/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py deleted file mode 100644 index 49d0fc20..00000000 --- a/web-server/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py +++ /dev/null @@ -1,17 +0,0 @@ -from opendc.models.simulation import Simulation -from opendc.util.rest import Response - - -def GET(request): - """Find all authorizations for a 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) - - authorizations = simulation.get_all_authorizations() - - return Response(200, 'Successfully retrieved simulation authorizations', authorizations) diff --git a/web-server/opendc/api/v2/simulations/simulationId/endpoint.py b/web-server/opendc/api/v2/simulations/simulationId/endpoint.py deleted file mode 100644 index 0cee3e9c..00000000 --- a/web-server/opendc/api/v2/simulations/simulationId/endpoint.py +++ /dev/null @@ -1,66 +0,0 @@ -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) diff --git a/web-server/opendc/api/v2/simulations/simulationId/topologies/endpoint.py b/web-server/opendc/api/v2/simulations/simulationId/topologies/endpoint.py deleted file mode 100644 index 7c07e63b..00000000 --- a/web-server/opendc/api/v2/simulations/simulationId/topologies/endpoint.py +++ /dev/null @@ -1,31 +0,0 @@ -from datetime import datetime - -from opendc.models.simulation import Simulation -from opendc.models.topology import Topology -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({ - 'simulationId': request.params_path['simulationId'], - 'name': request.params_body['topology']['name'], - 'rooms': request.params_body['topology']['rooms'], - }) - - topology.insert() - - simulation.obj['topologyIds'].append(topology.get_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/topologies/topologyId/endpoint.py b/web-server/opendc/api/v2/topologies/topologyId/endpoint.py index 1dcccb3e..512b050a 100644 --- a/web-server/opendc/api/v2/topologies/topologyId/endpoint.py +++ b/web-server/opendc/api/v2/topologies/topologyId/endpoint.py @@ -1,7 +1,7 @@ from datetime import datetime from opendc.util.database import Database -from opendc.models.simulation import Simulation +from opendc.models.project import Project from opendc.models.topology import Topology from opendc.util.rest import Response @@ -45,11 +45,11 @@ def DELETE(request): 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() + project = Project.from_id(topology.obj['projectId']) + project.check_exists() + if request.params_path['topologyId'] in project.obj['topologyIds']: + project.obj['topologyIds'].remove(request.params_path['topologyId']) + project.update() old_object = topology.delete() diff --git a/web-server/opendc/api/v2/topologies/topologyId/test_endpoint.py b/web-server/opendc/api/v2/topologies/topologyId/test_endpoint.py index fa88c497..b25cb798 100644 --- a/web-server/opendc/api/v2/topologies/topologyId/test_endpoint.py +++ b/web-server/opendc/api/v2/topologies/topologyId/test_endpoint.py @@ -1,7 +1,4 @@ from opendc.util.database import DB -''' -GET /topologies/{topologyId} -''' def test_get_topology(client, mocker): @@ -9,9 +6,9 @@ def test_get_topology(client, mocker): 'fetch_one', return_value={ '_id': '1', - 'simulationId': '1', + 'projectId': '1', 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'EDIT' }] }) @@ -29,9 +26,9 @@ def test_get_topology_not_authorized(client, mocker): 'fetch_one', return_value={ '_id': '1', - 'simulationId': '1', + 'projectId': '1', 'authorizations': [{ - 'simulationId': '2', + 'projectId': '2', 'authorizationLevel': 'OWN' }] }) @@ -40,16 +37,11 @@ def test_get_topology_not_authorized(client, mocker): def test_get_topology_no_authorizations(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value={'authorizations': []}) + mocker.patch.object(DB, 'fetch_one', return_value={'projectId': '1', 'authorizations': []}) res = client.get('/api/v2/topologies/1') assert '403' in res.status -''' -PUT /topologies/{topologyId} -''' - - def test_update_topology_missing_parameter(client): assert '400' in client.put('/api/v2/topologies/1').status @@ -64,9 +56,9 @@ def test_update_topology_not_authorized(client, mocker): 'fetch_one', return_value={ '_id': '1', - 'simulationId': '1', + 'projectId': '1', 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'VIEW' }] }) @@ -84,9 +76,9 @@ def test_update_topology(client, mocker): 'fetch_one', return_value={ '_id': '1', - 'simulationId': '1', + 'projectId': '1', 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'OWN' }] }) @@ -100,21 +92,16 @@ def test_update_topology(client, mocker): }).status -''' -DELETE /topologies/{topologyId} -''' - - def test_delete_topology(client, mocker): mocker.patch.object(DB, 'fetch_one', return_value={ '_id': '1', - 'simulationId': '1', + 'projectId': '1', 'googleId': 'test', 'topologyIds': ['1'], 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'OWN' }] }) diff --git a/web-server/opendc/api/v2/users/userId/endpoint.py b/web-server/opendc/api/v2/users/userId/endpoint.py index 7a4fb104..be3462c0 100644 --- a/web-server/opendc/api/v2/users/userId/endpoint.py +++ b/web-server/opendc/api/v2/users/userId/endpoint.py @@ -1,4 +1,4 @@ -from opendc.models.simulation import Simulation +from opendc.models.project import Project from opendc.models.user import User from opendc.util.rest import Response @@ -51,8 +51,8 @@ def DELETE(request): if authorization['authorizationLevel'] != 'OWN': continue - simulation = Simulation.from_id(authorization['simulationId']) - simulation.delete() + project = Project.from_id(authorization['projectId']) + project.delete() old_object = user.delete() diff --git a/web-server/opendc/models/experiment.py b/web-server/opendc/models/experiment.py index 36b5d415..46373b99 100644 --- a/web-server/opendc/models/experiment.py +++ b/web-server/opendc/models/experiment.py @@ -12,13 +12,13 @@ class Experiment(Model): def check_user_access(self, google_id, edit_access): """Raises an error if the user with given [google_id] has insufficient access. - Checks access on the parent simulation. + Checks access on the parent project. :param google_id: The Google ID of the user. :param edit_access: True when edit access should be checked, otherwise view access. """ user = User.from_google_id(google_id) authorizations = list( - filter(lambda x: str(x['simulationId']) == str(self.obj['simulationId']), user.obj['authorizations'])) + filter(lambda x: str(x['projectId']) == str(self.obj['projectId']), user.obj['authorizations'])) if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'): raise ClientError(Response(403, 'Forbidden from retrieving/editing experiment.')) diff --git a/web-server/opendc/models/prefab.py b/web-server/opendc/models/prefab.py index d3b1a5d5..42c29697 100644 --- a/web-server/opendc/models/prefab.py +++ b/web-server/opendc/models/prefab.py @@ -6,7 +6,7 @@ from opendc.util.rest import Response class Prefab(Model): - """Model representing a Prefab.""" + """Model representing a Project.""" collection_name = 'prefabs' @@ -17,13 +17,12 @@ class Prefab(Model): :param edit_access: True when edit access should be checked, otherwise view access. """ user = User.from_google_id(google_id) - authorizations = list(filter(lambda x: str(x['prefabId']) == str(self.get_id()), - user.obj['authorizations'])) + authorizations = list(filter(lambda x: str(x['prefabId']) == str(self.get_id()), user.obj['authorizations'])) if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'): raise ClientError(Response(403, "Forbidden from retrieving prefab.")) def get_all_authorizations(self): - """Get all user IDs having access to this prefab.""" + """Get all user IDs having access to this project.""" return [ str(user['_id']) for user in DB.fetch_all({'authorizations': { 'prefabId': self.obj['_id'] diff --git a/web-server/opendc/models/simulation.py b/web-server/opendc/models/project.py index 9a2770cf..b57e9f77 100644 --- a/web-server/opendc/models/simulation.py +++ b/web-server/opendc/models/project.py @@ -5,10 +5,10 @@ from opendc.util.exceptions import ClientError from opendc.util.rest import Response -class Simulation(Model): - """Model representing a Simulation.""" +class Project(Model): + """Model representing a Project.""" - collection_name = 'simulations' + collection_name = 'projects' def check_user_access(self, google_id, edit_access): """Raises an error if the user with given [google_id] has insufficient access. @@ -17,15 +17,15 @@ class Simulation(Model): :param edit_access: True when edit access should be checked, otherwise view access. """ user = User.from_google_id(google_id) - authorizations = list(filter(lambda x: str(x['simulationId']) == str(self.get_id()), + authorizations = list(filter(lambda x: str(x['projectId']) == str(self.get_id()), user.obj['authorizations'])) if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'): - raise ClientError(Response(403, "Forbidden from retrieving simulation.")) + raise ClientError(Response(403, "Forbidden from retrieving project.")) def get_all_authorizations(self): - """Get all user IDs having access to this simulation.""" + """Get all user IDs having access to this project.""" return [ str(user['_id']) for user in DB.fetch_all({'authorizations': { - 'simulationId': self.obj['_id'] + 'projectId': self.obj['_id'] }}, User.collection_name) ] diff --git a/web-server/opendc/models/topology.py b/web-server/opendc/models/topology.py index 0ceecec5..cb4c4bab 100644 --- a/web-server/opendc/models/topology.py +++ b/web-server/opendc/models/topology.py @@ -5,23 +5,23 @@ from opendc.util.rest import Response class Topology(Model): - """Model representing a Simulation.""" + """Model representing a Project.""" collection_name = 'topologies' def check_user_access(self, google_id, edit_access): """Raises an error if the user with given [google_id] has insufficient access. - Checks access on the parent simulation. + Checks access on the parent project. :param google_id: The Google ID of the user. :param edit_access: True when edit access should be checked, otherwise view access. """ user = User.from_google_id(google_id) - if 'simulationId' not in self.obj: - raise ClientError(Response(400, 'Missing simulationId in topology.')) + if 'projectId' not in self.obj: + raise ClientError(Response(400, 'Missing projectId in topology.')) authorizations = list( - filter(lambda x: str(x['simulationId']) == str(self.obj['simulationId']), user.obj['authorizations'])) + filter(lambda x: str(x['projectId']) == str(self.obj['projectId']), user.obj['authorizations'])) if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'): raise ClientError(Response(403, 'Forbidden from retrieving topology.')) |
