diff options
| author | Georgios Andreadis <info@gandreadis.com> | 2020-06-29 16:05:23 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2020-08-24 16:18:36 +0200 |
| commit | 4f9a40abdc7836345113c047f27fcc96800cb3f5 (patch) | |
| tree | e443d14e34a884b1a4d9c549f81d51202eddd5f7 /opendc | |
| parent | cd5f7bf3a72913e1602cb4c575e61ac7d5519be0 (diff) | |
Prepare web-server repository for monorepo
This change prepares the web-server Git repository for the monorepo residing at
https://github.com/atlarge-research.com/opendc. To accomodate for this, we
move all files into a web-server subdirectory.
Diffstat (limited to 'opendc')
96 files changed, 0 insertions, 3216 deletions
diff --git a/opendc/__init__.py b/opendc/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/__init__.py +++ /dev/null diff --git a/opendc/api/__init__.py b/opendc/api/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/__init__.py +++ /dev/null diff --git a/opendc/api/v2/__init__.py b/opendc/api/v2/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/__init__.py +++ /dev/null diff --git a/opendc/api/v2/experiments/__init__.py b/opendc/api/v2/experiments/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/experiments/__init__.py +++ /dev/null diff --git a/opendc/api/v2/experiments/experimentId/__init__.py b/opendc/api/v2/experiments/experimentId/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/experiments/experimentId/__init__.py +++ /dev/null diff --git a/opendc/api/v2/experiments/experimentId/endpoint.py b/opendc/api/v2/experiments/experimentId/endpoint.py deleted file mode 100644 index bc2b139e..00000000 --- a/opendc/api/v2/experiments/experimentId/endpoint.py +++ /dev/null @@ -1,113 +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.""" - - 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 - - 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()) - - -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 - - 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 edit this Experiment - - if not experiment.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from updating {}.'.format(experiment)) - - # 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()) - - -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 - - if not experiment.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from deleting {}.'.format(experiment)) - - # Delete and return this Experiment - - experiment.delete() - - return Response(200, 'Successfully deleted {}.'.format(experiment), experiment.to_JSON()) diff --git a/opendc/api/v2/experiments/experimentId/last-simulated-tick/__init__.py b/opendc/api/v2/experiments/experimentId/last-simulated-tick/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/experiments/experimentId/last-simulated-tick/__init__.py +++ /dev/null diff --git a/opendc/api/v2/experiments/experimentId/last-simulated-tick/endpoint.py b/opendc/api/v2/experiments/experimentId/last-simulated-tick/endpoint.py deleted file mode 100644 index 3309502c..00000000 --- a/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}) diff --git a/opendc/api/v2/experiments/experimentId/machine-states/__init__.py b/opendc/api/v2/experiments/experimentId/machine-states/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/experiments/experimentId/machine-states/__init__.py +++ /dev/null diff --git a/opendc/api/v2/experiments/experimentId/machine-states/endpoint.py b/opendc/api/v2/experiments/experimentId/machine-states/endpoint.py deleted file mode 100644 index c7dcad9a..00000000 --- a/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/opendc/api/v2/experiments/experimentId/rack-states/__init__.py b/opendc/api/v2/experiments/experimentId/rack-states/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/experiments/experimentId/rack-states/__init__.py +++ /dev/null diff --git a/opendc/api/v2/experiments/experimentId/rack-states/endpoint.py b/opendc/api/v2/experiments/experimentId/rack-states/endpoint.py deleted file mode 100644 index f3acf56a..00000000 --- a/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/opendc/api/v2/experiments/experimentId/room-states/__init__.py b/opendc/api/v2/experiments/experimentId/room-states/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/experiments/experimentId/room-states/__init__.py +++ /dev/null diff --git a/opendc/api/v2/experiments/experimentId/room-states/endpoint.py b/opendc/api/v2/experiments/experimentId/room-states/endpoint.py deleted file mode 100644 index db3f8b14..00000000 --- a/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]) diff --git a/opendc/api/v2/experiments/experimentId/statistics/__init__.py b/opendc/api/v2/experiments/experimentId/statistics/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/experiments/experimentId/statistics/__init__.py +++ /dev/null diff --git a/opendc/api/v2/experiments/experimentId/statistics/task-durations/__init__.py b/opendc/api/v2/experiments/experimentId/statistics/task-durations/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/experiments/experimentId/statistics/task-durations/__init__.py +++ /dev/null diff --git a/opendc/api/v2/experiments/experimentId/statistics/task-durations/endpoint.py b/opendc/api/v2/experiments/experimentId/statistics/task-durations/endpoint.py deleted file mode 100644 index 498db239..00000000 --- a/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/opendc/api/v2/experiments/experimentId/task-states/__init__.py b/opendc/api/v2/experiments/experimentId/task-states/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/experiments/experimentId/task-states/__init__.py +++ /dev/null diff --git a/opendc/api/v2/experiments/experimentId/task-states/endpoint.py b/opendc/api/v2/experiments/experimentId/task-states/endpoint.py deleted file mode 100644 index c0ae47fc..00000000 --- a/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]) diff --git a/opendc/api/v2/paths.json b/opendc/api/v2/paths.json deleted file mode 100644 index ce054a8c..00000000 --- a/opendc/api/v2/paths.json +++ /dev/null @@ -1,19 +0,0 @@ -[ - "/users", - "/users/{userId}", - "/simulations", - "/simulations/{simulationId}", - "/simulations/{simulationId}/authorizations", - "/simulations/{simulationId}/topologies", - "/experiments/{experimentId}/last-simulated-tick", - "/experiments/{experimentId}/machine-states", - "/experiments/{experimentId}/rack-states", - "/experiments/{experimentId}/room-states", - "/experiments/{experimentId}/task-states", - "/topologies/{topologyId}", - "/simulations/{simulationId}/experiments", - "/experiments/{experimentId}", - "/schedulers", - "/traces", - "/traces/{traceId}" -] diff --git a/opendc/api/v2/schedulers/__init__.py b/opendc/api/v2/schedulers/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/schedulers/__init__.py +++ /dev/null diff --git a/opendc/api/v2/schedulers/endpoint.py b/opendc/api/v2/schedulers/endpoint.py deleted file mode 100644 index 0bbc3322..00000000 --- a/opendc/api/v2/schedulers/endpoint.py +++ /dev/null @@ -1,10 +0,0 @@ -from opendc.util.rest import Response - - -SCHEDULERS = ['DEFAULT'] - - -def GET(_): - """Get all available Schedulers.""" - - return Response(200, 'Successfully retrieved Schedulers.', [{'name': name} for name in SCHEDULERS]) diff --git a/opendc/api/v2/schedulers/test_endpoint.py b/opendc/api/v2/schedulers/test_endpoint.py deleted file mode 100644 index a0bd8758..00000000 --- a/opendc/api/v2/schedulers/test_endpoint.py +++ /dev/null @@ -1,2 +0,0 @@ -def test_get_schedulers(client): - assert '200' in client.get('/api/v2/schedulers').status diff --git a/opendc/api/v2/simulations/__init__.py b/opendc/api/v2/simulations/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/simulations/__init__.py +++ /dev/null diff --git a/opendc/api/v2/simulations/endpoint.py b/opendc/api/v2/simulations/endpoint.py deleted file mode 100644 index 232df2ff..00000000 --- a/opendc/api/v2/simulations/endpoint.py +++ /dev/null @@ -1,30 +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 import exceptions -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'}) - topology.insert() - - simulation = 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.obj['_id']]) - simulation.set_property('experimentIds', []) - simulation.insert() - - user = User.from_google_id(request.google_id) - user.obj['authorizations'].append({'simulationId': simulation.obj['_id'], 'authorizationLevel': 'OWN'}) - user.update() - - return Response(200, 'Successfully created simulation.', simulation.obj) diff --git a/opendc/api/v2/simulations/simulationId/__init__.py b/opendc/api/v2/simulations/simulationId/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/simulations/simulationId/__init__.py +++ /dev/null diff --git a/opendc/api/v2/simulations/simulationId/authorizations/__init__.py b/opendc/api/v2/simulations/simulationId/authorizations/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/simulations/simulationId/authorizations/__init__.py +++ /dev/null diff --git a/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py b/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py deleted file mode 100644 index df2b5cfd..00000000 --- a/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py +++ /dev/null @@ -1,37 +0,0 @@ -from opendc.models_old.authorization import Authorization -from opendc.models_old.simulation import Simulation -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Find all authorizations for a Simulation.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters(path={'simulationId': 'string'}) - - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Instantiate a Simulation and make sure it exists - - simulation = Simulation.from_primary_key((request.params_path['simulationId'], )) - - if not simulation.exists(): - return Response(404, '{} not found.'.format(simulation)) - - # Make sure this User is allowed to view this Simulation's Authorizations - - if not simulation.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from retrieving Authorizations for {}.'.format(simulation)) - - # Get the Authorizations - - authorizations = Authorization.query('simulation_id', request.params_path['simulationId']) - - # Return the Authorizations - - return Response(200, 'Successfully retrieved Authorizations for {}.'.format(simulation), - [x.to_JSON() for x in authorizations]) diff --git a/opendc/api/v2/simulations/simulationId/authorizations/userId/__init__.py b/opendc/api/v2/simulations/simulationId/authorizations/userId/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/simulations/simulationId/authorizations/userId/__init__.py +++ /dev/null diff --git a/opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py b/opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py deleted file mode 100644 index 121530db..00000000 --- a/opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py +++ /dev/null @@ -1,178 +0,0 @@ -from opendc.models_old.authorization import Authorization -from opendc.models_old.simulation import Simulation -from opendc.models_old.user import User -from opendc.util import exceptions -from opendc.util.rest import Response - - -def DELETE(request): - """Delete a user's authorization level over a simulation.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters(path={'simulationId': 'string', 'userId': 'string'}) - - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Instantiate an Authorization - - authorization = Authorization.from_primary_key((request.params_path['userId'], request.params_path['simulationId'])) - - # Make sure this Authorization exists in the database - - if not authorization.exists(): - return Response(404, '{} not found.'.format(authorization)) - - # Make sure this User is allowed to delete this Authorization - - if not authorization.google_id_has_at_least(request.google_id, 'OWN'): - return Response(403, 'Forbidden from deleting {}.'.format(authorization)) - - # Delete this Authorization - - authorization.delete() - - return Response(200, 'Successfully deleted {}.'.format(authorization), authorization.to_JSON()) - - -def GET(request): - """Get this User's Authorization over this Simulation.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters(path={'simulationId': 'string', 'userId': 'string'}) - - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Instantiate an Authorization - - authorization = Authorization.from_primary_key((request.params_path['userId'], request.params_path['simulationId'])) - - # Make sure this Authorization exists in the database - - if not authorization.exists(): - return Response(404, '{} not found.'.format(authorization)) - - # Read this Authorization from the database - - authorization.read() - - # Return this Authorization - - return Response(200, 'Successfully retrieved {}'.format(authorization), authorization.to_JSON()) - - -def POST(request): - """Add an authorization for a user's access to a simulation.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters(path={ - 'userId': 'string', - 'simulationId': 'string' - }, - body={'authorization': { - 'authorizationLevel': 'string' - }}) - - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Instantiate an Authorization - - authorization = Authorization.from_JSON({ - 'userId': - request.params_path['userId'], - 'simulationId': - request.params_path['simulationId'], - 'authorizationLevel': - request.params_body['authorization']['authorizationLevel'] - }) - - # Make sure the Simulation and User exist - - user = User.from_primary_key((authorization.user_id, )) - if not user.exists(): - return Response(404, '{} not found.'.format(user)) - - simulation = Simulation.from_primary_key((authorization.simulation_id, )) - if not simulation.exists(): - return Response(404, '{} not found.'.format(simulation)) - - # Make sure this User is allowed to add this Authorization - - if not simulation.google_id_has_at_least(request.google_id, 'OWN'): - return Response(403, 'Forbidden from creating {}.'.format(authorization)) - - # Make sure this Authorization does not already exist - - if authorization.exists(): - return Response(409, '{} already exists.'.format(authorization)) - - # Try to insert this Authorization into the database - - try: - authorization.insert() - - except exceptions.ForeignKeyError: - return Response(400, 'Invalid authorizationLevel') - - # Return this Authorization - - return Response(200, 'Successfully added {}'.format(authorization), authorization.to_JSON()) - - -def PUT(request): - """Change a user's authorization level over a simulation.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters(path={ - 'simulationId': 'string', - 'userId': 'string' - }, - body={'authorization': { - 'authorizationLevel': 'string' - }}) - - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Instantiate and Authorization - - authorization = Authorization.from_JSON({ - 'userId': - request.params_path['userId'], - 'simulationId': - request.params_path['simulationId'], - 'authorizationLevel': - request.params_body['authorization']['authorizationLevel'] - }) - - # Make sure this Authorization exists - - if not authorization.exists(): - return Response(404, '{} not found.'.format(authorization)) - - # Make sure this User is allowed to edit this Authorization - - if not authorization.google_id_has_at_least(request.google_id, 'OWN'): - return Response(403, 'Forbidden from updating {}.'.format(authorization)) - - # Try to update this Authorization - - try: - authorization.update() - - except exceptions.ForeignKeyError as e: - return Response(400, 'Invalid authorization level.') - - # Return this Authorization - - return Response(200, 'Successfully updated {}.'.format(authorization), authorization.to_JSON()) diff --git a/opendc/api/v2/simulations/simulationId/endpoint.py b/opendc/api/v2/simulations/simulationId/endpoint.py deleted file mode 100644 index 282e3291..00000000 --- a/opendc/api/v2/simulations/simulationId/endpoint.py +++ /dev/null @@ -1,57 +0,0 @@ -from datetime import datetime - -from opendc.models.simulation import Simulation -from opendc.models.topology import Topology -from opendc.util.database import Database -from opendc.util.rest import Response - - -def GET(request): - """Get this Simulation.""" - - request.check_required_parameters(path={'simulationId': 'string'}) - - simulation = Simulation.from_id(request.params_path['simulationId']) - - simulation.check_exists() - simulation.check_user_access(request.google_id, False) - - return Response(200, 'Successfully retrieved simulation', simulation.obj) - - -def PUT(request): - """Update a simulation's name.""" - - request.check_required_parameters(body={'simulation': {'name': 'name'}}, path={'simulationId': 'string'}) - - simulation = Simulation.from_id(request.params_path['simulationId']) - - simulation.check_exists() - simulation.check_user_access(request.google_id, True) - - simulation.set_property('name', request.params_body['simulation']['name']) - simulation.set_property('datetime_last_edited', Database.datetime_to_string(datetime.now())) - simulation.update() - - return Response(200, 'Successfully updated simulation.', simulation.obj) - - -def DELETE(request): - """Delete this Simulation.""" - - request.check_required_parameters(path={'simulationId': 'string'}) - - simulation = Simulation.from_id(request.params_path['simulationId']) - - simulation.check_exists() - simulation.check_user_access(request.google_id, True) - - for topology_id in simulation.obj['topologyIds']: - topology = Topology.from_id(topology_id) - topology.delete() - - # TODO remove all experiments - - simulation.delete() - - return Response(200, f'Successfully deleted simulation.', simulation.obj) diff --git a/opendc/api/v2/simulations/simulationId/experiments/__init__.py b/opendc/api/v2/simulations/simulationId/experiments/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/simulations/simulationId/experiments/__init__.py +++ /dev/null diff --git a/opendc/api/v2/simulations/simulationId/experiments/endpoint.py b/opendc/api/v2/simulations/simulationId/experiments/endpoint.py deleted file mode 100644 index 9df84838..00000000 --- a/opendc/api/v2/simulations/simulationId/experiments/endpoint.py +++ /dev/null @@ -1,97 +0,0 @@ -from opendc.models_old.experiment import Experiment -from opendc.models_old.simulation import Simulation -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get this Simulation's Experiments.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters(path={'simulationId': 'string'}) - - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Instantiate a Simulation from the database - - simulation = Simulation.from_primary_key((request.params_path['simulationId'], )) - - # Make sure this Simulation exists - - if not simulation.exists(): - return Response(404, '{} not found.'.format(simulation)) - - # Make sure this user is authorized to view this Simulation's Experiments - - if not simulation.google_id_has_at_least(request.google_id, 'VIEW'): - return Reponse(403, 'Forbidden from viewing Experiments for {}.'.format(simulation)) - - # Get and return the Experiments - - experiments = Experiment.query('simulation_id', request.params_path['simulationId']) - - return Response(200, 'Successfully retrieved Experiments for {}.'.format(simulation), - [x.to_JSON() for x in experiments]) - - -def POST(request): - """Add a new Experiment for this Simulation.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters(path={'simulationId': 'string'}, - body={ - 'experiment': { - 'simulationId': 'string', - 'pathId': 'int', - 'traceId': 'int', - 'schedulerName': 'string', - 'name': 'string' - } - }) - - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Make sure the passed object's simulation id matches the path simulation id - - if request.params_path['simulationId'] != request.params_body['experiment']['simulationId']: - return Response(403, 'ID mismatch.') - - # Instantiate a Simulation from the database - - simulation = Simulation.from_primary_key((request.params_path['simulationId'], )) - - # Make sure this Simulation exists - - if not simulation.exists(): - return Response(404, '{} not found.'.format(simulation)) - - # Make sure this user is authorized to edit this Simulation's Experiments - - if not simulation.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from adding an experiment to {}.'.format(simulation)) - - # Instantiate an Experiment - - experiment = Experiment.from_JSON(request.params_body['experiment']) - experiment.state = 'QUEUED' - experiment.last_simulated_tick = 0 - - # Try to insert this Experiment - - try: - experiment.insert() - - except exceptions.ForeignKeyError as e: - return Response(400, 'Foreign key constraint not met.' + e) - - # Return this Experiment - - experiment.read() - - return Response(200, 'Successfully added {}.'.format(experiment), experiment.to_JSON()) diff --git a/opendc/api/v2/simulations/simulationId/test_endpoint.py b/opendc/api/v2/simulations/simulationId/test_endpoint.py deleted file mode 100644 index a0586aab..00000000 --- a/opendc/api/v2/simulations/simulationId/test_endpoint.py +++ /dev/null @@ -1,97 +0,0 @@ -from opendc.util.database import DB - - -def test_get_simulation_non_existing(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value=None) - assert '404' in client.get('/api/v2/simulations/1').status - - -def test_get_simulation_no_authorizations(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value={'authorizations': []}) - res = client.get('/api/v2/simulations/1') - assert '403' in res.status - - -def test_get_simulation_not_authorized(client, mocker): - mocker.patch.object(DB, - 'fetch_one', - return_value={ - '_id': '1', - 'authorizations': [{ - 'simulationId': '2', - 'authorizationLevel': 'OWN' - }] - }) - res = client.get('/api/v2/simulations/1') - assert '403' in res.status - - -def test_get_simulation(client, mocker): - mocker.patch.object(DB, - 'fetch_one', - return_value={ - '_id': '1', - 'authorizations': [{ - 'simulationId': '1', - 'authorizationLevel': 'EDIT' - }] - }) - res = client.get('/api/v2/simulations/1') - assert '200' in res.status - - -def test_update_simulation_missing_parameter(client): - assert '400' in client.put('/api/v2/simulations/1').status - - -def test_update_simulation_non_existing(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value=None) - assert '404' in client.put('/api/v2/simulations/1', json={'simulation': {'name': 'S'}}).status - - -def test_update_simulation_not_authorized(client, mocker): - mocker.patch.object(DB, - 'fetch_one', - return_value={ - '_id': '1', - 'authorizations': [{ - 'simulationId': '1', - 'authorizationLevel': 'VIEW' - }] - }) - mocker.patch.object(DB, 'update', return_value={}) - assert '403' in client.put('/api/v2/simulations/1', json={'simulation': {'name': 'S'}}).status - - -def test_update_simulation(client, mocker): - mocker.patch.object(DB, - 'fetch_one', - return_value={ - '_id': '1', - 'authorizations': [{ - 'simulationId': '1', - 'authorizationLevel': 'OWN' - }] - }) - mocker.patch.object(DB, 'update', return_value={}) - - res = client.put('/api/v2/simulations/1', json={'simulation': {'name': 'S'}}) - assert '200' in res.status - - -def test_delete_simulation_non_existing(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value=None) - assert '404' in client.delete('/api/v2/simulations/1').status - - -def test_delete_simulation_different_user(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1', 'googleId': 'other_test', 'authorizations': [{'simulationId': '1', 'authorizationLevel': 'VIEW'}], 'topologyIds': []}) - mocker.patch.object(DB, 'delete_one', return_value=None) - assert '403' in client.delete('/api/v2/simulations/1').status - - -def test_delete_simulation(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1', 'googleId': 'test', 'authorizations': [{'simulationId': '1', 'authorizationLevel': 'OWN'}], 'topologyIds': []}) - mocker.patch.object(DB, 'delete_one', return_value={'googleId': 'test'}) - res = client.delete('/api/v2/simulations/1') - assert '200' in res.status diff --git a/opendc/api/v2/simulations/simulationId/topologies/__init__.py b/opendc/api/v2/simulations/simulationId/topologies/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/simulations/simulationId/topologies/__init__.py +++ /dev/null diff --git a/opendc/api/v2/simulations/simulationId/topologies/endpoint.py b/opendc/api/v2/simulations/simulationId/topologies/endpoint.py deleted file mode 100644 index ab7b7006..00000000 --- a/opendc/api/v2/simulations/simulationId/topologies/endpoint.py +++ /dev/null @@ -1,29 +0,0 @@ -from datetime import datetime - -from opendc.models.simulation import Simulation -from opendc.models.topology import Topology -from opendc.util import exceptions -from opendc.util.rest import Response -from opendc.util.database import Database - - -def POST(request): - """Add a new Topology to the specified simulation and return it""" - - request.check_required_parameters(path={'simulationId': 'string'}, body={'topology': {'name': 'string'}}) - - simulation = Simulation.from_id(request.params_path['simulationId']) - - simulation.check_exists() - simulation.check_user_access(request.google_id, True) - - topology = Topology({'name': request.params_body['topology']['name']}) - topology.set_property('datetimeCreated', Database.datetime_to_string(datetime.now())) - topology.set_property('datetimeLastEdited', Database.datetime_to_string(datetime.now())) - topology.insert() - - simulation.obj['topologyIds'].append(topology.obj['_id']) - simulation.set_property('datetimeLastEdited', Database.datetime_to_string(datetime.now())) - simulation.update() - - return Response(200, 'Successfully inserted topology.', topology.obj) diff --git a/opendc/api/v2/simulations/simulationId/topologies/test_endpoint.py b/opendc/api/v2/simulations/simulationId/topologies/test_endpoint.py deleted file mode 100644 index 10b5e3c9..00000000 --- a/opendc/api/v2/simulations/simulationId/topologies/test_endpoint.py +++ /dev/null @@ -1,26 +0,0 @@ -from opendc.util.database import DB - - -def test_add_topology_missing_parameter(client): - assert '400' in client.post('/api/v2/simulations/1/topologies/').status - - -def test_add_topology(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1', 'authorizations': [{'simulationId': '1', 'authorizationLevel': 'OWN'}], 'topologyIds': []}) - mocker.patch.object(DB, - 'insert', - return_value={ - '_id': '1', - 'datetimeCreated': '000', - 'datetimeEdit': '000', - 'topologyIds': [] - }) - mocker.patch.object(DB, 'update', return_value={}) - res = client.post('/api/v2/simulations/1/topologies/', json={'topology': {'name': 'test simulation'}}) - assert 'datetimeCreated' in res.json['content'] - assert 'datetimeEdit' in res.json['content'] - assert 'topologyIds' in res.json['content'] - assert '200' in res.status - -def test_add_topology_no_authorizations(client, mocker): - pass
\ No newline at end of file diff --git a/opendc/api/v2/simulations/test_endpoint.py b/opendc/api/v2/simulations/test_endpoint.py deleted file mode 100644 index d23df74a..00000000 --- a/opendc/api/v2/simulations/test_endpoint.py +++ /dev/null @@ -1,23 +0,0 @@ -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_simulation(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1', 'authorizations': []}) - mocker.patch.object(DB, - 'insert', - return_value={ - '_id': '1', - 'datetimeCreated': '000', - 'datetimeEdit': '000', - 'topologyIds': [] - }) - mocker.patch.object(DB, 'update', return_value={}) - res = client.post('/api/v2/simulations', json={'simulation': {'name': 'test simulation'}}) - assert 'datetimeCreated' in res.json['content'] - assert 'datetimeEdit' in res.json['content'] - assert 'topologyIds' in res.json['content'] - assert '200' in res.status diff --git a/opendc/api/v2/topologies/__init__.py b/opendc/api/v2/topologies/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/topologies/__init__.py +++ /dev/null diff --git a/opendc/api/v2/topologies/topologyId/__init__.py b/opendc/api/v2/topologies/topologyId/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/topologies/topologyId/__init__.py +++ /dev/null diff --git a/opendc/api/v2/topologies/topologyId/endpoint.py b/opendc/api/v2/topologies/topologyId/endpoint.py deleted file mode 100644 index 6c6ab9c2..00000000 --- a/opendc/api/v2/topologies/topologyId/endpoint.py +++ /dev/null @@ -1,16 +0,0 @@ -from opendc.models.topology import Topology -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get this Topology.""" - - request.check_required_parameters(path={'topologyId': 'int'}) - - topology = Topology.from_id(request.params_path['topologyId']) - - topology.check_exists() - topology.check_user_access(request.google_id, False) - - return Response(200, 'Successfully retrieved topology.', topology.obj) diff --git a/opendc/api/v2/topologies/topologyId/rooms/__init__.py b/opendc/api/v2/topologies/topologyId/rooms/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/topologies/topologyId/rooms/__init__.py +++ /dev/null diff --git a/opendc/api/v2/topologies/topologyId/rooms/endpoint.py b/opendc/api/v2/topologies/topologyId/rooms/endpoint.py deleted file mode 100644 index 96ee7028..00000000 --- a/opendc/api/v2/topologies/topologyId/rooms/endpoint.py +++ /dev/null @@ -1,93 +0,0 @@ -from opendc.models_old.datacenter import Datacenter -from opendc.models_old.room import Room -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get this Datacenter's Rooms.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters(path={'datacenterId': 'int'}) - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Instantiate a Datacenter from the database - - datacenter = Datacenter.from_primary_key((request.params_path['datacenterId'], )) - - # Make sure this Datacenter exists - - if not datacenter.exists(): - return Response(404, '{} not found.'.format(datacenter)) - - # Make sure this user is authorized to view this Datacenter's Rooms - - if not datacenter.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing Rooms for {}.'.format(datacenter)) - - # Get and return the Rooms - - rooms = Room.query('datacenter_id', datacenter.id) - - return Response(200, 'Successfully retrieved Rooms for {}.'.format(datacenter), [x.to_JSON() for x in rooms]) - - -def POST(request): - """Add a Room.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters(path={'datacenterId': 'int'}, - body={'room': { - 'id': 'int', - 'datacenterId': 'int', - 'roomType': 'string' - }}) - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Make sure the passed object's datacenter id matches the path datacenter id - - if request.params_path['datacenterId'] != request.params_body['room']['datacenterId']: - return Response(400, 'ID mismatch.') - - # Instantiate a Datacenter from the database - - datacenter = Datacenter.from_primary_key((request.params_path['datacenterId'], )) - - # Make sure this Datacenter exists - - if not datacenter.exists(): - return Response(404, '{} not found.'.format(datacenter)) - - # Make sure this user is authorized to edit this Datacenter's Rooms - - if not datacenter.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from adding a Room to {}.'.format(datacenter)) - - # Add a name if not provided - - if 'name' not in request.params_body['room']: - room_count = len(Room.query('datacenter_id', datacenter.id)) - request.params_body['room']['name'] = 'Room {}'.format(room_count) - - # Instantiate a Room - - room = Room.from_JSON(request.params_body['room']) - - # Try to insert this Room - - try: - room.insert() - except: - return Response(400, 'Invalid `roomType` or existing `name`.') - - # Return this Room - - room.read() - - return Response(200, 'Successfully added {}.'.format(room), room.to_JSON()) diff --git a/opendc/api/v2/topologies/topologyId/test_endpoint.py b/opendc/api/v2/topologies/topologyId/test_endpoint.py deleted file mode 100644 index e54052aa..00000000 --- a/opendc/api/v2/topologies/topologyId/test_endpoint.py +++ /dev/null @@ -1,46 +0,0 @@ -from opendc.util.database import DB - -''' -GET /topologies/{topologyId} -''' - -def test_get_topology(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value={ - '_id': '1', - 'authorizations': [{ - 'topologyId': '1', - 'authorizationLevel': 'EDIT' - }] - }) - res = client.get('/api/v2/topologies/1') - assert '200' in res.status - -def test_get_topology_non_existing(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value=None) - assert '404' in client.get('/api/v2/topologies/1').status - -def test_get_topology_not_authorized(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value={ - '_id': '1', - 'authorizations': [{ - 'topologyId': '2', - 'authorizationLevel': 'OWN' - }] - }) - res = client.get('/api/v2/topologies/1') - assert '403' in res.status - -def test_get_topology_no_authorizations(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value={'authorizations': []}) - res = client.get('/api/v2/topologies/1') - assert '403' in res.status - - -''' -PUT /topologies/{topologyId} -''' - - -''' -DELETE /topologies/{topologyId} -'''
\ No newline at end of file diff --git a/opendc/api/v2/traces/__init__.py b/opendc/api/v2/traces/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/traces/__init__.py +++ /dev/null diff --git a/opendc/api/v2/traces/endpoint.py b/opendc/api/v2/traces/endpoint.py deleted file mode 100644 index 58cc6153..00000000 --- a/opendc/api/v2/traces/endpoint.py +++ /dev/null @@ -1,14 +0,0 @@ -from opendc.models_old.trace import Trace -from opendc.util.rest import Response - - -def GET(request): - """Get all available Traces.""" - - # Get the Traces - - traces = Trace.query() - - # Return the Traces - - return Response(200, 'Successfully retrieved Traces', [x.to_JSON() for x in traces]) diff --git a/opendc/api/v2/traces/traceId/__init__.py b/opendc/api/v2/traces/traceId/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/traces/traceId/__init__.py +++ /dev/null diff --git a/opendc/api/v2/traces/traceId/endpoint.py b/opendc/api/v2/traces/traceId/endpoint.py deleted file mode 100644 index f6442a31..00000000 --- a/opendc/api/v2/traces/traceId/endpoint.py +++ /dev/null @@ -1,26 +0,0 @@ -from opendc.models_old.trace import Trace -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get this Trace.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters(path={'traceId': 'int'}) - - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Instantiate a Trace and make sure it exists - - trace = Trace.from_primary_key((request.params_path['traceId'], )) - - if not trace.exists(): - return Response(404, '{} not found.'.format(trace)) - - # Return this Trace - - return Response(200, 'Successfully retrieved {}.'.format(trace), trace.to_JSON()) diff --git a/opendc/api/v2/users/__init__.py b/opendc/api/v2/users/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/users/__init__.py +++ /dev/null diff --git a/opendc/api/v2/users/endpoint.py b/opendc/api/v2/users/endpoint.py deleted file mode 100644 index c6041756..00000000 --- a/opendc/api/v2/users/endpoint.py +++ /dev/null @@ -1,31 +0,0 @@ -from opendc.models.user import User -from opendc.util import exceptions -from opendc.util.database import DB -from opendc.util.rest import Response - - -def GET(request): - """Search for a User using their email address.""" - - request.check_required_parameters(query={'email': 'string'}) - - user = User.from_email(request.params_query['email']) - - user.check_exists() - - return Response(200, f'Successfully retrieved user.', user.obj) - - -def POST(request): - """Add a new User.""" - - request.check_required_parameters(body={'user': {'email': 'string'}}) - - user = User(request.params_body['user']) - user.set_property('googleId', request.google_id) - user.set_property('authorizations', []) - - user.check_already_exists() - - user.insert() - return Response(200, f'Successfully created user.', user.obj) diff --git a/opendc/api/v2/users/test_endpoint.py b/opendc/api/v2/users/test_endpoint.py deleted file mode 100644 index d60429b3..00000000 --- a/opendc/api/v2/users/test_endpoint.py +++ /dev/null @@ -1,34 +0,0 @@ -from opendc.util.database import DB - - -def test_get_user_by_email_missing_parameter(client): - assert '400' in client.get('/api/v2/users').status - - -def test_get_user_by_email_non_existing(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value=None) - assert '404' in client.get('/api/v2/users?email=test@test.com').status - - -def test_get_user_by_email(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value={'email': 'test@test.com'}) - res = client.get('/api/v2/users?email=test@test.com') - assert 'email' in res.json['content'] - assert '200' in res.status - - -def test_add_user_missing_parameter(client): - assert '400' in client.post('/api/v2/users').status - - -def test_add_user_existing(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value={'email': 'test@test.com'}) - assert '409' in client.post('/api/v2/users', json={'user': {'email': 'test@test.com'}}).status - - -def test_add_user(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value=None) - mocker.patch.object(DB, 'insert', return_value={'email': 'test@test.com'}) - res = client.post('/api/v2/users', json={'user': {'email': 'test@test.com'}}) - assert 'email' in res.json['content'] - assert '200' in res.status diff --git a/opendc/api/v2/users/userId/__init__.py b/opendc/api/v2/users/userId/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/api/v2/users/userId/__init__.py +++ /dev/null diff --git a/opendc/api/v2/users/userId/endpoint.py b/opendc/api/v2/users/userId/endpoint.py deleted file mode 100644 index e68a2bb3..00000000 --- a/opendc/api/v2/users/userId/endpoint.py +++ /dev/null @@ -1,52 +0,0 @@ -from opendc.models.user import User -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get this User.""" - - request.check_required_parameters(path={'userId': 'string'}) - - user = User.from_id(request.params_path['userId']) - - user.check_exists() - - return Response(200, f'Successfully retrieved user.', user.obj) - - -def PUT(request): - """Update this User's given name and/or family name.""" - - request.check_required_parameters(body={'user': { - 'givenName': 'string', - 'familyName': 'string' - }}, - path={'userId': 'string'}) - - user = User.from_id(request.params_path['userId']) - - user.check_exists() - user.check_correct_user(request.google_id) - - user.set_property('givenName', request.params_body['user']['givenName']) - user.set_property('familyName', request.params_body['user']['familyName']) - - user.update() - - return Response(200, f'Successfully updated user.', user.obj) - - -def DELETE(request): - """Delete this User.""" - - request.check_required_parameters(path={'userId': 'string'}) - - user = User.from_id(request.params_path['userId']) - - user.check_exists() - user.check_correct_user(request.google_id) - - user.delete() - - return Response(200, f'Successfully deleted user.', user.obj) diff --git a/opendc/api/v2/users/userId/test_endpoint.py b/opendc/api/v2/users/userId/test_endpoint.py deleted file mode 100644 index 0d590129..00000000 --- a/opendc/api/v2/users/userId/test_endpoint.py +++ /dev/null @@ -1,53 +0,0 @@ -from opendc.util.database import DB - - -def test_get_user_non_existing(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value=None) - assert '404' in client.get('/api/v2/users/1').status - - -def test_get_user(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value={'email': 'test@test.com'}) - res = client.get('/api/v2/users/1') - assert 'email' in res.json['content'] - assert '200' in res.status - - -def test_update_user_missing_parameter(client): - assert '400' in client.put('/api/v2/users/1').status - - -def test_update_user_non_existing(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value=None) - assert '404' in client.put('/api/v2/users/1', json={'user': {'givenName': 'A', 'familyName': 'B'}}).status - - -def test_update_user_different_user(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1', 'googleId': 'other_test'}) - assert '403' in client.put('/api/v2/users/1', json={'user': {'givenName': 'A', 'familyName': 'B'}}).status - - -def test_update_user(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1', 'googleId': 'test'}) - mocker.patch.object(DB, 'update', return_value={'givenName': 'A', 'familyName': 'B'}) - res = client.put('/api/v2/users/1', json={'user': {'givenName': 'A', 'familyName': 'B'}}) - assert 'givenName' in res.json['content'] - assert '200' in res.status - - -def test_delete_user_non_existing(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value=None) - assert '404' in client.delete('/api/v2/users/1').status - - -def test_delete_user_different_user(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1', 'googleId': 'other_test'}) - assert '403' in client.delete('/api/v2/users/1').status - - -def test_delete_user(client, mocker): - mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1', 'googleId': 'test'}) - mocker.patch.object(DB, 'delete_one', return_value={'googleId': 'test'}) - res = client.delete('/api/v2/users/1') - assert 'googleId' in res.json['content'] - assert '200' in res.status diff --git a/opendc/models/__init__.py b/opendc/models/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/models/__init__.py +++ /dev/null diff --git a/opendc/models/model.py b/opendc/models/model.py deleted file mode 100644 index 2505ae61..00000000 --- a/opendc/models/model.py +++ /dev/null @@ -1,30 +0,0 @@ -from opendc.util.database import DB -from opendc.util.exceptions import ClientError -from opendc.util.rest import Response - - -class Model: - collection_name = '<specified in subclasses>' - - @classmethod - def from_id(cls, _id): - return cls(DB.fetch_one({'_id': _id}, Model.collection_name)) - - def __init__(self, obj): - self.obj = obj - - def check_exists(self): - if self.obj is None: - raise ClientError(Response(404, 'Not found.')) - - def set_property(self, key, value): - self.obj[key] = value - - def insert(self): - self.obj = DB.insert(self.obj, self.collection_name) - - def update(self): - self.obj = DB.update(self.obj['_id'], self.obj, self.collection_name) - - def delete(self): - self.obj = DB.delete_one({'_id': self.obj['_id']}, self.collection_name) diff --git a/opendc/models/simulation.py b/opendc/models/simulation.py deleted file mode 100644 index 5cd3d49e..00000000 --- a/opendc/models/simulation.py +++ /dev/null @@ -1,15 +0,0 @@ -from opendc.models.model import Model -from opendc.models.user import User -from opendc.util.exceptions import ClientError -from opendc.util.rest import Response - - -class Simulation(Model): - collection_name = 'simulations' - - def check_user_access(self, google_id, edit_access): - user = User.from_google_id(google_id) - authorizations = list( - filter(lambda x: str(x['simulationId']) == str(self.obj['_id']), user.obj['authorizations'])) - if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'): - raise ClientError(Response(403, "Forbidden from retrieving simulation.")) diff --git a/opendc/models/topology.py b/opendc/models/topology.py deleted file mode 100644 index 6dde3e2a..00000000 --- a/opendc/models/topology.py +++ /dev/null @@ -1,15 +0,0 @@ -from opendc.models.model import Model -from opendc.models.user import User -from opendc.util.exceptions import ClientError -from opendc.util.rest import Response - - -class Topology(Model): - collection_name = 'topologies' - - def check_user_access(self, google_id, edit_access): - user = User.from_google_id(google_id) - authorizations = list( - filter(lambda x: str(x['topologyId']) == str(self.obj['_id']), user.obj['authorizations'])) - if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'): - raise ClientError(Response(403, "Forbidden from retrieving topology.")) diff --git a/opendc/models/user.py b/opendc/models/user.py deleted file mode 100644 index cd314457..00000000 --- a/opendc/models/user.py +++ /dev/null @@ -1,26 +0,0 @@ -from opendc.models.model import Model -from opendc.util.database import DB -from opendc.util.exceptions import ClientError -from opendc.util.rest import Response - - -class User(Model): - collection_name = 'users' - - @classmethod - def from_email(cls, email): - return User(DB.fetch_one({'email': email}, User.collection_name)) - - @classmethod - def from_google_id(cls, google_id): - return User(DB.fetch_one({'googleId': google_id}, User.collection_name)) - - def check_correct_user(self, request_google_id): - if request_google_id is not None and self.obj['googleId'] != request_google_id: - raise ClientError(Response(403, f'Forbidden from editing user with ID {self.obj["_id"]}.')) - - def check_already_exists(self): - existing_user = DB.fetch_one({'googleId': self.obj['googleId']}, self.collection_name) - - if existing_user is not None: - raise ClientError(Response(409, 'User already exists.')) diff --git a/opendc/models_old/__init__.py b/opendc/models_old/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/models_old/__init__.py +++ /dev/null diff --git a/opendc/models_old/allowed_object.py b/opendc/models_old/allowed_object.py deleted file mode 100644 index bcadf025..00000000 --- a/opendc/models_old/allowed_object.py +++ /dev/null @@ -1,22 +0,0 @@ -from opendc.models_old.model import Model - - -class AllowedObject(Model): - JSON_TO_PYTHON_DICT = {'AllowedObject': {'roomType': 'room_type', 'objectType': 'object_type'}} - - COLLECTION_NAME = 'allowed_objects' - COLUMNS = ['room_type', 'object_type'] - COLUMNS_PRIMARY_KEY = ['room_type', 'object_type'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the user has at least the given auth level over this AllowedObject.""" - - if authorization_level in ['EDIT', 'OWN']: - return False - - return True - - def to_JSON(self): - """Return a JSON representation of this AllowedObject.""" - - return self.object_type diff --git a/opendc/models_old/authorization.py b/opendc/models_old/authorization.py deleted file mode 100644 index 43d784e9..00000000 --- a/opendc/models_old/authorization.py +++ /dev/null @@ -1,45 +0,0 @@ -from opendc.models_old.model import Model -from opendc.models_old.user import User - - -class Authorization(Model): - JSON_TO_PYTHON_DICT = { - 'Authorization': { - 'userId': 'user_id', - 'simulationId': 'simulation_id', - 'authorizationLevel': 'authorization_level' - } - } - - COLLECTION_NAME = 'authorizations' - COLUMNS = ['user_id', 'simulation_id', 'authorization_level'] - COLUMNS_PRIMARY_KEY = ['user_id', 'simulation_id'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the User has at least the given auth level over this Authorization.""" - - authorization = Authorization.from_primary_key((User.from_google_id(google_id).id, self.simulation_id)) - - if authorization is None: - return False - - return authorization.has_at_least(authorization_level) - - def has_at_least(self, required_level): - """Return True if this Authorization has at least the required level.""" - - if not self.exists(): - return False - - authorization_levels = ['VIEW', 'EDIT', 'OWN'] - - try: - index_actual = authorization_levels.index(self.authorization_level) - index_required = authorization_levels.index(required_level) - except: - return False - - if index_actual >= index_required: - return True - else: - return False diff --git a/opendc/models_old/cpu.py b/opendc/models_old/cpu.py deleted file mode 100644 index 0f50ce1c..00000000 --- a/opendc/models_old/cpu.py +++ /dev/null @@ -1,34 +0,0 @@ -from opendc.models_old.model import Model - - -class CPU(Model): - JSON_TO_PYTHON_DICT = { - 'CPU': { - 'id': 'id', - 'manufacturer': 'manufacturer', - 'family': 'family', - 'generation': 'generation', - 'model': 'model', - 'clockRateMhz': 'clock_rate_mhz', - 'numberOfCores': 'number_of_cores', - 'energyConsumptionW': 'energy_consumption_w', - 'failureModelId': 'failure_model_id' - } - } - - COLLECTION_NAME = 'cpus' - - COLUMNS = [ - 'id', 'manufacturer', 'family', 'generation', 'model', 'clock_rate_mhz', 'number_of_cores', - 'energy_consumption_w', 'failure_model_id' - ] - - COLUMNS_PRIMARY_KEY = ['id'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the User has at least the given auth level over this CPU.""" - - if authorization_level in ['EDIT', 'OWN']: - return False - - return True diff --git a/opendc/models_old/datacenter.py b/opendc/models_old/datacenter.py deleted file mode 100644 index b1ed2eee..00000000 --- a/opendc/models_old/datacenter.py +++ /dev/null @@ -1,27 +0,0 @@ -from opendc.models_old.model import Model -from opendc.models_old.section import Section - - -class Datacenter(Model): - JSON_TO_PYTHON_DICT = {'datacenter': {'id': 'id', 'starred': 'starred', 'simulationId': 'simulation_id'}} - - PATH = '/v1/simulations/{simulationId}/datacenters' - - COLLECTION_NAME = 'datacenters' - COLUMNS = ['id', 'simulation_id', 'starred'] - COLUMNS_PRIMARY_KEY = ['id'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the user has at least the given auth level over this Datacenter.""" - - # Get a Section that contains this Datacenter. It doesn't matter which one, since all Sections that have this - # Datacenter belong to the same Simulation, so the User's Authorization is the same for each one. - - try: - section = Section.query('datacenter_id', self.id)[0] - except: - return False - - # Check the Section's Authorization - - return section.google_id_has_at_least(google_id, authorization_level) diff --git a/opendc/models_old/experiment.py b/opendc/models_old/experiment.py deleted file mode 100644 index 64b99212..00000000 --- a/opendc/models_old/experiment.py +++ /dev/null @@ -1,36 +0,0 @@ -from opendc.models_old.model import Model -from opendc.models_old.simulation import Simulation -from opendc.util import exceptions - - -class Experiment(Model): - JSON_TO_PYTHON_DICT = { - 'Experiment': { - 'id': 'id', - 'simulationId': 'simulation_id', - 'pathId': 'path_id', - 'traceId': 'trace_id', - 'schedulerName': 'scheduler_name', - 'name': 'name', - 'state': 'state', - 'lastSimulatedTick': 'last_simulated_tick' - } - } - - COLLECTION_NAME = 'experiments' - COLUMNS = ['id', 'simulation_id', 'path_id', 'trace_id', 'scheduler_name', 'name', 'state', 'last_simulated_tick'] - COLUMNS_PRIMARY_KEY = ['id'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the user has at least the given auth level over this Experiment.""" - - # Get the Simulation - - try: - simulation = Simulation.from_primary_key((self.simulation_id, )) - except exceptions.RowNotFoundError: - return False - - # Check the Simulation's Authorization - - return simulation.google_id_has_at_least(google_id, authorization_level) diff --git a/opendc/models_old/failure_model.py b/opendc/models_old/failure_model.py deleted file mode 100644 index d1a8c1cc..00000000 --- a/opendc/models_old/failure_model.py +++ /dev/null @@ -1,17 +0,0 @@ -from opendc.models_old.model import Model - - -class FailureModel(Model): - JSON_TO_PYTHON_DICT = {'FailureModel': {'id': 'id', 'name': 'name', 'rate': 'rate'}} - - COLLECTION_NAME = 'failure_models' - COLUMNS = ['id', 'name', 'rate'] - COLUMNS_PRIMARY_KEY = ['id'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the user has at least the given auth level over this FailureModel.""" - - if authorization_level in ['EDIT', 'OWN']: - return False - - return True diff --git a/opendc/models_old/gpu.py b/opendc/models_old/gpu.py deleted file mode 100644 index 31b3b6b1..00000000 --- a/opendc/models_old/gpu.py +++ /dev/null @@ -1,34 +0,0 @@ -from opendc.models_old.model import Model - - -class GPU(Model): - JSON_TO_PYTHON_DICT = { - 'GPU': { - 'id': 'id', - 'manufacturer': 'manufacturer', - 'family': 'family', - 'generation': 'generation', - 'model': 'model', - 'clockRateMhz': 'clock_rate_mhz', - 'numberOfCores': 'number_of_cores', - 'energyConsumptionW': 'energy_consumption_w', - 'failureModelId': 'failure_model_id' - } - } - - COLLECTION_NAME = 'gpus' - - COLUMNS = [ - 'id', 'manufacturer', 'family', 'generation', 'model', 'clock_rate_mhz', 'number_of_cores', - 'energy_consumption_w', 'failure_model_id' - ] - - COLUMNS_PRIMARY_KEY = ['id'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the User has at least the given auth level over this GPU.""" - - if authorization_level in ['EDIT', 'OWN']: - return False - - return True diff --git a/opendc/models_old/job.py b/opendc/models_old/job.py deleted file mode 100644 index 9cb7cd7e..00000000 --- a/opendc/models_old/job.py +++ /dev/null @@ -1,14 +0,0 @@ -from opendc.models_old.model import Model - - -class Job(Model): - JSON_TO_PYTHON_DICT = {'Job': {'id': 'id', 'name': 'name'}} - - COLLECTION_NAME = 'jobs' - COLUMNS = ['id', 'name'] - COLUMNS_PRIMARY_KEY = ['id'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the user has at least the given auth level over this Job.""" - - return authorization_level not in ['EDIT', 'OWN'] diff --git a/opendc/models_old/machine.py b/opendc/models_old/machine.py deleted file mode 100644 index 8e5ccb44..00000000 --- a/opendc/models_old/machine.py +++ /dev/null @@ -1,122 +0,0 @@ -import copy - -from opendc.models_old.model import Model -from opendc.models_old.rack import Rack -from opendc.util import database, exceptions - - -class Machine(Model): - JSON_TO_PYTHON_DICT = { - 'machine': { - 'id': 'id', - 'rackId': 'rack_id', - 'position': 'position', - 'tags': 'tags', - 'cpuIds': 'cpu_ids', - 'gpuIds': 'gpu_ids', - 'memoryIds': 'memory_ids', - 'storageIds': 'storage_ids', - 'topologyId': 'topology_id' - } - } - - PATH = '/v1/tiles/{tileId}/rack/machines' - - COLLECTION_NAME = 'machines' - COLUMNS = ['id', 'rack_id', 'position', 'topology_id'] - COLUMNS_PRIMARY_KEY = ['id'] - - device_table_to_attribute = { - 'cpus': 'cpu_ids', - 'gpus': 'gpu_ids', - 'memories': 'memory_ids', - 'storages': 'storage_ids' - } - - def _update_devices(self, before_insert): - """Update this Machine's devices in the database.""" - - for device_table in self.device_table_to_attribute.keys(): - - # First, delete current machine-device links - - statement = 'DELETE FROM machine_{} WHERE machine_id = %s'.format(device_table) - database.execute(statement, (before_insert.id, )) - - # Then, add current ones - - for device_id in getattr(before_insert, before_insert.device_table_to_attribute[device_table]): - statement = 'INSERT INTO machine_{} (machine_id, {}) VALUES (%s, %s)'.format( - device_table, before_insert.device_table_to_attribute[device_table][:-1]) - - database.execute(statement, (before_insert.id, device_id)) - - @classmethod - def from_tile_id_and_rack_position(cls, tile_id, position): - """Get a Rack from the ID of the tile its Rack is on, and its position in the Rack.""" - - try: - rack = Rack.from_tile_id(tile_id) - except: - return cls(id=-1) - - try: - statement = 'SELECT id FROM machines WHERE rack_id = %s AND position = %s' - machine_id = database.fetch_one(statement, (rack.id, position))[0] - except: - return cls(id=-1) - - return cls.from_primary_key((machine_id, )) - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the user has at least the given auth level over this Machine.""" - - # Get the Rack - - try: - rack = Rack.from_primary_key((self.rack_id, )) - except exceptions.RowNotFoundError: - return False - - # Check the Rack's Authorization - - return rack.google_id_has_at_least(google_id, authorization_level) - - def insert(self): - """Insert this Machine by also updating its devices.""" - - before_insert = copy.deepcopy(self) - - super(Machine, self).insert() - - before_insert.id = self.id - self._update_devices(before_insert) - - def read(self): - """Read this Machine by also getting its CPU, GPU, Memory and Storage IDs.""" - - super(Machine, self).read() - - for device_table in self.device_table_to_attribute.keys(): - - statement = 'SELECT * FROM machine_{} WHERE machine_id = %s'.format(device_table) - results = database.fetch_all(statement, (self.id, )) - - device_ids = [] - - for row in results: - device_ids.append(row[2]) - - setattr(self, self.device_table_to_attribute[device_table], device_ids) - - setattr(self, 'tags', []) - - def update(self): - """Update this Machine by also updating its devices.""" - - before_update = copy.deepcopy(self) - - super(Machine, self).update() - - before_update.id = self.id - self._update_devices(before_update) diff --git a/opendc/models_old/machine_state.py b/opendc/models_old/machine_state.py deleted file mode 100644 index 0e9f7dad..00000000 --- a/opendc/models_old/machine_state.py +++ /dev/null @@ -1,71 +0,0 @@ -from opendc.models_old.model import Model -from opendc.util import database - - -class MachineState(Model): - JSON_TO_PYTHON_DICT = { - 'MachineState': { - 'machineId': 'machine_id', - 'temperatureC': 'temperature_c', - 'inUseMemoryMb': 'in_use_memory_mb', - 'loadFraction': 'load_fraction', - 'tick': 'tick' - } - } - - COLLECTION_NAME = 'machine_states' - COLUMNS = ['id', 'machine_id', 'experiment_id', 'tick', 'temperature_c', 'in_use_memory_mb', 'load_fraction'] - - COLUMNS_PRIMARY_KEY = ['id'] - - @classmethod - def _from_database_row(cls, row): - """Instantiate a MachineState from a database row (including tick from the TaskState).""" - - return cls(machine_id=row[1], temperature_c=row[4], in_use_memory_mb=row[5], load_fraction=row[6], tick=row[3]) - - @classmethod - def from_experiment_id(cls, experiment_id): - """Query MachineStates by their Experiment id.""" - - machine_states = [] - - statement = 'SELECT * FROM machine_states WHERE experiment_id = %s' - results = database.fetch_all(statement, (experiment_id, )) - - for row in results: - machine_states.append(cls._from_database_row(row)) - - return machine_states - - @classmethod - def from_experiment_id_and_tick(cls, experiment_id, tick): - """Query MachineStates by their Experiment id and tick.""" - - machine_states = [] - - statement = 'SELECT * FROM machine_states WHERE experiment_id = %s AND machine_states.tick = %s' - results = database.fetch_all(statement, (experiment_id, tick)) - - for row in results: - machine_states.append(cls._from_database_row(row)) - - return machine_states - - def read(self): - """Read this MachineState by also getting its tick.""" - - super(MachineState, self).read() - - statement = 'SELECT tick FROM task_states WHERE id = %s' - result = database.fetch_one(statement, (self.task_state_id, )) - - self.tick = result[0] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the User has at least the given auth level over this MachineState.""" - - if authorization_level in ['EDIT', 'OWN']: - return False - - return True diff --git a/opendc/models_old/memory.py b/opendc/models_old/memory.py deleted file mode 100644 index 8edf8f5d..00000000 --- a/opendc/models_old/memory.py +++ /dev/null @@ -1,34 +0,0 @@ -from opendc.models_old.model import Model - - -class Memory(Model): - JSON_TO_PYTHON_DICT = { - 'Memory': { - 'id': 'id', - 'manufacturer': 'manufacturer', - 'family': 'family', - 'generation': 'generation', - 'model': 'model', - 'speedMbPerS': 'speed_mb_per_s', - 'sizeMb': 'size_mb', - 'energyConsumptionW': 'energy_consumption_w', - 'failureModelId': 'failure_model_id' - } - } - - COLLECTION_NAME = 'memories' - - COLUMNS = [ - 'id', 'manufacturer', 'family', 'generation', 'model', 'speed_mb_per_s', 'size_mb', 'energy_consumption_w', - 'failure_model_id' - ] - - COLUMNS_PRIMARY_KEY = ['id'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the User has at least the given auth level over this Memory.""" - - if authorization_level in ['EDIT', 'OWN']: - return False - - return True diff --git a/opendc/models_old/model.py b/opendc/models_old/model.py deleted file mode 100644 index 73eabd26..00000000 --- a/opendc/models_old/model.py +++ /dev/null @@ -1,303 +0,0 @@ -from opendc.util import database, exceptions - - -class Model(object): - # MUST OVERRIDE IN DERIVED CLASS - - JSON_TO_PYTHON_DICT = {'Model': {'jsonParameterName': 'python_parameter_name'}} - - PATH = '' - PATH_PARAMETERS = {} - - COLLECTION_NAME = '' - COLUMNS = [] - COLUMNS_PRIMARY_KEY = [] - - # INITIALIZATION - - def __init__(self, **kwargs): - """Initialize a model from its keyword arguments.""" - - for name, value in kwargs.items(): - setattr(self, name, value) - - def __repr__(self): - """Return a string representation of this object.""" - - identifiers = [] - - for attribute in self.COLUMNS_PRIMARY_KEY: - identifiers.append('{} = {}'.format(attribute, getattr(self, attribute))) - - return '{} ({})'.format(self.COLLECTION_NAME[:-1].title().replace('_', ''), '; '.join(identifiers)) - - # JSON CONVERSION METHODS - - @classmethod - def from_JSON(cls, json_object): - """Initialize a Model from its JSON object representation.""" - - parameters = {} - parameter_map = cls.JSON_TO_PYTHON_DICT.values()[0] - - for json_name in parameter_map: - - python_name = parameter_map[json_name] - - if json_name in json_object: - parameters[python_name] = json_object.get(json_name) - - else: - parameters[python_name] = None - - return cls(**parameters) - - def to_JSON(self): - """Return a JSON-serializable object representation of this Model.""" - - parameters = {} - parameter_map = self.JSON_TO_PYTHON_DICT.values()[0] - - for json_name in parameter_map: - - python_name = parameter_map[json_name] - - if hasattr(self, python_name): - parameters[json_name] = getattr(self, python_name) - - else: - parameters[json_name] = None - - return parameters - - # API CALL GENERATION - - def generate_api_call(self, path_parameters, token): - """Return a message that can be executed by a Request to recreate this object.""" - - return { - 'id': 0, - 'path': self.PATH, - 'method': 'POST', - 'parameters': { - 'body': { - self.JSON_TO_PYTHON_DICT.keys()[0]: self.to_JSON() - }, - 'path': path_parameters, - 'query': {} - }, - 'token': token - } - - # SQL STATEMENT GENERATION METHODS - - @classmethod - def _generate_insert_columns_string(cls): - """Generate a SQLite insertion columns string for this Model""" - - return ', '.join(cls.COLUMNS) - - @classmethod - def _generate_insert_placeholders_string(cls): - """Generate a SQLite insertion placeholders string for this Model.""" - - return ', '.join(['%s'] * len(cls.COLUMNS)) - - @classmethod - def _generate_primary_key_string(cls): - """Generate the SQLite primary key string for this Model.""" - - return ' AND '.join(['{} = %s'.format(x) for x in cls.COLUMNS_PRIMARY_KEY]) - - @classmethod - def _generate_update_columns_string(cls): - """Generate a SQLite updatable columns string for this Model.""" - - return ', '.join(['{} = %s'.format(x) for x in cls.COLUMNS if x not in cls.COLUMNS_PRIMARY_KEY]) - - # SQL TUPLE GENERATION METHODS - - def _generate_insert_columns_tuple(self): - """Generate the tuple of insertion column values for this object.""" - - value_list = [] - - for column in self.COLUMNS: - value_list.append(getattr(self, column, None)) - - return tuple(value_list) - - def _generate_primary_key_tuple(self): - """Generate the tuple of primary key values for this object.""" - - primary_key_list = [] - - for column in self.COLUMNS_PRIMARY_KEY: - primary_key_list.append(getattr(self, column, None)) - - return tuple(primary_key_list) - - def _generate_update_columns_tuple(self): - """Generate the tuple of updatable column values for this object.""" - - value_list = [] - - for column in [x for x in self.COLUMNS if x not in self.COLUMNS_PRIMARY_KEY]: - value_list.append(getattr(self, column, None)) - - return tuple(value_list) - - # DATABASE HELPER METHODS - - @classmethod - def _from_database(cls, statement, values): - """Initialize a Model by fetching it from the database.""" - - parameters = {} - model_from_database = database.fetch_one(statement, values) - - if model_from_database is None: - return None - - for i in range(len(cls.COLUMNS)): - parameters[cls.COLUMNS[i]] = model_from_database[i] - - return cls(**parameters) - - # PUBLIC DATABASE INTERACTION METHODS - - @classmethod - def from_primary_key(cls, primary_key_tuple): - """Initialize a Model by fetching it by its primary key tuple. - - If the primary key does not exist in the database, return a stub. - """ - - query = 'SELECT * FROM {} WHERE {}'.format(cls.COLLECTION_NAME, cls._generate_primary_key_string()) - - # Return an instantiation of the Model with values from the row if it exists - - model = cls._from_database(query, primary_key_tuple) - if model is not None: - return model - - # Return a stub instantiation of the Model with just the primary key if it does not - - parameters = {} - for i, column in enumerate(cls.COLUMNS_PRIMARY_KEY): - parameters[column] = primary_key_tuple[i] - - return cls(**parameters) - - @classmethod - def query(cls, column_name=None, value=None): - """Return all instances of the Model in the database where column_name = value.""" - - if column_name is not None and value is not None: - statement = 'SELECT * FROM {} WHERE {} = %s'.format(cls.COLLECTION_NAME, column_name) - database_models = database.fetch_all(statement, (value, )) - - else: - statement = 'SELECT * FROM {}'.format(cls.COLLECTION_NAME) - database_models = database.fetch_all(statement) - - models = [] - - for database_model in database_models: - - parameters = {} - for i, parameter in enumerate(cls.COLUMNS): - parameters[parameter] = database_model[i] - - models.append(cls(**parameters)) - - return models - - def delete(self): - """Delete this Model from the database.""" - - self.read() - - statement = 'DELETE FROM {} WHERE {}'.format(self.COLLECTION_NAME, self._generate_primary_key_string()) - - values = self._generate_primary_key_tuple() - - database.execute(statement, values) - - def exists(self, column=None): - """Return True if this Model exists in the database. - - Check the primary key by default, or a column if one is specified. - """ - - query = """ - SELECT EXISTS ( - SELECT 1 FROM {} - WHERE {} - LIMIT 1 - ) - """ - - if column is None: - query = query.format(self.COLLECTION_NAME, self._generate_primary_key_string()) - values = self._generate_primary_key_tuple() - - else: - query = query.format(self.COLLECTION_NAME, '{} = %s'.format(column)) - values = (getattr(self, column), ) - - return database.fetch_one(query, values)[0] == 1 - - def insert(self): - """Insert this Model into the database.""" - - if hasattr(self, 'id'): - self.id = None - - self.insert_with_id() - - def insert_with_id(self, is_auto_generated=True): - """Insert this Model into the database without removing its id.""" - - statement = 'INSERT INTO {} ({}) VALUES ({})'.format(self.COLLECTION_NAME, - self._generate_insert_columns_string(), - self._generate_insert_placeholders_string()) - - values = self._generate_insert_columns_tuple() - - try: - last_row_id = database.execute(statement, values) - except Exception as e: - print(e) - raise exceptions.ForeignKeyError(e) - - if 'id' in self.COLUMNS_PRIMARY_KEY: - if is_auto_generated: - setattr(self, 'id', last_row_id) - self.read() - - def read(self): - """Read this Model's non-primary key attributes from the database.""" - - if not self.exists(): - raise exceptions.RowNotFoundError(self.COLLECTION_NAME) - - database_model = self.from_primary_key(self._generate_primary_key_tuple()) - - for attribute in self.COLUMNS: - setattr(self, attribute, getattr(database_model, attribute)) - - return self - - def update(self): - """Update this Model's non-primary key attributes in the database.""" - - statement = 'UPDATE {} SET {} WHERE {}'.format(self.COLLECTION_NAME, self._generate_update_columns_string(), - self._generate_primary_key_string()) - - values = self._generate_update_columns_tuple() + self._generate_primary_key_tuple() - - try: - database.execute(statement, values) - except Exception as e: - raise exceptions.ForeignKeyError(e) diff --git a/opendc/models_old/object.py b/opendc/models_old/object.py deleted file mode 100644 index 8f2e308b..00000000 --- a/opendc/models_old/object.py +++ /dev/null @@ -1,14 +0,0 @@ -from opendc.models_old.model import Model - - -class Object(Model): - JSON_TO_PYTHON_DICT = {'Object': {'id': 'id', 'type': 'type'}} - - COLLECTION_NAME = 'objects' - COLUMNS = ['id', 'type'] - COLUMNS_PRIMARY_KEY = ['id'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the user has at least the given auth level over this Tile.""" - - return True diff --git a/opendc/models_old/path.py b/opendc/models_old/path.py deleted file mode 100644 index 4d07b2d5..00000000 --- a/opendc/models_old/path.py +++ /dev/null @@ -1,35 +0,0 @@ -from opendc.models_old.authorization import Authorization -from opendc.models_old.model import Model -from opendc.models_old.user import User -from opendc.util import exceptions - - -class Path(Model): - JSON_TO_PYTHON_DICT = { - 'Path': { - 'id': 'id', - 'simulationId': 'simulation_id', - 'name': 'name', - 'datetimeCreated': 'datetime_created' - } - } - - COLLECTION_NAME = 'paths' - COLUMNS = ['id', 'simulation_id', 'name', 'datetime_created'] - COLUMNS_PRIMARY_KEY = ['id'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the user has at least the given auth level over this Path.""" - - # Get the User id - - try: - user_id = User.from_google_id(google_id).read().id - except exceptions.RowNotFoundError: - return False - - # Check the Authorization - - authorization = Authorization.from_primary_key((user_id, self.simulation_id)) - - return authorization.has_at_least(authorization_level) diff --git a/opendc/models_old/queued_experiment.py b/opendc/models_old/queued_experiment.py deleted file mode 100644 index b474dc31..00000000 --- a/opendc/models_old/queued_experiment.py +++ /dev/null @@ -1,9 +0,0 @@ -from opendc.models_old.model import Model - - -class QueuedExperiment(Model): - JSON_TO_PYTHON_DICT = {'QueuedExperiment': {'experimentId': 'experiment_id'}} - - COLLECTION_NAME = 'queued_experiments' - COLUMNS = ['experiment_id'] - COLUMNS_PRIMARY_KEY = ['experiment_id'] diff --git a/opendc/models_old/rack.py b/opendc/models_old/rack.py deleted file mode 100644 index dc08eb6a..00000000 --- a/opendc/models_old/rack.py +++ /dev/null @@ -1,61 +0,0 @@ -from opendc.models_old.model import Model -from opendc.models_old.object import Object -from opendc.models_old.tile import Tile - - -class Rack(Model): - JSON_TO_PYTHON_DICT = { - 'rack': { - 'id': 'id', - 'name': 'name', - 'capacity': 'capacity', - 'powerCapacityW': 'power_capacity_w', - 'topologyId': 'topology_id' - } - } - - PATH = '/v1/tiles/{tileId}/rack' - - COLLECTION_NAME = 'racks' - COLUMNS = ['id', 'name', 'capacity', 'power_capacity_w', 'topology_id'] - COLUMNS_PRIMARY_KEY = ['id'] - - @classmethod - def from_tile_id(cls, tile_id): - """Get a Rack from the ID of the Tile it's on.""" - - tile = Tile.from_primary_key((tile_id, )) - - if not tile.exists(): - return Rack(id=-1) - - return cls.from_primary_key((tile.object_id, )) - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the user has at least the given auth level over this Rack.""" - - # Get the Tile - - try: - tile = Tile.query('object_id', self.id)[0] - except: - return False - - # Check the Tile's Authorization - - return tile.google_id_has_at_least(google_id, authorization_level) - - def insert(self): - """Insert a Rack by first inserting an object.""" - - obj = Object(type='RACK') - obj.insert() - - self.id = obj.id - self.insert_with_id(is_auto_generated=False) - - def delete(self): - """Delete a Rack by deleting its associated object.""" - - obj = Object.from_primary_key((self.id, )) - obj.delete() diff --git a/opendc/models_old/rack_state.py b/opendc/models_old/rack_state.py deleted file mode 100644 index 12e0f931..00000000 --- a/opendc/models_old/rack_state.py +++ /dev/null @@ -1,63 +0,0 @@ -from opendc.models_old.model import Model -from opendc.util import database - - -class RackState(Model): - JSON_TO_PYTHON_DICT = {'RackState': {'rackId': 'rack_id', 'loadFraction': 'load_fraction', 'tick': 'tick'}} - - @classmethod - def _from_database_row(cls, row): - """Instantiate a RackState from a database row.""" - - return cls(rack_id=row[0], load_fraction=row[1], tick=row[2]) - - @classmethod - def from_experiment_id(cls, experiment_id): - """Query RackStates by their Experiment id.""" - - rack_states = [] - - statement = ''' - SELECT racks.id, avg(machine_states.load_fraction), machine_states.tick - FROM racks - JOIN machines ON racks.id = machines.rack_id - JOIN machine_states ON machines.id = machine_states.machine_id - WHERE machine_states.experiment_id = %s - GROUP BY machine_states.tick, racks.id - ''' - results = database.fetch_all(statement, (experiment_id, )) - - for row in results: - rack_states.append(cls._from_database_row(row)) - - return rack_states - - @classmethod - def from_experiment_id_and_tick(cls, experiment_id, tick): - """Query RackStates by their Experiment id.""" - - rack_states = [] - - statement = ''' - SELECT racks.id, avg(machine_states.load_fraction), machine_states.tick - FROM racks - JOIN machines ON racks.id = machines.rack_id - JOIN machine_states ON machines.id = machine_states.machine_id - WHERE machine_states.experiment_id = %s - AND machine_states.tick = %s - GROUP BY machine_states.tick, racks.id - ''' - results = database.fetch_all(statement, (experiment_id, tick)) - - for row in results: - rack_states.append(cls._from_database_row(row)) - - return rack_states - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the User has at least the given auth level over this RackState.""" - - if authorization_level in ['EDIT', 'OWN']: - return False - - return True diff --git a/opendc/models_old/room.py b/opendc/models_old/room.py deleted file mode 100644 index e0eb7c2f..00000000 --- a/opendc/models_old/room.py +++ /dev/null @@ -1,35 +0,0 @@ -from opendc.models_old.datacenter import Datacenter -from opendc.models_old.model import Model -from opendc.util import exceptions - - -class Room(Model): - JSON_TO_PYTHON_DICT = { - 'room': { - 'id': 'id', - 'datacenterId': 'datacenter_id', - 'name': 'name', - 'roomType': 'type', - 'topologyId': 'topology_id' - } - } - - PATH = '/v1/datacenters/{datacenterId}/rooms' - - COLLECTION_NAME = 'rooms' - COLUMNS = ['id', 'name', 'datacenter_id', 'type', 'topology_id'] - COLUMNS_PRIMARY_KEY = ['id'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the user has at least the given auth level over this Room.""" - - # Get the Datacenter - - try: - datacenter = Datacenter.from_primary_key((self.datacenter_id, )) - except exceptions.RowNotFoundError: - return False - - # Check the Datacenter's Authorization - - return datacenter.google_id_has_at_least(google_id, authorization_level) diff --git a/opendc/models_old/room_state.py b/opendc/models_old/room_state.py deleted file mode 100644 index c6635649..00000000 --- a/opendc/models_old/room_state.py +++ /dev/null @@ -1,71 +0,0 @@ -from opendc.models_old.model import Model -from opendc.util import database - - -class RoomState(Model): - JSON_TO_PYTHON_DICT = {'RoomState': {'roomId': 'room_id', 'loadFraction': 'load_fraction', 'tick': 'tick'}} - - @classmethod - def _from_database_row(cls, row): - """Instantiate a RoomState from a database row.""" - - return cls(room_id=row[0], load_fraction=row[1], tick=row[2]) - - @classmethod - def from_experiment_id(cls, experiment_id): - """Query RoomStates by their Experiment id.""" - - room_states = [] - - statement = ''' - SELECT rooms.id, avg(machine_states.load_fraction), machine_states.tick - FROM rooms - JOIN tiles ON rooms.id = tiles.room_id - JOIN objects ON tiles.object_id = objects.id - JOIN racks ON objects.id = racks.id - JOIN machines ON racks.id = machines.rack_id - JOIN machine_states ON machines.id = machine_states.machine_id - WHERE objects.type = "RACK" - AND machine_states.experiment_id = %s - GROUP BY machine_states.tick, rooms.id - ''' - results = database.fetch_all(statement, (experiment_id, )) - - for row in results: - room_states.append(cls._from_database_row(row)) - - return room_states - - @classmethod - def from_experiment_id_and_tick(cls, experiment_id, tick): - """Query RoomStates by their Experiment id.""" - - room_states = [] - - statement = ''' - SELECT rooms.id, avg(machine_states.load_fraction), machine_states.tick - FROM rooms - JOIN tiles ON rooms.id = tiles.room_id - JOIN objects ON tiles.object_id = objects.id - JOIN racks ON objects.id = racks.id - JOIN machines ON racks.id = machines.rack_id - JOIN machine_states ON machines.id = machine_states.machine_id - WHERE objects.type = "RACK" - AND machine_states.experiment_id = %s - AND machine_states.tick = %s - GROUP BY rooms.id - ''' - results = database.fetch_all(statement, (experiment_id, tick)) - - for row in results: - room_states.append(cls._from_database_row(row)) - - return room_states - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the User has at least the given auth level over this RackState.""" - - if authorization_level in ['EDIT', 'OWN']: - return False - - return True diff --git a/opendc/models_old/room_type.py b/opendc/models_old/room_type.py deleted file mode 100644 index 755572f8..00000000 --- a/opendc/models_old/room_type.py +++ /dev/null @@ -1,17 +0,0 @@ -from opendc.models_old.model import Model - - -class RoomType(Model): - JSON_TO_PYTHON_DICT = {'RoomType': {'name': 'name'}} - - COLLECTION_NAME = 'room_types' - COLUMNS = ['name'] - COLUMNS_PRIMARY_KEY = ['name'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the user has at least the given auth level over this RoomType.""" - - if authorization_level in ['EDIT', 'OWN']: - return False - - return True diff --git a/opendc/models_old/scheduler.py b/opendc/models_old/scheduler.py deleted file mode 100644 index b9939321..00000000 --- a/opendc/models_old/scheduler.py +++ /dev/null @@ -1,14 +0,0 @@ -from opendc.models_old.model import Model - - -class Scheduler(Model): - JSON_TO_PYTHON_DICT = {'Scheduler': {'name': 'name'}} - - COLLECTION_NAME = 'schedulers' - COLUMNS = ['name'] - COLUMNS_PRIMARY_KEY = ['name'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the user has at least the given auth level over this Scheduler.""" - - return authorization_level not in ['EDIT', 'OWN'] diff --git a/opendc/models_old/section.py b/opendc/models_old/section.py deleted file mode 100644 index 0df4967c..00000000 --- a/opendc/models_old/section.py +++ /dev/null @@ -1,32 +0,0 @@ -from opendc.models_old.model import Model -from opendc.models_old.path import Path -from opendc.util import exceptions - - -class Section(Model): - JSON_TO_PYTHON_DICT = { - 'Section': { - 'id': 'id', - 'pathId': 'path_id', - 'datacenterId': 'datacenter_id', - 'startTick': 'start_tick' - } - } - - COLLECTION_NAME = 'sections' - COLUMNS = ['id', 'path_id', 'datacenter_id', 'start_tick'] - COLUMNS_PRIMARY_KEY = ['id'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the user has at least the given auth level over this Section.""" - - # Get the Path - - try: - path = Path.from_primary_key((self.path_id, )) - except exceptions.RowNotFoundError: - return False - - # Check the Path's Authorization - - return path.google_id_has_at_least(google_id, authorization_level) diff --git a/opendc/models_old/simulation.py b/opendc/models_old/simulation.py deleted file mode 100644 index 9c1820a3..00000000 --- a/opendc/models_old/simulation.py +++ /dev/null @@ -1,39 +0,0 @@ -from opendc.models_old.authorization import Authorization -from opendc.models_old.model import Model -from opendc.models_old.user import User -from opendc.util import exceptions - - -class Simulation(Model): - JSON_TO_PYTHON_DICT = { - 'Simulation': { - 'id': 'id', - 'name': 'name', - 'datetimeCreated': 'datetime_created', - 'datetimeLastEdited': 'datetime_last_edited' - } - } - - COLLECTION_NAME = 'simulations' - COLUMNS = ['id', 'datetime_created', 'datetime_last_edited', 'name'] - COLUMNS_PRIMARY_KEY = ['id'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the user has at least the given auth level over this Simulation.""" - - # Get the User id - - try: - user_id = User.from_google_id(google_id).read().id - except exceptions.RowNotFoundError: - return False - - # Get the Simulation id - - simulation_id = self.id - - # Check the Authorization - - authorization = Authorization.from_primary_key((user_id, simulation_id)) - - return authorization.has_at_least(authorization_level) diff --git a/opendc/models_old/storage.py b/opendc/models_old/storage.py deleted file mode 100644 index 9f0f9215..00000000 --- a/opendc/models_old/storage.py +++ /dev/null @@ -1,34 +0,0 @@ -from opendc.models_old.model import Model - - -class Storage(Model): - JSON_TO_PYTHON_DICT = { - 'Storage': { - 'id': 'id', - 'manufacturer': 'manufacturer', - 'family': 'family', - 'generation': 'generation', - 'model': 'model', - 'speedMbPerS': 'speed_mb_per_s', - 'sizeMb': 'size_mb', - 'energyConsumptionW': 'energy_consumption_w', - 'failureModelId': 'failure_model_id' - } - } - - COLLECTION_NAME = 'storages' - - COLUMNS = [ - 'id', 'manufacturer', 'family', 'generation', 'model', 'speed_mb_per_s', 'size_mb', 'energy_consumption_w', - 'failure_model_id' - ] - - COLUMNS_PRIMARY_KEY = ['id'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the User has at least the given auth level over this Storage.""" - - if authorization_level in ['EDIT', 'OWN']: - return False - - return True diff --git a/opendc/models_old/task.py b/opendc/models_old/task.py deleted file mode 100644 index e6f99014..00000000 --- a/opendc/models_old/task.py +++ /dev/null @@ -1,22 +0,0 @@ -from opendc.models_old.model import Model - - -class Task(Model): - JSON_TO_PYTHON_DICT = { - 'Task': { - 'id': 'id', - 'startTick': 'start_tick', - 'totalFlopCount': 'total_flop_count', - 'coreCount': 'core_count', - 'jobId': 'job_id', - } - } - - COLLECTION_NAME = 'tasks' - COLUMNS = ['id', 'start_tick', 'total_flop_count', 'job_id', 'core_count'] - COLUMNS_PRIMARY_KEY = ['id'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the user has at least the given auth level over this Task.""" - - return authorization_level not in ['EDIT', 'OWN'] diff --git a/opendc/models_old/task_duration.py b/opendc/models_old/task_duration.py deleted file mode 100644 index 5058e8de..00000000 --- a/opendc/models_old/task_duration.py +++ /dev/null @@ -1,39 +0,0 @@ -from opendc.models_old.model import Model -from opendc.util import database - - -class TaskDuration(Model): - JSON_TO_PYTHON_DICT = {'TaskDuration': {'taskId': 'task_id', 'duration': 'duration'}} - - @classmethod - def _from_database_row(cls, row): - """Instantiate a RoomState from a database row.""" - - return cls(task_id=row[0], duration=row[1]) - - @classmethod - def from_experiment_id(cls, experiment_id): - """Query RoomStates by their Experiment id.""" - - room_states = [] - - statement = ''' - SELECT task_id, MAX(tick) - MIN(tick) as duration FROM task_states - WHERE experiment_id = %s - GROUP BY task_id - ''' - - results = database.fetch_all(statement, (experiment_id, )) - - for row in results: - room_states.append(cls._from_database_row(row)) - - return room_states - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the User has at least the given auth level over this TaskDuration.""" - - if authorization_level in ['EDIT', 'OWN']: - return False - - return True diff --git a/opendc/models_old/task_state.py b/opendc/models_old/task_state.py deleted file mode 100644 index cc3fdd89..00000000 --- a/opendc/models_old/task_state.py +++ /dev/null @@ -1,42 +0,0 @@ -from opendc.models_old.model import Model -from opendc.util import database - - -class TaskState(Model): - JSON_TO_PYTHON_DICT = { - 'TaskState': { - 'id': 'id', - 'taskId': 'task_id', - 'experimentId': 'experiment_id', - 'tick': 'tick', - 'flopsLeft': 'flops_left', - 'coresUsed': 'cores_used' - } - } - - COLLECTION_NAME = 'task_states' - - COLUMNS = ['id', 'task_id', 'experiment_id', 'tick', 'flops_left', 'cores_used'] - COLUMNS_PRIMARY_KEY = ['id'] - - @classmethod - def from_experiment_id_and_tick(cls, experiment_id, tick): - """Query Task States by their Experiment id and tick.""" - - task_states = [] - - statement = 'SELECT * FROM task_states WHERE experiment_id = %s AND tick = %s' - results = database.fetch_all(statement, (experiment_id, tick)) - - for row in results: - task_states.append(cls(id=row[0], task_id=row[1], experiment_id=row[2], tick=row[3], flops_left=row[4])) - - return task_states - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the User has at least the given auth level over this TaskState.""" - - if authorization_level in ['EDIT', 'OWN']: - return False - - return True diff --git a/opendc/models_old/tile.py b/opendc/models_old/tile.py deleted file mode 100644 index e46b29a6..00000000 --- a/opendc/models_old/tile.py +++ /dev/null @@ -1,47 +0,0 @@ -from opendc.models_old.model import Model -from opendc.models_old.object import Object -from opendc.models_old.room import Room -from opendc.util import exceptions - - -class Tile(Model): - JSON_TO_PYTHON_DICT = { - 'tile': { - 'id': 'id', - 'roomId': 'room_id', - 'objectId': 'object_id', - 'objectType': 'object_type', - 'positionX': 'position_x', - 'positionY': 'position_y', - 'topologyId': 'topology_id' - } - } - - PATH = '/v1/rooms/{roomId}/tiles' - - COLLECTION_NAME = 'tiles' - COLUMNS = ['id', 'position_x', 'position_y', 'room_id', 'object_id', 'topology_id'] - COLUMNS_PRIMARY_KEY = ['id'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the user has at least the given auth level over this Tile.""" - - # Get the Room - - try: - room = Room.from_primary_key((self.room_id, )) - except exceptions.RowNotFoundError: - return False - - # Check the Room's Authorization - - return room.google_id_has_at_least(google_id, authorization_level) - - def read(self): - """Read this Tile by also getting its associated object type.""" - - super(Tile, self).read() - - if self.object_id is not None: - obj = Object.from_primary_key((self.object_id, )) - self.object_type = obj.type diff --git a/opendc/models_old/trace.py b/opendc/models_old/trace.py deleted file mode 100644 index 58abe058..00000000 --- a/opendc/models_old/trace.py +++ /dev/null @@ -1,14 +0,0 @@ -from opendc.models_old.model import Model - - -class Trace(Model): - JSON_TO_PYTHON_DICT = {'Trace': {'id': 'id', 'name': 'name'}} - - COLLECTION_NAME = 'traces' - COLUMNS = ['id', 'name'] - COLUMNS_PRIMARY_KEY = ['id'] - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the user has at least the given auth level over this Trace.""" - - return authorization_level not in ['EDIT', 'OWN'] diff --git a/opendc/models_old/user.py b/opendc/models_old/user.py deleted file mode 100644 index 657d5019..00000000 --- a/opendc/models_old/user.py +++ /dev/null @@ -1,47 +0,0 @@ -from opendc.models_old.model import Model - - -class User(Model): - JSON_TO_PYTHON_DICT = { - 'User': { - 'id': 'id', - 'googleId': 'google_id', - 'email': 'email', - 'givenName': 'given_name', - 'familyName': 'family_name' - } - } - - COLLECTION_NAME = 'users' - COLUMNS = ['id', 'google_id', 'email', 'given_name', 'family_name'] - COLUMNS_PRIMARY_KEY = ['id'] - - @classmethod - def from_google_id(cls, google_id): - """Initialize a User by fetching them by their google id.""" - - user = cls._from_database('SELECT * FROM users WHERE google_id = %s', (google_id, )) - - if user is not None: - return user - - return User() - - @classmethod - def from_email(cls, email): - """Initialize a User by fetching them by their email.""" - - user = cls._from_database('SELECT * FROM users WHERE email = %s', (email, )) - - if user is not None: - return user - - return User() - - def google_id_has_at_least(self, google_id, authorization_level): - """Return True if the User has at least the given auth level over this User.""" - - if authorization_level in ['EDIT', 'OWN']: - return google_id == self.google_id - - return True diff --git a/opendc/util/__init__.py b/opendc/util/__init__.py deleted file mode 100644 index e69de29b..00000000 --- a/opendc/util/__init__.py +++ /dev/null diff --git a/opendc/util/database.py b/opendc/util/database.py deleted file mode 100644 index 50bc93a8..00000000 --- a/opendc/util/database.py +++ /dev/null @@ -1,93 +0,0 @@ -import json -import urllib.parse -from datetime import datetime - -from bson.json_util import dumps -from pymongo import MongoClient - -DATETIME_STRING_FORMAT = '%Y-%m-%dT%H:%M:%S' -CONNECTION_POOL = None - - -class Database: - def __init__(self): - self.opendc_db = None - - def init_database(self, user, password, database, host): - user = urllib.parse.quote_plus(user) # TODO: replace this with environment variable - password = urllib.parse.quote_plus(password) # TODO: same as above - database = urllib.parse.quote_plus(database) - host = urllib.parse.quote_plus(host) - - client = MongoClient('mongodb://%s:%s@%s/default_db?authSource=%s' % (user, password, host, database)) - self.opendc_db = client.opendc - - def fetch_one(self, query, collection): - """Uses existing mongo connection to return a single (the first) document in a collection matching the given - query as a JSON object. - - The query needs to be in json format, i.e.: `{'name': prefab_name}`. - """ - bson = getattr(self.opendc_db, collection).find_one(query) - - return self.convert_bson_to_json(bson) - - def fetch_all(self, query, collection): - """Uses existing mongo connection to return all documents matching a given query, as a list of JSON objects. - - The query needs to be in json format, i.e.: `{'name': prefab_name}`. - """ - results = [] - cursor = getattr(self.opendc_db, collection).find(query) - for doc in cursor: - results.append(self.convert_bson_to_json(doc)) - return results - - def insert(self, obj, collection): - """Updates an existing object.""" - bson = getattr(self.opendc_db, collection).insert(obj) - - return self.convert_bson_to_json(bson) - - def update(self, _id, obj, collection): - """Updates an existing object.""" - bson = getattr(self.opendc_db, collection).update({'_id': _id}, obj) - - return self.convert_bson_to_json(bson) - - def delete_one(self, query, collection): - """Deletes one object matching the given query. - - The query needs to be in json format, i.e.: `{'name': prefab_name}`. - """ - bson = getattr(self.opendc_db, collection).delete_one(query) - - return self.convert_bson_to_json(bson) - - def delete_all(self, query, collection): - """Deletes all objects matching the given query. - - The query needs to be in json format, i.e.: `{'name': prefab_name}`. - """ - bson = getattr(self.opendc_db, collection).delete_many(query) - - return self.convert_bson_to_json(bson) - - @staticmethod - def convert_bson_to_json(bson): - """Converts a BSON representation to JSON and returns the JSON representation.""" - json_string = dumps(bson) - return json.loads(json_string) - - @staticmethod - def datetime_to_string(datetime_to_convert): - """Return a database-compatible string representation of the given datetime object.""" - return datetime_to_convert.strftime(DATETIME_STRING_FORMAT) - - @staticmethod - def string_to_datetime(string_to_convert): - """Return a datetime corresponding to the given string representation.""" - return datetime.strptime(string_to_convert, DATETIME_STRING_FORMAT) - - -DB = Database() diff --git a/opendc/util/exceptions.py b/opendc/util/exceptions.py deleted file mode 100644 index 2563c419..00000000 --- a/opendc/util/exceptions.py +++ /dev/null @@ -1,65 +0,0 @@ -class RequestInitializationError(Exception): - """Raised when a Request cannot successfully be initialized""" - - -class UnimplementedEndpointError(RequestInitializationError): - """Raised when a Request path does not point to a module.""" - - -class MissingRequestParameterError(RequestInitializationError): - """Raised when a Request does not contain one or more required parameters.""" - - -class UnsupportedMethodError(RequestInitializationError): - """Raised when a Request does not use a supported REST method. - - The method must be in all-caps, supported by REST, and implemented by the module. - """ - - -class AuthorizationTokenError(RequestInitializationError): - """Raised when an authorization token is not correctly verified.""" - - -class ForeignKeyError(Exception): - """Raised when a foreign key constraint is not met.""" - - -class RowNotFoundError(Exception): - """Raised when a database row is not found.""" - def __init__(self, table_name): - super(RowNotFoundError, self).__init__('Row in `{}` table not found.'.format(table_name)) - - self.table_name = table_name - - -class ParameterError(Exception): - """Raised when a parameter is either missing or incorrectly typed.""" - - -class IncorrectParameterError(ParameterError): - """Raised when a parameter is of the wrong type.""" - def __init__(self, parameter_name, parameter_location): - super(IncorrectParameterError, - self).__init__('Incorrectly typed `{}` {} parameter.'.format(parameter_name, parameter_location)) - - self.parameter_name = parameter_name - self.parameter_location = parameter_location - - -class MissingParameterError(ParameterError): - """Raised when a parameter is missing.""" - def __init__(self, parameter_name, parameter_location): - super(MissingParameterError, - self).__init__('Missing required `{}` {} parameter.'.format(parameter_name, parameter_location)) - - self.parameter_name = parameter_name - self.parameter_location = parameter_location - - -class ClientError(Exception): - """Raised when a 4xx response is to be returned.""" - - def __init__(self, response): - super(ClientError, self).__init__(str(response)) - self.response = response diff --git a/opendc/util/parameter_checker.py b/opendc/util/parameter_checker.py deleted file mode 100644 index f55e780e..00000000 --- a/opendc/util/parameter_checker.py +++ /dev/null @@ -1,78 +0,0 @@ -from opendc.util import database, exceptions - - -def _missing_parameter(params_required, params_actual, parent=''): - """Recursively search for the first missing parameter.""" - - for param_name in params_required: - - if param_name not in params_actual: - return '{}.{}'.format(parent, param_name) - - param_required = params_required.get(param_name) - param_actual = params_actual.get(param_name) - - if isinstance(param_required, dict): - - param_missing = _missing_parameter(param_required, param_actual, param_name) - - if param_missing is not None: - return '{}.{}'.format(parent, param_missing) - - return None - - -def _incorrect_parameter(params_required, params_actual, parent=''): - """Recursively make sure each parameter is of the correct type.""" - - for param_name in params_required: - - param_required = params_required.get(param_name) - param_actual = params_actual.get(param_name) - - if isinstance(param_required, dict): - - param_incorrect = _incorrect_parameter(param_required, param_actual, param_name) - - if param_incorrect is not None: - return '{}.{}'.format(parent, param_incorrect) - - else: - - if param_required == 'datetime': - try: - database.string_to_datetime(param_actual) - except: - return '{}.{}'.format(parent, param_name) - - if param_required == 'int' and not isinstance(param_actual, int): - return '{}.{}'.format(parent, param_name) - - if param_required == 'string' and not isinstance(param_actual, str) and not isinstance(param_actual, int): - return '{}.{}'.format(parent, param_name) - - if param_required.startswith('list') and not isinstance(param_actual, list): - return '{}.{}'.format(parent, param_name) - - -def _format_parameter(parameter): - """Format the output of a parameter check.""" - - parts = parameter.split('.') - inner = ['["{}"]'.format(x) for x in parts[2:]] - return parts[1] + ''.join(inner) - - -def check(request, **kwargs): - """Return True if all required parameters are there.""" - - for location, params_required in kwargs.items(): - params_actual = getattr(request, 'params_{}'.format(location)) - - missing_parameter = _missing_parameter(params_required, params_actual) - if missing_parameter is not None: - raise exceptions.MissingParameterError(_format_parameter(missing_parameter), location) - - incorrect_parameter = _incorrect_parameter(params_required, params_actual) - if incorrect_parameter is not None: - raise exceptions.IncorrectParameterError(_format_parameter(incorrect_parameter), location) diff --git a/opendc/util/path_parser.py b/opendc/util/path_parser.py deleted file mode 100644 index a8bbdeba..00000000 --- a/opendc/util/path_parser.py +++ /dev/null @@ -1,39 +0,0 @@ -import json -import os - - -def parse(version, endpoint_path): - """Map an HTTP endpoint path to an API path""" - - # Get possible paths - with open(os.path.join(os.path.dirname(__file__), '..', 'api', '{}', 'paths.json').format(version)) as paths_file: - paths = json.load(paths_file) - - # Find API path that matches endpoint_path - endpoint_path_parts = endpoint_path.strip('/').split('/') - paths_parts = [x.strip('/').split('/') for x in paths if len(x.strip('/').split('/')) == len(endpoint_path_parts)] - path = None - - for path_parts in paths_parts: - found = True - for (endpoint_part, part) in zip(endpoint_path_parts, path_parts): - if not part.startswith('{') and endpoint_part != part: - found = False - break - if found: - path = path_parts - - if path is None: - return None - - # Extract path parameters - parameters = {} - - for (name, value) in zip(path, endpoint_path_parts): - if name.startswith('{'): - try: - parameters[name.strip('{}')] = int(value) - except: - parameters[name.strip('{}')] = value - - return '{}/{}'.format(version, '/'.join(path)), parameters diff --git a/opendc/util/rest.py b/opendc/util/rest.py deleted file mode 100644 index dc5478de..00000000 --- a/opendc/util/rest.py +++ /dev/null @@ -1,141 +0,0 @@ -import importlib -import json -import os -import sys - -from oauth2client import client, crypt - -from opendc.util import exceptions, parameter_checker -from opendc.util.exceptions import ClientError - - -class Request(object): - """WebSocket message to REST request mapping.""" - def __init__(self, message=None): - """"Initialize a Request from a socket message.""" - - # Get the Request parameters from the message - - if message is None: - return - - try: - self.message = message - - self.id = message['id'] - - self.path = message['path'] - self.method = message['method'] - - self.params_body = message['parameters']['body'] - self.params_path = message['parameters']['path'] - self.params_query = message['parameters']['query'] - - self.token = message['token'] - - except KeyError as exception: - raise exceptions.MissingRequestParameterError(exception) - - # Parse the path and import the appropriate module - - try: - self.path = message['path'].strip('/') - - module_base = 'opendc.api.{}.endpoint' - module_path = self.path.replace('{', '').replace('}', '').replace('/', '.') - - self.module = importlib.import_module(module_base.format(module_path)) - except ImportError as e: - print(e) - raise exceptions.UnimplementedEndpointError('Unimplemented endpoint: {}.'.format(self.path)) - - # Check the method - - if self.method not in ['POST', 'GET', 'PUT', 'PATCH', 'DELETE']: - raise exceptions.UnsupportedMethodError('Non-rest method: {}'.format(self.method)) - - if not hasattr(self.module, self.method): - raise exceptions.UnsupportedMethodError('Unimplemented method at endpoint {}: {}'.format( - self.path, self.method)) - - # Verify the user - - if "OPENDC_FLASK_TESTING" in os.environ: - self.google_id = 'test' - return - - try: - self.google_id = self._verify_token(self.token) - except crypt.AppIdentityError as e: - raise exceptions.AuthorizationTokenError(e) - - def check_required_parameters(self, **kwargs): - """Raise an error if a parameter is missing or of the wrong type.""" - - try: - parameter_checker.check(self, **kwargs) - except exceptions.ParameterError as e: - raise ClientError(Response(400, str(e))) - - def process(self): - """Process the Request and return a Response.""" - - method = getattr(self.module, self.method) - - try: - response = method(self) - except ClientError as e: - e.response.id = self.id - return e.response - - response.id = self.id - - return response - - def to_JSON(self): - """Return a JSON representation of this Request""" - - self.message['id'] = 0 - self.message['token'] = None - - return json.dumps(self.message) - - @staticmethod - def _verify_token(token): - """Return the ID of the signed-in user. - - Or throw an Exception if the token is invalid. - """ - - try: - id_info = client.verify_id_token(token, os.environ['OPENDC_OAUTH_CLIENT_ID']) - except Exception as e: - print(e) - raise crypt.AppIdentityError('Exception caught trying to verify ID token: {}'.format(e)) - - if id_info['aud'] != os.environ['OPENDC_OAUTH_CLIENT_ID']: - raise crypt.AppIdentityError('Unrecognized client.') - - if id_info['iss'] not in ['accounts.google.com', 'https://accounts.google.com']: - raise crypt.AppIdentityError('Wrong issuer.') - - return id_info['sub'] - - -class Response(object): - """Response to websocket mapping""" - def __init__(self, status_code, status_description, content=None): - """Initialize a new Response.""" - - self.status = {'code': status_code, 'description': status_description} - self.content = content - - def to_JSON(self): - """"Return a JSON representation of this Response""" - - data = {'id': self.id, 'status': self.status} - - if self.content is not None: - data['content'] = self.content - - return json.dumps(data) |
