From 4ec6212a220c3627bdad070ac2f0e05e2d663979 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Tue, 30 Jun 2020 11:17:06 +0200 Subject: Add new experiment endpoints --- .../api/v2/experiments/experimentId/endpoint.py | 112 ++++++--------------- .../experimentId/statistics/__init__.py | 0 .../statistics/task-durations/__init__.py | 0 .../statistics/task-durations/endpoint.py | 37 ------- .../experimentId/task-states/__init__.py | 0 .../experimentId/task-states/endpoint.py | 42 -------- 6 files changed, 28 insertions(+), 163 deletions(-) delete mode 100644 web-server/opendc/api/v2/experiments/experimentId/statistics/__init__.py delete mode 100644 web-server/opendc/api/v2/experiments/experimentId/statistics/task-durations/__init__.py delete mode 100644 web-server/opendc/api/v2/experiments/experimentId/statistics/task-durations/endpoint.py delete mode 100644 web-server/opendc/api/v2/experiments/experimentId/task-states/__init__.py delete mode 100644 web-server/opendc/api/v2/experiments/experimentId/task-states/endpoint.py (limited to 'web-server/opendc/api/v2/experiments') diff --git a/web-server/opendc/api/v2/experiments/experimentId/endpoint.py b/web-server/opendc/api/v2/experiments/experimentId/endpoint.py index bc2b139e..8632e19e 100644 --- a/web-server/opendc/api/v2/experiments/experimentId/endpoint.py +++ b/web-server/opendc/api/v2/experiments/experimentId/endpoint.py @@ -1,4 +1,4 @@ -from opendc.models_old.experiment import Experiment +from opendc.models.experiment import Experiment from opendc.util import exceptions from opendc.util.rest import Response @@ -6,108 +6,52 @@ from opendc.util.rest import Response def GET(request): """Get this Experiment.""" - try: - request.check_required_parameters(path={'experimentId': 'int'}) + request.check_required_parameters(path={'experimentId': 'string'}) - except exceptions.ParameterError as e: - return Response(400, str(e)) + experiment = Experiment.from_id(request.params_path['experimentId']) - # Instantiate an Experiment from the database + experiment.check_exists() + experiment.check_user_access(request.google_id, False) - experiment = Experiment.from_primary_key((request.params_path['experimentId'], )) - - # Make sure this Experiment exists - - if not experiment.exists(): - return Response(404, '{} not found.'.format(experiment)) - - # Make sure this user is authorized to view this Experiment - - if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from retrieving {}.'.format(experiment)) - - # Return this Experiment - - experiment.read() - - return Response(200, 'Successfully retrieved {}.'.format(experiment), experiment.to_JSON()) + return Response(200, f'Successfully retrieved Experiment.', experiment.obj) def PUT(request): - """Update this Experiment's Path, Trace, Scheduler, and/or name.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={'experimentId': 'int'}, - body={'experiment': { - 'pathId': 'int', - 'traceId': 'int', - 'schedulerName': 'string', - 'name': 'string' - }}) - - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Instantiate an Experiment from the database + """Update this Experiment.""" - experiment = Experiment.from_primary_key((request.params_path['experimentId'], )) + request.check_required_parameters(path={'experimentId': 'string'}, + body={'experiment': { + 'topologyId': 'string', + 'traceId': 'string', + 'schedulerName': 'string', + 'name': 'string', + }}) - # Make sure this Experiment exists + experiment = Experiment.from_id(request.params_path['experimentId']) - if not experiment.exists(): - return Response(404, '{} not found.'.format(experiment)) + experiment.check_exists() + experiment.check_user_access(request.google_id, True) - # Make sure this user is authorized to edit this Experiment + experiment.set_property('topologyId', request.params_body['experiment']['topologyId']) + experiment.set_property('traceId', request.params_body['experiment']['traceId']) + experiment.set_property('scheduleName', request.params_body['experiment']['scheduleName']) + experiment.set_property('name', request.params_body['experiment']['name']) - if not experiment.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from updating {}.'.format(experiment)) + experiment.update() - # Update this Experiment - - experiment.path_id = request.params_body['experiment']['pathId'] - experiment.trace_id = request.params_body['experiment']['traceId'] - experiment.scheduler_name = request.params_body['experiment']['schedulerName'] - experiment.name = request.params_body['experiment']['name'] - - try: - experiment.update() - - except exceptions.ForeignKeyError: - return Response(400, 'Foreign key error.') - - # Return this Experiment - - return Response(200, 'Successfully updated {}.'.format(experiment), experiment.to_JSON()) + return Response(200, 'Successfully updated experiment', experiment.obj) def DELETE(request): """Delete this Experiment.""" - # Make sure required parameters are there - - try: - request.check_required_parameters(path={'experimentId': 'int'}) - - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Instantiate an Experiment and make sure it exists - - experiment = Experiment.from_primary_key((request.params_path['experimentId'], )) - - if not experiment.exists(): - return Response(404, '{} not found.'.format(experiment)) - - # Make sure this user is authorized to delete this Experiment + request.check_required_parameters(path={'userId': 'string'}) - if not experiment.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from deleting {}.'.format(experiment)) + experiment = Experiment.from_id(request.params_path['experimentId']) - # Delete and return this Experiment + experiment.check_exists() + experiment.check_user_access(request.google_id, True) experiment.delete() - return Response(200, 'Successfully deleted {}.'.format(experiment), experiment.to_JSON()) + return Response(200, 'Successfully deleted experiment.', experiment.obj) diff --git a/web-server/opendc/api/v2/experiments/experimentId/statistics/__init__.py b/web-server/opendc/api/v2/experiments/experimentId/statistics/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/web-server/opendc/api/v2/experiments/experimentId/statistics/task-durations/__init__.py b/web-server/opendc/api/v2/experiments/experimentId/statistics/task-durations/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/web-server/opendc/api/v2/experiments/experimentId/statistics/task-durations/endpoint.py b/web-server/opendc/api/v2/experiments/experimentId/statistics/task-durations/endpoint.py deleted file mode 100644 index 498db239..00000000 --- a/web-server/opendc/api/v2/experiments/experimentId/statistics/task-durations/endpoint.py +++ /dev/null @@ -1,37 +0,0 @@ -from opendc.models_old.experiment import Experiment -from opendc.models_old.task_duration import TaskDuration -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get this Experiment's Task Durations.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters(path={'experimentId': 'int'}) - - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Instantiate an Experiment from the database - - experiment = Experiment.from_primary_key((request.params_path['experimentId'], )) - - # Make sure this Experiment exists - - if not experiment.exists(): - return Response(404, '{} not found.'.format(experiment)) - - # Make sure this user is authorized to view this Experiment's Task Durations - - if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing Task Durations for {}.'.format(experiment)) - - # Get and return the Task Durations - - task_durations = TaskDuration.from_experiment_id(request.params_path['experimentId']) - - return Response(200, 'Successfully retrieved Task Durations for {}.'.format(experiment), - [x.to_JSON() for x in task_durations]) diff --git a/web-server/opendc/api/v2/experiments/experimentId/task-states/__init__.py b/web-server/opendc/api/v2/experiments/experimentId/task-states/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/web-server/opendc/api/v2/experiments/experimentId/task-states/endpoint.py b/web-server/opendc/api/v2/experiments/experimentId/task-states/endpoint.py deleted file mode 100644 index c0ae47fc..00000000 --- a/web-server/opendc/api/v2/experiments/experimentId/task-states/endpoint.py +++ /dev/null @@ -1,42 +0,0 @@ -from opendc.models_old.experiment import Experiment -from opendc.models_old.task_state import TaskState -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get this Experiment's Task States.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters(path={'experimentId': 'int'}) - - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Instantiate an Experiment from the database - - experiment = Experiment.from_primary_key((request.params_path['experimentId'], )) - - # Make sure this Experiment exists - - if not experiment.exists(): - return Response(404, '{} not found.'.format(experiment)) - - # Make sure this user is authorized to view Task States for this Experiment - - if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing Task States for {}.'.format(experiment)) - - # Get and return the Task States - - if 'tick' in request.params_query: - task_states = TaskState.from_experiment_id_and_tick(request.params_path['experimentId'], - request.params_query['tick']) - - else: - task_states = TaskState.query('experiment_id', request.params_path['experimentId']) - - return Response(200, 'Successfully retrieved Task States for {}.'.format(experiment), - [x.to_JSON() for x in task_states]) -- cgit v1.2.3 From 9b910a49270311fd6f099f0f0674a1fd0ab48a20 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Tue, 30 Jun 2020 11:29:36 +0200 Subject: Add experiment endpoint tests --- .../api/v2/experiments/experimentId/endpoint.py | 4 +- .../v2/experiments/experimentId/test_endpoint.py | 136 +++++++++++++++++++++ 2 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 web-server/opendc/api/v2/experiments/experimentId/test_endpoint.py (limited to 'web-server/opendc/api/v2/experiments') diff --git a/web-server/opendc/api/v2/experiments/experimentId/endpoint.py b/web-server/opendc/api/v2/experiments/experimentId/endpoint.py index 8632e19e..2637fca7 100644 --- a/web-server/opendc/api/v2/experiments/experimentId/endpoint.py +++ b/web-server/opendc/api/v2/experiments/experimentId/endpoint.py @@ -34,7 +34,7 @@ def PUT(request): experiment.set_property('topologyId', request.params_body['experiment']['topologyId']) experiment.set_property('traceId', request.params_body['experiment']['traceId']) - experiment.set_property('scheduleName', request.params_body['experiment']['scheduleName']) + experiment.set_property('schedulerName', request.params_body['experiment']['schedulerName']) experiment.set_property('name', request.params_body['experiment']['name']) experiment.update() @@ -45,7 +45,7 @@ def PUT(request): def DELETE(request): """Delete this Experiment.""" - request.check_required_parameters(path={'userId': 'string'}) + request.check_required_parameters(path={'experimentId': 'string'}) experiment = Experiment.from_id(request.params_path['experimentId']) diff --git a/web-server/opendc/api/v2/experiments/experimentId/test_endpoint.py b/web-server/opendc/api/v2/experiments/experimentId/test_endpoint.py new file mode 100644 index 00000000..0c9c9abc --- /dev/null +++ b/web-server/opendc/api/v2/experiments/experimentId/test_endpoint.py @@ -0,0 +1,136 @@ +from opendc.util.database import DB + + +def test_get_experiment_non_existing(client, mocker): + mocker.patch.object(DB, 'fetch_one', return_value=None) + assert '404' in client.get('/api/v2/experiments/1').status + + +def test_get_experiment_no_authorizations(client, mocker): + mocker.patch.object(DB, 'fetch_one', return_value={'simulationId': '1', 'authorizations': []}) + res = client.get('/api/v2/experiments/1') + assert '403' in res.status + + +def test_get_experiment_not_authorized(client, mocker): + mocker.patch.object(DB, + 'fetch_one', + return_value={ + 'simulationId': '1', + '_id': '1', + 'authorizations': [{ + 'simulationId': '2', + 'authorizationLevel': 'OWN' + }] + }) + res = client.get('/api/v2/experiments/1') + assert '403' in res.status + + +def test_get_experiment(client, mocker): + mocker.patch.object(DB, + 'fetch_one', + return_value={ + 'simulationId': '1', + '_id': '1', + 'authorizations': [{ + 'simulationId': '1', + 'authorizationLevel': 'EDIT' + }] + }) + res = client.get('/api/v2/experiments/1') + assert '200' in res.status + + +def test_update_experiment_missing_parameter(client): + assert '400' in client.put('/api/v2/experiments/1').status + + +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': { + 'topologyId': '1', + 'traceId': '1', + 'schedulerName': 'default', + 'name': 'test', + }}).status + + +def test_update_experiment_not_authorized(client, mocker): + mocker.patch.object(DB, + 'fetch_one', + return_value={ + '_id': '1', + 'simulationId': '1', + 'authorizations': [{ + 'simulationId': '1', + 'authorizationLevel': 'VIEW' + }] + }) + mocker.patch.object(DB, 'update', return_value={}) + assert '403' in client.put('/api/v2/experiments/1', json={'experiment': { + 'topologyId': '1', + 'traceId': '1', + 'schedulerName': 'default', + 'name': 'test', + }}).status + + +def test_update_experiment(client, mocker): + mocker.patch.object(DB, + 'fetch_one', + return_value={ + '_id': '1', + 'simulationId': '1', + 'authorizations': [{ + 'simulationId': '1', + 'authorizationLevel': 'OWN' + }] + }) + mocker.patch.object(DB, 'update', return_value={}) + + res = client.put('/api/v2/experiments/1', json={'experiment': { + 'topologyId': '1', + 'traceId': '1', + 'schedulerName': 'default', + 'name': 'test', + }}) + 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/experiments/1').status + + +def test_delete_simulation_different_user(client, mocker): + mocker.patch.object(DB, + 'fetch_one', + return_value={ + '_id': '1', + 'simulationId': '1', + 'googleId': 'other_test', + 'authorizations': [{ + 'simulationId': '1', + 'authorizationLevel': 'VIEW' + }] + }) + mocker.patch.object(DB, 'delete_one', return_value=None) + assert '403' in client.delete('/api/v2/experiments/1').status + + +def test_delete_simulation(client, mocker): + mocker.patch.object(DB, + 'fetch_one', + return_value={ + '_id': '1', + 'simulationId': '1', + 'googleId': 'test', + 'authorizations': [{ + 'simulationId': '1', + 'authorizationLevel': 'OWN' + }] + }) + mocker.patch.object(DB, 'delete_one', return_value={}) + res = client.delete('/api/v2/experiments/1') + assert '200' in res.status -- cgit v1.2.3 From 676ccde281841005e3f318bf267ea1dbcdf6fc87 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Tue, 30 Jun 2020 11:43:07 +0200 Subject: Add experiment POST --- .../api/v2/experiments/experimentId/endpoint.py | 14 ++++--- .../v2/experiments/experimentId/test_endpoint.py | 44 +++++++++++++--------- 2 files changed, 34 insertions(+), 24 deletions(-) (limited to 'web-server/opendc/api/v2/experiments') diff --git a/web-server/opendc/api/v2/experiments/experimentId/endpoint.py b/web-server/opendc/api/v2/experiments/experimentId/endpoint.py index 2637fca7..7dacdf48 100644 --- a/web-server/opendc/api/v2/experiments/experimentId/endpoint.py +++ b/web-server/opendc/api/v2/experiments/experimentId/endpoint.py @@ -20,12 +20,14 @@ def PUT(request): """Update this Experiment.""" request.check_required_parameters(path={'experimentId': 'string'}, - body={'experiment': { - 'topologyId': 'string', - 'traceId': 'string', - 'schedulerName': 'string', - 'name': 'string', - }}) + body={ + 'experiment': { + 'topologyId': 'string', + 'traceId': 'string', + 'schedulerName': 'string', + 'name': 'string', + } + }) experiment = Experiment.from_id(request.params_path['experimentId']) 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 0c9c9abc..35b8b762 100644 --- a/web-server/opendc/api/v2/experiments/experimentId/test_endpoint.py +++ b/web-server/opendc/api/v2/experiments/experimentId/test_endpoint.py @@ -48,12 +48,15 @@ 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': { - 'topologyId': '1', - 'traceId': '1', - 'schedulerName': 'default', - 'name': 'test', - }}).status + assert '404' in client.put('/api/v2/experiments/1', + json={ + 'experiment': { + 'topologyId': '1', + 'traceId': '1', + 'schedulerName': 'default', + 'name': 'test', + } + }).status def test_update_experiment_not_authorized(client, mocker): @@ -68,12 +71,15 @@ def test_update_experiment_not_authorized(client, mocker): }] }) mocker.patch.object(DB, 'update', return_value={}) - assert '403' in client.put('/api/v2/experiments/1', json={'experiment': { - 'topologyId': '1', - 'traceId': '1', - 'schedulerName': 'default', - 'name': 'test', - }}).status + assert '403' in client.put('/api/v2/experiments/1', + json={ + 'experiment': { + 'topologyId': '1', + 'traceId': '1', + 'schedulerName': 'default', + 'name': 'test', + } + }).status def test_update_experiment(client, mocker): @@ -89,12 +95,14 @@ def test_update_experiment(client, mocker): }) mocker.patch.object(DB, 'update', return_value={}) - res = client.put('/api/v2/experiments/1', json={'experiment': { - 'topologyId': '1', - 'traceId': '1', - 'schedulerName': 'default', - 'name': 'test', - }}) + res = client.put( + '/api/v2/experiments/1', + json={'experiment': { + 'topologyId': '1', + 'traceId': '1', + 'schedulerName': 'default', + 'name': 'test', + }}) assert '200' in res.status -- cgit v1.2.3 From 5c673272747ed14e5668b2f4301f0f853b400ee1 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Tue, 30 Jun 2020 12:18:14 +0200 Subject: Remove last simulated tick --- .../experimentId/last-simulated-tick/__init__.py | 0 .../experimentId/last-simulated-tick/endpoint.py | 32 ---------------------- 2 files changed, 32 deletions(-) delete mode 100644 web-server/opendc/api/v2/experiments/experimentId/last-simulated-tick/__init__.py delete mode 100644 web-server/opendc/api/v2/experiments/experimentId/last-simulated-tick/endpoint.py (limited to 'web-server/opendc/api/v2/experiments') diff --git a/web-server/opendc/api/v2/experiments/experimentId/last-simulated-tick/__init__.py b/web-server/opendc/api/v2/experiments/experimentId/last-simulated-tick/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/web-server/opendc/api/v2/experiments/experimentId/last-simulated-tick/endpoint.py b/web-server/opendc/api/v2/experiments/experimentId/last-simulated-tick/endpoint.py deleted file mode 100644 index 3309502c..00000000 --- a/web-server/opendc/api/v2/experiments/experimentId/last-simulated-tick/endpoint.py +++ /dev/null @@ -1,32 +0,0 @@ -from opendc.models_old.experiment import Experiment -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get this Experiment's last simulated tick.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters(path={'experimentId': 'int'}) - - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Instantiate an Experiment from the database - - experiment = Experiment.from_primary_key((request.params_path['experimentId'], )) - - # Make sure this Experiment exists - - if not experiment.exists(): - return Response(404, '{} not found.'.format(experiment)) - - # Make sure this user is authorized to view this Experiment's last simulated tick - - if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing last simulated tick for {}.'.format(experiment)) - - return Response(200, 'Successfully retrieved last simulated tick for {}.'.format(experiment), - {'lastSimulatedTick': experiment.last_simulated_tick}) -- cgit v1.2.3 From b9dcda0fb4bbb52ebd8a15acf324561a6d687eba Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Tue, 30 Jun 2020 13:33:43 +0200 Subject: Remove old models --- .../experimentId/machine-states/__init__.py | 0 .../experimentId/machine-states/endpoint.py | 42 ---------------------- .../experimentId/rack-states/__init__.py | 0 .../experimentId/rack-states/endpoint.py | 42 ---------------------- .../experimentId/room-states/__init__.py | 0 .../experimentId/room-states/endpoint.py | 42 ---------------------- 6 files changed, 126 deletions(-) delete mode 100644 web-server/opendc/api/v2/experiments/experimentId/machine-states/__init__.py delete mode 100644 web-server/opendc/api/v2/experiments/experimentId/machine-states/endpoint.py delete mode 100644 web-server/opendc/api/v2/experiments/experimentId/rack-states/__init__.py delete mode 100644 web-server/opendc/api/v2/experiments/experimentId/rack-states/endpoint.py delete mode 100644 web-server/opendc/api/v2/experiments/experimentId/room-states/__init__.py delete mode 100644 web-server/opendc/api/v2/experiments/experimentId/room-states/endpoint.py (limited to 'web-server/opendc/api/v2/experiments') diff --git a/web-server/opendc/api/v2/experiments/experimentId/machine-states/__init__.py b/web-server/opendc/api/v2/experiments/experimentId/machine-states/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/web-server/opendc/api/v2/experiments/experimentId/machine-states/endpoint.py b/web-server/opendc/api/v2/experiments/experimentId/machine-states/endpoint.py deleted file mode 100644 index c7dcad9a..00000000 --- a/web-server/opendc/api/v2/experiments/experimentId/machine-states/endpoint.py +++ /dev/null @@ -1,42 +0,0 @@ -from opendc.models_old.experiment import Experiment -from opendc.models_old.machine_state import MachineState -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get this Experiment's Machine States.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters(path={'experimentId': 'int'}) - - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Instantiate an Experiment from the database - - experiment = Experiment.from_primary_key((request.params_path['experimentId'], )) - - # Make sure this Experiment exists - - if not experiment.exists(): - return Response(404, '{} not found.'.format(experiment)) - - # Make sure this user is authorized to view this Experiment's Machine States - - if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing Machine States for {}.'.format(experiment)) - - # Get and return the Machine States - - if 'tick' in request.params_query: - machine_states = MachineState.from_experiment_id_and_tick(request.params_path['experimentId'], - request.params_query['tick']) - - else: - machine_states = MachineState.from_experiment_id(request.params_path['experimentId']) - - return Response(200, 'Successfully retrieved Machine States for {}.'.format(experiment), - [x.to_JSON() for x in machine_states]) diff --git a/web-server/opendc/api/v2/experiments/experimentId/rack-states/__init__.py b/web-server/opendc/api/v2/experiments/experimentId/rack-states/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/web-server/opendc/api/v2/experiments/experimentId/rack-states/endpoint.py b/web-server/opendc/api/v2/experiments/experimentId/rack-states/endpoint.py deleted file mode 100644 index f3acf56a..00000000 --- a/web-server/opendc/api/v2/experiments/experimentId/rack-states/endpoint.py +++ /dev/null @@ -1,42 +0,0 @@ -from opendc.models_old.experiment import Experiment -from opendc.models_old.rack_state import RackState -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get this Experiment's Tack States.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters(path={'experimentId': 'int'}) - - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Instantiate an Experiment from the database - - experiment = Experiment.from_primary_key((request.params_path['experimentId'], )) - - # Make sure this Experiment exists - - if not experiment.exists(): - return Response(404, '{} not found.'.format(experiment)) - - # Make sure this user is authorized to view this Experiment's Rack States - - if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing Rack States for {}.'.format(experiment)) - - # Get and return the Rack States - - if 'tick' in request.params_query: - rack_states = RackState.from_experiment_id_and_tick(request.params_path['experimentId'], - request.params_query['tick']) - - else: - rack_states = RackState.from_experiment_id(request.params_path['experimentId']) - - return Response(200, 'Successfully retrieved Rack States for {}.'.format(experiment), - [x.to_JSON() for x in rack_states]) diff --git a/web-server/opendc/api/v2/experiments/experimentId/room-states/__init__.py b/web-server/opendc/api/v2/experiments/experimentId/room-states/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/web-server/opendc/api/v2/experiments/experimentId/room-states/endpoint.py b/web-server/opendc/api/v2/experiments/experimentId/room-states/endpoint.py deleted file mode 100644 index db3f8b14..00000000 --- a/web-server/opendc/api/v2/experiments/experimentId/room-states/endpoint.py +++ /dev/null @@ -1,42 +0,0 @@ -from opendc.models_old.experiment import Experiment -from opendc.models_old.room_state import RoomState -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get this Experiment's Room States.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters(path={'experimentId': 'int'}) - - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Instantiate an Experiment from the database - - experiment = Experiment.from_primary_key((request.params_path['experimentId'], )) - - # Make sure this Experiment exists - - if not experiment.exists(): - return Response(404, '{} not found.'.format(experiment)) - - # Make sure this user is authorized to view this Experiment's Room States - - if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing Room States for {}.'.format(experiment)) - - # Get and return the Room States - - if 'tick' in request.params_query: - room_states = RoomState.from_experiment_id_and_tick(request.params_path['experimentId'], - request.params_query['tick']) - - else: - room_states = RoomState.from_experiment_id(request.params_path['experimentId']) - - return Response(200, 'Successfully retrieved Room States for {}.'.format(experiment), - [x.to_JSON() for x in room_states]) -- cgit v1.2.3 From 1c58ae3b25120ac670b897665bac7d8f18156220 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Tue, 30 Jun 2020 14:01:17 +0200 Subject: Fix more violations and ensure experimentIds handling --- web-server/opendc/api/v2/experiments/experimentId/endpoint.py | 10 ++++++++-- .../opendc/api/v2/experiments/experimentId/test_endpoint.py | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'web-server/opendc/api/v2/experiments') diff --git a/web-server/opendc/api/v2/experiments/experimentId/endpoint.py b/web-server/opendc/api/v2/experiments/experimentId/endpoint.py index 7dacdf48..103c24ac 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.util import exceptions +from opendc.models.simulation import Simulation from opendc.util.rest import Response @@ -13,7 +13,7 @@ def GET(request): experiment.check_exists() experiment.check_user_access(request.google_id, False) - return Response(200, f'Successfully retrieved Experiment.', experiment.obj) + return Response(200, 'Successfully retrieved Experiment.', experiment.obj) def PUT(request): @@ -54,6 +54,12 @@ 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() + experiment.delete() return Response(200, 'Successfully deleted experiment.', experiment.obj) 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 35b8b762..c460585f 100644 --- a/web-server/opendc/api/v2/experiments/experimentId/test_endpoint.py +++ b/web-server/opendc/api/v2/experiments/experimentId/test_endpoint.py @@ -134,11 +134,13 @@ def test_delete_simulation(client, mocker): '_id': '1', 'simulationId': '1', 'googleId': 'test', + 'experimentIds': ['1'], 'authorizations': [{ 'simulationId': '1', 'authorizationLevel': 'OWN' }] }) mocker.patch.object(DB, 'delete_one', return_value={}) + mocker.patch.object(DB, 'update', return_value=None) res = client.delete('/api/v2/experiments/1') assert '200' in res.status -- cgit v1.2.3