summaryrefslogtreecommitdiff
path: root/opendc/api/v2/simulations/simulationId
diff options
context:
space:
mode:
authorGeorgios Andreadis <info@gandreadis.com>2020-06-23 18:08:28 +0200
committerGeorgios Andreadis <info@gandreadis.com>2020-06-23 18:08:28 +0200
commit565ede0dc50c3b2df09c066ea3a28a4901cce547 (patch)
treee81a1bbed43d2eaf7e2be0bc61007963f807b62d /opendc/api/v2/simulations/simulationId
parent6f41be7d9c244b67bfa5ff72f1e90d18fa45b590 (diff)
Add DB handlers and rename to v2
Diffstat (limited to 'opendc/api/v2/simulations/simulationId')
-rw-r--r--opendc/api/v2/simulations/simulationId/__init__.py0
-rw-r--r--opendc/api/v2/simulations/simulationId/authorizations/__init__.py0
-rw-r--r--opendc/api/v2/simulations/simulationId/authorizations/endpoint.py44
-rw-r--r--opendc/api/v2/simulations/simulationId/authorizations/userId/__init__.py0
-rw-r--r--opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py212
-rw-r--r--opendc/api/v2/simulations/simulationId/datacenters/__init__.py0
-rw-r--r--opendc/api/v2/simulations/simulationId/datacenters/endpoint.py61
-rw-r--r--opendc/api/v2/simulations/simulationId/endpoint.py133
-rw-r--r--opendc/api/v2/simulations/simulationId/experiments/__init__.py0
-rw-r--r--opendc/api/v2/simulations/simulationId/experiments/endpoint.py112
-rw-r--r--opendc/api/v2/simulations/simulationId/paths/__init__.py0
-rw-r--r--opendc/api/v2/simulations/simulationId/paths/endpoint.py43
12 files changed, 605 insertions, 0 deletions
diff --git a/opendc/api/v2/simulations/simulationId/__init__.py b/opendc/api/v2/simulations/simulationId/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/opendc/api/v2/simulations/simulationId/__init__.py
diff --git a/opendc/api/v2/simulations/simulationId/authorizations/__init__.py b/opendc/api/v2/simulations/simulationId/authorizations/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/opendc/api/v2/simulations/simulationId/authorizations/__init__.py
diff --git a/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py b/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py
new file mode 100644
index 00000000..1d6b494e
--- /dev/null
+++ b/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py
@@ -0,0 +1,44 @@
+from opendc.models.authorization import Authorization
+from opendc.models.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': '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'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
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/opendc/api/v2/simulations/simulationId/authorizations/userId/__init__.py
diff --git a/opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py b/opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py
new file mode 100644
index 00000000..46458ffc
--- /dev/null
+++ b/opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py
@@ -0,0 +1,212 @@
+from opendc.models.authorization import Authorization
+from opendc.models.simulation import Simulation
+from opendc.models.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': 'int',
+ 'userId': 'int'
+ }
+ )
+
+ except exceptions.ParameterError as e:
+ return Response(400, e.message)
+
+ # 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': 'int',
+ 'userId': 'int'
+ }
+ )
+
+ except exceptions.ParameterError as e:
+ return Response(400, e.message)
+
+ # 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': '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()
+ )
+
+
+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': 'int',
+ 'userId': 'int'
+ },
+ body={
+ 'authorization': {
+ 'authorizationLevel': 'string'
+ }
+ }
+ )
+
+ except exceptions.ParameterError as e:
+ return Response(400, e.message)
+
+ # 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/datacenters/__init__.py b/opendc/api/v2/simulations/simulationId/datacenters/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/opendc/api/v2/simulations/simulationId/datacenters/__init__.py
diff --git a/opendc/api/v2/simulations/simulationId/datacenters/endpoint.py b/opendc/api/v2/simulations/simulationId/datacenters/endpoint.py
new file mode 100644
index 00000000..e28402e4
--- /dev/null
+++ b/opendc/api/v2/simulations/simulationId/datacenters/endpoint.py
@@ -0,0 +1,61 @@
+from opendc.models.datacenter import Datacenter
+from opendc.models.simulation import Simulation
+from opendc.util import exceptions
+from opendc.util.rest import Response
+
+
+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()
+ )
diff --git a/opendc/api/v2/simulations/simulationId/endpoint.py b/opendc/api/v2/simulations/simulationId/endpoint.py
new file mode 100644
index 00000000..5e595740
--- /dev/null
+++ b/opendc/api/v2/simulations/simulationId/endpoint.py
@@ -0,0 +1,133 @@
+from datetime import datetime
+
+from opendc.models.simulation import Simulation
+from opendc.util import database, exceptions
+from opendc.util.rest import Response
+
+
+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 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()
+ )
diff --git a/opendc/api/v2/simulations/simulationId/experiments/__init__.py b/opendc/api/v2/simulations/simulationId/experiments/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/opendc/api/v2/simulations/simulationId/experiments/__init__.py
diff --git a/opendc/api/v2/simulations/simulationId/experiments/endpoint.py b/opendc/api/v2/simulations/simulationId/experiments/endpoint.py
new file mode 100644
index 00000000..86fadb24
--- /dev/null
+++ b/opendc/api/v2/simulations/simulationId/experiments/endpoint.py
@@ -0,0 +1,112 @@
+from opendc.models.experiment import Experiment
+from opendc.models.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': '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 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': '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()
+ )
diff --git a/opendc/api/v2/simulations/simulationId/paths/__init__.py b/opendc/api/v2/simulations/simulationId/paths/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/opendc/api/v2/simulations/simulationId/paths/__init__.py
diff --git a/opendc/api/v2/simulations/simulationId/paths/endpoint.py b/opendc/api/v2/simulations/simulationId/paths/endpoint.py
new file mode 100644
index 00000000..e4aab427
--- /dev/null
+++ b/opendc/api/v2/simulations/simulationId/paths/endpoint.py
@@ -0,0 +1,43 @@
+from opendc.models.path import Path
+from opendc.models.simulation import Simulation
+from opendc.util import exceptions
+from opendc.util.rest import Response
+
+
+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]
+ )