summaryrefslogtreecommitdiff
path: root/opendc/api/v2/simulations
diff options
context:
space:
mode:
Diffstat (limited to 'opendc/api/v2/simulations')
-rw-r--r--opendc/api/v2/simulations/simulationId/authorizations/endpoint.py2
-rw-r--r--opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py8
-rw-r--r--opendc/api/v2/simulations/simulationId/datacenters/__init__.py0
-rw-r--r--opendc/api/v2/simulations/simulationId/datacenters/endpoint.py51
-rw-r--r--opendc/api/v2/simulations/simulationId/endpoint.py86
-rw-r--r--opendc/api/v2/simulations/simulationId/experiments/endpoint.py6
-rw-r--r--opendc/api/v2/simulations/simulationId/paths/__init__.py0
-rw-r--r--opendc/api/v2/simulations/simulationId/paths/endpoint.py35
-rw-r--r--opendc/api/v2/simulations/simulationId/test_endpoint.py24
9 files changed, 72 insertions, 140 deletions
diff --git a/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py b/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py
index bc1a7dc3..df2b5cfd 100644
--- a/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py
+++ b/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py
@@ -10,7 +10,7 @@ def GET(request):
# Make sure required parameters are there
try:
- request.check_required_parameters(path={'simulationId': 'int'})
+ request.check_required_parameters(path={'simulationId': 'string'})
except exceptions.ParameterError as e:
return Response(400, str(e))
diff --git a/opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py b/opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py
index d132b1dd..121530db 100644
--- a/opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py
+++ b/opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py
@@ -11,7 +11,7 @@ def DELETE(request):
# Make sure required parameters are there
try:
- request.check_required_parameters(path={'simulationId': 'int', 'userId': 'string'})
+ request.check_required_parameters(path={'simulationId': 'string', 'userId': 'string'})
except exceptions.ParameterError as e:
return Response(400, str(e))
@@ -43,7 +43,7 @@ def GET(request):
# Make sure required parameters are there
try:
- request.check_required_parameters(path={'simulationId': 'int', 'userId': 'string'})
+ request.check_required_parameters(path={'simulationId': 'string', 'userId': 'string'})
except exceptions.ParameterError as e:
return Response(400, str(e))
@@ -74,7 +74,7 @@ def POST(request):
try:
request.check_required_parameters(path={
'userId': 'string',
- 'simulationId': 'int'
+ 'simulationId': 'string'
},
body={'authorization': {
'authorizationLevel': 'string'
@@ -134,7 +134,7 @@ def PUT(request):
try:
request.check_required_parameters(path={
- 'simulationId': 'int',
+ 'simulationId': 'string',
'userId': 'string'
},
body={'authorization': {
diff --git a/opendc/api/v2/simulations/simulationId/datacenters/__init__.py b/opendc/api/v2/simulations/simulationId/datacenters/__init__.py
deleted file mode 100644
index e69de29b..00000000
--- a/opendc/api/v2/simulations/simulationId/datacenters/__init__.py
+++ /dev/null
diff --git a/opendc/api/v2/simulations/simulationId/datacenters/endpoint.py b/opendc/api/v2/simulations/simulationId/datacenters/endpoint.py
deleted file mode 100644
index dbef2e8f..00000000
--- a/opendc/api/v2/simulations/simulationId/datacenters/endpoint.py
+++ /dev/null
@@ -1,51 +0,0 @@
-from opendc.models_old.datacenter import Datacenter
-from opendc.models_old.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, str(e))
-
- # 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
index 09714560..5c9d126f 100644
--- a/opendc/api/v2/simulations/simulationId/endpoint.py
+++ b/opendc/api/v2/simulations/simulationId/endpoint.py
@@ -1,49 +1,41 @@
from datetime import datetime
-from opendc.models_old.simulation import Simulation
+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."""
-
- # Make sure required parameters are there
+def GET(request):
+ """Get this Simulation."""
try:
- request.check_required_parameters(path={'simulationId': 'int'})
-
+ 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'], ))
+ simulation = Simulation.from_id(request.params_path['simulationId'])
+ validation_error = simulation.validate()
+ if validation_error is not None:
+ return validation_error
- if not simulation.exists():
- return Response(404, '{} not found.'.format(simulation))
+ user = User.from_google_id(request.google_id)
+ authorizations = list(filter(
+ lambda x: str(x['simulationId']) == str(request.params_path['simulationId']),
+ user.obj['authorizations']))
+ if len(authorizations) == 0 or authorizations[0]['authorizationLevel'] == 'VIEW':
+ return Response(403, "Forbidden from retrieving simulation.")
- # Make sure this User is allowed to delete this Simulation
+ return Response(200, 'Successfully retrieved simulation', simulation.obj)
- 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."""
+def PUT(request):
+ """Update a simulation's name."""
# Make sure required parameters are there
try:
- request.check_required_parameters(path={'simulationId': 'int'})
+ request.check_required_parameters(body={'simulation': {'name': 'name'}}, path={'simulationId': 'string'})
except exceptions.ParameterError as e:
return Response(400, str(e))
@@ -55,25 +47,32 @@ def GET(request):
if not simulation.exists():
return Response(404, '{} not found.'.format(simulation))
- # Make sure this User is allowed to view this Simulation
+ # Make sure this User is allowed to edit this Simulation
- if not simulation.google_id_has_at_least(request.google_id, 'VIEW'):
- return Response(403, 'Forbidden from retrieving {}.'.format(simulation))
+ if not simulation.google_id_has_at_least(request.google_id, 'EDIT'):
+ return Response(403, 'Forbidden from editing {}.'.format(simulation))
- # Return this Simulation
+ # Update this Simulation in the database
simulation.read()
- return Response(200, 'Successfully retrieved {}'.format(simulation), simulation.to_JSON())
+ simulation.name = request.params_body['simulation']['name']
+ simulation.datetime_last_edited = database.datetime_to_string(datetime.now())
+ simulation.update()
-def PUT(request):
- """Update a simulation's name."""
+ # Return this Simulation
+
+ return Response(200, 'Successfully updated {}.'.format(simulation), simulation.to_JSON())
+
+
+def DELETE(request):
+ """Delete this Simulation."""
# Make sure required parameters are there
try:
- request.check_required_parameters(body={'simulation': {'name': 'name'}}, path={'simulationId': 'int'})
+ request.check_required_parameters(path={'simulationId': 'string'})
except exceptions.ParameterError as e:
return Response(400, str(e))
@@ -85,20 +84,15 @@ def PUT(request):
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
+ # Make sure this User is allowed to delete this Simulation
- simulation.read()
+ if not simulation.google_id_has_at_least(request.google_id, 'OWN'):
+ return Response(403, 'Forbidden from deleting {}.'.format(simulation))
- simulation.name = request.params_body['simulation']['name']
- simulation.datetime_last_edited = database.datetime_to_string(datetime.now())
+ # Delete this Simulation from the database
- simulation.update()
+ simulation.delete()
# Return this Simulation
- return Response(200, 'Successfully updated {}.'.format(simulation), simulation.to_JSON())
+ return Response(200, 'Successfully deleted {}.'.format(simulation), simulation.to_JSON())
diff --git a/opendc/api/v2/simulations/simulationId/experiments/endpoint.py b/opendc/api/v2/simulations/simulationId/experiments/endpoint.py
index 5ecaffb7..9df84838 100644
--- a/opendc/api/v2/simulations/simulationId/experiments/endpoint.py
+++ b/opendc/api/v2/simulations/simulationId/experiments/endpoint.py
@@ -10,7 +10,7 @@ def GET(request):
# Make sure required parameters are there
try:
- request.check_required_parameters(path={'simulationId': 'int'})
+ request.check_required_parameters(path={'simulationId': 'string'})
except exceptions.ParameterError as e:
return Response(400, str(e))
@@ -43,10 +43,10 @@ def POST(request):
# Make sure required parameters are there
try:
- request.check_required_parameters(path={'simulationId': 'int'},
+ request.check_required_parameters(path={'simulationId': 'string'},
body={
'experiment': {
- 'simulationId': 'int',
+ 'simulationId': 'string',
'pathId': 'int',
'traceId': 'int',
'schedulerName': 'string',
diff --git a/opendc/api/v2/simulations/simulationId/paths/__init__.py b/opendc/api/v2/simulations/simulationId/paths/__init__.py
deleted file mode 100644
index e69de29b..00000000
--- a/opendc/api/v2/simulations/simulationId/paths/__init__.py
+++ /dev/null
diff --git a/opendc/api/v2/simulations/simulationId/paths/endpoint.py b/opendc/api/v2/simulations/simulationId/paths/endpoint.py
deleted file mode 100644
index 4e4a1ec7..00000000
--- a/opendc/api/v2/simulations/simulationId/paths/endpoint.py
+++ /dev/null
@@ -1,35 +0,0 @@
-from opendc.models_old.path import Path
-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 Paths."""
-
- # Make sure required parameters are there
-
- try:
- request.check_required_parameters(path={'simulationId': 'int'})
- 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 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])
diff --git a/opendc/api/v2/simulations/simulationId/test_endpoint.py b/opendc/api/v2/simulations/simulationId/test_endpoint.py
new file mode 100644
index 00000000..cf4b375a
--- /dev/null
+++ b/opendc/api/v2/simulations/simulationId/test_endpoint.py
@@ -0,0 +1,24 @@
+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={'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={'authorizations': [{'simulationId': '1', 'authorizationLevel': 'EDIT'}]})
+ res = client.get('/api/v2/simulations/1')
+ assert '200' in res.status