From f589682b0840aab0624122052eb863cf8dc3a0b9 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Sun, 13 Aug 2017 23:12:16 +0300 Subject: Convert API codebase to flat model --- opendc/api/v1/datacenters/__init__.py | 0 opendc/api/v1/datacenters/datacenterId/__init__.py | 0 opendc/api/v1/datacenters/datacenterId/endpoint.py | 42 ++++ .../v1/datacenters/datacenterId/rooms/__init__.py | 0 .../v1/datacenters/datacenterId/rooms/endpoint.py | 109 +++++++++ opendc/api/v1/experiments/__init__.py | 0 opendc/api/v1/experiments/experimentId/__init__.py | 0 opendc/api/v1/experiments/experimentId/endpoint.py | 135 ++++++++++++ .../experimentId/last-simulated-tick/__init__.py | 0 .../experimentId/last-simulated-tick/endpoint.py | 38 ++++ .../experimentId/machine-states/__init__.py | 0 .../experimentId/machine-states/endpoint.py | 50 +++++ .../experimentId/rack-states/__init__.py | 0 .../experimentId/rack-states/endpoint.py | 50 +++++ .../experimentId/room-states/__init__.py | 0 .../experimentId/room-states/endpoint.py | 50 +++++ .../experimentId/statistics/__init__.py | 0 .../statistics/task-durations/__init__.py | 0 .../statistics/task-durations/endpoint.py | 43 ++++ .../experimentId/task-states/__init__.py | 0 .../experimentId/task-states/endpoint.py | 50 +++++ opendc/api/v1/jobs/__init__.py | 0 opendc/api/v1/jobs/jobId/__init__.py | 0 opendc/api/v1/jobs/jobId/endpoint.py | 33 +++ opendc/api/v1/jobs/jobId/tasks/__init__.py | 0 opendc/api/v1/jobs/jobId/tasks/endpoint.py | 36 +++ opendc/api/v1/paths.json | 44 ++-- opendc/api/v1/paths/__init__.py | 0 opendc/api/v1/paths/pathId/__init__.py | 0 opendc/api/v1/paths/pathId/branches/__init__.py | 0 opendc/api/v1/paths/pathId/branches/endpoint.py | 165 ++++++++++++++ opendc/api/v1/paths/pathId/endpoint.py | 43 ++++ opendc/api/v1/paths/pathId/sections/__init__.py | 0 opendc/api/v1/paths/pathId/sections/endpoint.py | 42 ++++ opendc/api/v1/rooms/__init__.py | 0 opendc/api/v1/rooms/roomId/__init__.py | 0 opendc/api/v1/rooms/roomId/endpoint.py | 134 +++++++++++ opendc/api/v1/rooms/roomId/tiles/__init__.py | 0 opendc/api/v1/rooms/roomId/tiles/endpoint.py | 119 ++++++++++ opendc/api/v1/sections/__init__.py | 0 opendc/api/v1/sections/sectionId/__init__.py | 0 opendc/api/v1/sections/sectionId/endpoint.py | 42 ++++ .../datacenters/datacenterId/__init__.py | 0 .../datacenters/datacenterId/endpoint.py | 43 ---- .../datacenters/datacenterId/rooms/__init__.py | 0 .../datacenters/datacenterId/rooms/endpoint.py | 111 ---------- .../datacenterId/rooms/roomId/__init__.py | 0 .../datacenterId/rooms/roomId/endpoint.py | 140 ------------ .../datacenterId/rooms/roomId/tiles/__init__.py | 0 .../datacenterId/rooms/roomId/tiles/endpoint.py | 123 ----------- .../rooms/roomId/tiles/tileId/__init__.py | 0 .../rooms/roomId/tiles/tileId/endpoint.py | 89 -------- .../rooms/roomId/tiles/tileId/rack/__init__.py | 0 .../rooms/roomId/tiles/tileId/rack/endpoint.py | 245 --------------------- .../roomId/tiles/tileId/rack/machines/__init__.py | 0 .../roomId/tiles/tileId/rack/machines/endpoint.py | 122 ---------- .../tileId/rack/machines/position/__init__.py | 0 .../tileId/rack/machines/position/endpoint.py | 170 -------------- .../experiments/experimentId/__init__.py | 0 .../experiments/experimentId/endpoint.py | 138 ------------ .../experimentId/last-simulated-tick/__init__.py | 0 .../experimentId/last-simulated-tick/endpoint.py | 39 ---- .../experimentId/machine-states/__init__.py | 0 .../experimentId/machine-states/endpoint.py | 51 ----- .../experimentId/rack-states/__init__.py | 0 .../experimentId/rack-states/endpoint.py | 51 ----- .../experimentId/room-states/__init__.py | 0 .../experimentId/room-states/endpoint.py | 51 ----- .../experimentId/statistics/__init__.py | 0 .../statistics/task-durations/__init__.py | 0 .../statistics/task-durations/endpoint.py | 44 ---- .../experimentId/task-states/__init__.py | 0 .../experimentId/task-states/endpoint.py | 51 ----- .../simulationId/paths/pathId/__init__.py | 0 .../simulationId/paths/pathId/branches/__init__.py | 0 .../simulationId/paths/pathId/branches/endpoint.py | 166 -------------- .../simulationId/paths/pathId/endpoint.py | 44 ---- .../simulationId/paths/pathId/sections/__init__.py | 0 .../simulationId/paths/pathId/sections/endpoint.py | 43 ---- .../paths/pathId/sections/sectionId/__init__.py | 0 .../paths/pathId/sections/sectionId/endpoint.py | 44 ---- opendc/api/v1/tiles/__init__.py | 0 opendc/api/v1/tiles/tileId/__init__.py | 0 opendc/api/v1/tiles/tileId/endpoint.py | 83 +++++++ opendc/api/v1/tiles/tileId/rack/__init__.py | 0 opendc/api/v1/tiles/tileId/rack/endpoint.py | 233 ++++++++++++++++++++ .../api/v1/tiles/tileId/rack/machines/__init__.py | 0 .../api/v1/tiles/tileId/rack/machines/endpoint.py | 116 ++++++++++ .../tileId/rack/machines/position/__init__.py | 0 .../tileId/rack/machines/position/endpoint.py | 161 ++++++++++++++ opendc/api/v1/traces/endpoint.py | 2 +- .../api/v1/traces/traceId/jobs/jobId/__init__.py | 0 .../api/v1/traces/traceId/jobs/jobId/endpoint.py | 34 --- .../v1/traces/traceId/jobs/jobId/tasks/__init__.py | 0 .../v1/traces/traceId/jobs/jobId/tasks/endpoint.py | 37 ---- opendc/models/machine.py | 2 +- opendc/models/rack.py | 2 +- opendc/models/room.py | 2 +- opendc/models/tile.py | 2 +- 99 files changed, 1801 insertions(+), 1863 deletions(-) create mode 100644 opendc/api/v1/datacenters/__init__.py create mode 100644 opendc/api/v1/datacenters/datacenterId/__init__.py create mode 100644 opendc/api/v1/datacenters/datacenterId/endpoint.py create mode 100644 opendc/api/v1/datacenters/datacenterId/rooms/__init__.py create mode 100644 opendc/api/v1/datacenters/datacenterId/rooms/endpoint.py create mode 100644 opendc/api/v1/experiments/__init__.py create mode 100644 opendc/api/v1/experiments/experimentId/__init__.py create mode 100644 opendc/api/v1/experiments/experimentId/endpoint.py create mode 100644 opendc/api/v1/experiments/experimentId/last-simulated-tick/__init__.py create mode 100644 opendc/api/v1/experiments/experimentId/last-simulated-tick/endpoint.py create mode 100644 opendc/api/v1/experiments/experimentId/machine-states/__init__.py create mode 100644 opendc/api/v1/experiments/experimentId/machine-states/endpoint.py create mode 100644 opendc/api/v1/experiments/experimentId/rack-states/__init__.py create mode 100644 opendc/api/v1/experiments/experimentId/rack-states/endpoint.py create mode 100644 opendc/api/v1/experiments/experimentId/room-states/__init__.py create mode 100644 opendc/api/v1/experiments/experimentId/room-states/endpoint.py create mode 100644 opendc/api/v1/experiments/experimentId/statistics/__init__.py create mode 100644 opendc/api/v1/experiments/experimentId/statistics/task-durations/__init__.py create mode 100644 opendc/api/v1/experiments/experimentId/statistics/task-durations/endpoint.py create mode 100644 opendc/api/v1/experiments/experimentId/task-states/__init__.py create mode 100644 opendc/api/v1/experiments/experimentId/task-states/endpoint.py create mode 100644 opendc/api/v1/jobs/__init__.py create mode 100644 opendc/api/v1/jobs/jobId/__init__.py create mode 100644 opendc/api/v1/jobs/jobId/endpoint.py create mode 100644 opendc/api/v1/jobs/jobId/tasks/__init__.py create mode 100644 opendc/api/v1/jobs/jobId/tasks/endpoint.py create mode 100644 opendc/api/v1/paths/__init__.py create mode 100644 opendc/api/v1/paths/pathId/__init__.py create mode 100644 opendc/api/v1/paths/pathId/branches/__init__.py create mode 100644 opendc/api/v1/paths/pathId/branches/endpoint.py create mode 100644 opendc/api/v1/paths/pathId/endpoint.py create mode 100644 opendc/api/v1/paths/pathId/sections/__init__.py create mode 100644 opendc/api/v1/paths/pathId/sections/endpoint.py create mode 100644 opendc/api/v1/rooms/__init__.py create mode 100644 opendc/api/v1/rooms/roomId/__init__.py create mode 100644 opendc/api/v1/rooms/roomId/endpoint.py create mode 100644 opendc/api/v1/rooms/roomId/tiles/__init__.py create mode 100644 opendc/api/v1/rooms/roomId/tiles/endpoint.py create mode 100644 opendc/api/v1/sections/__init__.py create mode 100644 opendc/api/v1/sections/sectionId/__init__.py create mode 100644 opendc/api/v1/sections/sectionId/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/datacenters/datacenterId/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/datacenters/datacenterId/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/machines/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/machines/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/machines/position/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/machines/position/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/experiments/experimentId/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/experiments/experimentId/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/experiments/experimentId/last-simulated-tick/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/experiments/experimentId/last-simulated-tick/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/experiments/experimentId/machine-states/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/experiments/experimentId/machine-states/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/experiments/experimentId/rack-states/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/experiments/experimentId/rack-states/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/experiments/experimentId/room-states/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/experiments/experimentId/room-states/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/task-durations/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/task-durations/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/experiments/experimentId/task-states/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/experiments/experimentId/task-states/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/paths/pathId/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/paths/pathId/branches/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/paths/pathId/branches/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/paths/pathId/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/paths/pathId/sections/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/paths/pathId/sections/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/paths/pathId/sections/sectionId/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/paths/pathId/sections/sectionId/endpoint.py create mode 100644 opendc/api/v1/tiles/__init__.py create mode 100644 opendc/api/v1/tiles/tileId/__init__.py create mode 100644 opendc/api/v1/tiles/tileId/endpoint.py create mode 100644 opendc/api/v1/tiles/tileId/rack/__init__.py create mode 100644 opendc/api/v1/tiles/tileId/rack/endpoint.py create mode 100644 opendc/api/v1/tiles/tileId/rack/machines/__init__.py create mode 100644 opendc/api/v1/tiles/tileId/rack/machines/endpoint.py create mode 100644 opendc/api/v1/tiles/tileId/rack/machines/position/__init__.py create mode 100644 opendc/api/v1/tiles/tileId/rack/machines/position/endpoint.py delete mode 100644 opendc/api/v1/traces/traceId/jobs/jobId/__init__.py delete mode 100644 opendc/api/v1/traces/traceId/jobs/jobId/endpoint.py delete mode 100644 opendc/api/v1/traces/traceId/jobs/jobId/tasks/__init__.py delete mode 100644 opendc/api/v1/traces/traceId/jobs/jobId/tasks/endpoint.py (limited to 'opendc') diff --git a/opendc/api/v1/datacenters/__init__.py b/opendc/api/v1/datacenters/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/datacenters/datacenterId/__init__.py b/opendc/api/v1/datacenters/datacenterId/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/datacenters/datacenterId/endpoint.py b/opendc/api/v1/datacenters/datacenterId/endpoint.py new file mode 100644 index 00000000..75d0a9cf --- /dev/null +++ b/opendc/api/v1/datacenters/datacenterId/endpoint.py @@ -0,0 +1,42 @@ +from opendc.models.datacenter import Datacenter +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Datacenter.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'datacenterId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Datacenter from the database + + datacenter = Datacenter.from_primary_key((request.params_path['datacenterId'],)) + + # Make sure this Datacenter exists + + if not datacenter.exists(): + return Response(404, '{} not found.'.format(datacenter)) + + # Make sure this user is authorized to view this Datacenter + + if not datacenter.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from retrieving {}.'.format(datacenter)) + + # Return this Datacenter + + datacenter.read() + + return Response( + 200, + 'Successfully retrieved {}.'.format(datacenter), + datacenter.to_JSON() + ) diff --git a/opendc/api/v1/datacenters/datacenterId/rooms/__init__.py b/opendc/api/v1/datacenters/datacenterId/rooms/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/datacenters/datacenterId/rooms/endpoint.py b/opendc/api/v1/datacenters/datacenterId/rooms/endpoint.py new file mode 100644 index 00000000..31545a1e --- /dev/null +++ b/opendc/api/v1/datacenters/datacenterId/rooms/endpoint.py @@ -0,0 +1,109 @@ +from opendc.models.room import Room +from opendc.models.datacenter import Datacenter +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Datacenter's Rooms.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'datacenterId': 'int' + } + ) + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Datacenter from the database + + datacenter = Datacenter.from_primary_key((request.params_path['datacenterId'],)) + + # Make sure this Datacenter exists + + if not datacenter.exists(): + return Response(404, '{} not found.'.format(datacenter)) + + # Make sure this user is authorized to view this Datacenter's Rooms + + if not datacenter.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from viewing Rooms for {}.'.format(datacenter)) + + # Get and return the Rooms + + rooms = Room.query('datacenter_id', datacenter.id) + + return Response( + 200, + 'Successfully retrieved Rooms for {}.'.format(datacenter), + [x.to_JSON() for x in rooms] + ) + +def POST(request): + """Add a Room.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'datacenterId': 'int' + }, + body = { + 'room': { + 'id': 'int', + 'datacenterId': 'int', + 'roomType': 'string' + } + } + ) + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Make sure the passed object's datacenter id matches the path datacenter id + + if request.params_path['datacenterId'] != request.params_body['room']['datacenterId']: + return Response(400, 'ID mismatch.') + + # Instantiate a Datacenter from the database + + datacenter = Datacenter.from_primary_key((request.params_path['datacenterId'],)) + + # Make sure this Datacenter exists + + if not datacenter.exists(): + return Response(404, '{} not found.'.format(datacenter)) + + # Make sure this user is authorized to edit this Datacenter's Rooms + + if not datacenter.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from adding a Room to {}.'.format(datacenter)) + + # Add a name if not provided + + if not 'name' in request.params_body['room']: + room_count = len(Room.query('datacenter_id', datacenter.id)) + request.params_body['room']['name'] = 'Room {}'.format(room_count) + + # Instantiate a Room + + room = Room.from_JSON(request.params_body['room']) + + # Try to insert this Room + + try: + room.insert() + except Exception as e: + return Response(400, 'Invalid `roomType` or existing `name`.') + + # Return this Room + + room.read() + + return Response( + 200, + 'Successfully added {}.'.format(room), + room.to_JSON() + ) diff --git a/opendc/api/v1/experiments/__init__.py b/opendc/api/v1/experiments/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/experiments/experimentId/__init__.py b/opendc/api/v1/experiments/experimentId/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/experiments/experimentId/endpoint.py b/opendc/api/v1/experiments/experimentId/endpoint.py new file mode 100644 index 00000000..3bdd30d2 --- /dev/null +++ b/opendc/api/v1/experiments/experimentId/endpoint.py @@ -0,0 +1,135 @@ +from opendc.models.experiment import Experiment +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Experiment.""" + + try: + request.check_required_parameters( + path = { + 'experimentId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate an Experiment from the database + + experiment = Experiment.from_primary_key((request.params_path['experimentId'],)) + + # Make sure this Experiment exists + + if not experiment.exists(): + return Response(404, '{} not found.'.format(experiment)) + + # Make sure this user is authorized to view this Experiment + + if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from retrieving {}.'.format(experiment)) + + # Return this Experiment + + experiment.read() + + return Response( + 200, + 'Successfully retrieved {}.'.format(experiment), + experiment.to_JSON() + ) + +def PUT(request): + """Update this Experiment's Path, Trace, Scheduler, and/or name.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'experimentId': 'int' + }, + body = { + 'experiment': { + 'pathId': 'int', + 'traceId': 'int', + 'schedulerName': 'string', + 'name': 'string' + } + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate an Experiment from the database + + experiment = Experiment.from_primary_key((request.params_path['experimentId'],)) + + # Make sure this Experiment exists + + if not experiment.exists(): + return Response(404, '{} not found.'.format(experiment)) + + # Make sure this user is authorized to edit this Experiment + + if not experiment.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from updating {}.'.format(experiment)) + + # Update this Experiment + + experiment.path_id = request.params_body['experiment']['pathId'] + experiment.trace_id = request.params_body['experiment']['traceId'] + experiment.scheduler_name = request.params_body['experiment']['schedulerName'] + experiment.name = request.params_body['experiment']['name'] + + try: + experiment.update() + + except exceptions.ForeignKeyError: + return Response(400, 'Foreign key error.') + + # Return this Experiment + + return Response( + 200, + 'Successfully updated {}.'.format(experiment), + experiment.to_JSON() + ) + +def DELETE(request): + """Delete this Experiment.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'experimentId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate an Experiment and make sure it exists + + experiment = Experiment.from_primary_key((request.params_path['experimentId'],)) + + if not experiment.exists(): + return Response(404, '{} not found.'.format(experiment)) + + # Make sure this user is authorized to delete this Experiment + + if not experiment.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from deleting {}.'.format(experiment)) + + # Delete and return this Experiment + + experiment.delete() + + return Response( + 200, + 'Successfully deleted {}.'.format(experiment), + experiment.to_JSON() + ) diff --git a/opendc/api/v1/experiments/experimentId/last-simulated-tick/__init__.py b/opendc/api/v1/experiments/experimentId/last-simulated-tick/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/experiments/experimentId/last-simulated-tick/endpoint.py b/opendc/api/v1/experiments/experimentId/last-simulated-tick/endpoint.py new file mode 100644 index 00000000..24eb8932 --- /dev/null +++ b/opendc/api/v1/experiments/experimentId/last-simulated-tick/endpoint.py @@ -0,0 +1,38 @@ +from opendc.models.experiment import Experiment +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Experiment's last simulated tick.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'experimentId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate an Experiment from the database + + experiment = Experiment.from_primary_key((request.params_path['experimentId'],)) + + # Make sure this Experiment exisits + + if not experiment.exists(): + return Response(404, '{} not found.'.format(experiment)) + + # Make sure this user is authorized to view this Experiment's last simulated tick + + if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from viewing last simulated tick for {}.'.format(experiment)) + + return Response( + 200, + 'Successfully retrieved last simulated tick for {}.'.format(experiment), + {'lastSimulatedTick': experiment.last_simulated_tick} + ) diff --git a/opendc/api/v1/experiments/experimentId/machine-states/__init__.py b/opendc/api/v1/experiments/experimentId/machine-states/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/experiments/experimentId/machine-states/endpoint.py b/opendc/api/v1/experiments/experimentId/machine-states/endpoint.py new file mode 100644 index 00000000..97113e4a --- /dev/null +++ b/opendc/api/v1/experiments/experimentId/machine-states/endpoint.py @@ -0,0 +1,50 @@ +from opendc.models.experiment import Experiment +from opendc.models.machine_state import MachineState +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Experiment's Machine States.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'experimentId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate an Experiment from the database + + experiment = Experiment.from_primary_key((request.params_path['experimentId'],)) + + # Make sure this Experiment exisits + + if not experiment.exists(): + return Response(404, '{} not found.'.format(experiment)) + + # Make sure this user is authorized to view this Experiment's Machine States + + if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from viewing Machine States for {}.'.format(experiment)) + + # Get and return the Machine States + + if 'tick' in request.params_query: + machine_states = MachineState.from_experiment_id_and_tick( + request.params_path['experimentId'], + request.params_query['tick'] + ) + + else: + machine_states = MachineState.from_experiment_id(request.params_path['experimentId']) + + return Response( + 200, + 'Successfully retrieved Machine States for {}.'.format(experiment), + [x.to_JSON() for x in machine_states] + ) diff --git a/opendc/api/v1/experiments/experimentId/rack-states/__init__.py b/opendc/api/v1/experiments/experimentId/rack-states/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/experiments/experimentId/rack-states/endpoint.py b/opendc/api/v1/experiments/experimentId/rack-states/endpoint.py new file mode 100644 index 00000000..daed45c5 --- /dev/null +++ b/opendc/api/v1/experiments/experimentId/rack-states/endpoint.py @@ -0,0 +1,50 @@ +from opendc.models.experiment import Experiment +from opendc.models.rack_state import RackState +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Experiment's Tack States.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'experimentId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate an Experiment from the database + + experiment = Experiment.from_primary_key((request.params_path['experimentId'],)) + + # Make sure this Experiment exisits + + if not experiment.exists(): + return Response(404, '{} not found.'.format(experiment)) + + # Make sure this user is authorized to view this Experiment's Rack States + + if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from viewing Rack States for {}.'.format(experiment)) + + # Get and return the Rack States + + if 'tick' in request.params_query: + rack_states = RackState.from_experiment_id_and_tick( + request.params_path['experimentId'], + request.params_query['tick'] + ) + + else: + rack_states = RackState.from_experiment_id(request.params_path['experimentId']) + + return Response( + 200, + 'Successfully retrieved Rack States for {}.'.format(experiment), + [x.to_JSON() for x in rack_states] + ) diff --git a/opendc/api/v1/experiments/experimentId/room-states/__init__.py b/opendc/api/v1/experiments/experimentId/room-states/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/experiments/experimentId/room-states/endpoint.py b/opendc/api/v1/experiments/experimentId/room-states/endpoint.py new file mode 100644 index 00000000..9e283d34 --- /dev/null +++ b/opendc/api/v1/experiments/experimentId/room-states/endpoint.py @@ -0,0 +1,50 @@ +from opendc.models.experiment import Experiment +from opendc.models.room_state import RoomState +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Experiment's Room States.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'experimentId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate an Experiment from the database + + experiment = Experiment.from_primary_key((request.params_path['experimentId'],)) + + # Make sure this Experiment exisits + + if not experiment.exists(): + return Response(404, '{} not found.'.format(experiment)) + + # Make sure this user is authorized to view this Experiment's Room States + + if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from viewing Room States for {}.'.format(experiment)) + + # Get and return the Room States + + if 'tick' in request.params_query: + room_states = RoomState.from_experiment_id_and_tick( + request.params_path['experimentId'], + request.params_query['tick'] + ) + + else: + room_states = RoomState.from_experiment_id(request.params_path['experimentId']) + + return Response( + 200, + 'Successfully retrieved Room States for {}.'.format(experiment), + [x.to_JSON() for x in room_states] + ) diff --git a/opendc/api/v1/experiments/experimentId/statistics/__init__.py b/opendc/api/v1/experiments/experimentId/statistics/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/experiments/experimentId/statistics/task-durations/__init__.py b/opendc/api/v1/experiments/experimentId/statistics/task-durations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/experiments/experimentId/statistics/task-durations/endpoint.py b/opendc/api/v1/experiments/experimentId/statistics/task-durations/endpoint.py new file mode 100644 index 00000000..ad73daa7 --- /dev/null +++ b/opendc/api/v1/experiments/experimentId/statistics/task-durations/endpoint.py @@ -0,0 +1,43 @@ +from opendc.models.experiment import Experiment +from opendc.models.task_duration import TaskDuration +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Experiment's Task Durations.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'experimentId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate an Experiment from the database + + experiment = Experiment.from_primary_key((request.params_path['experimentId'],)) + + # Make sure this Experiment exisits + + if not experiment.exists(): + return Response(404, '{} not found.'.format(experiment)) + + # Make sure this user is authorized to view this Experiment's Task Durations + + if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from viewing Task Durations for {}.'.format(experiment)) + + # Get and return the Task Durations + + task_durations = TaskDuration.from_experiment_id(request.params_path['experimentId']) + + return Response( + 200, + 'Successfully retrieved Task Durations for {}.'.format(experiment), + [x.to_JSON() for x in task_durations] + ) diff --git a/opendc/api/v1/experiments/experimentId/task-states/__init__.py b/opendc/api/v1/experiments/experimentId/task-states/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/experiments/experimentId/task-states/endpoint.py b/opendc/api/v1/experiments/experimentId/task-states/endpoint.py new file mode 100644 index 00000000..805d86fb --- /dev/null +++ b/opendc/api/v1/experiments/experimentId/task-states/endpoint.py @@ -0,0 +1,50 @@ +from opendc.models.experiment import Experiment +from opendc.models.task_state import TaskState +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Experiment's Task States.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'experimentId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate an Experiment from the database + + experiment = Experiment.from_primary_key((request.params_path['experimentId'],)) + + # Make sure this Experiment exists + + if not experiment.exists(): + return Response(404, '{} not found.'.format(experiment)) + + # Make sure this user is authorized to view Task States for this Experiment + + if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from viewing Task States for {}.'.format(experiment)) + + # Get and return the Task States + + if 'tick' in request.params_query: + task_states = TaskState.from_experiment_id_and_tick( + request.params_path['experimentId'], + request.params_query['tick'] + ) + + else: + task_states = TaskState.query('experiment_id', request.params_path['experimentId']) + + return Response( + 200, + 'Successfully retrieved Task States for {}.'.format(experiment), + [x.to_JSON() for x in task_states] + ) diff --git a/opendc/api/v1/jobs/__init__.py b/opendc/api/v1/jobs/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/jobs/jobId/__init__.py b/opendc/api/v1/jobs/jobId/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/jobs/jobId/endpoint.py b/opendc/api/v1/jobs/jobId/endpoint.py new file mode 100644 index 00000000..84b8f3c4 --- /dev/null +++ b/opendc/api/v1/jobs/jobId/endpoint.py @@ -0,0 +1,33 @@ +from opendc.models.job import Job +from opendc.util import exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Job.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'jobId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Job and make sure it exists + + job = Job.from_primary_key((request.params_path['jobId'],)) + + if not job.exists(): + return Response(404, '{} not found.'.format(job)) + + # Return this Job + + return Response( + 200, + 'Successfully retrieved {}.'.format(job), + job.to_JSON() + ) diff --git a/opendc/api/v1/jobs/jobId/tasks/__init__.py b/opendc/api/v1/jobs/jobId/tasks/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/jobs/jobId/tasks/endpoint.py b/opendc/api/v1/jobs/jobId/tasks/endpoint.py new file mode 100644 index 00000000..9b6a1cab --- /dev/null +++ b/opendc/api/v1/jobs/jobId/tasks/endpoint.py @@ -0,0 +1,36 @@ +from opendc.models.job import Job +from opendc.models.task import Task +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Job's Tasks.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'jobId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Job and make sure it exists + + job = Job.from_primary_key((request.params_path['jobId'],)) + + if not job.exists(): + return Response(404, '{} not found.'.format(job)) + + # Get and return the Tasks + + tasks = Task.query('job_id', request.params_path['jobId']) + + return Response( + 200, + 'Successfully retrieved Tasks for {}.'.format(job), + [x.to_JSON() for x in tasks] + ) diff --git a/opendc/api/v1/paths.json b/opendc/api/v1/paths.json index 1e56d162..79133968 100644 --- a/opendc/api/v1/paths.json +++ b/opendc/api/v1/paths.json @@ -6,34 +6,34 @@ "/simulations/{simulationId}", "/simulations/{simulationId}/authorizations", "/simulations/{simulationId}/authorizations/{userId}", - "/simulations/{simulationId}/datacenters/{datacenterId}", - "/simulations/{simulationId}/datacenters/{datacenterId}/rooms", - "/simulations/{simulationId}/datacenters/{datacenterId}/rooms/{roomId}", - "/simulations/{simulationId}/datacenters/{datacenterId}/rooms/{roomId}/tiles", - "/simulations/{simulationId}/datacenters/{datacenterId}/rooms/{roomId}/tiles/{tileId}", - "/simulations/{simulationId}/datacenters/{datacenterId}/rooms/{roomId}/tiles/{tileId}/cooling-item", - "/simulations/{simulationId}/datacenters/{datacenterId}/rooms/{roomId}/tiles/{tileId}/psu", - "/simulations/{simulationId}/datacenters/{datacenterId}/rooms/{roomId}/tiles/{tileId}/rack", - "/simulations/{simulationId}/datacenters/{datacenterId}/rooms/{roomId}/tiles/{tileId}/rack/machines", - "/simulations/{simulationId}/datacenters/{datacenterId}/rooms/{roomId}/tiles/{tileId}/rack/machines/{position}", + "/datacenters/{datacenterId}", + "/datacenters/{datacenterId}/rooms", + "/rooms/{roomId}", + "/rooms/{roomId}/tiles", + "/tiles/{tileId}", + "/tiles/{tileId}/cooling-item", + "/tiles/{tileId}/psu", + "/tiles/{tileId}/rack", + "/tiles/{tileId}/rack/machines", + "/tiles/{tileId}/rack/machines/{position}", "/simulations/{simulationId}/experiments", - "/simulations/{simulationId}/experiments/{experimentId}", - "/simulations/{simulationId}/experiments/{experimentId}/last-simulated-tick", - "/simulations/{simulationId}/experiments/{experimentId}/machine-states", - "/simulations/{simulationId}/experiments/{experimentId}/rack-states", - "/simulations/{simulationId}/experiments/{experimentId}/room-states", - "/simulations/{simulationId}/experiments/{experimentId}/task-states", + "/experiments/{experimentId}", + "/experiments/{experimentId}/last-simulated-tick", + "/experiments/{experimentId}/machine-states", + "/experiments/{experimentId}/rack-states", + "/experiments/{experimentId}/room-states", + "/experiments/{experimentId}/task-states", "/simulations/{simulationId}/paths", - "/simulations/{simulationId}/paths/{pathId}", - "/simulations/{simulationId}/paths/{pathId}/branches", - "/simulations/{simulationId}/paths/{pathId}/sections", - "/simulations/{simulationId}/paths/{pathId}/sections/{sectionId}", + "/paths/{pathId}", + "/paths/{pathId}/branches", + "/paths/{pathId}/sections", + "/sections/{sectionId}", "/schedulers", "/traces", "/traces/{traceId}", "/traces/{traceId}/jobs", - "/traces/{traceId}/jobs/{jobId}", - "/traces/{traceId}/jobs/{jobId}/tasks", + "/jobs/{jobId}", + "/jobs/{jobId}/tasks", "/room-types", "/room-types/{name}/allowed-objects", "/specifications/cooling-items", diff --git a/opendc/api/v1/paths/__init__.py b/opendc/api/v1/paths/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/paths/pathId/__init__.py b/opendc/api/v1/paths/pathId/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/paths/pathId/branches/__init__.py b/opendc/api/v1/paths/pathId/branches/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/paths/pathId/branches/endpoint.py b/opendc/api/v1/paths/pathId/branches/endpoint.py new file mode 100644 index 00000000..2ac4236a --- /dev/null +++ b/opendc/api/v1/paths/pathId/branches/endpoint.py @@ -0,0 +1,165 @@ +from datetime import datetime + +from opendc.models.datacenter import Datacenter +from opendc.models.machine import Machine +from opendc.models.object import Object +from opendc.models.path import Path +from opendc.models.rack import Rack +from opendc.models.room import Room +from opendc.models.section import Section +from opendc.models.tile import Tile +from opendc.util import database, exceptions +from opendc.util.rest import Request, Response + +def POST(request): + """Create a new Path that branches off of this Path at the specified tick.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'pathId': 'int' + }, + body = { + 'section': { + 'startTick': 'int' + } + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate the current Path from the database + + current_path = Path.from_primary_key((request.params_path['pathId'],)) + + # Make sure the current Path exists + + if not current_path.exists(): + return Response(404, '{} not found.'.format(current_path)) + + # Make sure this user is authorized to branch off the current Path + + if not current_path.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from branching off {}.'.format(current_path)) + + # Create the new Path + + new_path = Path( + simulation_id = current_path.simulation_id, + datetime_created = database.datetime_to_string(datetime.now()) + ) + + new_path.insert() + + # Get the current Path's sections and add them to the new Path if they're before the branch + + current_sections = Section.query('path_id', current_path.id) + last_section = None + + for current_section in current_sections: + + if current_section.start_tick < request.params_body['section']['startTick'] or current_section.start_tick == 0: + + new_section = Section( + path_id = new_path.id, + datacenter_id = current_section.datacenter_id, + start_tick = current_section.start_tick + ) + + new_section.insert() + + last_section = current_section + + # Make a deep copy of the last section's datacenter, its rooms, their tiles, etc. + + path_parameters = { + 'simulationId': new_path.simulation_id + } + + # Copy the Datacenter + + old_datacenter = Datacenter.from_primary_key((last_section.datacenter_id,)) + + message = old_datacenter.generate_api_call(path_parameters, request.token) + response = Request(message).process() + + path_parameters['datacenterId'] = response.content['id'] + + # Create the new last Section, with the IDs of the new Path and new Datacenter + + if last_section.start_tick != 0: + new_section = Section( + path_id = new_path.id, + datacenter_id = path_parameters['datacenterId'], + start_tick = request.params_body['section']['startTick'] + ) + + new_section.insert() + + else: + last_section.datacenter_id = path_parameters['datacenterId'] + last_section.update() + + # Copy the rest of the Datacenter, starting with the Rooms... + + old_rooms = Room.query('datacenter_id', old_datacenter.id) + + for old_room in old_rooms: + + old_room.datacenter_id = path_parameters['datacenterId'] + + message = old_room.generate_api_call(path_parameters, request.token) + response = Request(message).process() + + path_parameters['roomId'] = response.content['id'] + + # ... then the Tiles, ... + + old_tiles = Tile.query('room_id', old_room.id) + + for old_tile in old_tiles: + + old_tile.room_id = path_parameters['roomId'] + + message = old_tile.generate_api_call(path_parameters, request.token) + response = Request(message).process() + + path_parameters['tileId'] = response.content['id'] + + old_objects = Object.query('id', old_tile.object_id) + + # ... then the Tile's Rack, ... + + if len(old_objects) == 1 and old_objects[0].type == 'RACK': + + old_rack = Rack.query('id', old_objects[0].id)[0] + + message = old_rack.generate_api_call(path_parameters, request.token) + response = Request(message).process() + + path_parameters['rackId'] = response.content['id'] + + # ... then the Rack's Machines ... + + old_machines = Machine.query('rack_id', old_rack.id) + + for old_machine in old_machines: + + old_machine.read() + old_machine.rack_id = path_parameters['rackId'] + + message = old_machine.generate_api_call(path_parameters, request.token) + response = Request(message).process() + + path_parameters['machineId'] = response.content['id'] + + # Return the new Path + + return Response( + 200, + 'Successfully created {}.'.format(new_path), + new_path.to_JSON() + ) diff --git a/opendc/api/v1/paths/pathId/endpoint.py b/opendc/api/v1/paths/pathId/endpoint.py new file mode 100644 index 00000000..470063d2 --- /dev/null +++ b/opendc/api/v1/paths/pathId/endpoint.py @@ -0,0 +1,43 @@ +from opendc.models.path import Path +from opendc.models.simulation import Simulation +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Path.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'pathId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Path from the database + + path = Path.from_primary_key((request.params_path['pathId'],)) + + # Make sure this Path exists + + if not path.exists(): + return Response(404, '{} not found.'.format(path)) + + # Make sure this user is authorized to view this Path + + if not path.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from retrieving {}.'.format(path)) + + # Return this Path + + path.read() + + return Response( + 200, + 'Successfully retrieved {}.'.format(path), + path.to_JSON() + ) diff --git a/opendc/api/v1/paths/pathId/sections/__init__.py b/opendc/api/v1/paths/pathId/sections/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/paths/pathId/sections/endpoint.py b/opendc/api/v1/paths/pathId/sections/endpoint.py new file mode 100644 index 00000000..5b08863b --- /dev/null +++ b/opendc/api/v1/paths/pathId/sections/endpoint.py @@ -0,0 +1,42 @@ +from opendc.models.section import Section +from opendc.models.path import Path +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Path's Sections.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'pathId': 'int' + } + ) + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Path from the database + + path = Path.from_primary_key((request.params_path['pathId'],)) + + # Make sure this Path exists + + if not path.exists(): + return Response(404, '{} not found.'.format(path)) + + # Make sure this user is authorized to view this Path's Sections + + if not path.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from viewing Sections for {}.'.format(path)) + + # Get and return the Sections + + sections = Section.query('path_id', request.params_path['pathId']) + + return Response( + 200, + 'Successfully retrieved Sections for {}.'.format(path), + [x.to_JSON() for x in sections] + ) diff --git a/opendc/api/v1/rooms/__init__.py b/opendc/api/v1/rooms/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/rooms/roomId/__init__.py b/opendc/api/v1/rooms/roomId/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/rooms/roomId/endpoint.py b/opendc/api/v1/rooms/roomId/endpoint.py new file mode 100644 index 00000000..6d7ab261 --- /dev/null +++ b/opendc/api/v1/rooms/roomId/endpoint.py @@ -0,0 +1,134 @@ +from opendc.models.room import Room +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Room.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'roomId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Room from the database + + room = Room.from_primary_key((request.params_path['roomId'],)) + + # Make sure this Room exists + + if not room.exists(): + return Response(404, '{} not found.'.format(room)) + + # Make sure this user is authorized to view this Room + + if not room.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from retrieving {}.'.format(room)) + + # Return this Room + + room.read() + + return Response( + 200, + 'Successfully retrieved {}.'.format(room), + room.to_JSON() + ) + +def PUT(request): + """Update this Room's name and type.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'roomId': 'int' + }, + body = { + 'room': { + 'name': 'string', + 'roomType': 'string' + } + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Room from the database + + room = Room.from_primary_key((request.params_path['roomId'],)) + + # Make sure this Room exists + + if not room.exists(): + return Response(404, '{} not found.'.format(room)) + + # Make sure this user is authorized to edit this Room + + if not room.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from updating {}.'.format(room)) + + # Update this Room + + room.name = request.params_body['room']['name'] + room.type = request.params_body['room']['roomType'] + + try: + room.update() + except exceptions.ForeignKeyError: + return Response(400, 'Invalid `roomType` or existing `name`.') + + # Return this Room + + return Response( + 200, + 'Successfully updated {}.'.format(room), + room.to_JSON() + ) + +def DELETE(request): + """Delete this Room.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'roomId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Room and make sure it exists + + room = Room.from_primary_key((request.params_path['roomId'],)) + + if not room.exists(): + return Response(404, '{} not found.'.format(room)) + + # Make sure this user is authorized to delete this Room + + if not room.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from deleting {}.'.format(room)) + + # Delete this Room + + room.delete() + + # Return this Room + + return Response( + 200, + 'Sucessfully deleted {}.'.format(room), + room.to_JSON() + ) diff --git a/opendc/api/v1/rooms/roomId/tiles/__init__.py b/opendc/api/v1/rooms/roomId/tiles/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/rooms/roomId/tiles/endpoint.py b/opendc/api/v1/rooms/roomId/tiles/endpoint.py new file mode 100644 index 00000000..9d17c644 --- /dev/null +++ b/opendc/api/v1/rooms/roomId/tiles/endpoint.py @@ -0,0 +1,119 @@ +from opendc.models.tile import Tile +from opendc.models.room import Room +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Room's Tiles.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'roomId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Room from the database + + room = Room.from_primary_key((request.params_path['roomId'],)) + + # Make sure this Room exists + + if not room.exists(): + return Response(404, '{} not found.'.format(room)) + + # Make sure this user is authorized to view this Room's Tiles + + if not room.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from viewing Tiles for {}.'.format(room)) + + # Get and return the Tiles + + tiles = Tile.query('room_id', room.id) + + for tile in tiles: + tile.read() + + return Response( + 200, + 'Successfully retrieved Tiles for {}.'.format(room), + [x.to_JSON() for x in tiles] + ) + +def POST(request): + """Add a Tile.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'roomId': 'int' + }, + body = { + 'tile': { + 'roomId': 'int', + 'positionX': 'int', + 'positionY': 'int' + } + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + if request.params_path['roomId'] != request.params_body['tile']['roomId']: + return Response(400, 'ID mismatch') + + # Instantiate a Room from the database + + room = Room.from_primary_key((request.params_path['roomId'],)) + + # Make sure this Room exists + + if not room.exists(): + return Response(404, '{} not found.'.format(room)) + + # Make sure this user is authorized to edit this Room's Tiles + + if not room.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from adding Tiles to {}.'.format(room)) + + # Clean the tile JSON + + tile_json = request.params_body['tile'] + + tile_json['objectId'] = None + tile_json['objectType'] = None + + # Instantiate a Tile + + tile = Tile.from_JSON(tile_json) + + # Try to insert this Tile + + try: + tile.insert() + + except exceptions.ForeignKeyError as e: + + if e.message == 'OccupiedTilePosition': + return Response(409, 'Tile position occupied.') + + elif e.message == 'InvalidTilePosition': + return Response(400, 'Invalid Tile position (new Tiles must neighbor existing Tiles).') + + # Return this Tile + + tile.read() + + return Response( + 200, + 'Successfully added {}.'.format(tile), + tile.to_JSON() + ) diff --git a/opendc/api/v1/sections/__init__.py b/opendc/api/v1/sections/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/sections/sectionId/__init__.py b/opendc/api/v1/sections/sectionId/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/sections/sectionId/endpoint.py b/opendc/api/v1/sections/sectionId/endpoint.py new file mode 100644 index 00000000..48e724bd --- /dev/null +++ b/opendc/api/v1/sections/sectionId/endpoint.py @@ -0,0 +1,42 @@ +from opendc.models.section import Section +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Path's Sections.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'sectionId': 'int' + } + ) + except exceptions.ParameterError as e: + return Response(400, e.message) + + + # Instantiate a Section from the database + + section = Section.from_primary_key((request.params_path['sectionId'],)) + + # Make sure this Section exists + + if not section.exists(): + return Response(404, '{} not found.'.format(section)) + + # Make sure this user is authorized to view this Section + + if not section.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from viewing {}.'.format(section)) + + # Return the Section + + section.read() + + return Response( + 200, + 'Successfully retrieved {}.'.format(section), + section.to_JSON() + ) diff --git a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/__init__.py b/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/endpoint.py b/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/endpoint.py deleted file mode 100644 index c9c78a8a..00000000 --- a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/endpoint.py +++ /dev/null @@ -1,43 +0,0 @@ -from opendc.models.datacenter import Datacenter -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Datacenter.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Datacenter from the database - - datacenter = Datacenter.from_primary_key((request.params_path['datacenterId'],)) - - # Make sure this Datacenter exists - - if not datacenter.exists(): - return Response(404, '{} not found.'.format(datacenter)) - - # Make sure this user is authorized to view this Datacenter - - if not datacenter.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from retrieving {}.'.format(datacenter)) - - # Return this Datacenter - - datacenter.read() - - return Response( - 200, - 'Successfully retrieved {}.'.format(datacenter), - datacenter.to_JSON() - ) diff --git a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/__init__.py b/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/endpoint.py b/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/endpoint.py deleted file mode 100644 index 1127ce95..00000000 --- a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/endpoint.py +++ /dev/null @@ -1,111 +0,0 @@ -from opendc.models.room import Room -from opendc.models.datacenter import Datacenter -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Datacenter's Rooms.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int' - } - ) - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Datacenter from the database - - datacenter = Datacenter.from_primary_key((request.params_path['datacenterId'],)) - - # Make sure this Datacenter exists - - if not datacenter.exists(): - return Response(404, '{} not found.'.format(datacenter)) - - # Make sure this user is authorized to view this Datacenter's Rooms - - if not datacenter.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing Rooms for {}.'.format(datacenter)) - - # Get and return the Rooms - - rooms = Room.query('datacenter_id', datacenter.id) - - return Response( - 200, - 'Successfully retrieved Rooms for {}.'.format(datacenter), - [x.to_JSON() for x in rooms] - ) - -def POST(request): - """Add a Room.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int' - }, - body = { - 'room': { - 'id': 'int', - 'datacenterId': 'int', - 'roomType': 'string' - } - } - ) - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Make sure the passed object's datacenter id matches the path datacenter id - - if request.params_path['datacenterId'] != request.params_body['room']['datacenterId']: - return Response(400, 'ID mismatch.') - - # Instantiate a Datacenter from the database - - datacenter = Datacenter.from_primary_key((request.params_path['datacenterId'],)) - - # Make sure this Datacenter exists - - if not datacenter.exists(): - return Response(404, '{} not found.'.format(datacenter)) - - # Make sure this user is authorized to edit this Datacenter's Rooms - - if not datacenter.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from adding a Room to {}.'.format(datacenter)) - - # Add a name if not provided - - if not 'name' in request.params_body['room']: - room_count = len(Room.query('datacenter_id', datacenter.id)) - request.params_body['room']['name'] = 'Room {}'.format(room_count) - - # Instantiate a Room - - room = Room.from_JSON(request.params_body['room']) - - # Try to insert this Room - - try: - room.insert() - except Exception as e: - return Response(400, 'Invalid `roomType` or existing `name`.') - - # Return this Room - - room.read() - - return Response( - 200, - 'Successfully added {}.'.format(room), - room.to_JSON() - ) diff --git a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/__init__.py b/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/endpoint.py b/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/endpoint.py deleted file mode 100644 index 44ed82d6..00000000 --- a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/endpoint.py +++ /dev/null @@ -1,140 +0,0 @@ -from opendc.models.room import Room -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Room.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int', - 'roomId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Room from the database - - room = Room.from_primary_key((request.params_path['roomId'],)) - - # Make sure this Room exists - - if not room.exists(): - return Response(404, '{} not found.'.format(room)) - - # Make sure this user is authorized to view this Room - - if not room.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from retrieving {}.'.format(room)) - - # Return this Room - - room.read() - - return Response( - 200, - 'Successfully retrieved {}.'.format(room), - room.to_JSON() - ) - -def PUT(request): - """Update this Room's name and type.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int', - 'roomId': 'int' - }, - body = { - 'room': { - 'name': 'string', - 'roomType': 'string' - } - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Room from the database - - room = Room.from_primary_key((request.params_path['roomId'],)) - - # Make sure this Room exists - - if not room.exists(): - return Response(404, '{} not found.'.format(room)) - - # Make sure this user is authorized to edit this Room - - if not room.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from updating {}.'.format(room)) - - # Update this Room - - room.name = request.params_body['room']['name'] - room.type = request.params_body['room']['roomType'] - - try: - room.update() - except exceptions.ForeignKeyError: - return Response(400, 'Invalid `roomType` or existing `name`.') - - # Return this Room - - return Response( - 200, - 'Successfully updated {}.'.format(room), - room.to_JSON() - ) - -def DELETE(request): - """Delete this Room.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int', - 'roomId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Room and make sure it exists - - room = Room.from_primary_key((request.params_path['roomId'],)) - - if not room.exists(): - return Response(404, '{} not found.'.format(room)) - - # Make sure this user is authorized to delete this Room - - if not room.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from deleting {}.'.format(room)) - - # Delete this Room - - room.delete() - - # Return this Room - - return Response( - 200, - 'Sucessfully deleted {}.'.format(room), - room.to_JSON() - ) diff --git a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/__init__.py b/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/endpoint.py b/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/endpoint.py deleted file mode 100644 index f615a6d4..00000000 --- a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/endpoint.py +++ /dev/null @@ -1,123 +0,0 @@ -from opendc.models.tile import Tile -from opendc.models.room import Room -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Room's Tiles.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int', - 'roomId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Room from the database - - room = Room.from_primary_key((request.params_path['roomId'],)) - - # Make sure this Room exists - - if not room.exists(): - return Response(404, '{} not found.'.format(room)) - - # Make sure this user is authorized to view this Room's Tiles - - if not room.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing Tiles for {}.'.format(room)) - - # Get and return the Tiles - - tiles = Tile.query('room_id', room.id) - - for tile in tiles: - tile.read() - - return Response( - 200, - 'Successfully retrieved Tiles for {}.'.format(room), - [x.to_JSON() for x in tiles] - ) - -def POST(request): - """Add a Tile.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int', - 'roomId': 'int' - }, - body = { - 'tile': { - 'roomId': 'int', - 'positionX': 'int', - 'positionY': 'int' - } - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - if request.params_path['roomId'] != request.params_body['tile']['roomId']: - return Response(400, 'ID mismatch') - - # Instantiate a Room from the database - - room = Room.from_primary_key((request.params_path['roomId'],)) - - # Make sure this Room exists - - if not room.exists(): - return Response(404, '{} not found.'.format(room)) - - # Make sure this user is authorized to edit this Room's Tiles - - if not room.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from adding Tiles to {}.'.format(room)) - - # Clean the tile JSON - - tile_json = request.params_body['tile'] - - tile_json['objectId'] = None - tile_json['objectType'] = None - - # Instantiate a Tile - - tile = Tile.from_JSON(tile_json) - - # Try to insert this Tile - - try: - tile.insert() - - except exceptions.ForeignKeyError as e: - - if e.message == 'OccupiedTilePosition': - return Response(409, 'Tile position occupied.') - - elif e.message == 'InvalidTilePosition': - return Response(400, 'Invalid Tile position (new Tiles must neighbor existing Tiles).') - - # Return this Tile - - tile.read() - - return Response( - 200, - 'Successfully added {}.'.format(tile), - tile.to_JSON() - ) diff --git a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/__init__.py b/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/endpoint.py b/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/endpoint.py deleted file mode 100644 index b15a10f7..00000000 --- a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/endpoint.py +++ /dev/null @@ -1,89 +0,0 @@ -from opendc.models.tile import Tile -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Tile.""" - - # Make sure request parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int', - 'roomId': 'int', - 'tileId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Tile from the database - - tile = Tile.from_primary_key((request.params_path['tileId'],)) - - # Make sure this Tile exists - - if not tile.exists(): - return Response(404, '{} not found.'.format(tile)) - - # Make sure this user is authorized to view this Tile - - if not tile.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from retrieving {}.'.format(tile)) - - # Return this Tile - - tile.read() - - return Response( - 200, - 'Successfully retrieved {}.'.format(tile), - tile.to_JSON() - ) - -def DELETE(request): - """Delete this Tile.""" - - # Make sure request parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int', - 'roomId': 'int', - 'tileId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Tile from the database - - tile = Tile.from_primary_key((request.params_path['tileId'],)) - - # Make sure this Tile exists - - if not tile.exists(): - return Response(404, '{} not found.'.format(tile)) - - # Make sure this user is authorized to edit this Tile - - if not tile.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from deleting {}.'.format(tile)) - - # Delete this Tile - - tile.delete() - - # Return this Tile - - return Response( - 200, - 'Successfully deleted {}.'.format(tile), - tile.to_JSON() - ) diff --git a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/__init__.py b/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/endpoint.py b/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/endpoint.py deleted file mode 100644 index d9fb1959..00000000 --- a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/endpoint.py +++ /dev/null @@ -1,245 +0,0 @@ -from opendc.models.rack import Rack -from opendc.models.tile import Tile -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Tile's Rack.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int', - 'roomId': 'int', - 'tileId': 'int' - }, - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Tile from the database - - tile = Tile.from_primary_key((request.params_path['tileId'],)) - - # Make sure this Tile exists - - if not tile.exists(): - return Response(404, '{} not found.'.format(tile)) - - # Make sure this user is authorized to view this Tile - - if not tile.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from editing {}'.format(tile)) - - # Instantiate a Rack from the database - - rack = Rack.from_primary_key((tile.object_id,)) - - # Make sure this Rack exists - - if not rack.exists(): - return Response(404, '{} not found'.format(rack)) - - # Return the Rack - - rack.read() - - return Response( - 200, - 'Successfully retrieved {}.'.format(rack), - rack.to_JSON() - ) - -def POST(request): - """Add a Rack to this Tile if it is empty.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int', - 'roomId': 'int', - 'tileId': 'int' - }, - body = { - 'rack': { - 'name': 'string', - 'capacity': 'int', - 'powerCapacityW': 'int' - } - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Tile from the database - - tile = Tile.from_primary_key((request.params_path['tileId'],)) - - # Make sure this Tile exists - - if not tile.exists(): - return Response(404, '{} not found.'.format(tile)) - - # Make sure this user is authorized to edit this Tile - - if not tile.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from editing {}'.format(tile)) - - # Make sure this Tile isn't occupied - - if tile.object_id is not None: - return Response(409, '{} occupied.'.format(tile)) - - # Instantiate a Rack and insert it into the database - - rack = Rack.from_JSON(request.params_body['rack']) - rack.insert() - - # Try to add this Rack to this Tile - - tile.object_id = rack.id - tile.object_type = 'RACK' - tile.update() - - # Return this Rack - - rack.read() - - return Response( - 200, - 'Successfully added {}.'.format(rack), - rack.to_JSON() - ) - -def PUT(request): - """Update the Rack on this Tile.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int', - 'roomId': 'int', - 'tileId': 'int' - }, - body = { - 'rack': { - 'name': 'string', - 'capacity': 'int', - 'powerCapacityW': 'int' - } - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Tile from the database - - tile = Tile.from_primary_key((request.params_path['tileId'],)) - - # Make sure this Tile exists - - if not tile.exists(): - return Response(404, '{} not found.'.format(tile)) - - # Make sure this user is authorized to edit this Tile - - if not tile.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from editing {}'.format(tile)) - - # Instantiate a Rack from the database - - rack = Rack.from_primary_key((tile.object_id,)) - - # Make sure this Rack exists - - if not rack.exists(): - return Response(404, '{} not found'.format(rack)) - - # Update this Rack - - rack.name = request.params_body['rack']['name'] - rack.capacity = request.params_body['rack']['capacity'] - - rack.update() - - # Return this Rack - - rack.read() - - return Response( - 200, - 'Successfully updated {}.'.format(rack), - rack.to_JSON() - ) - -def DELETE(request): - """Delete this Tile's Rack.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int', - 'roomId': 'int', - 'tileId': 'int' - }, - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Tile from the database - - tile = Tile.from_primary_key((request.params_path['tileId'],)) - - # Make sure this Tile exists - - if not tile.exists(): - return Response(404, '{} not found.'.format(tile)) - - # Make sure this user is authorized to edit this Tile - - if not tile.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from editing {}'.format(tile)) - - # Instantiate a Rack from the database - - rack = Rack.from_primary_key((tile.object_id,)) - - # Make sure this Rack exists - - if not rack.exists(): - return Response(404, '{} not found'.format(rack)) - - # Remove this Rack from this Tile - - tile.object_id = None - tile.object_type = None - - tile.update() - - # Delete this Rack - - rack.delete() - - # Return this Rack - - return Response( - 200, - 'Successfully deleted {}.'.format(rack), - rack.to_JSON() - ) diff --git a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/machines/__init__.py b/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/machines/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/machines/endpoint.py b/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/machines/endpoint.py deleted file mode 100644 index a67bb1c3..00000000 --- a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/machines/endpoint.py +++ /dev/null @@ -1,122 +0,0 @@ -from opendc.models.machine import Machine -from opendc.models.rack import Rack -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Rack's Machines.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int', - 'roomId': 'int', - 'tileId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Rack from the database - - rack = Rack.from_tile_id(request.params_path['tileId']) - - # Make sure this Rack exists - - if not rack.exists(): - return Response(404, '{} not found.'.format(rack)) - - # Make sure this user is authorized to view this Rack's Machines - - if not rack.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing {}.'.format(rack)) - - # Get and return the Machines - - machines = Machine.query('rack_id', rack.id) - - for machine in machines: - machine.read() - - return Response( - 200, - 'Successfully retrieved Machines for {}.'.format(rack), - [x.to_JSON() for x in machines] - ) - -def POST(request): - """Add a Machine to this rack.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int', - 'roomId': 'int', - 'tileId': 'int' - }, - body = { - 'machine': { - 'rackId': 'int', - 'position': 'int', - 'tags': 'list-string', - 'cpuIds': 'list-int', - 'gpuIds': 'list-int', - 'memoryIds': 'list-int', - 'storageIds': 'list-int' - } - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Rack from the database - - rack = Rack.from_tile_id(request.params_path['tileId']) - - # Make sure this Rack exists - - if not rack.exists(): - return Response(404, '{} not found.'.format(rack)) - - # Make sure this Rack's ID matches the given rack ID - - if rack.id != request.params_body['machine']['rackId']: - return Response(400, 'Rack ID in `machine` and path do not match.') - - # Make sure this user is authorized to edit this Rack's Machines - - if not rack.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing {}.'.format(rack)) - - # Instantiate a Machine - - machine = Machine.from_JSON(request.params_body['machine']) - - # Try to insert this Machine - - try: - machine.insert() - - except exceptions.ForeignKeyError: - return Response(409, 'Rack position occupied.') - - except: - return Response(400, 'Invalid Machine.') - - # Return this Machine - - machine.read() - - return Response( - 200, - 'Successfully added {}.'.format(machine), - machine.to_JSON() - ) diff --git a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/machines/position/__init__.py b/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/machines/position/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/machines/position/endpoint.py b/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/machines/position/endpoint.py deleted file mode 100644 index e33da5d6..00000000 --- a/opendc/api/v1/simulations/simulationId/datacenters/datacenterId/rooms/roomId/tiles/tileId/rack/machines/position/endpoint.py +++ /dev/null @@ -1,170 +0,0 @@ -from opendc.models.machine import Machine -from opendc.models.rack import Rack -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get the Machine at this location in this Rack.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int', - 'roomId': 'int', - 'tileId': 'int', - 'position': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Machine from the database - - machine = Machine.from_tile_id_and_rack_position(request.params_path['tileId'], request.params_path['position']) - - # Make sure this Machine exists - - if not machine.exists(): - return Response(404, '{} not found.'.format(machine)) - - # Make sure this user is authorized to view this Machine - - if not machine.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from retrieving {}.'.format(machine)) - - # Return this Machine - - machine.read() - - return Response( - 200, - 'Successfully retrieved {}.'.format(machine), - machine.to_JSON() - ) - -def PUT(request): - """Update the Machine at this location in this Rack.""" - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int', - 'roomId': 'int', - 'tileId': 'int', - 'position': 'int' - }, - body = { - 'machine': { - 'rackId': 'int', - 'position': 'int', - 'tags': 'list-string', - 'cpuIds': 'list-int', - 'gpuIds': 'list-int', - 'memoryIds': 'list-int', - 'storageIds': 'list-int' - } - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Machine from the database - - machine = Machine.from_tile_id_and_rack_position(request.params_path['tileId'], request.params_path['position']) - - # Make sure this Machine exists - - if not machine.exists(): - return Response(404, '{} not found.'.format(machine)) - - # Make sure this Machine's rack ID is right - - rack = Rack.from_tile_id(request.params_path['tileId']) - - if not rack.exists() or rack.id != request.params_body['machine']['rackId']: - return Response(400, 'Mismatch in Rack IDs.') - - # Make sure this user is authorized to edit this Machine - - if not machine.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from retrieving {}.'.format(machine)) - - # Update this Machine - - machine.positoin = request.params_body['machine']['position'] - machine.tags = request.params_body['machine']['tags'] - machine.cpu_ids = request.params_body['machine']['cpuIds'] - machine.gpu_ids = request.params_body['machine']['gpuIds'] - machine.memory_ids = request.params_body['machine']['memoryIds'] - machine.storage_ids = request.params_body['machine']['storageIds'] - - try: - machine.update() - - except exceptions.ForeignKeyError: - return Response(409, 'Rack position occupied.') - - except Exception as e: - print e - return Response(400, 'Invalid Machine.') - - # Return this Machine - - machine.read() - - return Response( - 200, - 'Successfully updated {}.'.format(machine), - machine.to_JSON() - ) - -def DELETE(request): - """Delete the Machine at this location in this Rack.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'datacenterId': 'int', - 'roomId': 'int', - 'tileId': 'int', - 'position': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Machine from the database - - machine = Machine.from_tile_id_and_rack_position(request.params_path['tileId'], request.params_path['position']) - - # Make sure this Machine exists - - if not machine.exists(): - return Response(404, '{} not found.'.format(machine)) - - # Make sure this user is authorized to edit this Machine - - if not machine.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from retrieving {}.'.format(machine)) - - # Delete this Machine - - machine.delete() - - # Return this Machine - - return Response( - 200, - 'Successfully deleted {}.'.format(machine), - machine.to_JSON() - ) diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/__init__.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/endpoint.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/endpoint.py deleted file mode 100644 index 7af62b92..00000000 --- a/opendc/api/v1/simulations/simulationId/experiments/experimentId/endpoint.py +++ /dev/null @@ -1,138 +0,0 @@ -from opendc.models.experiment import Experiment -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Experiment.""" - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'experimentId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate an Experiment from the database - - experiment = Experiment.from_primary_key((request.params_path['experimentId'],)) - - # Make sure this Experiment exists - - if not experiment.exists(): - return Response(404, '{} not found.'.format(experiment)) - - # Make sure this user is authorized to view this Experiment - - if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from retrieving {}.'.format(experiment)) - - # Return this Experiment - - experiment.read() - - return Response( - 200, - 'Successfully retrieved {}.'.format(experiment), - experiment.to_JSON() - ) - -def PUT(request): - """Update this Experiment's Path, Trace, Scheduler, and/or name.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'experimentId': 'int' - }, - body = { - 'experiment': { - 'pathId': 'int', - 'traceId': 'int', - 'schedulerName': 'string', - 'name': 'string' - } - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate an Experiment from the database - - experiment = Experiment.from_primary_key((request.params_path['experimentId'],)) - - # Make sure this Experiment exists - - if not experiment.exists(): - return Response(404, '{} not found.'.format(experiment)) - - # Make sure this user is authorized to edit this Experiment - - if not experiment.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from updating {}.'.format(experiment)) - - # Update this Experiment - - experiment.path_id = request.params_body['experiment']['pathId'] - experiment.trace_id = request.params_body['experiment']['traceId'] - experiment.scheduler_name = request.params_body['experiment']['schedulerName'] - experiment.name = request.params_body['experiment']['name'] - - try: - experiment.update() - - except exceptions.ForeignKeyError: - return Response(400, 'Foreign key error.') - - # Return this Experiment - - return Response( - 200, - 'Successfully updated {}.'.format(experiment), - experiment.to_JSON() - ) - -def DELETE(request): - """Delete this Experiment.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'experimentId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate an Experiment and make sure it exists - - experiment = Experiment.from_primary_key((request.params_path['experimentId'],)) - - if not experiment.exists(): - return Response(404, '{} not found.'.format(experiment)) - - # Make sure this user is authorized to delete this Experiment - - if not experiment.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from deleting {}.'.format(experiment)) - - # Delete and return this Experiment - - experiment.delete() - - return Response( - 200, - 'Successfully deleted {}.'.format(experiment), - experiment.to_JSON() - ) diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/last-simulated-tick/__init__.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/last-simulated-tick/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/last-simulated-tick/endpoint.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/last-simulated-tick/endpoint.py deleted file mode 100644 index d39fafe4..00000000 --- a/opendc/api/v1/simulations/simulationId/experiments/experimentId/last-simulated-tick/endpoint.py +++ /dev/null @@ -1,39 +0,0 @@ -from opendc.models.experiment import Experiment -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Experiment's last simulated tick.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'experimentId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate an Experiment from the database - - experiment = Experiment.from_primary_key((request.params_path['experimentId'],)) - - # Make sure this Experiment exisits - - if not experiment.exists(): - return Response(404, '{} not found.'.format(experiment)) - - # Make sure this user is authorized to view this Experiment's last simulated tick - - if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing last simulated tick for {}.'.format(experiment)) - - return Response( - 200, - 'Successfully retrieved last simulated tick for {}.'.format(experiment), - {'lastSimulatedTick': experiment.last_simulated_tick} - ) diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/machine-states/__init__.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/machine-states/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/machine-states/endpoint.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/machine-states/endpoint.py deleted file mode 100644 index c5ba0abe..00000000 --- a/opendc/api/v1/simulations/simulationId/experiments/experimentId/machine-states/endpoint.py +++ /dev/null @@ -1,51 +0,0 @@ -from opendc.models.experiment import Experiment -from opendc.models.machine_state import MachineState -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Experiment's Machine States.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'experimentId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate an Experiment from the database - - experiment = Experiment.from_primary_key((request.params_path['experimentId'],)) - - # Make sure this Experiment exisits - - if not experiment.exists(): - return Response(404, '{} not found.'.format(experiment)) - - # Make sure this user is authorized to view this Experiment's Machine States - - if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing Machine States for {}.'.format(experiment)) - - # Get and return the Machine States - - if 'tick' in request.params_query: - machine_states = MachineState.from_experiment_id_and_tick( - request.params_path['experimentId'], - request.params_query['tick'] - ) - - else: - machine_states = MachineState.from_experiment_id(request.params_path['experimentId']) - - return Response( - 200, - 'Successfully retrieved Machine States for {}.'.format(experiment), - [x.to_JSON() for x in machine_states] - ) diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/rack-states/__init__.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/rack-states/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/rack-states/endpoint.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/rack-states/endpoint.py deleted file mode 100644 index 6e71c496..00000000 --- a/opendc/api/v1/simulations/simulationId/experiments/experimentId/rack-states/endpoint.py +++ /dev/null @@ -1,51 +0,0 @@ -from opendc.models.experiment import Experiment -from opendc.models.rack_state import RackState -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Experiment's Tack States.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'experimentId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate an Experiment from the database - - experiment = Experiment.from_primary_key((request.params_path['experimentId'],)) - - # Make sure this Experiment exisits - - if not experiment.exists(): - return Response(404, '{} not found.'.format(experiment)) - - # Make sure this user is authorized to view this Experiment's Rack States - - if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing Rack States for {}.'.format(experiment)) - - # Get and return the Rack States - - if 'tick' in request.params_query: - rack_states = RackState.from_experiment_id_and_tick( - request.params_path['experimentId'], - request.params_query['tick'] - ) - - else: - rack_states = RackState.from_experiment_id(request.params_path['experimentId']) - - return Response( - 200, - 'Successfully retrieved Rack States for {}.'.format(experiment), - [x.to_JSON() for x in rack_states] - ) diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/room-states/__init__.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/room-states/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/room-states/endpoint.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/room-states/endpoint.py deleted file mode 100644 index d8c9fb45..00000000 --- a/opendc/api/v1/simulations/simulationId/experiments/experimentId/room-states/endpoint.py +++ /dev/null @@ -1,51 +0,0 @@ -from opendc.models.experiment import Experiment -from opendc.models.room_state import RoomState -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Experiment's Room States.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'experimentId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate an Experiment from the database - - experiment = Experiment.from_primary_key((request.params_path['experimentId'],)) - - # Make sure this Experiment exisits - - if not experiment.exists(): - return Response(404, '{} not found.'.format(experiment)) - - # Make sure this user is authorized to view this Experiment's Room States - - if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing Room States for {}.'.format(experiment)) - - # Get and return the Room States - - if 'tick' in request.params_query: - room_states = RoomState.from_experiment_id_and_tick( - request.params_path['experimentId'], - request.params_query['tick'] - ) - - else: - room_states = RoomState.from_experiment_id(request.params_path['experimentId']) - - return Response( - 200, - 'Successfully retrieved Room States for {}.'.format(experiment), - [x.to_JSON() for x in room_states] - ) diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/__init__.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/task-durations/__init__.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/task-durations/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/task-durations/endpoint.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/task-durations/endpoint.py deleted file mode 100644 index 1bb60ca2..00000000 --- a/opendc/api/v1/simulations/simulationId/experiments/experimentId/statistics/task-durations/endpoint.py +++ /dev/null @@ -1,44 +0,0 @@ -from opendc.models.experiment import Experiment -from opendc.models.task_duration import TaskDuration -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Experiment's Task Durations.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'experimentId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate an Experiment from the database - - experiment = Experiment.from_primary_key((request.params_path['experimentId'],)) - - # Make sure this Experiment exisits - - if not experiment.exists(): - return Response(404, '{} not found.'.format(experiment)) - - # Make sure this user is authorized to view this Experiment's Task Durations - - if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing Task Durations for {}.'.format(experiment)) - - # Get and return the Task Durations - - task_durations = TaskDuration.from_experiment_id(request.params_path['experimentId']) - - return Response( - 200, - 'Successfully retrieved Task Durations for {}.'.format(experiment), - [x.to_JSON() for x in task_durations] - ) diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/task-states/__init__.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/task-states/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/experiments/experimentId/task-states/endpoint.py b/opendc/api/v1/simulations/simulationId/experiments/experimentId/task-states/endpoint.py deleted file mode 100644 index b11512c0..00000000 --- a/opendc/api/v1/simulations/simulationId/experiments/experimentId/task-states/endpoint.py +++ /dev/null @@ -1,51 +0,0 @@ -from opendc.models.experiment import Experiment -from opendc.models.task_state import TaskState -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Experiment's Task States.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'experimentId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate an Experiment from the database - - experiment = Experiment.from_primary_key((request.params_path['experimentId'],)) - - # Make sure this Experiment exists - - if not experiment.exists(): - return Response(404, '{} not found.'.format(experiment)) - - # Make sure this user is authorized to view Task States for this Experiment - - if not experiment.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing Task States for {}.'.format(experiment)) - - # Get and return the Task States - - if 'tick' in request.params_query: - task_states = TaskState.from_experiment_id_and_tick( - request.params_path['experimentId'], - request.params_query['tick'] - ) - - else: - task_states = TaskState.query('experiment_id', request.params_path['experimentId']) - - return Response( - 200, - 'Successfully retrieved Task States for {}.'.format(experiment), - [x.to_JSON() for x in task_states] - ) diff --git a/opendc/api/v1/simulations/simulationId/paths/pathId/__init__.py b/opendc/api/v1/simulations/simulationId/paths/pathId/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/paths/pathId/branches/__init__.py b/opendc/api/v1/simulations/simulationId/paths/pathId/branches/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/paths/pathId/branches/endpoint.py b/opendc/api/v1/simulations/simulationId/paths/pathId/branches/endpoint.py deleted file mode 100644 index 2aa34238..00000000 --- a/opendc/api/v1/simulations/simulationId/paths/pathId/branches/endpoint.py +++ /dev/null @@ -1,166 +0,0 @@ -from datetime import datetime - -from opendc.models.datacenter import Datacenter -from opendc.models.machine import Machine -from opendc.models.object import Object -from opendc.models.path import Path -from opendc.models.rack import Rack -from opendc.models.room import Room -from opendc.models.section import Section -from opendc.models.tile import Tile -from opendc.util import database, exceptions -from opendc.util.rest import Request, Response - -def POST(request): - """Create a new Path that branches off of this Path at the specified tick.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'pathId': 'int' - }, - body = { - 'section': { - 'startTick': 'int' - } - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate the current Path from the database - - current_path = Path.from_primary_key((request.params_path['pathId'],)) - - # Make sure the current Path exists - - if not current_path.exists(): - return Response(404, '{} not found.'.format(current_path)) - - # Make sure this user is authorized to branch off the current Path - - if not current_path.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from branching off {}.'.format(current_path)) - - # Create the new Path - - new_path = Path( - simulation_id = current_path.simulation_id, - datetime_created = database.datetime_to_string(datetime.now()) - ) - - new_path.insert() - - # Get the current Path's sections and add them to the new Path if they're before the branch - - current_sections = Section.query('path_id', current_path.id) - last_section = None - - for current_section in current_sections: - - if current_section.start_tick < request.params_body['section']['startTick'] or current_section.start_tick == 0: - - new_section = Section( - path_id = new_path.id, - datacenter_id = current_section.datacenter_id, - start_tick = current_section.start_tick - ) - - new_section.insert() - - last_section = current_section - - # Make a deep copy of the last section's datacenter, its rooms, their tiles, etc. - - path_parameters = { - 'simulationId': new_path.simulation_id - } - - # Copy the Datacenter - - old_datacenter = Datacenter.from_primary_key((last_section.datacenter_id,)) - - message = old_datacenter.generate_api_call(path_parameters, request.token) - response = Request(message).process() - - path_parameters['datacenterId'] = response.content['id'] - - # Create the new last Section, with the IDs of the new Path and new Datacenter - - if last_section.start_tick != 0: - new_section = Section( - path_id = new_path.id, - datacenter_id = path_parameters['datacenterId'], - start_tick = request.params_body['section']['startTick'] - ) - - new_section.insert() - - else: - last_section.datacenter_id = path_parameters['datacenterId'] - last_section.update() - - # Copy the rest of the Datacenter, starting with the Rooms... - - old_rooms = Room.query('datacenter_id', old_datacenter.id) - - for old_room in old_rooms: - - old_room.datacenter_id = path_parameters['datacenterId'] - - message = old_room.generate_api_call(path_parameters, request.token) - response = Request(message).process() - - path_parameters['roomId'] = response.content['id'] - - # ... then the Tiles, ... - - old_tiles = Tile.query('room_id', old_room.id) - - for old_tile in old_tiles: - - old_tile.room_id = path_parameters['roomId'] - - message = old_tile.generate_api_call(path_parameters, request.token) - response = Request(message).process() - - path_parameters['tileId'] = response.content['id'] - - old_objects = Object.query('id', old_tile.object_id) - - # ... then the Tile's Rack, ... - - if len(old_objects) == 1 and old_objects[0].type == 'RACK': - - old_rack = Rack.query('id', old_objects[0].id)[0] - - message = old_rack.generate_api_call(path_parameters, request.token) - response = Request(message).process() - - path_parameters['rackId'] = response.content['id'] - - # ... then the Rack's Machines ... - - old_machines = Machine.query('rack_id', old_rack.id) - - for old_machine in old_machines: - - old_machine.read() - old_machine.rack_id = path_parameters['rackId'] - - message = old_machine.generate_api_call(path_parameters, request.token) - response = Request(message).process() - - path_parameters['machineId'] = response.content['id'] - - # Return the new Path - - return Response( - 200, - 'Successfully created {}.'.format(new_path), - new_path.to_JSON() - ) diff --git a/opendc/api/v1/simulations/simulationId/paths/pathId/endpoint.py b/opendc/api/v1/simulations/simulationId/paths/pathId/endpoint.py deleted file mode 100644 index 71d97a67..00000000 --- a/opendc/api/v1/simulations/simulationId/paths/pathId/endpoint.py +++ /dev/null @@ -1,44 +0,0 @@ -from opendc.models.path import Path -from opendc.models.simulation import Simulation -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Path.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'pathId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Path from the database - - path = Path.from_primary_key((request.params_path['pathId'],)) - - # Make sure this Path exists - - if not path.exists(): - return Response(404, '{} not found.'.format(path)) - - # Make sure this user is authorized to view this Path - - if not path.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from retrieving {}.'.format(path)) - - # Return this Path - - path.read() - - return Response( - 200, - 'Successfully retrieved {}.'.format(path), - path.to_JSON() - ) diff --git a/opendc/api/v1/simulations/simulationId/paths/pathId/sections/__init__.py b/opendc/api/v1/simulations/simulationId/paths/pathId/sections/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/paths/pathId/sections/endpoint.py b/opendc/api/v1/simulations/simulationId/paths/pathId/sections/endpoint.py deleted file mode 100644 index 775de346..00000000 --- a/opendc/api/v1/simulations/simulationId/paths/pathId/sections/endpoint.py +++ /dev/null @@ -1,43 +0,0 @@ -from opendc.models.section import Section -from opendc.models.path import Path -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Path's Sections.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'pathId': 'int' - } - ) - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Path from the database - - path = Path.from_primary_key((request.params_path['pathId'],)) - - # Make sure this Path exists - - if not path.exists(): - return Response(404, '{} not found.'.format(path)) - - # Make sure this user is authorized to view this Path's Sections - - if not path.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing Sections for {}.'.format(path)) - - # Get and return the Sections - - sections = Section.query('path_id', request.params_path['pathId']) - - return Response( - 200, - 'Successfully retrieved Sections for {}.'.format(path), - [x.to_JSON() for x in sections] - ) diff --git a/opendc/api/v1/simulations/simulationId/paths/pathId/sections/sectionId/__init__.py b/opendc/api/v1/simulations/simulationId/paths/pathId/sections/sectionId/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/paths/pathId/sections/sectionId/endpoint.py b/opendc/api/v1/simulations/simulationId/paths/pathId/sections/sectionId/endpoint.py deleted file mode 100644 index 9cd397d0..00000000 --- a/opendc/api/v1/simulations/simulationId/paths/pathId/sections/sectionId/endpoint.py +++ /dev/null @@ -1,44 +0,0 @@ -from opendc.models.section import Section -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Path's Sections.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'simulationId': 'int', - 'pathId': 'int', - 'sectionId': 'int' - } - ) - except exceptions.ParameterError as e: - return Response(400, e.message) - - - # Instantiate a Section from the database - - section = Section.from_primary_key((request.params_path['sectionId'],)) - - # Make sure this Section exists - - if not section.exists(): - return Response(404, '{} not found.'.format(section)) - - # Make sure this user is authorized to view this Section - - if not section.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing {}.'.format(section)) - - # Return the Section - - section.read() - - return Response( - 200, - 'Successfully retrieved {}.'.format(section), - section.to_JSON() - ) diff --git a/opendc/api/v1/tiles/__init__.py b/opendc/api/v1/tiles/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/tiles/tileId/__init__.py b/opendc/api/v1/tiles/tileId/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/tiles/tileId/endpoint.py b/opendc/api/v1/tiles/tileId/endpoint.py new file mode 100644 index 00000000..deb76195 --- /dev/null +++ b/opendc/api/v1/tiles/tileId/endpoint.py @@ -0,0 +1,83 @@ +from opendc.models.tile import Tile +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Tile.""" + + # Make sure request parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Tile from the database + + tile = Tile.from_primary_key((request.params_path['tileId'],)) + + # Make sure this Tile exists + + if not tile.exists(): + return Response(404, '{} not found.'.format(tile)) + + # Make sure this user is authorized to view this Tile + + if not tile.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from retrieving {}.'.format(tile)) + + # Return this Tile + + tile.read() + + return Response( + 200, + 'Successfully retrieved {}.'.format(tile), + tile.to_JSON() + ) + +def DELETE(request): + """Delete this Tile.""" + + # Make sure request parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Tile from the database + + tile = Tile.from_primary_key((request.params_path['tileId'],)) + + # Make sure this Tile exists + + if not tile.exists(): + return Response(404, '{} not found.'.format(tile)) + + # Make sure this user is authorized to edit this Tile + + if not tile.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from deleting {}.'.format(tile)) + + # Delete this Tile + + tile.delete() + + # Return this Tile + + return Response( + 200, + 'Successfully deleted {}.'.format(tile), + tile.to_JSON() + ) diff --git a/opendc/api/v1/tiles/tileId/rack/__init__.py b/opendc/api/v1/tiles/tileId/rack/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/tiles/tileId/rack/endpoint.py b/opendc/api/v1/tiles/tileId/rack/endpoint.py new file mode 100644 index 00000000..59338e9a --- /dev/null +++ b/opendc/api/v1/tiles/tileId/rack/endpoint.py @@ -0,0 +1,233 @@ +from opendc.models.rack import Rack +from opendc.models.tile import Tile +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Tile's Rack.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int' + }, + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Tile from the database + + tile = Tile.from_primary_key((request.params_path['tileId'],)) + + # Make sure this Tile exists + + if not tile.exists(): + return Response(404, '{} not found.'.format(tile)) + + # Make sure this user is authorized to view this Tile + + if not tile.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from editing {}'.format(tile)) + + # Instantiate a Rack from the database + + rack = Rack.from_primary_key((tile.object_id,)) + + # Make sure this Rack exists + + if not rack.exists(): + return Response(404, '{} not found'.format(rack)) + + # Return the Rack + + rack.read() + + return Response( + 200, + 'Successfully retrieved {}.'.format(rack), + rack.to_JSON() + ) + +def POST(request): + """Add a Rack to this Tile if it is empty.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int' + }, + body = { + 'rack': { + 'name': 'string', + 'capacity': 'int', + 'powerCapacityW': 'int' + } + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Tile from the database + + tile = Tile.from_primary_key((request.params_path['tileId'],)) + + # Make sure this Tile exists + + if not tile.exists(): + return Response(404, '{} not found.'.format(tile)) + + # Make sure this user is authorized to edit this Tile + + if not tile.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from editing {}'.format(tile)) + + # Make sure this Tile isn't occupied + + if tile.object_id is not None: + return Response(409, '{} occupied.'.format(tile)) + + # Instantiate a Rack and insert it into the database + + rack = Rack.from_JSON(request.params_body['rack']) + rack.insert() + + # Try to add this Rack to this Tile + + tile.object_id = rack.id + tile.object_type = 'RACK' + tile.update() + + # Return this Rack + + rack.read() + + return Response( + 200, + 'Successfully added {}.'.format(rack), + rack.to_JSON() + ) + +def PUT(request): + """Update the Rack on this Tile.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int' + }, + body = { + 'rack': { + 'name': 'string', + 'capacity': 'int', + 'powerCapacityW': 'int' + } + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Tile from the database + + tile = Tile.from_primary_key((request.params_path['tileId'],)) + + # Make sure this Tile exists + + if not tile.exists(): + return Response(404, '{} not found.'.format(tile)) + + # Make sure this user is authorized to edit this Tile + + if not tile.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from editing {}'.format(tile)) + + # Instantiate a Rack from the database + + rack = Rack.from_primary_key((tile.object_id,)) + + # Make sure this Rack exists + + if not rack.exists(): + return Response(404, '{} not found'.format(rack)) + + # Update this Rack + + rack.name = request.params_body['rack']['name'] + rack.capacity = request.params_body['rack']['capacity'] + + rack.update() + + # Return this Rack + + rack.read() + + return Response( + 200, + 'Successfully updated {}.'.format(rack), + rack.to_JSON() + ) + +def DELETE(request): + """Delete this Tile's Rack.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int' + }, + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Tile from the database + + tile = Tile.from_primary_key((request.params_path['tileId'],)) + + # Make sure this Tile exists + + if not tile.exists(): + return Response(404, '{} not found.'.format(tile)) + + # Make sure this user is authorized to edit this Tile + + if not tile.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from editing {}'.format(tile)) + + # Instantiate a Rack from the database + + rack = Rack.from_primary_key((tile.object_id,)) + + # Make sure this Rack exists + + if not rack.exists(): + return Response(404, '{} not found'.format(rack)) + + # Remove this Rack from this Tile + + tile.object_id = None + tile.object_type = None + + tile.update() + + # Delete this Rack + + rack.delete() + + # Return this Rack + + return Response( + 200, + 'Successfully deleted {}.'.format(rack), + rack.to_JSON() + ) diff --git a/opendc/api/v1/tiles/tileId/rack/machines/__init__.py b/opendc/api/v1/tiles/tileId/rack/machines/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/tiles/tileId/rack/machines/endpoint.py b/opendc/api/v1/tiles/tileId/rack/machines/endpoint.py new file mode 100644 index 00000000..2d160713 --- /dev/null +++ b/opendc/api/v1/tiles/tileId/rack/machines/endpoint.py @@ -0,0 +1,116 @@ +from opendc.models.machine import Machine +from opendc.models.rack import Rack +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Rack's Machines.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Rack from the database + + rack = Rack.from_tile_id(request.params_path['tileId']) + + # Make sure this Rack exists + + if not rack.exists(): + return Response(404, '{} not found.'.format(rack)) + + # Make sure this user is authorized to view this Rack's Machines + + if not rack.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from viewing {}.'.format(rack)) + + # Get and return the Machines + + machines = Machine.query('rack_id', rack.id) + + for machine in machines: + machine.read() + + return Response( + 200, + 'Successfully retrieved Machines for {}.'.format(rack), + [x.to_JSON() for x in machines] + ) + +def POST(request): + """Add a Machine to this rack.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int' + }, + body = { + 'machine': { + 'rackId': 'int', + 'position': 'int', + 'tags': 'list-string', + 'cpuIds': 'list-int', + 'gpuIds': 'list-int', + 'memoryIds': 'list-int', + 'storageIds': 'list-int' + } + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Rack from the database + + rack = Rack.from_tile_id(request.params_path['tileId']) + + # Make sure this Rack exists + + if not rack.exists(): + return Response(404, '{} not found.'.format(rack)) + + # Make sure this Rack's ID matches the given rack ID + + if rack.id != request.params_body['machine']['rackId']: + return Response(400, 'Rack ID in `machine` and path do not match.') + + # Make sure this user is authorized to edit this Rack's Machines + + if not rack.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from viewing {}.'.format(rack)) + + # Instantiate a Machine + + machine = Machine.from_JSON(request.params_body['machine']) + + # Try to insert this Machine + + try: + machine.insert() + + except exceptions.ForeignKeyError: + return Response(409, 'Rack position occupied.') + + except: + return Response(400, 'Invalid Machine.') + + # Return this Machine + + machine.read() + + return Response( + 200, + 'Successfully added {}.'.format(machine), + machine.to_JSON() + ) diff --git a/opendc/api/v1/tiles/tileId/rack/machines/position/__init__.py b/opendc/api/v1/tiles/tileId/rack/machines/position/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v1/tiles/tileId/rack/machines/position/endpoint.py b/opendc/api/v1/tiles/tileId/rack/machines/position/endpoint.py new file mode 100644 index 00000000..15e6cf96 --- /dev/null +++ b/opendc/api/v1/tiles/tileId/rack/machines/position/endpoint.py @@ -0,0 +1,161 @@ +from opendc.models.machine import Machine +from opendc.models.rack import Rack +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get the Machine at this location in this Rack.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int', + 'position': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Machine from the database + + machine = Machine.from_tile_id_and_rack_position(request.params_path['tileId'], request.params_path['position']) + + # Make sure this Machine exists + + if not machine.exists(): + return Response(404, '{} not found.'.format(machine)) + + # Make sure this user is authorized to view this Machine + + if not machine.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from retrieving {}.'.format(machine)) + + # Return this Machine + + machine.read() + + return Response( + 200, + 'Successfully retrieved {}.'.format(machine), + machine.to_JSON() + ) + +def PUT(request): + """Update the Machine at this location in this Rack.""" + + try: + request.check_required_parameters( + path = { + 'tileId': 'int', + 'position': 'int' + }, + body = { + 'machine': { + 'rackId': 'int', + 'position': 'int', + 'tags': 'list-string', + 'cpuIds': 'list-int', + 'gpuIds': 'list-int', + 'memoryIds': 'list-int', + 'storageIds': 'list-int' + } + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Machine from the database + + machine = Machine.from_tile_id_and_rack_position(request.params_path['tileId'], request.params_path['position']) + + # Make sure this Machine exists + + if not machine.exists(): + return Response(404, '{} not found.'.format(machine)) + + # Make sure this Machine's rack ID is right + + rack = Rack.from_tile_id(request.params_path['tileId']) + + if not rack.exists() or rack.id != request.params_body['machine']['rackId']: + return Response(400, 'Mismatch in Rack IDs.') + + # Make sure this user is authorized to edit this Machine + + if not machine.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from retrieving {}.'.format(machine)) + + # Update this Machine + + machine.positoin = request.params_body['machine']['position'] + machine.tags = request.params_body['machine']['tags'] + machine.cpu_ids = request.params_body['machine']['cpuIds'] + machine.gpu_ids = request.params_body['machine']['gpuIds'] + machine.memory_ids = request.params_body['machine']['memoryIds'] + machine.storage_ids = request.params_body['machine']['storageIds'] + + try: + machine.update() + + except exceptions.ForeignKeyError: + return Response(409, 'Rack position occupied.') + + except Exception as e: + print e + return Response(400, 'Invalid Machine.') + + # Return this Machine + + machine.read() + + return Response( + 200, + 'Successfully updated {}.'.format(machine), + machine.to_JSON() + ) + +def DELETE(request): + """Delete the Machine at this location in this Rack.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int', + 'position': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Machine from the database + + machine = Machine.from_tile_id_and_rack_position(request.params_path['tileId'], request.params_path['position']) + + # Make sure this Machine exists + + if not machine.exists(): + return Response(404, '{} not found.'.format(machine)) + + # Make sure this user is authorized to edit this Machine + + if not machine.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from retrieving {}.'.format(machine)) + + # Delete this Machine + + machine.delete() + + # Return this Machine + + return Response( + 200, + 'Successfully deleted {}.'.format(machine), + machine.to_JSON() + ) diff --git a/opendc/api/v1/traces/endpoint.py b/opendc/api/v1/traces/endpoint.py index d383b335..05d6fd48 100644 --- a/opendc/api/v1/traces/endpoint.py +++ b/opendc/api/v1/traces/endpoint.py @@ -2,7 +2,7 @@ from opendc.models.trace import Trace from opendc.util.rest import Response def GET(request): - """Get all avaialble Traces.""" + """Get all available Traces.""" # Get the Traces diff --git a/opendc/api/v1/traces/traceId/jobs/jobId/__init__.py b/opendc/api/v1/traces/traceId/jobs/jobId/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/traces/traceId/jobs/jobId/endpoint.py b/opendc/api/v1/traces/traceId/jobs/jobId/endpoint.py deleted file mode 100644 index 8bf26ab5..00000000 --- a/opendc/api/v1/traces/traceId/jobs/jobId/endpoint.py +++ /dev/null @@ -1,34 +0,0 @@ -from opendc.models.job import Job -from opendc.util import exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Job.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'traceId': 'int', - 'jobId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Trace and make sure it exists - - job = Job.from_primary_key((request.params_path['jobId'],)) - - if not job.exists(): - return Response(404, '{} not found.'.format(job)) - - # Return this Job - - return Response( - 200, - 'Successfully retrieved {}.'.format(job), - job.to_JSON() - ) diff --git a/opendc/api/v1/traces/traceId/jobs/jobId/tasks/__init__.py b/opendc/api/v1/traces/traceId/jobs/jobId/tasks/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/traces/traceId/jobs/jobId/tasks/endpoint.py b/opendc/api/v1/traces/traceId/jobs/jobId/tasks/endpoint.py deleted file mode 100644 index ecd38758..00000000 --- a/opendc/api/v1/traces/traceId/jobs/jobId/tasks/endpoint.py +++ /dev/null @@ -1,37 +0,0 @@ -from opendc.models.job import Job -from opendc.models.task import Task -from opendc.util import database, exceptions -from opendc.util.rest import Response - -def GET(request): - """Get this Job's Tasks.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path = { - 'traceId': 'int', - 'jobId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Job and make sure it exists - - job = Job.from_primary_key((request.params_path['jobId'],)) - - if not job.exists(): - return Response(404, '{} not found.'.format(job)) - - # Get and return the Tasks - - tasks = Task.query('job_id', request.params_path['jobId']) - - return Response( - 200, - 'Successfully retrieved Tasks for {}.'.format(job), - [x.to_JSON() for x in tasks] - ) diff --git a/opendc/models/machine.py b/opendc/models/machine.py index 52cf221b..54838ffd 100644 --- a/opendc/models/machine.py +++ b/opendc/models/machine.py @@ -19,7 +19,7 @@ class Machine(Model): } } - PATH = '/v1/simulations/{simulationId}/datacenters/{datacenterId}/rooms/{roomId}/tiles/{tileId}/rack/machines' + PATH = '/v1/tiles/{tileId}/rack/machines' TABLE_NAME = 'machines' COLUMNS = ['id', 'rack_id', 'position'] diff --git a/opendc/models/rack.py b/opendc/models/rack.py index a9aeeff9..b547afa1 100644 --- a/opendc/models/rack.py +++ b/opendc/models/rack.py @@ -14,7 +14,7 @@ class Rack(Model): } } - PATH = '/v1/simulations/{simulationId}/datacenters/{datacenterId}/rooms/{roomId}/tiles/{tileId}/rack' + PATH = '/v1/tiles/{tileId}/rack' TABLE_NAME = 'racks' COLUMNS = ['id', 'name', 'capacity', 'power_capacity_w'] diff --git a/opendc/models/room.py b/opendc/models/room.py index b8be88f9..76f9f7b3 100644 --- a/opendc/models/room.py +++ b/opendc/models/room.py @@ -13,7 +13,7 @@ class Room(Model): } } - PATH = '/v1/simulations/{simulationId}/datacenters/{datacenterId}/rooms' + PATH = '/v1/datacenters/{datacenterId}/rooms' TABLE_NAME = 'rooms' COLUMNS = ['id', 'name', 'datacenter_id', 'type'] diff --git a/opendc/models/tile.py b/opendc/models/tile.py index 95445419..748c76c5 100644 --- a/opendc/models/tile.py +++ b/opendc/models/tile.py @@ -16,7 +16,7 @@ class Tile(Model): } } - PATH = '/v1/simulations/{simulationId}/datacenters/{datacenterId}/rooms/{roomId}/tiles' + PATH = '/v1/rooms/{roomId}/tiles' TABLE_NAME = 'tiles' COLUMNS = ['id', 'position_x', 'position_y', 'room_id', 'object_id'] -- cgit v1.2.3