diff options
Diffstat (limited to 'opendc/api/v1/simulations')
7 files changed, 58 insertions, 48 deletions
diff --git a/opendc/api/v1/simulations/endpoint.py b/opendc/api/v1/simulations/endpoint.py index ff669290..a8637728 100644 --- a/opendc/api/v1/simulations/endpoint.py +++ b/opendc/api/v1/simulations/endpoint.py @@ -9,6 +9,7 @@ from opendc.models.user import User from opendc.util import database, exceptions from opendc.util.rest import Response + def POST(request): """Create a new simulation, and return that new simulation.""" @@ -16,7 +17,7 @@ def POST(request): try: request.check_required_parameters( - body = { + body={ 'simulation': { 'name': 'string' } @@ -42,9 +43,9 @@ def POST(request): # Instantiate an Authorization and insert it into the database authorization = Authorization( - user_id = User.from_google_id(request.google_id).id, - simulation_id = simulation.id, - authorization_level = 'OWN' + user_id=User.from_google_id(request.google_id).id, + simulation_id=simulation.id, + authorization_level='OWN' ) authorization.insert() @@ -52,8 +53,8 @@ def POST(request): # Instantiate a Path and insert it into the database path = Path( - simulation_id = simulation.id, - datetime_created = database.datetime_to_string(datetime.now()) + simulation_id=simulation.id, + datetime_created=database.datetime_to_string(datetime.now()) ) path.insert() @@ -61,8 +62,8 @@ def POST(request): # Instantiate a Datacenter and insert it into the database datacenter = Datacenter( - starred = 0, - simulation_id = simulation.id + starred=0, + simulation_id=simulation.id ) datacenter.insert() @@ -70,9 +71,9 @@ def POST(request): # Instantiate a Section and insert it into the database section = Section( - path_id = path.id, - datacenter_id = datacenter.id, - start_tick = 0 + path_id=path.id, + datacenter_id=datacenter.id, + start_tick=0 ) section.insert() diff --git a/opendc/api/v1/simulations/simulationId/authorizations/endpoint.py b/opendc/api/v1/simulations/simulationId/authorizations/endpoint.py index d880564e..1d6b494e 100644 --- a/opendc/api/v1/simulations/simulationId/authorizations/endpoint.py +++ b/opendc/api/v1/simulations/simulationId/authorizations/endpoint.py @@ -1,8 +1,9 @@ from opendc.models.authorization import Authorization from opendc.models.simulation import Simulation -from opendc.util import database, exceptions +from opendc.util import exceptions from opendc.util.rest import Response + def GET(request): """Find all authorizations for a Simulation.""" @@ -10,7 +11,7 @@ def GET(request): try: request.check_required_parameters( - path = { + path={ 'simulationId': 'int' } ) diff --git a/opendc/api/v1/simulations/simulationId/authorizations/userId/endpoint.py b/opendc/api/v1/simulations/simulationId/authorizations/userId/endpoint.py index c3e599cf..46458ffc 100644 --- a/opendc/api/v1/simulations/simulationId/authorizations/userId/endpoint.py +++ b/opendc/api/v1/simulations/simulationId/authorizations/userId/endpoint.py @@ -1,9 +1,10 @@ from opendc.models.authorization import Authorization from opendc.models.simulation import Simulation from opendc.models.user import User -from opendc.util import database, exceptions +from opendc.util import exceptions from opendc.util.rest import Response + def DELETE(request): """Delete a user's authorization level over a simulation.""" @@ -11,7 +12,7 @@ def DELETE(request): try: request.check_required_parameters( - path = { + path={ 'simulationId': 'int', 'userId': 'int' } @@ -21,7 +22,7 @@ def DELETE(request): return Response(400, e.message) # Instantiate an Authorization - + authorization = Authorization.from_primary_key(( request.params_path['userId'], request.params_path['simulationId'] @@ -30,7 +31,7 @@ def DELETE(request): # Make sure this Authorization exists in the database if not authorization.exists(): - return Response (404, '{} not found.'.format(authorization)) + return Response(404, '{} not found.'.format(authorization)) # Make sure this User is allowed to delete this Authorization @@ -47,14 +48,15 @@ def DELETE(request): 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 = { + path={ 'simulationId': 'int', 'userId': 'int' } @@ -62,7 +64,7 @@ def GET(request): except exceptions.ParameterError as e: return Response(400, e.message) - + # Instantiate an Authorization authorization = Authorization.from_primary_key(( @@ -87,6 +89,7 @@ def GET(request): authorization.to_JSON() ) + def POST(request): """Add an authorization for a user's access to a simulation.""" @@ -94,11 +97,11 @@ def POST(request): try: request.check_required_parameters( - path = { + path={ 'userId': 'int', 'simulationId': 'int' }, - body = { + body={ 'authorization': { 'authorizationLevel': 'string' } @@ -143,7 +146,7 @@ def POST(request): except exceptions.ForeignKeyError: return Response(400, 'Invalid authorizationLevel') - + # Return this Authorization return Response( @@ -152,6 +155,7 @@ def POST(request): authorization.to_JSON() ) + def PUT(request): """Change a user's authorization level over a simulation.""" @@ -159,11 +163,11 @@ def PUT(request): try: request.check_required_parameters( - path = { + path={ 'simulationId': 'int', 'userId': 'int' }, - body = { + body={ 'authorization': { 'authorizationLevel': 'string' } @@ -192,7 +196,7 @@ def PUT(request): return Response(403, 'Forbidden from updating {}.'.format(authorization)) # Try to update this Authorization - + try: authorization.update() @@ -200,7 +204,7 @@ def PUT(request): return Response(400, 'Invalid authorization level.') # Return this Authorization - + return Response( 200, 'Successfully updated {}.'.format(authorization), diff --git a/opendc/api/v1/simulations/simulationId/datacenters/endpoint.py b/opendc/api/v1/simulations/simulationId/datacenters/endpoint.py index d53cd1c8..e28402e4 100644 --- a/opendc/api/v1/simulations/simulationId/datacenters/endpoint.py +++ b/opendc/api/v1/simulations/simulationId/datacenters/endpoint.py @@ -1,8 +1,9 @@ from opendc.models.datacenter import Datacenter from opendc.models.simulation import Simulation -from opendc.util import database, exceptions +from opendc.util import exceptions from opendc.util.rest import Response + def POST(request): """Add a new Datacenter to this Simulation.""" @@ -10,10 +11,10 @@ def POST(request): try: request.check_required_parameters( - path = { + path={ 'simulationId': 'int' }, - body = { + body={ 'datacenter': { 'starred': 'int', 'simulationId': 'int' diff --git a/opendc/api/v1/simulations/simulationId/endpoint.py b/opendc/api/v1/simulations/simulationId/endpoint.py index 01623973..5e595740 100644 --- a/opendc/api/v1/simulations/simulationId/endpoint.py +++ b/opendc/api/v1/simulations/simulationId/endpoint.py @@ -1,11 +1,10 @@ from datetime import datetime -from opendc.models.authorization import Authorization from opendc.models.simulation import Simulation -from opendc.models.user import User from opendc.util import database, exceptions from opendc.util.rest import Response + def DELETE(request): """Delete this Simulation.""" @@ -13,14 +12,14 @@ def DELETE(request): try: request.check_required_parameters( - path = { + path={ 'simulationId': 'int' } ) - + except exceptions.ParameterError as e: return Response(400, e.message) - + # Instantiate a Simulation and make sure it exists simulation = Simulation.from_primary_key((request.params_path['simulationId'],)) @@ -45,6 +44,7 @@ def DELETE(request): simulation.to_JSON() ) + def GET(request): """Get this Simulation.""" @@ -52,7 +52,7 @@ def GET(request): try: request.check_required_parameters( - path = { + path={ 'simulationId': 'int' } ) @@ -82,6 +82,7 @@ def GET(request): simulation.to_JSON() ) + def PUT(request): """Update a simulation's name.""" @@ -89,12 +90,12 @@ def PUT(request): try: request.check_required_parameters( - body = { + body={ 'simulation': { 'name': 'name' } }, - path = { + path={ 'simulationId': 'int' } ) @@ -115,7 +116,7 @@ def PUT(request): return Response(403, 'Forbidden from editing {}.'.format(simulation)) # Update this Simulation in the database - + simulation.read() simulation.name = request.params_body['simulation']['name'] diff --git a/opendc/api/v1/simulations/simulationId/experiments/endpoint.py b/opendc/api/v1/simulations/simulationId/experiments/endpoint.py index 034e82a1..86fadb24 100644 --- a/opendc/api/v1/simulations/simulationId/experiments/endpoint.py +++ b/opendc/api/v1/simulations/simulationId/experiments/endpoint.py @@ -1,9 +1,9 @@ from opendc.models.experiment import Experiment -from opendc.models.queued_experiment import QueuedExperiment from opendc.models.simulation import Simulation -from opendc.util import database, exceptions +from opendc.util import exceptions from opendc.util.rest import Response + def GET(request): """Get this Simulation's Experiments.""" @@ -11,7 +11,7 @@ def GET(request): try: request.check_required_parameters( - path = { + path={ 'simulationId': 'int' } ) @@ -43,6 +43,7 @@ def GET(request): [x.to_JSON() for x in experiments] ) + def POST(request): """Add a new Experiment for this Simulation.""" @@ -50,10 +51,10 @@ def POST(request): try: request.check_required_parameters( - path = { + path={ 'simulationId': 'int' }, - body = { + body={ 'experiment': { 'simulationId': 'int', 'pathId': 'int', @@ -96,12 +97,12 @@ def POST(request): try: experiment.insert() - + except exceptions.ForeignKeyError as e: return Response(400, 'Foreign key constraint not met.' + e.message) # Return this Experiment - + experiment.read() return Response( diff --git a/opendc/api/v1/simulations/simulationId/paths/endpoint.py b/opendc/api/v1/simulations/simulationId/paths/endpoint.py index e74c8d22..e4aab427 100644 --- a/opendc/api/v1/simulations/simulationId/paths/endpoint.py +++ b/opendc/api/v1/simulations/simulationId/paths/endpoint.py @@ -1,8 +1,9 @@ from opendc.models.path import Path from opendc.models.simulation import Simulation -from opendc.util import database, exceptions +from opendc.util import exceptions from opendc.util.rest import Response + def GET(request): """Get this Simulation's Paths.""" @@ -10,7 +11,7 @@ def GET(request): try: request.check_required_parameters( - path = { + path={ 'simulationId': 'int' } ) |
