diff options
| author | Georgios Andreadis <info@gandreadis.com> | 2020-07-07 09:55:10 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2020-08-24 19:47:25 +0200 |
| commit | b4bdf9fde013bb7ff9579693b64ff575f7b00e44 (patch) | |
| tree | 5e05ceba918849391a639bbeeab37d290a86523c /web-server/opendc/api/v2 | |
| parent | 7331e9baf2cfe7bdfb24effcf0a4801da1e7ea4d (diff) | |
Rename simulations to projects and remove experiment view
Diffstat (limited to 'web-server/opendc/api/v2')
25 files changed, 254 insertions, 254 deletions
diff --git a/web-server/opendc/api/v2/experiments/experimentId/endpoint.py b/web-server/opendc/api/v2/experiments/experimentId/endpoint.py index dc056454..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 @@ -45,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 3532f9ae..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' }] }) @@ -60,9 +60,9 @@ 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' }] }) @@ -79,9 +79,9 @@ def test_update_experiment(client, mocker): 'fetch_one', return_value={ '_id': '1', - 'simulationId': '1', + 'projectId': '1', 'authorizations': [{ - 'simulationId': '1', + 'projectId': '1', 'authorizationLevel': 'OWN' }] }) @@ -93,20 +93,20 @@ def test_update_experiment(client, mocker): 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' }] }) @@ -114,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 c0aa4dd4..01a7dcff 100644 --- a/web-server/opendc/api/v2/paths.json +++ b/web-server/opendc/api/v2/paths.json @@ -1,12 +1,12 @@ [ "/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", 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 1314be13..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,7 +25,7 @@ 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', 'rooms': []}}) + 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 @@ -35,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 d3c20de3..b25cb798 100644 --- a/web-server/opendc/api/v2/topologies/topologyId/test_endpoint.py +++ b/web-server/opendc/api/v2/topologies/topologyId/test_endpoint.py @@ -6,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' }] }) @@ -26,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' }] }) @@ -37,7 +37,7 @@ def test_get_topology_not_authorized(client, mocker): def test_get_topology_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/topologies/1') assert '403' in res.status @@ -56,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' }] }) @@ -76,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' }] }) @@ -97,11 +97,11 @@ def test_delete_topology(client, mocker): '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() |
