summaryrefslogtreecommitdiff
path: root/opendc/api/v2/simulations/simulationId/datacenters/endpoint.py
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/datacenters/endpoint.py
parent6f41be7d9c244b67bfa5ff72f1e90d18fa45b590 (diff)
Add DB handlers and rename to v2
Diffstat (limited to 'opendc/api/v2/simulations/simulationId/datacenters/endpoint.py')
-rw-r--r--opendc/api/v2/simulations/simulationId/datacenters/endpoint.py61
1 files changed, 61 insertions, 0 deletions
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()
+ )