summaryrefslogtreecommitdiff
path: root/opendc/api/v2/simulations/endpoint.py
diff options
context:
space:
mode:
Diffstat (limited to 'opendc/api/v2/simulations/endpoint.py')
-rw-r--r--opendc/api/v2/simulations/endpoint.py63
1 files changed, 15 insertions, 48 deletions
diff --git a/opendc/api/v2/simulations/endpoint.py b/opendc/api/v2/simulations/endpoint.py
index 7ef90e97..dfeb8d5e 100644
--- a/opendc/api/v2/simulations/endpoint.py
+++ b/opendc/api/v2/simulations/endpoint.py
@@ -1,65 +1,32 @@
from datetime import datetime
-from opendc.models_old.authorization import Authorization
-from opendc.models_old.datacenter import Datacenter
-from opendc.models_old.path import Path
-from opendc.models_old.section import Section
-from opendc.models_old.simulation import Simulation
-from opendc.models_old.user import User
-from opendc.util import database, exceptions
+from opendc.models.simulation import Simulation
+from opendc.models.topology import Topology
+from opendc.models.user import User
+from opendc.util import exceptions
+from opendc.util.database import Database
from opendc.util.rest import Response
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, str(e))
- # 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
+ topology = Topology({'name': 'Default topology'})
+ topology.insert()
+ simulation = Simulation({'simulation': request.params_body['simulation']})
+ simulation.set_property('datetimeCreated', Database.datetime_to_string(datetime.now()))
+ simulation.set_property('datetimeLastEdited', Database.datetime_to_string(datetime.now()))
+ simulation.set_property('topologyIds', [topology.obj['_id']])
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
+ user = User.from_google_id(request.google_id)
+ user.obj['authorizations'].append({'simulationId': simulation.obj['_id'], 'authorizationLevel': 'OWN'})
+ user.update()
- return Response(200, 'Successfully created {}.'.format(simulation), simulation.to_JSON())
+ return Response(200, 'Successfully created simulation.', simulation.obj)