def DELETE(request): """Delete this Simulation.""" # Make sure required parameters are there try: request.check_required_parameters(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'], )) if not simulation.exists(): return Response(404, '{} not found.'.format(simulation)) # Make sure this User is allowed to delete this Simulation if not simulation.google_id_has_at_least(request.google_id, 'OWN'): return Response(403, 'Forbidden from deleting {}.'.format(simulation)) # Delete this Simulation from the database simulation.delete() # Return this Simulation return Response(200, 'Successfully deleted {}.'.format(simulation), simulation.to_JSON())
def GET(request): """Get this Simulation.""" # Make sure required parameters are there try: request.check_required_parameters(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'], )) if not simulation.exists(): return Response(404, '{} not found.'.format(simulation)) # Make sure this User is allowed to view this Simulation if not simulation.google_id_has_at_least(request.google_id, 'VIEW'): return Response(403, 'Forbidden from retrieving {}.'.format(simulation)) # Return this Simulation simulation.read() return Response(200, 'Successfully retrieved {}'.format(simulation), simulation.to_JSON())
def GET(request): """Get this Simulation's Paths.""" # Make sure required parameters are there try: request.check_required_parameters(path={'simulationId': 'int'}) except exceptions.ParameterError as e: return Response(400, e.message) # 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 path if not simulation.google_id_has_at_least(request.google_id, 'VIEW'): return Response( 403, 'Forbidden from viewing Paths for {}.'.format(simulation)) # Get and return the Paths paths = Path.query('simulation_id', request.params_path['simulationId']) return Response(200, 'Successfully retrieved Paths for {}.'.format(simulation), [x.to_JSON() for x in paths])
def POST(request): """Add a new Datacenter to this Simulation.""" # Make sure required parameters are there try: request.check_required_parameters( path={ 'simulationId': 'int' }, body={ 'datacenter': { 'starred': 'int', 'simulationId': 'int' } } ) except exceptions.ParameterError as e: return Response(400, e.message) # Make sure the passed object's simulation id matches the path simulation id if request.params_path['simulationId'] != request.params_body['datacenter']['simulationId']: return Response(400, '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 Databases if not simulation.google_id_has_at_least(request.google_id, 'EDIT'): return Response(403, 'Forbidden from adding a datacenter to {}.'.format(simulation)) # Instantiate a Datacenter datacenter = Datacenter.from_JSON(request.params_body['datacenter']) datacenter.insert() # return this Datacenter datacenter.read() return Response( 200, 'Successfully added {}.'.format(datacenter), datacenter.to_JSON() )
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)
def PUT(request): """Update a simulation's name.""" # Make sure required parameters are there try: request.check_required_parameters( body={'simulation': { 'name': 'name' }}, 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'], )) if not simulation.exists(): return Response(404, '{} not found.'.format(simulation)) # Make sure this User is allowed to edit this Simulation if not simulation.google_id_has_at_least(request.google_id, 'EDIT'): return Response(403, 'Forbidden from editing {}.'.format(simulation)) # Update this Simulation in the database simulation.read() simulation.name = request.params_body['simulation']['name'] simulation.datetime_last_edited = database.datetime_to_string( datetime.now()) simulation.update() # Return this Simulation return Response(200, 'Successfully updated {}.'.format(simulation), simulation.to_JSON())
def POST(request): """Create a new simulation, and return that new simulation.""" # Make sure required parameters are there try: request.check_required_parameters( body={'simulation': { 'name': 'string' }}) except exceptions.ParameterError as e: return Response(400, e.message) # Instantiate a Simulation simulation_data = request.params_body['simulation'] simulation_data['datetimeCreated'] = database.datetime_to_string( datetime.now()) simulation_data['datetimeLastEdited'] = database.datetime_to_string( datetime.now()) simulation = Simulation.from_JSON(simulation_data) # Insert this Simulation into the database simulation.insert() # 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') authorization.insert() # Instantiate a Path and insert it into the database path = Path(simulation_id=simulation.id, datetime_created=database.datetime_to_string(datetime.now())) path.insert() # Instantiate a Datacenter and insert it into the database datacenter = Datacenter(starred=0, simulation_id=simulation.id) datacenter.insert() # Instantiate a Section and insert it into the database section = Section(path_id=path.id, datacenter_id=datacenter.id, start_tick=0) section.insert() # Return this Simulation return Response(200, 'Successfully created {}.'.format(simulation), simulation.to_JSON())
def POST(request): """Add a new Experiment for this Simulation.""" # Make sure required parameters are there try: request.check_required_parameters(path={'simulationId': 'int'}, body={ 'experiment': { 'simulationId': 'int', 'pathId': 'int', 'traceId': 'int', 'schedulerName': 'string', 'name': 'string' } }) except exceptions.ParameterError as e: return Response(400, e.message) # 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.message) # Return this Experiment experiment.read() return Response(200, 'Successfully added {}.'.format(experiment), experiment.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': 'int', 'simulationId': 'int' }, body={'authorization': { 'authorizationLevel': 'string' }}) except exceptions.ParameterError as e: return Response(400, e.message) # 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())