From 565ede0dc50c3b2df09c066ea3a28a4901cce547 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Tue, 23 Jun 2020 18:08:28 +0200 Subject: Add DB handlers and rename to v2 --- main.py | 6 +- opendc/api/v1/__init__.py | 0 opendc/api/v1/datacenters/__init__.py | 0 opendc/api/v1/datacenters/datacenterId/__init__.py | 0 opendc/api/v1/datacenters/datacenterId/endpoint.py | 43 ---- .../v1/datacenters/datacenterId/rooms/__init__.py | 0 .../v1/datacenters/datacenterId/rooms/endpoint.py | 111 ---------- opendc/api/v1/experiments/__init__.py | 0 opendc/api/v1/experiments/experimentId/__init__.py | 0 opendc/api/v1/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 ----- opendc/api/v1/jobs/__init__.py | 0 opendc/api/v1/jobs/jobId/__init__.py | 0 opendc/api/v1/jobs/jobId/endpoint.py | 34 --- opendc/api/v1/jobs/jobId/tasks/__init__.py | 0 opendc/api/v1/jobs/jobId/tasks/endpoint.py | 37 ---- opendc/api/v1/paths.json | 53 ----- 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 | 176 --------------- 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 | 43 ---- opendc/api/v1/room-types/__init__.py | 0 opendc/api/v1/room-types/endpoint.py | 18 -- opendc/api/v1/room-types/name/__init__.py | 0 .../v1/room-types/name/allowed-objects/__init__.py | 0 .../v1/room-types/name/allowed-objects/endpoint.py | 31 --- opendc/api/v1/rooms/__init__.py | 0 opendc/api/v1/rooms/roomId/__init__.py | 0 opendc/api/v1/rooms/roomId/endpoint.py | 137 ------------ opendc/api/v1/rooms/roomId/tiles/__init__.py | 0 opendc/api/v1/rooms/roomId/tiles/endpoint.py | 121 ----------- opendc/api/v1/schedulers/__init__.py | 0 opendc/api/v1/schedulers/endpoint.py | 18 -- opendc/api/v1/sections/__init__.py | 0 opendc/api/v1/sections/sectionId/__init__.py | 0 opendc/api/v1/sections/sectionId/endpoint.py | 42 ---- opendc/api/v1/simulations/__init__.py | 0 opendc/api/v1/simulations/endpoint.py | 87 -------- opendc/api/v1/simulations/simulationId/__init__.py | 0 .../simulationId/authorizations/__init__.py | 0 .../simulationId/authorizations/endpoint.py | 44 ---- .../simulationId/authorizations/userId/__init__.py | 0 .../simulationId/authorizations/userId/endpoint.py | 212 ------------------ .../simulationId/datacenters/__init__.py | 0 .../simulationId/datacenters/endpoint.py | 61 ------ opendc/api/v1/simulations/simulationId/endpoint.py | 133 ------------ .../simulationId/experiments/__init__.py | 0 .../simulationId/experiments/endpoint.py | 112 ---------- .../v1/simulations/simulationId/paths/__init__.py | 0 .../v1/simulations/simulationId/paths/endpoint.py | 43 ---- opendc/api/v1/specifications/__init__.py | 0 opendc/api/v1/specifications/cpus/__init__.py | 0 opendc/api/v1/specifications/cpus/endpoint.py | 18 -- opendc/api/v1/specifications/cpus/id/__init__.py | 0 opendc/api/v1/specifications/cpus/id/endpoint.py | 34 --- .../v1/specifications/failure-models/__init__.py | 0 .../v1/specifications/failure-models/endpoint.py | 18 -- .../specifications/failure-models/id/__init__.py | 0 .../specifications/failure-models/id/endpoint.py | 34 --- opendc/api/v1/specifications/gpus/__init__.py | 0 opendc/api/v1/specifications/gpus/endpoint.py | 18 -- opendc/api/v1/specifications/gpus/id/__init__.py | 0 opendc/api/v1/specifications/gpus/id/endpoint.py | 34 --- opendc/api/v1/specifications/memories/__init__.py | 0 opendc/api/v1/specifications/memories/endpoint.py | 18 -- .../api/v1/specifications/memories/id/__init__.py | 0 .../api/v1/specifications/memories/id/endpoint.py | 34 --- opendc/api/v1/specifications/storages/__init__.py | 0 opendc/api/v1/specifications/storages/endpoint.py | 18 -- .../api/v1/specifications/storages/id/__init__.py | 0 .../api/v1/specifications/storages/id/endpoint.py | 34 --- opendc/api/v1/tiles/__init__.py | 0 opendc/api/v1/tiles/tileId/__init__.py | 0 opendc/api/v1/tiles/tileId/endpoint.py | 85 -------- opendc/api/v1/tiles/tileId/rack/__init__.py | 0 opendc/api/v1/tiles/tileId/rack/endpoint.py | 237 --------------------- .../api/v1/tiles/tileId/rack/machines/__init__.py | 0 .../api/v1/tiles/tileId/rack/machines/endpoint.py | 118 ---------- .../tileId/rack/machines/position/__init__.py | 0 .../tileId/rack/machines/position/endpoint.py | 164 -------------- opendc/api/v1/traces/__init__.py | 0 opendc/api/v1/traces/endpoint.py | 18 -- opendc/api/v1/traces/traceId/__init__.py | 0 opendc/api/v1/traces/traceId/endpoint.py | 34 --- opendc/api/v1/traces/traceId/jobs/__init__.py | 0 opendc/api/v1/traces/traceId/jobs/endpoint.py | 37 ---- opendc/api/v1/users/__init__.py | 0 opendc/api/v1/users/endpoint.py | 82 ------- opendc/api/v1/users/userId/__init__.py | 0 .../api/v1/users/userId/authorizations/__init__.py | 0 .../api/v1/users/userId/authorizations/endpoint.py | 42 ---- opendc/api/v1/users/userId/endpoint.py | 123 ----------- opendc/api/v2/__init__.py | 0 opendc/api/v2/datacenters/__init__.py | 0 opendc/api/v2/datacenters/datacenterId/__init__.py | 0 opendc/api/v2/datacenters/datacenterId/endpoint.py | 43 ++++ .../v2/datacenters/datacenterId/rooms/__init__.py | 0 .../v2/datacenters/datacenterId/rooms/endpoint.py | 111 ++++++++++ opendc/api/v2/experiments/__init__.py | 0 opendc/api/v2/experiments/experimentId/__init__.py | 0 opendc/api/v2/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 +++++ opendc/api/v2/jobs/__init__.py | 0 opendc/api/v2/jobs/jobId/__init__.py | 0 opendc/api/v2/jobs/jobId/endpoint.py | 34 +++ opendc/api/v2/jobs/jobId/tasks/__init__.py | 0 opendc/api/v2/jobs/jobId/tasks/endpoint.py | 37 ++++ opendc/api/v2/paths.json | 53 +++++ opendc/api/v2/paths/__init__.py | 0 opendc/api/v2/paths/pathId/__init__.py | 0 opendc/api/v2/paths/pathId/branches/__init__.py | 0 opendc/api/v2/paths/pathId/branches/endpoint.py | 176 +++++++++++++++ opendc/api/v2/paths/pathId/endpoint.py | 43 ++++ opendc/api/v2/paths/pathId/sections/__init__.py | 0 opendc/api/v2/paths/pathId/sections/endpoint.py | 43 ++++ opendc/api/v2/room-types/__init__.py | 0 opendc/api/v2/room-types/endpoint.py | 18 ++ opendc/api/v2/room-types/name/__init__.py | 0 .../v2/room-types/name/allowed-objects/__init__.py | 0 .../v2/room-types/name/allowed-objects/endpoint.py | 31 +++ opendc/api/v2/rooms/__init__.py | 0 opendc/api/v2/rooms/roomId/__init__.py | 0 opendc/api/v2/rooms/roomId/endpoint.py | 137 ++++++++++++ opendc/api/v2/rooms/roomId/tiles/__init__.py | 0 opendc/api/v2/rooms/roomId/tiles/endpoint.py | 121 +++++++++++ opendc/api/v2/schedulers/__init__.py | 0 opendc/api/v2/schedulers/endpoint.py | 18 ++ opendc/api/v2/sections/__init__.py | 0 opendc/api/v2/sections/sectionId/__init__.py | 0 opendc/api/v2/sections/sectionId/endpoint.py | 42 ++++ opendc/api/v2/simulations/__init__.py | 0 opendc/api/v2/simulations/endpoint.py | 87 ++++++++ opendc/api/v2/simulations/simulationId/__init__.py | 0 .../simulationId/authorizations/__init__.py | 0 .../simulationId/authorizations/endpoint.py | 44 ++++ .../simulationId/authorizations/userId/__init__.py | 0 .../simulationId/authorizations/userId/endpoint.py | 212 ++++++++++++++++++ .../simulationId/datacenters/__init__.py | 0 .../simulationId/datacenters/endpoint.py | 61 ++++++ opendc/api/v2/simulations/simulationId/endpoint.py | 133 ++++++++++++ .../simulationId/experiments/__init__.py | 0 .../simulationId/experiments/endpoint.py | 112 ++++++++++ .../v2/simulations/simulationId/paths/__init__.py | 0 .../v2/simulations/simulationId/paths/endpoint.py | 43 ++++ opendc/api/v2/specifications/__init__.py | 0 opendc/api/v2/specifications/cpus/__init__.py | 0 opendc/api/v2/specifications/cpus/endpoint.py | 18 ++ opendc/api/v2/specifications/cpus/id/__init__.py | 0 opendc/api/v2/specifications/cpus/id/endpoint.py | 34 +++ .../v2/specifications/failure-models/__init__.py | 0 .../v2/specifications/failure-models/endpoint.py | 18 ++ .../specifications/failure-models/id/__init__.py | 0 .../specifications/failure-models/id/endpoint.py | 34 +++ opendc/api/v2/specifications/gpus/__init__.py | 0 opendc/api/v2/specifications/gpus/endpoint.py | 18 ++ opendc/api/v2/specifications/gpus/id/__init__.py | 0 opendc/api/v2/specifications/gpus/id/endpoint.py | 34 +++ opendc/api/v2/specifications/memories/__init__.py | 0 opendc/api/v2/specifications/memories/endpoint.py | 18 ++ .../api/v2/specifications/memories/id/__init__.py | 0 .../api/v2/specifications/memories/id/endpoint.py | 34 +++ opendc/api/v2/specifications/storages/__init__.py | 0 opendc/api/v2/specifications/storages/endpoint.py | 18 ++ .../api/v2/specifications/storages/id/__init__.py | 0 .../api/v2/specifications/storages/id/endpoint.py | 34 +++ opendc/api/v2/tiles/__init__.py | 0 opendc/api/v2/tiles/tileId/__init__.py | 0 opendc/api/v2/tiles/tileId/endpoint.py | 85 ++++++++ opendc/api/v2/tiles/tileId/rack/__init__.py | 0 opendc/api/v2/tiles/tileId/rack/endpoint.py | 237 +++++++++++++++++++++ .../api/v2/tiles/tileId/rack/machines/__init__.py | 0 .../api/v2/tiles/tileId/rack/machines/endpoint.py | 118 ++++++++++ .../tileId/rack/machines/position/__init__.py | 0 .../tileId/rack/machines/position/endpoint.py | 164 ++++++++++++++ opendc/api/v2/traces/__init__.py | 0 opendc/api/v2/traces/endpoint.py | 18 ++ opendc/api/v2/traces/traceId/__init__.py | 0 opendc/api/v2/traces/traceId/endpoint.py | 34 +++ opendc/api/v2/traces/traceId/jobs/__init__.py | 0 opendc/api/v2/traces/traceId/jobs/endpoint.py | 37 ++++ opendc/api/v2/users/__init__.py | 0 opendc/api/v2/users/endpoint.py | 82 +++++++ opendc/api/v2/users/userId/__init__.py | 0 .../api/v2/users/userId/authorizations/__init__.py | 0 .../api/v2/users/userId/authorizations/endpoint.py | 42 ++++ opendc/api/v2/users/userId/endpoint.py | 123 +++++++++++ opendc/models/machine.py | 4 +- opendc/models/machine_state.py | 6 +- opendc/models/model.py | 8 +- opendc/models/rack_state.py | 4 +- opendc/models/room_state.py | 4 +- opendc/models/task_duration.py | 2 +- opendc/models/task_state.py | 2 +- opendc/util/database.py | 78 +++---- 221 files changed, 3274 insertions(+), 3288 deletions(-) delete mode 100644 opendc/api/v1/__init__.py delete mode 100644 opendc/api/v1/datacenters/__init__.py delete mode 100644 opendc/api/v1/datacenters/datacenterId/__init__.py delete mode 100644 opendc/api/v1/datacenters/datacenterId/endpoint.py delete mode 100644 opendc/api/v1/datacenters/datacenterId/rooms/__init__.py delete mode 100644 opendc/api/v1/datacenters/datacenterId/rooms/endpoint.py delete mode 100644 opendc/api/v1/experiments/__init__.py delete mode 100644 opendc/api/v1/experiments/experimentId/__init__.py delete mode 100644 opendc/api/v1/experiments/experimentId/endpoint.py delete mode 100644 opendc/api/v1/experiments/experimentId/last-simulated-tick/__init__.py delete mode 100644 opendc/api/v1/experiments/experimentId/last-simulated-tick/endpoint.py delete mode 100644 opendc/api/v1/experiments/experimentId/machine-states/__init__.py delete mode 100644 opendc/api/v1/experiments/experimentId/machine-states/endpoint.py delete mode 100644 opendc/api/v1/experiments/experimentId/rack-states/__init__.py delete mode 100644 opendc/api/v1/experiments/experimentId/rack-states/endpoint.py delete mode 100644 opendc/api/v1/experiments/experimentId/room-states/__init__.py delete mode 100644 opendc/api/v1/experiments/experimentId/room-states/endpoint.py delete mode 100644 opendc/api/v1/experiments/experimentId/statistics/__init__.py delete mode 100644 opendc/api/v1/experiments/experimentId/statistics/task-durations/__init__.py delete mode 100644 opendc/api/v1/experiments/experimentId/statistics/task-durations/endpoint.py delete mode 100644 opendc/api/v1/experiments/experimentId/task-states/__init__.py delete mode 100644 opendc/api/v1/experiments/experimentId/task-states/endpoint.py delete mode 100644 opendc/api/v1/jobs/__init__.py delete mode 100644 opendc/api/v1/jobs/jobId/__init__.py delete mode 100644 opendc/api/v1/jobs/jobId/endpoint.py delete mode 100644 opendc/api/v1/jobs/jobId/tasks/__init__.py delete mode 100644 opendc/api/v1/jobs/jobId/tasks/endpoint.py delete mode 100644 opendc/api/v1/paths.json delete mode 100644 opendc/api/v1/paths/__init__.py delete mode 100644 opendc/api/v1/paths/pathId/__init__.py delete mode 100644 opendc/api/v1/paths/pathId/branches/__init__.py delete mode 100644 opendc/api/v1/paths/pathId/branches/endpoint.py delete mode 100644 opendc/api/v1/paths/pathId/endpoint.py delete mode 100644 opendc/api/v1/paths/pathId/sections/__init__.py delete mode 100644 opendc/api/v1/paths/pathId/sections/endpoint.py delete mode 100644 opendc/api/v1/room-types/__init__.py delete mode 100644 opendc/api/v1/room-types/endpoint.py delete mode 100644 opendc/api/v1/room-types/name/__init__.py delete mode 100644 opendc/api/v1/room-types/name/allowed-objects/__init__.py delete mode 100644 opendc/api/v1/room-types/name/allowed-objects/endpoint.py delete mode 100644 opendc/api/v1/rooms/__init__.py delete mode 100644 opendc/api/v1/rooms/roomId/__init__.py delete mode 100644 opendc/api/v1/rooms/roomId/endpoint.py delete mode 100644 opendc/api/v1/rooms/roomId/tiles/__init__.py delete mode 100644 opendc/api/v1/rooms/roomId/tiles/endpoint.py delete mode 100644 opendc/api/v1/schedulers/__init__.py delete mode 100644 opendc/api/v1/schedulers/endpoint.py delete mode 100644 opendc/api/v1/sections/__init__.py delete mode 100644 opendc/api/v1/sections/sectionId/__init__.py delete mode 100644 opendc/api/v1/sections/sectionId/endpoint.py delete mode 100644 opendc/api/v1/simulations/__init__.py delete mode 100644 opendc/api/v1/simulations/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/authorizations/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/authorizations/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/authorizations/userId/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/authorizations/userId/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/datacenters/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/datacenters/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/experiments/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/experiments/endpoint.py delete mode 100644 opendc/api/v1/simulations/simulationId/paths/__init__.py delete mode 100644 opendc/api/v1/simulations/simulationId/paths/endpoint.py delete mode 100644 opendc/api/v1/specifications/__init__.py delete mode 100644 opendc/api/v1/specifications/cpus/__init__.py delete mode 100644 opendc/api/v1/specifications/cpus/endpoint.py delete mode 100644 opendc/api/v1/specifications/cpus/id/__init__.py delete mode 100644 opendc/api/v1/specifications/cpus/id/endpoint.py delete mode 100644 opendc/api/v1/specifications/failure-models/__init__.py delete mode 100644 opendc/api/v1/specifications/failure-models/endpoint.py delete mode 100644 opendc/api/v1/specifications/failure-models/id/__init__.py delete mode 100644 opendc/api/v1/specifications/failure-models/id/endpoint.py delete mode 100644 opendc/api/v1/specifications/gpus/__init__.py delete mode 100644 opendc/api/v1/specifications/gpus/endpoint.py delete mode 100644 opendc/api/v1/specifications/gpus/id/__init__.py delete mode 100644 opendc/api/v1/specifications/gpus/id/endpoint.py delete mode 100644 opendc/api/v1/specifications/memories/__init__.py delete mode 100644 opendc/api/v1/specifications/memories/endpoint.py delete mode 100644 opendc/api/v1/specifications/memories/id/__init__.py delete mode 100644 opendc/api/v1/specifications/memories/id/endpoint.py delete mode 100644 opendc/api/v1/specifications/storages/__init__.py delete mode 100644 opendc/api/v1/specifications/storages/endpoint.py delete mode 100644 opendc/api/v1/specifications/storages/id/__init__.py delete mode 100644 opendc/api/v1/specifications/storages/id/endpoint.py delete mode 100644 opendc/api/v1/tiles/__init__.py delete mode 100644 opendc/api/v1/tiles/tileId/__init__.py delete mode 100644 opendc/api/v1/tiles/tileId/endpoint.py delete mode 100644 opendc/api/v1/tiles/tileId/rack/__init__.py delete mode 100644 opendc/api/v1/tiles/tileId/rack/endpoint.py delete mode 100644 opendc/api/v1/tiles/tileId/rack/machines/__init__.py delete mode 100644 opendc/api/v1/tiles/tileId/rack/machines/endpoint.py delete mode 100644 opendc/api/v1/tiles/tileId/rack/machines/position/__init__.py delete mode 100644 opendc/api/v1/tiles/tileId/rack/machines/position/endpoint.py delete mode 100644 opendc/api/v1/traces/__init__.py delete mode 100644 opendc/api/v1/traces/endpoint.py delete mode 100644 opendc/api/v1/traces/traceId/__init__.py delete mode 100644 opendc/api/v1/traces/traceId/endpoint.py delete mode 100644 opendc/api/v1/traces/traceId/jobs/__init__.py delete mode 100644 opendc/api/v1/traces/traceId/jobs/endpoint.py delete mode 100644 opendc/api/v1/users/__init__.py delete mode 100644 opendc/api/v1/users/endpoint.py delete mode 100644 opendc/api/v1/users/userId/__init__.py delete mode 100644 opendc/api/v1/users/userId/authorizations/__init__.py delete mode 100644 opendc/api/v1/users/userId/authorizations/endpoint.py delete mode 100644 opendc/api/v1/users/userId/endpoint.py create mode 100644 opendc/api/v2/__init__.py create mode 100644 opendc/api/v2/datacenters/__init__.py create mode 100644 opendc/api/v2/datacenters/datacenterId/__init__.py create mode 100644 opendc/api/v2/datacenters/datacenterId/endpoint.py create mode 100644 opendc/api/v2/datacenters/datacenterId/rooms/__init__.py create mode 100644 opendc/api/v2/datacenters/datacenterId/rooms/endpoint.py create mode 100644 opendc/api/v2/experiments/__init__.py create mode 100644 opendc/api/v2/experiments/experimentId/__init__.py create mode 100644 opendc/api/v2/experiments/experimentId/endpoint.py create mode 100644 opendc/api/v2/experiments/experimentId/last-simulated-tick/__init__.py create mode 100644 opendc/api/v2/experiments/experimentId/last-simulated-tick/endpoint.py create mode 100644 opendc/api/v2/experiments/experimentId/machine-states/__init__.py create mode 100644 opendc/api/v2/experiments/experimentId/machine-states/endpoint.py create mode 100644 opendc/api/v2/experiments/experimentId/rack-states/__init__.py create mode 100644 opendc/api/v2/experiments/experimentId/rack-states/endpoint.py create mode 100644 opendc/api/v2/experiments/experimentId/room-states/__init__.py create mode 100644 opendc/api/v2/experiments/experimentId/room-states/endpoint.py create mode 100644 opendc/api/v2/experiments/experimentId/statistics/__init__.py create mode 100644 opendc/api/v2/experiments/experimentId/statistics/task-durations/__init__.py create mode 100644 opendc/api/v2/experiments/experimentId/statistics/task-durations/endpoint.py create mode 100644 opendc/api/v2/experiments/experimentId/task-states/__init__.py create mode 100644 opendc/api/v2/experiments/experimentId/task-states/endpoint.py create mode 100644 opendc/api/v2/jobs/__init__.py create mode 100644 opendc/api/v2/jobs/jobId/__init__.py create mode 100644 opendc/api/v2/jobs/jobId/endpoint.py create mode 100644 opendc/api/v2/jobs/jobId/tasks/__init__.py create mode 100644 opendc/api/v2/jobs/jobId/tasks/endpoint.py create mode 100644 opendc/api/v2/paths.json create mode 100644 opendc/api/v2/paths/__init__.py create mode 100644 opendc/api/v2/paths/pathId/__init__.py create mode 100644 opendc/api/v2/paths/pathId/branches/__init__.py create mode 100644 opendc/api/v2/paths/pathId/branches/endpoint.py create mode 100644 opendc/api/v2/paths/pathId/endpoint.py create mode 100644 opendc/api/v2/paths/pathId/sections/__init__.py create mode 100644 opendc/api/v2/paths/pathId/sections/endpoint.py create mode 100644 opendc/api/v2/room-types/__init__.py create mode 100644 opendc/api/v2/room-types/endpoint.py create mode 100644 opendc/api/v2/room-types/name/__init__.py create mode 100644 opendc/api/v2/room-types/name/allowed-objects/__init__.py create mode 100644 opendc/api/v2/room-types/name/allowed-objects/endpoint.py create mode 100644 opendc/api/v2/rooms/__init__.py create mode 100644 opendc/api/v2/rooms/roomId/__init__.py create mode 100644 opendc/api/v2/rooms/roomId/endpoint.py create mode 100644 opendc/api/v2/rooms/roomId/tiles/__init__.py create mode 100644 opendc/api/v2/rooms/roomId/tiles/endpoint.py create mode 100644 opendc/api/v2/schedulers/__init__.py create mode 100644 opendc/api/v2/schedulers/endpoint.py create mode 100644 opendc/api/v2/sections/__init__.py create mode 100644 opendc/api/v2/sections/sectionId/__init__.py create mode 100644 opendc/api/v2/sections/sectionId/endpoint.py create mode 100644 opendc/api/v2/simulations/__init__.py create mode 100644 opendc/api/v2/simulations/endpoint.py create mode 100644 opendc/api/v2/simulations/simulationId/__init__.py create mode 100644 opendc/api/v2/simulations/simulationId/authorizations/__init__.py create mode 100644 opendc/api/v2/simulations/simulationId/authorizations/endpoint.py create mode 100644 opendc/api/v2/simulations/simulationId/authorizations/userId/__init__.py create mode 100644 opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py create mode 100644 opendc/api/v2/simulations/simulationId/datacenters/__init__.py create mode 100644 opendc/api/v2/simulations/simulationId/datacenters/endpoint.py create mode 100644 opendc/api/v2/simulations/simulationId/endpoint.py create mode 100644 opendc/api/v2/simulations/simulationId/experiments/__init__.py create mode 100644 opendc/api/v2/simulations/simulationId/experiments/endpoint.py create mode 100644 opendc/api/v2/simulations/simulationId/paths/__init__.py create mode 100644 opendc/api/v2/simulations/simulationId/paths/endpoint.py create mode 100644 opendc/api/v2/specifications/__init__.py create mode 100644 opendc/api/v2/specifications/cpus/__init__.py create mode 100644 opendc/api/v2/specifications/cpus/endpoint.py create mode 100644 opendc/api/v2/specifications/cpus/id/__init__.py create mode 100644 opendc/api/v2/specifications/cpus/id/endpoint.py create mode 100644 opendc/api/v2/specifications/failure-models/__init__.py create mode 100644 opendc/api/v2/specifications/failure-models/endpoint.py create mode 100644 opendc/api/v2/specifications/failure-models/id/__init__.py create mode 100644 opendc/api/v2/specifications/failure-models/id/endpoint.py create mode 100644 opendc/api/v2/specifications/gpus/__init__.py create mode 100644 opendc/api/v2/specifications/gpus/endpoint.py create mode 100644 opendc/api/v2/specifications/gpus/id/__init__.py create mode 100644 opendc/api/v2/specifications/gpus/id/endpoint.py create mode 100644 opendc/api/v2/specifications/memories/__init__.py create mode 100644 opendc/api/v2/specifications/memories/endpoint.py create mode 100644 opendc/api/v2/specifications/memories/id/__init__.py create mode 100644 opendc/api/v2/specifications/memories/id/endpoint.py create mode 100644 opendc/api/v2/specifications/storages/__init__.py create mode 100644 opendc/api/v2/specifications/storages/endpoint.py create mode 100644 opendc/api/v2/specifications/storages/id/__init__.py create mode 100644 opendc/api/v2/specifications/storages/id/endpoint.py create mode 100644 opendc/api/v2/tiles/__init__.py create mode 100644 opendc/api/v2/tiles/tileId/__init__.py create mode 100644 opendc/api/v2/tiles/tileId/endpoint.py create mode 100644 opendc/api/v2/tiles/tileId/rack/__init__.py create mode 100644 opendc/api/v2/tiles/tileId/rack/endpoint.py create mode 100644 opendc/api/v2/tiles/tileId/rack/machines/__init__.py create mode 100644 opendc/api/v2/tiles/tileId/rack/machines/endpoint.py create mode 100644 opendc/api/v2/tiles/tileId/rack/machines/position/__init__.py create mode 100644 opendc/api/v2/tiles/tileId/rack/machines/position/endpoint.py create mode 100644 opendc/api/v2/traces/__init__.py create mode 100644 opendc/api/v2/traces/endpoint.py create mode 100644 opendc/api/v2/traces/traceId/__init__.py create mode 100644 opendc/api/v2/traces/traceId/endpoint.py create mode 100644 opendc/api/v2/traces/traceId/jobs/__init__.py create mode 100644 opendc/api/v2/traces/traceId/jobs/endpoint.py create mode 100644 opendc/api/v2/users/__init__.py create mode 100644 opendc/api/v2/users/endpoint.py create mode 100644 opendc/api/v2/users/userId/__init__.py create mode 100644 opendc/api/v2/users/userId/authorizations/__init__.py create mode 100644 opendc/api/v2/users/userId/authorizations/endpoint.py create mode 100644 opendc/api/v2/users/userId/endpoint.py diff --git a/main.py b/main.py index 1360ee77..8ca358db 100644 --- a/main.py +++ b/main.py @@ -22,11 +22,11 @@ with open(sys.argv[1]) as f: STATIC_ROOT = os.path.join(KEYS['ROOT_DIR'], 'opendc-frontend', 'build') -database.init_connection_pool(user=KEYS['MONGODB_USER'], password=KEYS['MONGODB_PASSWORD'], - database=KEYS['MONGODB_DATABASE'], host=KEYS['MONGODB_HOST'], port=KEYS['MONGODB_PORT']) +database.init_connection_pool(user=KEYS['OPENDC_DB_USERNAME'], password=KEYS['OPENDC_DB_PASSWORD'], + database=KEYS['OPENDC_DB'], host='localhost', port=27017) FLASK_CORE_APP = Flask(__name__, static_url_path='', static_folder=STATIC_ROOT) -FLASK_CORE_APP.config['SECREY_KEY'] = KEYS['FLASK_SECRET'] +FLASK_CORE_APP.config['SECRET_KEY'] = KEYS['FLASK_SECRET'] if 'localhost' in KEYS['SERVER_BASE_URL']: CORS(FLASK_CORE_APP) diff --git a/opendc/api/v1/__init__.py b/opendc/api/v1/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/datacenters/__init__.py b/opendc/api/v1/datacenters/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/datacenters/datacenterId/__init__.py b/opendc/api/v1/datacenters/datacenterId/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/datacenters/datacenterId/endpoint.py b/opendc/api/v1/datacenters/datacenterId/endpoint.py deleted file mode 100644 index 9444fb80..00000000 --- a/opendc/api/v1/datacenters/datacenterId/endpoint.py +++ /dev/null @@ -1,43 +0,0 @@ -from opendc.models.datacenter import Datacenter -from opendc.util import 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 deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/datacenters/datacenterId/rooms/endpoint.py b/opendc/api/v1/datacenters/datacenterId/rooms/endpoint.py deleted file mode 100644 index e0155c7c..00000000 --- a/opendc/api/v1/datacenters/datacenterId/rooms/endpoint.py +++ /dev/null @@ -1,111 +0,0 @@ -from opendc.models.datacenter import Datacenter -from opendc.models.room import Room -from opendc.util import 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 'name' not 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: - 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 deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/experiments/experimentId/__init__.py b/opendc/api/v1/experiments/experimentId/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/experiments/experimentId/endpoint.py b/opendc/api/v1/experiments/experimentId/endpoint.py deleted file mode 100644 index 59e0e0fe..00000000 --- a/opendc/api/v1/experiments/experimentId/endpoint.py +++ /dev/null @@ -1,138 +0,0 @@ -from opendc.models.experiment import Experiment -from opendc.util import 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 deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/experiments/experimentId/last-simulated-tick/endpoint.py b/opendc/api/v1/experiments/experimentId/last-simulated-tick/endpoint.py deleted file mode 100644 index b4b19f59..00000000 --- a/opendc/api/v1/experiments/experimentId/last-simulated-tick/endpoint.py +++ /dev/null @@ -1,39 +0,0 @@ -from opendc.models.experiment import Experiment -from opendc.util import 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 exists - - 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 deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/experiments/experimentId/machine-states/endpoint.py b/opendc/api/v1/experiments/experimentId/machine-states/endpoint.py deleted file mode 100644 index 8ad588d2..00000000 --- a/opendc/api/v1/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 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 exists - - 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 deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/experiments/experimentId/rack-states/endpoint.py b/opendc/api/v1/experiments/experimentId/rack-states/endpoint.py deleted file mode 100644 index 03570039..00000000 --- a/opendc/api/v1/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 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 exists - - 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 deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/experiments/experimentId/room-states/endpoint.py b/opendc/api/v1/experiments/experimentId/room-states/endpoint.py deleted file mode 100644 index 2693dc89..00000000 --- a/opendc/api/v1/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 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 exists - - 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 deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/experiments/experimentId/statistics/task-durations/__init__.py b/opendc/api/v1/experiments/experimentId/statistics/task-durations/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/experiments/experimentId/statistics/task-durations/endpoint.py b/opendc/api/v1/experiments/experimentId/statistics/task-durations/endpoint.py deleted file mode 100644 index b8311f08..00000000 --- a/opendc/api/v1/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 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 exists - - 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 deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/experiments/experimentId/task-states/endpoint.py b/opendc/api/v1/experiments/experimentId/task-states/endpoint.py deleted file mode 100644 index 0c3fae89..00000000 --- a/opendc/api/v1/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 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 deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/jobs/jobId/__init__.py b/opendc/api/v1/jobs/jobId/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/jobs/jobId/endpoint.py b/opendc/api/v1/jobs/jobId/endpoint.py deleted file mode 100644 index da4dcd9d..00000000 --- a/opendc/api/v1/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={ - '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 deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/jobs/jobId/tasks/endpoint.py b/opendc/api/v1/jobs/jobId/tasks/endpoint.py deleted file mode 100644 index 04ac5b8c..00000000 --- a/opendc/api/v1/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 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 deleted file mode 100644 index bac7fc71..00000000 --- a/opendc/api/v1/paths.json +++ /dev/null @@ -1,53 +0,0 @@ -[ - "/users", - "/users/{userId}", - "/users/{userId}/authorizations", - "/simulations", - "/simulations/{simulationId}", - "/simulations/{simulationId}/authorizations", - "/simulations/{simulationId}/authorizations/{userId}", - "/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", - "/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", - "/paths/{pathId}", - "/paths/{pathId}/branches", - "/paths/{pathId}/sections", - "/sections/{sectionId}", - "/schedulers", - "/traces", - "/traces/{traceId}", - "/traces/{traceId}/jobs", - "/jobs/{jobId}", - "/jobs/{jobId}/tasks", - "/room-types", - "/room-types/{name}/allowed-objects", - "/specifications/cooling-items", - "/specifications/cooling-items/{id}", - "/specifications/cpus", - "/specifications/cpus/{id}", - "/specifications/failure-models", - "/specifications/failure-models/{id}", - "/specifications/gpus", - "/specifications/gpus/{id}", - "/specifications/memories", - "/specifications/memories/{id}", - "/specifications/psus", - "/specifications/psus/{id}", - "/specifications/storages", - "/specifications/storages/{id}" -] \ No newline at end of file diff --git a/opendc/api/v1/paths/__init__.py b/opendc/api/v1/paths/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/paths/pathId/__init__.py b/opendc/api/v1/paths/pathId/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/paths/pathId/branches/__init__.py b/opendc/api/v1/paths/pathId/branches/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/paths/pathId/branches/endpoint.py b/opendc/api/v1/paths/pathId/branches/endpoint.py deleted file mode 100644 index 13638ffa..00000000 --- a/opendc/api/v1/paths/pathId/branches/endpoint.py +++ /dev/null @@ -1,176 +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={ - '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'] - - if old_room.topology_id is None: - old_room.topology_id = old_room.id - - 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'] - - if old_tile.topology_id is None: - old_tile.topology_id = old_tile.id - - 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] - - if old_rack.topology_id is None: - old_rack.topology_id = old_rack.id - - 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'] - - if old_machine.topology_id is None: - old_machine.topology_id = old_machine.id - - 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 deleted file mode 100644 index 7ade19ce..00000000 --- a/opendc/api/v1/paths/pathId/endpoint.py +++ /dev/null @@ -1,43 +0,0 @@ -from opendc.models.path import Path -from opendc.util import 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 deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/paths/pathId/sections/endpoint.py b/opendc/api/v1/paths/pathId/sections/endpoint.py deleted file mode 100644 index d4161839..00000000 --- a/opendc/api/v1/paths/pathId/sections/endpoint.py +++ /dev/null @@ -1,43 +0,0 @@ -from opendc.models.path import Path -from opendc.models.section import Section -from opendc.util import 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/room-types/__init__.py b/opendc/api/v1/room-types/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/room-types/endpoint.py b/opendc/api/v1/room-types/endpoint.py deleted file mode 100644 index fe00f226..00000000 --- a/opendc/api/v1/room-types/endpoint.py +++ /dev/null @@ -1,18 +0,0 @@ -from opendc.models.room_type import RoomType -from opendc.util.rest import Response - - -def GET(request): - """Get all available room types.""" - - # Get the RoomTypes - - room_types = RoomType.query() - - # Return the RoomTypes - - return Response( - 200, - 'Successfully retrieved RoomTypes.', - [x.to_JSON() for x in room_types] - ) diff --git a/opendc/api/v1/room-types/name/__init__.py b/opendc/api/v1/room-types/name/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/room-types/name/allowed-objects/__init__.py b/opendc/api/v1/room-types/name/allowed-objects/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/room-types/name/allowed-objects/endpoint.py b/opendc/api/v1/room-types/name/allowed-objects/endpoint.py deleted file mode 100644 index a574591c..00000000 --- a/opendc/api/v1/room-types/name/allowed-objects/endpoint.py +++ /dev/null @@ -1,31 +0,0 @@ -from opendc.models.allowed_object import AllowedObject -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get this room's allowed objects.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'name': 'string' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Get the AllowedObjects - - allowed_objects = AllowedObject.query('room_type', request.params_path['name']) - - # Return the AllowedObjects - - return Response( - 200, - 'Successfully retrieved AllowedObjects.', - [x.to_JSON() for x in allowed_objects] - ) diff --git a/opendc/api/v1/rooms/__init__.py b/opendc/api/v1/rooms/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/rooms/roomId/__init__.py b/opendc/api/v1/rooms/roomId/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/rooms/roomId/endpoint.py b/opendc/api/v1/rooms/roomId/endpoint.py deleted file mode 100644 index 1dfc32cc..00000000 --- a/opendc/api/v1/rooms/roomId/endpoint.py +++ /dev/null @@ -1,137 +0,0 @@ -from opendc.models.room import Room -from opendc.util import 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, - 'Successfully 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 deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/rooms/roomId/tiles/endpoint.py b/opendc/api/v1/rooms/roomId/tiles/endpoint.py deleted file mode 100644 index a4ef51e7..00000000 --- a/opendc/api/v1/rooms/roomId/tiles/endpoint.py +++ /dev/null @@ -1,121 +0,0 @@ -from opendc.models.room import Room -from opendc.models.tile import Tile -from opendc.util import 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/schedulers/__init__.py b/opendc/api/v1/schedulers/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/schedulers/endpoint.py b/opendc/api/v1/schedulers/endpoint.py deleted file mode 100644 index 36537764..00000000 --- a/opendc/api/v1/schedulers/endpoint.py +++ /dev/null @@ -1,18 +0,0 @@ -from opendc.models.scheduler import Scheduler -from opendc.util.rest import Response - - -def GET(request): - """Get all available Schedulers.""" - - # Get the Schedulers - - schedulers = Scheduler.query() - - # Return the Schedulers - - return Response( - 200, - 'Successfully retrieved Schedulers.', - [x.to_JSON() for x in schedulers] - ) diff --git a/opendc/api/v1/sections/__init__.py b/opendc/api/v1/sections/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/sections/sectionId/__init__.py b/opendc/api/v1/sections/sectionId/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/sections/sectionId/endpoint.py b/opendc/api/v1/sections/sectionId/endpoint.py deleted file mode 100644 index 94c81d37..00000000 --- a/opendc/api/v1/sections/sectionId/endpoint.py +++ /dev/null @@ -1,42 +0,0 @@ -from opendc.models.section import Section -from opendc.util import 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/__init__.py b/opendc/api/v1/simulations/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/endpoint.py b/opendc/api/v1/simulations/endpoint.py deleted file mode 100644 index a8637728..00000000 --- a/opendc/api/v1/simulations/endpoint.py +++ /dev/null @@ -1,87 +0,0 @@ -from datetime import datetime - -from opendc.models.authorization import Authorization -from opendc.models.datacenter import Datacenter -from opendc.models.path import Path -from opendc.models.section import Section -from opendc.models.simulation import Simulation -from opendc.models.user import User -from opendc.util import database, exceptions -from opendc.util.rest import Response - - -def POST(request): - """Create a new simulation, and return that new simulation.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - body={ - 'simulation': { - 'name': 'string' - } - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Simulation - - simulation_data = request.params_body['simulation'] - - simulation_data['datetimeCreated'] = database.datetime_to_string(datetime.now()) - simulation_data['datetimeLastEdited'] = database.datetime_to_string(datetime.now()) - - simulation = Simulation.from_JSON(simulation_data) - - # Insert this Simulation into the database - - simulation.insert() - - # Instantiate an Authorization and insert it into the database - - authorization = Authorization( - user_id=User.from_google_id(request.google_id).id, - simulation_id=simulation.id, - authorization_level='OWN' - ) - - authorization.insert() - - # Instantiate a Path and insert it into the database - - path = Path( - simulation_id=simulation.id, - datetime_created=database.datetime_to_string(datetime.now()) - ) - - path.insert() - - # Instantiate a Datacenter and insert it into the database - - datacenter = Datacenter( - starred=0, - simulation_id=simulation.id - ) - - datacenter.insert() - - # Instantiate a Section and insert it into the database - - section = Section( - path_id=path.id, - datacenter_id=datacenter.id, - start_tick=0 - ) - - section.insert() - - # Return this Simulation - - return Response( - 200, - 'Successfully created {}.'.format(simulation), - simulation.to_JSON() - ) diff --git a/opendc/api/v1/simulations/simulationId/__init__.py b/opendc/api/v1/simulations/simulationId/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/authorizations/__init__.py b/opendc/api/v1/simulations/simulationId/authorizations/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/authorizations/endpoint.py b/opendc/api/v1/simulations/simulationId/authorizations/endpoint.py deleted file mode 100644 index 1d6b494e..00000000 --- a/opendc/api/v1/simulations/simulationId/authorizations/endpoint.py +++ /dev/null @@ -1,44 +0,0 @@ -from opendc.models.authorization import Authorization -from opendc.models.simulation import Simulation -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Find all authorizations for a Simulation.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'simulationId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Simulation and make sure it exists - - simulation = Simulation.from_primary_key((request.params_path['simulationId'],)) - - if not simulation.exists(): - return Response(404, '{} not found.'.format(simulation)) - - # Make sure this User is allowed to view this Simulation's Authorizations - - if not simulation.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from retrieving Authorizations for {}.'.format(simulation)) - - # Get the Authorizations - - authorizations = Authorization.query('simulation_id', request.params_path['simulationId']) - - # Return the Authorizations - - return Response( - 200, - 'Successfully retrieved Authorizations for {}.'.format(simulation), - [x.to_JSON() for x in authorizations] - ) diff --git a/opendc/api/v1/simulations/simulationId/authorizations/userId/__init__.py b/opendc/api/v1/simulations/simulationId/authorizations/userId/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/authorizations/userId/endpoint.py b/opendc/api/v1/simulations/simulationId/authorizations/userId/endpoint.py deleted file mode 100644 index 46458ffc..00000000 --- a/opendc/api/v1/simulations/simulationId/authorizations/userId/endpoint.py +++ /dev/null @@ -1,212 +0,0 @@ -from opendc.models.authorization import Authorization -from opendc.models.simulation import Simulation -from opendc.models.user import User -from opendc.util import exceptions -from opendc.util.rest import Response - - -def DELETE(request): - """Delete a user's authorization level over a simulation.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'simulationId': 'int', - 'userId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate an Authorization - - authorization = Authorization.from_primary_key(( - request.params_path['userId'], - request.params_path['simulationId'] - )) - - # Make sure this Authorization exists in the database - - if not authorization.exists(): - return Response(404, '{} not found.'.format(authorization)) - - # Make sure this User is allowed to delete this Authorization - - if not authorization.google_id_has_at_least(request.google_id, 'OWN'): - return Response(403, 'Forbidden from deleting {}.'.format(authorization)) - - # Delete this Authorization - - authorization.delete() - - return Response( - 200, - 'Successfully deleted {}.'.format(authorization), - authorization.to_JSON() - ) - - -def GET(request): - """Get this User's Authorization over this Simulation.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'simulationId': 'int', - 'userId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate an Authorization - - authorization = Authorization.from_primary_key(( - request.params_path['userId'], - request.params_path['simulationId'] - )) - - # Make sure this Authorization exists in the database - - if not authorization.exists(): - return Response(404, '{} not found.'.format(authorization)) - - # Read this Authorization from the database - - authorization.read() - - # Return this Authorization - - return Response( - 200, - 'Successfully retrieved {}'.format(authorization), - authorization.to_JSON() - ) - - -def POST(request): - """Add an authorization for a user's access to a simulation.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'userId': 'int', - 'simulationId': 'int' - }, - body={ - 'authorization': { - 'authorizationLevel': 'string' - } - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate an Authorization - - authorization = Authorization.from_JSON({ - 'userId': request.params_path['userId'], - 'simulationId': request.params_path['simulationId'], - 'authorizationLevel': request.params_body['authorization']['authorizationLevel'] - }) - - # Make sure the Simulation and User exist - - user = User.from_primary_key((authorization.user_id,)) - if not user.exists(): - return Response(404, '{} not found.'.format(user)) - - simulation = Simulation.from_primary_key((authorization.simulation_id,)) - if not simulation.exists(): - return Response(404, '{} not found.'.format(simulation)) - - # Make sure this User is allowed to add this Authorization - - if not simulation.google_id_has_at_least(request.google_id, 'OWN'): - return Response(403, 'Forbidden from creating {}.'.format(authorization)) - - # Make sure this Authorization does not already exist - - if authorization.exists(): - return Response(409, '{} already exists.'.format(authorization)) - - # Try to insert this Authorization into the database - - try: - authorization.insert() - - except exceptions.ForeignKeyError: - return Response(400, 'Invalid authorizationLevel') - - # Return this Authorization - - return Response( - 200, - 'Successfully added {}'.format(authorization), - authorization.to_JSON() - ) - - -def PUT(request): - """Change a user's authorization level over a simulation.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'simulationId': 'int', - 'userId': 'int' - }, - body={ - 'authorization': { - 'authorizationLevel': 'string' - } - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate and Authorization - - authorization = Authorization.from_JSON({ - 'userId': request.params_path['userId'], - 'simulationId': request.params_path['simulationId'], - 'authorizationLevel': request.params_body['authorization']['authorizationLevel'] - }) - - # Make sure this Authorization exists - - if not authorization.exists(): - return Response(404, '{} not found.'.format(authorization)) - - # Make sure this User is allowed to edit this Authorization - - if not authorization.google_id_has_at_least(request.google_id, 'OWN'): - return Response(403, 'Forbidden from updating {}.'.format(authorization)) - - # Try to update this Authorization - - try: - authorization.update() - - except exceptions.ForeignKeyError as e: - return Response(400, 'Invalid authorization level.') - - # Return this Authorization - - return Response( - 200, - 'Successfully updated {}.'.format(authorization), - authorization.to_JSON() - ) diff --git a/opendc/api/v1/simulations/simulationId/datacenters/__init__.py b/opendc/api/v1/simulations/simulationId/datacenters/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/datacenters/endpoint.py b/opendc/api/v1/simulations/simulationId/datacenters/endpoint.py deleted file mode 100644 index e28402e4..00000000 --- a/opendc/api/v1/simulations/simulationId/datacenters/endpoint.py +++ /dev/null @@ -1,61 +0,0 @@ -from opendc.models.datacenter import Datacenter -from opendc.models.simulation import Simulation -from opendc.util import exceptions -from opendc.util.rest import Response - - -def POST(request): - """Add a new Datacenter to this Simulation.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'simulationId': 'int' - }, - body={ - 'datacenter': { - 'starred': 'int', - 'simulationId': 'int' - } - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Make sure the passed object's simulation id matches the path simulation id - - if request.params_path['simulationId'] != request.params_body['datacenter']['simulationId']: - return Response(400, 'ID mismatch.') - - # Instantiate a Simulation from the database - - simulation = Simulation.from_primary_key((request.params_path['simulationId'],)) - - # Make sure this Simulation exists - - if not simulation.exists(): - return Response(404, '{} not found.'.format(simulation)) - - # Make sure this user is authorized to edit this Simulation's Databases - - if not simulation.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from adding a datacenter to {}.'.format(simulation)) - - # Instantiate a Datacenter - - datacenter = Datacenter.from_JSON(request.params_body['datacenter']) - - datacenter.insert() - - # return this Datacenter - - datacenter.read() - - return Response( - 200, - 'Successfully added {}.'.format(datacenter), - datacenter.to_JSON() - ) diff --git a/opendc/api/v1/simulations/simulationId/endpoint.py b/opendc/api/v1/simulations/simulationId/endpoint.py deleted file mode 100644 index 5e595740..00000000 --- a/opendc/api/v1/simulations/simulationId/endpoint.py +++ /dev/null @@ -1,133 +0,0 @@ -from datetime import datetime - -from opendc.models.simulation import Simulation -from opendc.util import database, exceptions -from opendc.util.rest import Response - - -def DELETE(request): - """Delete this Simulation.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'simulationId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Simulation and make sure it exists - - simulation = Simulation.from_primary_key((request.params_path['simulationId'],)) - - if not simulation.exists(): - return Response(404, '{} not found.'.format(simulation)) - - # Make sure this User is allowed to delete this Simulation - - if not simulation.google_id_has_at_least(request.google_id, 'OWN'): - return Response(403, 'Forbidden from deleting {}.'.format(simulation)) - - # Delete this Simulation from the database - - simulation.delete() - - # Return this Simulation - - return Response( - 200, - 'Successfully deleted {}.'.format(simulation), - simulation.to_JSON() - ) - - -def GET(request): - """Get this Simulation.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'simulationId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Simulation and make sure it exists - - simulation = Simulation.from_primary_key((request.params_path['simulationId'],)) - - if not simulation.exists(): - return Response(404, '{} not found.'.format(simulation)) - - # Make sure this User is allowed to view this Simulation - - if not simulation.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from retrieving {}.'.format(simulation)) - - # Return this Simulation - - simulation.read() - - return Response( - 200, - 'Successfully retrieved {}'.format(simulation), - simulation.to_JSON() - ) - - -def PUT(request): - """Update a simulation's name.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - body={ - 'simulation': { - 'name': 'name' - } - }, - path={ - 'simulationId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Simulation and make sure it exists - - simulation = Simulation.from_primary_key((request.params_path['simulationId'],)) - - if not simulation.exists(): - return Response(404, '{} not found.'.format(simulation)) - - # Make sure this User is allowed to edit this Simulation - - if not simulation.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from editing {}.'.format(simulation)) - - # Update this Simulation in the database - - simulation.read() - - simulation.name = request.params_body['simulation']['name'] - simulation.datetime_last_edited = database.datetime_to_string(datetime.now()) - - simulation.update() - - # Return this Simulation - - return Response( - 200, - 'Successfully updated {}.'.format(simulation), - simulation.to_JSON() - ) diff --git a/opendc/api/v1/simulations/simulationId/experiments/__init__.py b/opendc/api/v1/simulations/simulationId/experiments/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/experiments/endpoint.py b/opendc/api/v1/simulations/simulationId/experiments/endpoint.py deleted file mode 100644 index 86fadb24..00000000 --- a/opendc/api/v1/simulations/simulationId/experiments/endpoint.py +++ /dev/null @@ -1,112 +0,0 @@ -from opendc.models.experiment import Experiment -from opendc.models.simulation import Simulation -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get this Simulation's Experiments.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'simulationId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Simulation from the database - - simulation = Simulation.from_primary_key((request.params_path['simulationId'],)) - - # Make sure this Simulation exists - - if not simulation.exists(): - return Response(404, '{} not found.'.format(simulation)) - - # Make sure this user is authorized to view this Simulation's Experiments - - if not simulation.google_id_has_at_least(request.google_id, 'VIEW'): - return Reponse(403, 'Forbidden from viewing Experiments for {}.'.format(simulation)) - - # Get and return the Experiments - - experiments = Experiment.query('simulation_id', request.params_path['simulationId']) - - return Response( - 200, - 'Successfully retrieved Experiments for {}.'.format(simulation), - [x.to_JSON() for x in experiments] - ) - - -def POST(request): - """Add a new Experiment for this Simulation.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'simulationId': 'int' - }, - body={ - 'experiment': { - 'simulationId': 'int', - 'pathId': 'int', - 'traceId': 'int', - 'schedulerName': 'string', - 'name': 'string' - } - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Make sure the passed object's simulation id matches the path simulation id - - if request.params_path['simulationId'] != request.params_body['experiment']['simulationId']: - return Response(403, 'ID mismatch.') - - # Instantiate a Simulation from the database - - simulation = Simulation.from_primary_key((request.params_path['simulationId'],)) - - # Make sure this Simulation exists - - if not simulation.exists(): - return Response(404, '{} not found.'.format(simulation)) - - # Make sure this user is authorized to edit this Simulation's Experiments - - if not simulation.google_id_has_at_least(request.google_id, 'EDIT'): - return Response(403, 'Forbidden from adding an experiment to {}.'.format(simulation)) - - # Instantiate an Experiment - - experiment = Experiment.from_JSON(request.params_body['experiment']) - experiment.state = 'QUEUED' - experiment.last_simulated_tick = 0 - - # Try to insert this Experiment - - try: - experiment.insert() - - except exceptions.ForeignKeyError as e: - return Response(400, 'Foreign key constraint not met.' + e.message) - - # Return this Experiment - - experiment.read() - - return Response( - 200, - 'Successfully added {}.'.format(experiment), - experiment.to_JSON() - ) diff --git a/opendc/api/v1/simulations/simulationId/paths/__init__.py b/opendc/api/v1/simulations/simulationId/paths/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/simulations/simulationId/paths/endpoint.py b/opendc/api/v1/simulations/simulationId/paths/endpoint.py deleted file mode 100644 index e4aab427..00000000 --- a/opendc/api/v1/simulations/simulationId/paths/endpoint.py +++ /dev/null @@ -1,43 +0,0 @@ -from opendc.models.path import Path -from opendc.models.simulation import Simulation -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get this Simulation's Paths.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'simulationId': 'int' - } - ) - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Simulation from the database - - simulation = Simulation.from_primary_key((request.params_path['simulationId'],)) - - # Make sure this Simulation exists - - if not simulation.exists(): - return Response(404, '{} not found.'.format(simulation)) - - # Make sure this user is authorized to view this Simulation's path - - if not simulation.google_id_has_at_least(request.google_id, 'VIEW'): - return Response(403, 'Forbidden from viewing Paths for {}.'.format(simulation)) - - # Get and return the Paths - - paths = Path.query('simulation_id', request.params_path['simulationId']) - - return Response( - 200, - 'Successfully retrieved Paths for {}.'.format(simulation), - [x.to_JSON() for x in paths] - ) diff --git a/opendc/api/v1/specifications/__init__.py b/opendc/api/v1/specifications/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/specifications/cpus/__init__.py b/opendc/api/v1/specifications/cpus/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/specifications/cpus/endpoint.py b/opendc/api/v1/specifications/cpus/endpoint.py deleted file mode 100644 index 5cdbb9ec..00000000 --- a/opendc/api/v1/specifications/cpus/endpoint.py +++ /dev/null @@ -1,18 +0,0 @@ -from opendc.models.cpu import CPU -from opendc.util.rest import Response - - -def GET(request): - """Get a list of the specifications of all CPUs.""" - - # Get the CPUs - - cpus = CPU.query() - - # Return the CPUs - - return Response( - 200, - 'Successfully retrieved CPUs.', - [x.to_JSON() for x in cpus] - ) diff --git a/opendc/api/v1/specifications/cpus/id/__init__.py b/opendc/api/v1/specifications/cpus/id/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/specifications/cpus/id/endpoint.py b/opendc/api/v1/specifications/cpus/id/endpoint.py deleted file mode 100644 index c2453e51..00000000 --- a/opendc/api/v1/specifications/cpus/id/endpoint.py +++ /dev/null @@ -1,34 +0,0 @@ -from opendc.models.cpu import CPU -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get the specs of a CPU.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'id': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a CPU and make sure it exists - - cpu = CPU.from_primary_key((request.params_path['id'],)) - - if not cpu.exists(): - return Response(404, '{} not found.'.format(cpu)) - - # Return this CPU - - return Response( - 200, - 'Successfully retrieved {}.'.format(cpu), - cpu.to_JSON() - ) diff --git a/opendc/api/v1/specifications/failure-models/__init__.py b/opendc/api/v1/specifications/failure-models/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/specifications/failure-models/endpoint.py b/opendc/api/v1/specifications/failure-models/endpoint.py deleted file mode 100644 index fff668c0..00000000 --- a/opendc/api/v1/specifications/failure-models/endpoint.py +++ /dev/null @@ -1,18 +0,0 @@ -from opendc.models.failure_model import FailureModel -from opendc.util.rest import Response - - -def GET(request): - """Get all Failure Models.""" - - # Get the FailureModels - - failure_models = FailureModel.query() - - # Return the FailureModels - - return Response( - 200, - 'Successfully retrieved FailureModels.', - [x.to_JSON() for x in failure_models] - ) diff --git a/opendc/api/v1/specifications/failure-models/id/__init__.py b/opendc/api/v1/specifications/failure-models/id/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/specifications/failure-models/id/endpoint.py b/opendc/api/v1/specifications/failure-models/id/endpoint.py deleted file mode 100644 index 0797c9c9..00000000 --- a/opendc/api/v1/specifications/failure-models/id/endpoint.py +++ /dev/null @@ -1,34 +0,0 @@ -from opendc.models.failure_model import FailureModel -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get this Failure Model.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'id': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a FailureModel and make sure it exists - - failure_model = FailureModel.from_primary_key((request.params_path['id'],)) - - if not failure_model.exists(): - return Response(404, '{} not found.'.format(failure_model)) - - # Return this FailureModel - - return Response( - 200, - 'Successfully retrieved {}.'.format(failure_model), - failure_model.to_JSON() - ) diff --git a/opendc/api/v1/specifications/gpus/__init__.py b/opendc/api/v1/specifications/gpus/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/specifications/gpus/endpoint.py b/opendc/api/v1/specifications/gpus/endpoint.py deleted file mode 100644 index 5676f62b..00000000 --- a/opendc/api/v1/specifications/gpus/endpoint.py +++ /dev/null @@ -1,18 +0,0 @@ -from opendc.models.gpu import GPU -from opendc.util.rest import Response - - -def GET(request): - """Get a list of the specifications of all GPUs.""" - - # Get the GPUs - - gpus = GPU.query() - - # Return the GPUs - - return Response( - 200, - 'Successfully retrieved GPUs.', - [x.to_JSON() for x in gpus] - ) diff --git a/opendc/api/v1/specifications/gpus/id/__init__.py b/opendc/api/v1/specifications/gpus/id/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/specifications/gpus/id/endpoint.py b/opendc/api/v1/specifications/gpus/id/endpoint.py deleted file mode 100644 index 81113dc3..00000000 --- a/opendc/api/v1/specifications/gpus/id/endpoint.py +++ /dev/null @@ -1,34 +0,0 @@ -from opendc.models.gpu import GPU -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get the specs of a GPU.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'id': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a GPU and make sure it exists - - gpu = GPU.from_primary_key((request.params_path['id'],)) - - if not gpu.exists(): - return Response(404, '{} not found.'.format(gpu)) - - # Return this GPU - - return Response( - 200, - 'Successfully retrieved {}.'.format(gpu), - gpu.to_JSON() - ) diff --git a/opendc/api/v1/specifications/memories/__init__.py b/opendc/api/v1/specifications/memories/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/specifications/memories/endpoint.py b/opendc/api/v1/specifications/memories/endpoint.py deleted file mode 100644 index 271824b3..00000000 --- a/opendc/api/v1/specifications/memories/endpoint.py +++ /dev/null @@ -1,18 +0,0 @@ -from opendc.models.memory import Memory -from opendc.util.rest import Response - - -def GET(request): - """Get a list of the specifications of all Memories.""" - - # Get the Memories - - memories = Memory.query() - - # Return the Memories - - return Response( - 200, - 'Successfully retrieved Memories.', - [x.to_JSON() for x in memories] - ) diff --git a/opendc/api/v1/specifications/memories/id/__init__.py b/opendc/api/v1/specifications/memories/id/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/specifications/memories/id/endpoint.py b/opendc/api/v1/specifications/memories/id/endpoint.py deleted file mode 100644 index 863099ca..00000000 --- a/opendc/api/v1/specifications/memories/id/endpoint.py +++ /dev/null @@ -1,34 +0,0 @@ -from opendc.models.memory import Memory -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get the specs of a Memory.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'id': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Memory and make sure it exists - - memory = Memory.from_primary_key((request.params_path['id'],)) - - if not memory.exists(): - return Response(404, '{} not found.'.format(memory)) - - # Return this Memory - - return Response( - 200, - 'Successfully retrieved {}.'.format(memory), - memory.to_JSON() - ) diff --git a/opendc/api/v1/specifications/storages/__init__.py b/opendc/api/v1/specifications/storages/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/specifications/storages/endpoint.py b/opendc/api/v1/specifications/storages/endpoint.py deleted file mode 100644 index 28f33177..00000000 --- a/opendc/api/v1/specifications/storages/endpoint.py +++ /dev/null @@ -1,18 +0,0 @@ -from opendc.models.storage import Storage -from opendc.util.rest import Response - - -def GET(request): - """Get a list of the specifications of all Storages.""" - - # Get the Storages - - storages = Storage.query() - - # Return the Storages - - return Response( - 200, - 'Successfully retrieved Storages.', - [x.to_JSON() for x in storages] - ) diff --git a/opendc/api/v1/specifications/storages/id/__init__.py b/opendc/api/v1/specifications/storages/id/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/specifications/storages/id/endpoint.py b/opendc/api/v1/specifications/storages/id/endpoint.py deleted file mode 100644 index ebe65857..00000000 --- a/opendc/api/v1/specifications/storages/id/endpoint.py +++ /dev/null @@ -1,34 +0,0 @@ -from opendc.models.storage import Storage -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get the specs of a Storage.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'id': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Storage and make sure it exists - - storage = Storage.from_primary_key((request.params_path['id'],)) - - if not storage.exists(): - return Response(404, '{} not found.'.format(storage)) - - # Return this CPU - - return Response( - 200, - 'Successfully retrieved {}.'.format(storage), - storage.to_JSON() - ) diff --git a/opendc/api/v1/tiles/__init__.py b/opendc/api/v1/tiles/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/tiles/tileId/__init__.py b/opendc/api/v1/tiles/tileId/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/tiles/tileId/endpoint.py b/opendc/api/v1/tiles/tileId/endpoint.py deleted file mode 100644 index 5ccc9cd7..00000000 --- a/opendc/api/v1/tiles/tileId/endpoint.py +++ /dev/null @@ -1,85 +0,0 @@ -from opendc.models.tile import Tile -from opendc.util import 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 deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/tiles/tileId/rack/endpoint.py b/opendc/api/v1/tiles/tileId/rack/endpoint.py deleted file mode 100644 index 64245856..00000000 --- a/opendc/api/v1/tiles/tileId/rack/endpoint.py +++ /dev/null @@ -1,237 +0,0 @@ -from opendc.models.rack import Rack -from opendc.models.tile import Tile -from opendc.util import 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 deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/tiles/tileId/rack/machines/endpoint.py b/opendc/api/v1/tiles/tileId/rack/machines/endpoint.py deleted file mode 100644 index 5272c117..00000000 --- a/opendc/api/v1/tiles/tileId/rack/machines/endpoint.py +++ /dev/null @@ -1,118 +0,0 @@ -from opendc.models.machine import Machine -from opendc.models.rack import Rack -from opendc.util import 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 deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/tiles/tileId/rack/machines/position/endpoint.py b/opendc/api/v1/tiles/tileId/rack/machines/position/endpoint.py deleted file mode 100644 index 99011fa4..00000000 --- a/opendc/api/v1/tiles/tileId/rack/machines/position/endpoint.py +++ /dev/null @@ -1,164 +0,0 @@ -from opendc.models.machine import Machine -from opendc.models.rack import Rack -from opendc.util import 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.position = 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/__init__.py b/opendc/api/v1/traces/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/traces/endpoint.py b/opendc/api/v1/traces/endpoint.py deleted file mode 100644 index 78930b0f..00000000 --- a/opendc/api/v1/traces/endpoint.py +++ /dev/null @@ -1,18 +0,0 @@ -from opendc.models.trace import Trace -from opendc.util.rest import Response - - -def GET(request): - """Get all available Traces.""" - - # Get the Traces - - traces = Trace.query() - - # Return the Traces - - return Response( - 200, - 'Successfully retrieved Traces', - [x.to_JSON() for x in traces] - ) diff --git a/opendc/api/v1/traces/traceId/__init__.py b/opendc/api/v1/traces/traceId/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/traces/traceId/endpoint.py b/opendc/api/v1/traces/traceId/endpoint.py deleted file mode 100644 index 50993c41..00000000 --- a/opendc/api/v1/traces/traceId/endpoint.py +++ /dev/null @@ -1,34 +0,0 @@ -from opendc.models.trace import Trace -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get this Trace.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'traceId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Trace and make sure it exists - - trace = Trace.from_primary_key((request.params_path['traceId'],)) - - if not trace.exists(): - return Response(404, '{} not found.'.format(trace)) - - # Return this Trace - - return Response( - 200, - 'Successfully retrieved {}.'.format(trace), - trace.to_JSON() - ) diff --git a/opendc/api/v1/traces/traceId/jobs/__init__.py b/opendc/api/v1/traces/traceId/jobs/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/traces/traceId/jobs/endpoint.py b/opendc/api/v1/traces/traceId/jobs/endpoint.py deleted file mode 100644 index bd2c6eb0..00000000 --- a/opendc/api/v1/traces/traceId/jobs/endpoint.py +++ /dev/null @@ -1,37 +0,0 @@ -from opendc.models.job import Job -from opendc.models.trace import Trace -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get this Trace's Jobs.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'traceId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a Trace and make sure it exists - - trace = Trace.from_primary_key((request.params_path['traceId'],)) - - if not trace.exists(): - return Response(404, '{} not found.'.format(trace)) - - # Get and return the Jobs - - jobs = Job.query('trace_id', request.params_path['traceId']) - - return Response( - 200, - 'Successfully retrieved Jobs for {}.'.format(trace), - [x.to_JSON() for x in jobs] - ) diff --git a/opendc/api/v1/users/__init__.py b/opendc/api/v1/users/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/users/endpoint.py b/opendc/api/v1/users/endpoint.py deleted file mode 100644 index abd54f27..00000000 --- a/opendc/api/v1/users/endpoint.py +++ /dev/null @@ -1,82 +0,0 @@ -from opendc.models.user import User -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Search for a User using their email address.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - query={ - 'email': 'string' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate and read a User from the database - - user = User.from_email(request.params_query['email']) - - # Make sure this User exists in the database - - if not user.exists(): - return Response(404, '{} not found'.format(user)) - - # Return this User - - return Response( - 200, - 'Successfully retrieved {}.'.format(user), - user.to_JSON() - ) - - -def POST(request): - """Add a new User.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - body={ - 'user': { - 'email': 'string' - } - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a User - - request.params_body['user']['googleId'] = request.google_id - user = User.from_JSON(request.params_body['user']) - - # Make sure a User with this Google ID does not already exist - - if user.exists('google_id'): - user = user.from_google_id(user.google_id) - return Response(409, '{} already exists.'.format(user)) - - # Make sure this User is authorized to create this User - - if not request.google_id == user.google_id: - return Response(403, 'Forbidden from creating this User.') - - # Insert the User - - user.insert() - - # Return a JSON representation of the User - - return Response( - 200, - 'Successfully created {}'.format(user), - user.to_JSON() - ) diff --git a/opendc/api/v1/users/userId/__init__.py b/opendc/api/v1/users/userId/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/users/userId/authorizations/__init__.py b/opendc/api/v1/users/userId/authorizations/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/opendc/api/v1/users/userId/authorizations/endpoint.py b/opendc/api/v1/users/userId/authorizations/endpoint.py deleted file mode 100644 index 46ca12ba..00000000 --- a/opendc/api/v1/users/userId/authorizations/endpoint.py +++ /dev/null @@ -1,42 +0,0 @@ -from opendc.models.authorization import Authorization -from opendc.models.user import User -from opendc.util import exceptions -from opendc.util.rest import Response - - -def GET(request): - """Get this User's Authorizations.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'userId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a User and make sure they exist - - user = User.from_primary_key((request.params_path['userId'],)) - - if not user.exists(): - return Response(404, '{} not found.'.format(user)) - - # Make sure this requester is allowed to retrieve this User's Authorizations - - if not user.google_id_has_at_least(request.google_id, 'OWN'): - return Response(403, 'Forbidden from retrieving Authorizations for {}.'.format(user)) - - # Return this User's Authorizations - - authorizations = Authorization.query('user_id', request.params_path['userId']) - - return Response( - 200, - 'Successfully retrieved Authorizations for {}.'.format(user), - [x.to_JSON() for x in authorizations] - ) diff --git a/opendc/api/v1/users/userId/endpoint.py b/opendc/api/v1/users/userId/endpoint.py deleted file mode 100644 index 767c5d13..00000000 --- a/opendc/api/v1/users/userId/endpoint.py +++ /dev/null @@ -1,123 +0,0 @@ -from opendc.models.user import User -from opendc.util import exceptions -from opendc.util.rest import Response - - -def DELETE(request): - """Delete this user.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'userId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a User and make sure they exist - - user = User.from_primary_key((request.params_path['userId'],)) - - if not user.exists(): - return Response(404, '{} not found'.format(user)) - - # Make sure this User is allowed to delete this User - - if not user.google_id_has_at_least(request.google_id, 'OWN'): - return Response(403, 'Forbidden from deleting {}.'.format(user)) - - # Delete this User - - user.delete() - - # Return this User - - return Response( - 200, - 'Successfully deleted {}'.format(user), - user.to_JSON() - ) - - -def GET(request): - """Get this User.""" - - # Make sure required parameters are there - - try: - request.check_required_parameters( - path={ - 'userId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a User and make sure they exist - - user = User.from_primary_key((request.params_path['userId'],)) - - if not user.exists(): - return Response(404, '{} not found.'.format(user)) - - # Return this User - - return Response( - 200, - 'Successfully retrieved {}'.format(user), - user.to_JSON(), - ) - - -def PUT(request): - """Update this User's given name and/ or family name.""" - - # Make sure the required parameters are there - - try: - request.check_required_parameters( - body={ - 'user': { - 'givenName': 'string', - 'familyName': 'string' - } - }, - path={ - 'userId': 'int' - } - ) - - except exceptions.ParameterError as e: - return Response(400, e.message) - - # Instantiate a User and make sure they exist - - user = User.from_primary_key((request.params_path['userId'],)) - - if not user.exists(): - return Response(404, '{} not found.'.format(user)) - - # Make sure this User is allowed to edit this User - - if not user.google_id_has_at_least(request.google_id, 'OWN'): - return Response(403, 'Forbidden from editing {}.'.format(user)) - - # Update this User - - user.given_name = request.params_body['user']['givenName'] - user.family_name = request.params_body['user']['familyName'] - - user.update() - - # Return this User - - return Response( - 200, - 'Successfully updated {}.'.format(user), - user.to_JSON() - ) diff --git a/opendc/api/v2/__init__.py b/opendc/api/v2/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/datacenters/__init__.py b/opendc/api/v2/datacenters/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/datacenters/datacenterId/__init__.py b/opendc/api/v2/datacenters/datacenterId/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/datacenters/datacenterId/endpoint.py b/opendc/api/v2/datacenters/datacenterId/endpoint.py new file mode 100644 index 00000000..9444fb80 --- /dev/null +++ b/opendc/api/v2/datacenters/datacenterId/endpoint.py @@ -0,0 +1,43 @@ +from opendc.models.datacenter import Datacenter +from opendc.util import 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/v2/datacenters/datacenterId/rooms/__init__.py b/opendc/api/v2/datacenters/datacenterId/rooms/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/datacenters/datacenterId/rooms/endpoint.py b/opendc/api/v2/datacenters/datacenterId/rooms/endpoint.py new file mode 100644 index 00000000..e0155c7c --- /dev/null +++ b/opendc/api/v2/datacenters/datacenterId/rooms/endpoint.py @@ -0,0 +1,111 @@ +from opendc.models.datacenter import Datacenter +from opendc.models.room import Room +from opendc.util import 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 'name' not 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: + 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/v2/experiments/__init__.py b/opendc/api/v2/experiments/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/experiments/experimentId/__init__.py b/opendc/api/v2/experiments/experimentId/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/experiments/experimentId/endpoint.py b/opendc/api/v2/experiments/experimentId/endpoint.py new file mode 100644 index 00000000..59e0e0fe --- /dev/null +++ b/opendc/api/v2/experiments/experimentId/endpoint.py @@ -0,0 +1,138 @@ +from opendc.models.experiment import Experiment +from opendc.util import 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/v2/experiments/experimentId/last-simulated-tick/__init__.py b/opendc/api/v2/experiments/experimentId/last-simulated-tick/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/experiments/experimentId/last-simulated-tick/endpoint.py b/opendc/api/v2/experiments/experimentId/last-simulated-tick/endpoint.py new file mode 100644 index 00000000..b4b19f59 --- /dev/null +++ b/opendc/api/v2/experiments/experimentId/last-simulated-tick/endpoint.py @@ -0,0 +1,39 @@ +from opendc.models.experiment import Experiment +from opendc.util import 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 exists + + 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/v2/experiments/experimentId/machine-states/__init__.py b/opendc/api/v2/experiments/experimentId/machine-states/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/experiments/experimentId/machine-states/endpoint.py b/opendc/api/v2/experiments/experimentId/machine-states/endpoint.py new file mode 100644 index 00000000..8ad588d2 --- /dev/null +++ b/opendc/api/v2/experiments/experimentId/machine-states/endpoint.py @@ -0,0 +1,51 @@ +from opendc.models.experiment import Experiment +from opendc.models.machine_state import MachineState +from opendc.util import 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 exists + + 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/v2/experiments/experimentId/rack-states/__init__.py b/opendc/api/v2/experiments/experimentId/rack-states/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/experiments/experimentId/rack-states/endpoint.py b/opendc/api/v2/experiments/experimentId/rack-states/endpoint.py new file mode 100644 index 00000000..03570039 --- /dev/null +++ b/opendc/api/v2/experiments/experimentId/rack-states/endpoint.py @@ -0,0 +1,51 @@ +from opendc.models.experiment import Experiment +from opendc.models.rack_state import RackState +from opendc.util import 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 exists + + 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/v2/experiments/experimentId/room-states/__init__.py b/opendc/api/v2/experiments/experimentId/room-states/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/experiments/experimentId/room-states/endpoint.py b/opendc/api/v2/experiments/experimentId/room-states/endpoint.py new file mode 100644 index 00000000..2693dc89 --- /dev/null +++ b/opendc/api/v2/experiments/experimentId/room-states/endpoint.py @@ -0,0 +1,51 @@ +from opendc.models.experiment import Experiment +from opendc.models.room_state import RoomState +from opendc.util import 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 exists + + 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/v2/experiments/experimentId/statistics/__init__.py b/opendc/api/v2/experiments/experimentId/statistics/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/experiments/experimentId/statistics/task-durations/__init__.py b/opendc/api/v2/experiments/experimentId/statistics/task-durations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/experiments/experimentId/statistics/task-durations/endpoint.py b/opendc/api/v2/experiments/experimentId/statistics/task-durations/endpoint.py new file mode 100644 index 00000000..b8311f08 --- /dev/null +++ b/opendc/api/v2/experiments/experimentId/statistics/task-durations/endpoint.py @@ -0,0 +1,44 @@ +from opendc.models.experiment import Experiment +from opendc.models.task_duration import TaskDuration +from opendc.util import 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 exists + + 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/v2/experiments/experimentId/task-states/__init__.py b/opendc/api/v2/experiments/experimentId/task-states/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/experiments/experimentId/task-states/endpoint.py b/opendc/api/v2/experiments/experimentId/task-states/endpoint.py new file mode 100644 index 00000000..0c3fae89 --- /dev/null +++ b/opendc/api/v2/experiments/experimentId/task-states/endpoint.py @@ -0,0 +1,51 @@ +from opendc.models.experiment import Experiment +from opendc.models.task_state import TaskState +from opendc.util import 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/v2/jobs/__init__.py b/opendc/api/v2/jobs/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/jobs/jobId/__init__.py b/opendc/api/v2/jobs/jobId/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/jobs/jobId/endpoint.py b/opendc/api/v2/jobs/jobId/endpoint.py new file mode 100644 index 00000000..da4dcd9d --- /dev/null +++ b/opendc/api/v2/jobs/jobId/endpoint.py @@ -0,0 +1,34 @@ +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/v2/jobs/jobId/tasks/__init__.py b/opendc/api/v2/jobs/jobId/tasks/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/jobs/jobId/tasks/endpoint.py b/opendc/api/v2/jobs/jobId/tasks/endpoint.py new file mode 100644 index 00000000..04ac5b8c --- /dev/null +++ b/opendc/api/v2/jobs/jobId/tasks/endpoint.py @@ -0,0 +1,37 @@ +from opendc.models.job import Job +from opendc.models.task import Task +from opendc.util import 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/v2/paths.json b/opendc/api/v2/paths.json new file mode 100644 index 00000000..bac7fc71 --- /dev/null +++ b/opendc/api/v2/paths.json @@ -0,0 +1,53 @@ +[ + "/users", + "/users/{userId}", + "/users/{userId}/authorizations", + "/simulations", + "/simulations/{simulationId}", + "/simulations/{simulationId}/authorizations", + "/simulations/{simulationId}/authorizations/{userId}", + "/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", + "/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", + "/paths/{pathId}", + "/paths/{pathId}/branches", + "/paths/{pathId}/sections", + "/sections/{sectionId}", + "/schedulers", + "/traces", + "/traces/{traceId}", + "/traces/{traceId}/jobs", + "/jobs/{jobId}", + "/jobs/{jobId}/tasks", + "/room-types", + "/room-types/{name}/allowed-objects", + "/specifications/cooling-items", + "/specifications/cooling-items/{id}", + "/specifications/cpus", + "/specifications/cpus/{id}", + "/specifications/failure-models", + "/specifications/failure-models/{id}", + "/specifications/gpus", + "/specifications/gpus/{id}", + "/specifications/memories", + "/specifications/memories/{id}", + "/specifications/psus", + "/specifications/psus/{id}", + "/specifications/storages", + "/specifications/storages/{id}" +] \ No newline at end of file diff --git a/opendc/api/v2/paths/__init__.py b/opendc/api/v2/paths/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/paths/pathId/__init__.py b/opendc/api/v2/paths/pathId/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/paths/pathId/branches/__init__.py b/opendc/api/v2/paths/pathId/branches/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/paths/pathId/branches/endpoint.py b/opendc/api/v2/paths/pathId/branches/endpoint.py new file mode 100644 index 00000000..13638ffa --- /dev/null +++ b/opendc/api/v2/paths/pathId/branches/endpoint.py @@ -0,0 +1,176 @@ +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'] + + if old_room.topology_id is None: + old_room.topology_id = old_room.id + + 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'] + + if old_tile.topology_id is None: + old_tile.topology_id = old_tile.id + + 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] + + if old_rack.topology_id is None: + old_rack.topology_id = old_rack.id + + 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'] + + if old_machine.topology_id is None: + old_machine.topology_id = old_machine.id + + 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/v2/paths/pathId/endpoint.py b/opendc/api/v2/paths/pathId/endpoint.py new file mode 100644 index 00000000..7ade19ce --- /dev/null +++ b/opendc/api/v2/paths/pathId/endpoint.py @@ -0,0 +1,43 @@ +from opendc.models.path import Path +from opendc.util import 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/v2/paths/pathId/sections/__init__.py b/opendc/api/v2/paths/pathId/sections/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/paths/pathId/sections/endpoint.py b/opendc/api/v2/paths/pathId/sections/endpoint.py new file mode 100644 index 00000000..d4161839 --- /dev/null +++ b/opendc/api/v2/paths/pathId/sections/endpoint.py @@ -0,0 +1,43 @@ +from opendc.models.path import Path +from opendc.models.section import Section +from opendc.util import 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/v2/room-types/__init__.py b/opendc/api/v2/room-types/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/room-types/endpoint.py b/opendc/api/v2/room-types/endpoint.py new file mode 100644 index 00000000..fe00f226 --- /dev/null +++ b/opendc/api/v2/room-types/endpoint.py @@ -0,0 +1,18 @@ +from opendc.models.room_type import RoomType +from opendc.util.rest import Response + + +def GET(request): + """Get all available room types.""" + + # Get the RoomTypes + + room_types = RoomType.query() + + # Return the RoomTypes + + return Response( + 200, + 'Successfully retrieved RoomTypes.', + [x.to_JSON() for x in room_types] + ) diff --git a/opendc/api/v2/room-types/name/__init__.py b/opendc/api/v2/room-types/name/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/room-types/name/allowed-objects/__init__.py b/opendc/api/v2/room-types/name/allowed-objects/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/room-types/name/allowed-objects/endpoint.py b/opendc/api/v2/room-types/name/allowed-objects/endpoint.py new file mode 100644 index 00000000..a574591c --- /dev/null +++ b/opendc/api/v2/room-types/name/allowed-objects/endpoint.py @@ -0,0 +1,31 @@ +from opendc.models.allowed_object import AllowedObject +from opendc.util import exceptions +from opendc.util.rest import Response + + +def GET(request): + """Get this room's allowed objects.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'name': 'string' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Get the AllowedObjects + + allowed_objects = AllowedObject.query('room_type', request.params_path['name']) + + # Return the AllowedObjects + + return Response( + 200, + 'Successfully retrieved AllowedObjects.', + [x.to_JSON() for x in allowed_objects] + ) diff --git a/opendc/api/v2/rooms/__init__.py b/opendc/api/v2/rooms/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/rooms/roomId/__init__.py b/opendc/api/v2/rooms/roomId/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/rooms/roomId/endpoint.py b/opendc/api/v2/rooms/roomId/endpoint.py new file mode 100644 index 00000000..1dfc32cc --- /dev/null +++ b/opendc/api/v2/rooms/roomId/endpoint.py @@ -0,0 +1,137 @@ +from opendc.models.room import Room +from opendc.util import 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, + 'Successfully deleted {}.'.format(room), + room.to_JSON() + ) diff --git a/opendc/api/v2/rooms/roomId/tiles/__init__.py b/opendc/api/v2/rooms/roomId/tiles/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/rooms/roomId/tiles/endpoint.py b/opendc/api/v2/rooms/roomId/tiles/endpoint.py new file mode 100644 index 00000000..a4ef51e7 --- /dev/null +++ b/opendc/api/v2/rooms/roomId/tiles/endpoint.py @@ -0,0 +1,121 @@ +from opendc.models.room import Room +from opendc.models.tile import Tile +from opendc.util import 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/v2/schedulers/__init__.py b/opendc/api/v2/schedulers/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/schedulers/endpoint.py b/opendc/api/v2/schedulers/endpoint.py new file mode 100644 index 00000000..36537764 --- /dev/null +++ b/opendc/api/v2/schedulers/endpoint.py @@ -0,0 +1,18 @@ +from opendc.models.scheduler import Scheduler +from opendc.util.rest import Response + + +def GET(request): + """Get all available Schedulers.""" + + # Get the Schedulers + + schedulers = Scheduler.query() + + # Return the Schedulers + + return Response( + 200, + 'Successfully retrieved Schedulers.', + [x.to_JSON() for x in schedulers] + ) diff --git a/opendc/api/v2/sections/__init__.py b/opendc/api/v2/sections/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/sections/sectionId/__init__.py b/opendc/api/v2/sections/sectionId/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/sections/sectionId/endpoint.py b/opendc/api/v2/sections/sectionId/endpoint.py new file mode 100644 index 00000000..94c81d37 --- /dev/null +++ b/opendc/api/v2/sections/sectionId/endpoint.py @@ -0,0 +1,42 @@ +from opendc.models.section import Section +from opendc.util import 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/v2/simulations/__init__.py b/opendc/api/v2/simulations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/simulations/endpoint.py b/opendc/api/v2/simulations/endpoint.py new file mode 100644 index 00000000..a8637728 --- /dev/null +++ b/opendc/api/v2/simulations/endpoint.py @@ -0,0 +1,87 @@ +from datetime import datetime + +from opendc.models.authorization import Authorization +from opendc.models.datacenter import Datacenter +from opendc.models.path import Path +from opendc.models.section import Section +from opendc.models.simulation import Simulation +from opendc.models.user import User +from opendc.util import database, exceptions +from opendc.util.rest import Response + + +def POST(request): + """Create a new simulation, and return that new simulation.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + body={ + 'simulation': { + 'name': 'string' + } + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Simulation + + simulation_data = request.params_body['simulation'] + + simulation_data['datetimeCreated'] = database.datetime_to_string(datetime.now()) + simulation_data['datetimeLastEdited'] = database.datetime_to_string(datetime.now()) + + simulation = Simulation.from_JSON(simulation_data) + + # Insert this Simulation into the database + + simulation.insert() + + # Instantiate an Authorization and insert it into the database + + authorization = Authorization( + user_id=User.from_google_id(request.google_id).id, + simulation_id=simulation.id, + authorization_level='OWN' + ) + + authorization.insert() + + # Instantiate a Path and insert it into the database + + path = Path( + simulation_id=simulation.id, + datetime_created=database.datetime_to_string(datetime.now()) + ) + + path.insert() + + # Instantiate a Datacenter and insert it into the database + + datacenter = Datacenter( + starred=0, + simulation_id=simulation.id + ) + + datacenter.insert() + + # Instantiate a Section and insert it into the database + + section = Section( + path_id=path.id, + datacenter_id=datacenter.id, + start_tick=0 + ) + + section.insert() + + # Return this Simulation + + return Response( + 200, + 'Successfully created {}.'.format(simulation), + simulation.to_JSON() + ) diff --git a/opendc/api/v2/simulations/simulationId/__init__.py b/opendc/api/v2/simulations/simulationId/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/simulations/simulationId/authorizations/__init__.py b/opendc/api/v2/simulations/simulationId/authorizations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py b/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py new file mode 100644 index 00000000..1d6b494e --- /dev/null +++ b/opendc/api/v2/simulations/simulationId/authorizations/endpoint.py @@ -0,0 +1,44 @@ +from opendc.models.authorization import Authorization +from opendc.models.simulation import Simulation +from opendc.util import exceptions +from opendc.util.rest import Response + + +def GET(request): + """Find all authorizations for a Simulation.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'simulationId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Simulation and make sure it exists + + simulation = Simulation.from_primary_key((request.params_path['simulationId'],)) + + if not simulation.exists(): + return Response(404, '{} not found.'.format(simulation)) + + # Make sure this User is allowed to view this Simulation's Authorizations + + if not simulation.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from retrieving Authorizations for {}.'.format(simulation)) + + # Get the Authorizations + + authorizations = Authorization.query('simulation_id', request.params_path['simulationId']) + + # Return the Authorizations + + return Response( + 200, + 'Successfully retrieved Authorizations for {}.'.format(simulation), + [x.to_JSON() for x in authorizations] + ) diff --git a/opendc/api/v2/simulations/simulationId/authorizations/userId/__init__.py b/opendc/api/v2/simulations/simulationId/authorizations/userId/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py b/opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py new file mode 100644 index 00000000..46458ffc --- /dev/null +++ b/opendc/api/v2/simulations/simulationId/authorizations/userId/endpoint.py @@ -0,0 +1,212 @@ +from opendc.models.authorization import Authorization +from opendc.models.simulation import Simulation +from opendc.models.user import User +from opendc.util import exceptions +from opendc.util.rest import Response + + +def DELETE(request): + """Delete a user's authorization level over a simulation.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'simulationId': 'int', + 'userId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate an Authorization + + authorization = Authorization.from_primary_key(( + request.params_path['userId'], + request.params_path['simulationId'] + )) + + # Make sure this Authorization exists in the database + + if not authorization.exists(): + return Response(404, '{} not found.'.format(authorization)) + + # Make sure this User is allowed to delete this Authorization + + if not authorization.google_id_has_at_least(request.google_id, 'OWN'): + return Response(403, 'Forbidden from deleting {}.'.format(authorization)) + + # Delete this Authorization + + authorization.delete() + + return Response( + 200, + 'Successfully deleted {}.'.format(authorization), + authorization.to_JSON() + ) + + +def GET(request): + """Get this User's Authorization over this Simulation.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'simulationId': 'int', + 'userId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate an Authorization + + authorization = Authorization.from_primary_key(( + request.params_path['userId'], + request.params_path['simulationId'] + )) + + # Make sure this Authorization exists in the database + + if not authorization.exists(): + return Response(404, '{} not found.'.format(authorization)) + + # Read this Authorization from the database + + authorization.read() + + # Return this Authorization + + return Response( + 200, + 'Successfully retrieved {}'.format(authorization), + authorization.to_JSON() + ) + + +def POST(request): + """Add an authorization for a user's access to a simulation.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'userId': 'int', + 'simulationId': 'int' + }, + body={ + 'authorization': { + 'authorizationLevel': 'string' + } + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate an Authorization + + authorization = Authorization.from_JSON({ + 'userId': request.params_path['userId'], + 'simulationId': request.params_path['simulationId'], + 'authorizationLevel': request.params_body['authorization']['authorizationLevel'] + }) + + # Make sure the Simulation and User exist + + user = User.from_primary_key((authorization.user_id,)) + if not user.exists(): + return Response(404, '{} not found.'.format(user)) + + simulation = Simulation.from_primary_key((authorization.simulation_id,)) + if not simulation.exists(): + return Response(404, '{} not found.'.format(simulation)) + + # Make sure this User is allowed to add this Authorization + + if not simulation.google_id_has_at_least(request.google_id, 'OWN'): + return Response(403, 'Forbidden from creating {}.'.format(authorization)) + + # Make sure this Authorization does not already exist + + if authorization.exists(): + return Response(409, '{} already exists.'.format(authorization)) + + # Try to insert this Authorization into the database + + try: + authorization.insert() + + except exceptions.ForeignKeyError: + return Response(400, 'Invalid authorizationLevel') + + # Return this Authorization + + return Response( + 200, + 'Successfully added {}'.format(authorization), + authorization.to_JSON() + ) + + +def PUT(request): + """Change a user's authorization level over a simulation.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'simulationId': 'int', + 'userId': 'int' + }, + body={ + 'authorization': { + 'authorizationLevel': 'string' + } + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate and Authorization + + authorization = Authorization.from_JSON({ + 'userId': request.params_path['userId'], + 'simulationId': request.params_path['simulationId'], + 'authorizationLevel': request.params_body['authorization']['authorizationLevel'] + }) + + # Make sure this Authorization exists + + if not authorization.exists(): + return Response(404, '{} not found.'.format(authorization)) + + # Make sure this User is allowed to edit this Authorization + + if not authorization.google_id_has_at_least(request.google_id, 'OWN'): + return Response(403, 'Forbidden from updating {}.'.format(authorization)) + + # Try to update this Authorization + + try: + authorization.update() + + except exceptions.ForeignKeyError as e: + return Response(400, 'Invalid authorization level.') + + # Return this Authorization + + return Response( + 200, + 'Successfully updated {}.'.format(authorization), + authorization.to_JSON() + ) diff --git a/opendc/api/v2/simulations/simulationId/datacenters/__init__.py b/opendc/api/v2/simulations/simulationId/datacenters/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/simulations/simulationId/datacenters/endpoint.py b/opendc/api/v2/simulations/simulationId/datacenters/endpoint.py new file mode 100644 index 00000000..e28402e4 --- /dev/null +++ b/opendc/api/v2/simulations/simulationId/datacenters/endpoint.py @@ -0,0 +1,61 @@ +from opendc.models.datacenter import Datacenter +from opendc.models.simulation import Simulation +from opendc.util import exceptions +from opendc.util.rest import Response + + +def POST(request): + """Add a new Datacenter to this Simulation.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'simulationId': 'int' + }, + body={ + 'datacenter': { + 'starred': 'int', + 'simulationId': 'int' + } + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Make sure the passed object's simulation id matches the path simulation id + + if request.params_path['simulationId'] != request.params_body['datacenter']['simulationId']: + return Response(400, 'ID mismatch.') + + # Instantiate a Simulation from the database + + simulation = Simulation.from_primary_key((request.params_path['simulationId'],)) + + # Make sure this Simulation exists + + if not simulation.exists(): + return Response(404, '{} not found.'.format(simulation)) + + # Make sure this user is authorized to edit this Simulation's Databases + + if not simulation.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from adding a datacenter to {}.'.format(simulation)) + + # Instantiate a Datacenter + + datacenter = Datacenter.from_JSON(request.params_body['datacenter']) + + datacenter.insert() + + # return this Datacenter + + datacenter.read() + + return Response( + 200, + 'Successfully added {}.'.format(datacenter), + datacenter.to_JSON() + ) diff --git a/opendc/api/v2/simulations/simulationId/endpoint.py b/opendc/api/v2/simulations/simulationId/endpoint.py new file mode 100644 index 00000000..5e595740 --- /dev/null +++ b/opendc/api/v2/simulations/simulationId/endpoint.py @@ -0,0 +1,133 @@ +from datetime import datetime + +from opendc.models.simulation import Simulation +from opendc.util import database, exceptions +from opendc.util.rest import Response + + +def DELETE(request): + """Delete this Simulation.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'simulationId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Simulation and make sure it exists + + simulation = Simulation.from_primary_key((request.params_path['simulationId'],)) + + if not simulation.exists(): + return Response(404, '{} not found.'.format(simulation)) + + # Make sure this User is allowed to delete this Simulation + + if not simulation.google_id_has_at_least(request.google_id, 'OWN'): + return Response(403, 'Forbidden from deleting {}.'.format(simulation)) + + # Delete this Simulation from the database + + simulation.delete() + + # Return this Simulation + + return Response( + 200, + 'Successfully deleted {}.'.format(simulation), + simulation.to_JSON() + ) + + +def GET(request): + """Get this Simulation.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'simulationId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Simulation and make sure it exists + + simulation = Simulation.from_primary_key((request.params_path['simulationId'],)) + + if not simulation.exists(): + return Response(404, '{} not found.'.format(simulation)) + + # Make sure this User is allowed to view this Simulation + + if not simulation.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from retrieving {}.'.format(simulation)) + + # Return this Simulation + + simulation.read() + + return Response( + 200, + 'Successfully retrieved {}'.format(simulation), + simulation.to_JSON() + ) + + +def PUT(request): + """Update a simulation's name.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + body={ + 'simulation': { + 'name': 'name' + } + }, + path={ + 'simulationId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Simulation and make sure it exists + + simulation = Simulation.from_primary_key((request.params_path['simulationId'],)) + + if not simulation.exists(): + return Response(404, '{} not found.'.format(simulation)) + + # Make sure this User is allowed to edit this Simulation + + if not simulation.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from editing {}.'.format(simulation)) + + # Update this Simulation in the database + + simulation.read() + + simulation.name = request.params_body['simulation']['name'] + simulation.datetime_last_edited = database.datetime_to_string(datetime.now()) + + simulation.update() + + # Return this Simulation + + return Response( + 200, + 'Successfully updated {}.'.format(simulation), + simulation.to_JSON() + ) diff --git a/opendc/api/v2/simulations/simulationId/experiments/__init__.py b/opendc/api/v2/simulations/simulationId/experiments/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/simulations/simulationId/experiments/endpoint.py b/opendc/api/v2/simulations/simulationId/experiments/endpoint.py new file mode 100644 index 00000000..86fadb24 --- /dev/null +++ b/opendc/api/v2/simulations/simulationId/experiments/endpoint.py @@ -0,0 +1,112 @@ +from opendc.models.experiment import Experiment +from opendc.models.simulation import Simulation +from opendc.util import exceptions +from opendc.util.rest import Response + + +def GET(request): + """Get this Simulation's Experiments.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'simulationId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Simulation from the database + + simulation = Simulation.from_primary_key((request.params_path['simulationId'],)) + + # Make sure this Simulation exists + + if not simulation.exists(): + return Response(404, '{} not found.'.format(simulation)) + + # Make sure this user is authorized to view this Simulation's Experiments + + if not simulation.google_id_has_at_least(request.google_id, 'VIEW'): + return Reponse(403, 'Forbidden from viewing Experiments for {}.'.format(simulation)) + + # Get and return the Experiments + + experiments = Experiment.query('simulation_id', request.params_path['simulationId']) + + return Response( + 200, + 'Successfully retrieved Experiments for {}.'.format(simulation), + [x.to_JSON() for x in experiments] + ) + + +def POST(request): + """Add a new Experiment for this Simulation.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'simulationId': 'int' + }, + body={ + 'experiment': { + 'simulationId': 'int', + 'pathId': 'int', + 'traceId': 'int', + 'schedulerName': 'string', + 'name': 'string' + } + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Make sure the passed object's simulation id matches the path simulation id + + if request.params_path['simulationId'] != request.params_body['experiment']['simulationId']: + return Response(403, 'ID mismatch.') + + # Instantiate a Simulation from the database + + simulation = Simulation.from_primary_key((request.params_path['simulationId'],)) + + # Make sure this Simulation exists + + if not simulation.exists(): + return Response(404, '{} not found.'.format(simulation)) + + # Make sure this user is authorized to edit this Simulation's Experiments + + if not simulation.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from adding an experiment to {}.'.format(simulation)) + + # Instantiate an Experiment + + experiment = Experiment.from_JSON(request.params_body['experiment']) + experiment.state = 'QUEUED' + experiment.last_simulated_tick = 0 + + # Try to insert this Experiment + + try: + experiment.insert() + + except exceptions.ForeignKeyError as e: + return Response(400, 'Foreign key constraint not met.' + e.message) + + # Return this Experiment + + experiment.read() + + return Response( + 200, + 'Successfully added {}.'.format(experiment), + experiment.to_JSON() + ) diff --git a/opendc/api/v2/simulations/simulationId/paths/__init__.py b/opendc/api/v2/simulations/simulationId/paths/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/simulations/simulationId/paths/endpoint.py b/opendc/api/v2/simulations/simulationId/paths/endpoint.py new file mode 100644 index 00000000..e4aab427 --- /dev/null +++ b/opendc/api/v2/simulations/simulationId/paths/endpoint.py @@ -0,0 +1,43 @@ +from opendc.models.path import Path +from opendc.models.simulation import Simulation +from opendc.util import exceptions +from opendc.util.rest import Response + + +def GET(request): + """Get this Simulation's Paths.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'simulationId': 'int' + } + ) + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Simulation from the database + + simulation = Simulation.from_primary_key((request.params_path['simulationId'],)) + + # Make sure this Simulation exists + + if not simulation.exists(): + return Response(404, '{} not found.'.format(simulation)) + + # Make sure this user is authorized to view this Simulation's path + + if not simulation.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from viewing Paths for {}.'.format(simulation)) + + # Get and return the Paths + + paths = Path.query('simulation_id', request.params_path['simulationId']) + + return Response( + 200, + 'Successfully retrieved Paths for {}.'.format(simulation), + [x.to_JSON() for x in paths] + ) diff --git a/opendc/api/v2/specifications/__init__.py b/opendc/api/v2/specifications/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/specifications/cpus/__init__.py b/opendc/api/v2/specifications/cpus/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/specifications/cpus/endpoint.py b/opendc/api/v2/specifications/cpus/endpoint.py new file mode 100644 index 00000000..5cdbb9ec --- /dev/null +++ b/opendc/api/v2/specifications/cpus/endpoint.py @@ -0,0 +1,18 @@ +from opendc.models.cpu import CPU +from opendc.util.rest import Response + + +def GET(request): + """Get a list of the specifications of all CPUs.""" + + # Get the CPUs + + cpus = CPU.query() + + # Return the CPUs + + return Response( + 200, + 'Successfully retrieved CPUs.', + [x.to_JSON() for x in cpus] + ) diff --git a/opendc/api/v2/specifications/cpus/id/__init__.py b/opendc/api/v2/specifications/cpus/id/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/specifications/cpus/id/endpoint.py b/opendc/api/v2/specifications/cpus/id/endpoint.py new file mode 100644 index 00000000..c2453e51 --- /dev/null +++ b/opendc/api/v2/specifications/cpus/id/endpoint.py @@ -0,0 +1,34 @@ +from opendc.models.cpu import CPU +from opendc.util import exceptions +from opendc.util.rest import Response + + +def GET(request): + """Get the specs of a CPU.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'id': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a CPU and make sure it exists + + cpu = CPU.from_primary_key((request.params_path['id'],)) + + if not cpu.exists(): + return Response(404, '{} not found.'.format(cpu)) + + # Return this CPU + + return Response( + 200, + 'Successfully retrieved {}.'.format(cpu), + cpu.to_JSON() + ) diff --git a/opendc/api/v2/specifications/failure-models/__init__.py b/opendc/api/v2/specifications/failure-models/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/specifications/failure-models/endpoint.py b/opendc/api/v2/specifications/failure-models/endpoint.py new file mode 100644 index 00000000..fff668c0 --- /dev/null +++ b/opendc/api/v2/specifications/failure-models/endpoint.py @@ -0,0 +1,18 @@ +from opendc.models.failure_model import FailureModel +from opendc.util.rest import Response + + +def GET(request): + """Get all Failure Models.""" + + # Get the FailureModels + + failure_models = FailureModel.query() + + # Return the FailureModels + + return Response( + 200, + 'Successfully retrieved FailureModels.', + [x.to_JSON() for x in failure_models] + ) diff --git a/opendc/api/v2/specifications/failure-models/id/__init__.py b/opendc/api/v2/specifications/failure-models/id/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/specifications/failure-models/id/endpoint.py b/opendc/api/v2/specifications/failure-models/id/endpoint.py new file mode 100644 index 00000000..0797c9c9 --- /dev/null +++ b/opendc/api/v2/specifications/failure-models/id/endpoint.py @@ -0,0 +1,34 @@ +from opendc.models.failure_model import FailureModel +from opendc.util import exceptions +from opendc.util.rest import Response + + +def GET(request): + """Get this Failure Model.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'id': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a FailureModel and make sure it exists + + failure_model = FailureModel.from_primary_key((request.params_path['id'],)) + + if not failure_model.exists(): + return Response(404, '{} not found.'.format(failure_model)) + + # Return this FailureModel + + return Response( + 200, + 'Successfully retrieved {}.'.format(failure_model), + failure_model.to_JSON() + ) diff --git a/opendc/api/v2/specifications/gpus/__init__.py b/opendc/api/v2/specifications/gpus/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/specifications/gpus/endpoint.py b/opendc/api/v2/specifications/gpus/endpoint.py new file mode 100644 index 00000000..5676f62b --- /dev/null +++ b/opendc/api/v2/specifications/gpus/endpoint.py @@ -0,0 +1,18 @@ +from opendc.models.gpu import GPU +from opendc.util.rest import Response + + +def GET(request): + """Get a list of the specifications of all GPUs.""" + + # Get the GPUs + + gpus = GPU.query() + + # Return the GPUs + + return Response( + 200, + 'Successfully retrieved GPUs.', + [x.to_JSON() for x in gpus] + ) diff --git a/opendc/api/v2/specifications/gpus/id/__init__.py b/opendc/api/v2/specifications/gpus/id/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/specifications/gpus/id/endpoint.py b/opendc/api/v2/specifications/gpus/id/endpoint.py new file mode 100644 index 00000000..81113dc3 --- /dev/null +++ b/opendc/api/v2/specifications/gpus/id/endpoint.py @@ -0,0 +1,34 @@ +from opendc.models.gpu import GPU +from opendc.util import exceptions +from opendc.util.rest import Response + + +def GET(request): + """Get the specs of a GPU.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'id': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a GPU and make sure it exists + + gpu = GPU.from_primary_key((request.params_path['id'],)) + + if not gpu.exists(): + return Response(404, '{} not found.'.format(gpu)) + + # Return this GPU + + return Response( + 200, + 'Successfully retrieved {}.'.format(gpu), + gpu.to_JSON() + ) diff --git a/opendc/api/v2/specifications/memories/__init__.py b/opendc/api/v2/specifications/memories/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/specifications/memories/endpoint.py b/opendc/api/v2/specifications/memories/endpoint.py new file mode 100644 index 00000000..271824b3 --- /dev/null +++ b/opendc/api/v2/specifications/memories/endpoint.py @@ -0,0 +1,18 @@ +from opendc.models.memory import Memory +from opendc.util.rest import Response + + +def GET(request): + """Get a list of the specifications of all Memories.""" + + # Get the Memories + + memories = Memory.query() + + # Return the Memories + + return Response( + 200, + 'Successfully retrieved Memories.', + [x.to_JSON() for x in memories] + ) diff --git a/opendc/api/v2/specifications/memories/id/__init__.py b/opendc/api/v2/specifications/memories/id/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/specifications/memories/id/endpoint.py b/opendc/api/v2/specifications/memories/id/endpoint.py new file mode 100644 index 00000000..863099ca --- /dev/null +++ b/opendc/api/v2/specifications/memories/id/endpoint.py @@ -0,0 +1,34 @@ +from opendc.models.memory import Memory +from opendc.util import exceptions +from opendc.util.rest import Response + + +def GET(request): + """Get the specs of a Memory.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'id': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Memory and make sure it exists + + memory = Memory.from_primary_key((request.params_path['id'],)) + + if not memory.exists(): + return Response(404, '{} not found.'.format(memory)) + + # Return this Memory + + return Response( + 200, + 'Successfully retrieved {}.'.format(memory), + memory.to_JSON() + ) diff --git a/opendc/api/v2/specifications/storages/__init__.py b/opendc/api/v2/specifications/storages/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/specifications/storages/endpoint.py b/opendc/api/v2/specifications/storages/endpoint.py new file mode 100644 index 00000000..28f33177 --- /dev/null +++ b/opendc/api/v2/specifications/storages/endpoint.py @@ -0,0 +1,18 @@ +from opendc.models.storage import Storage +from opendc.util.rest import Response + + +def GET(request): + """Get a list of the specifications of all Storages.""" + + # Get the Storages + + storages = Storage.query() + + # Return the Storages + + return Response( + 200, + 'Successfully retrieved Storages.', + [x.to_JSON() for x in storages] + ) diff --git a/opendc/api/v2/specifications/storages/id/__init__.py b/opendc/api/v2/specifications/storages/id/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/specifications/storages/id/endpoint.py b/opendc/api/v2/specifications/storages/id/endpoint.py new file mode 100644 index 00000000..ebe65857 --- /dev/null +++ b/opendc/api/v2/specifications/storages/id/endpoint.py @@ -0,0 +1,34 @@ +from opendc.models.storage import Storage +from opendc.util import exceptions +from opendc.util.rest import Response + + +def GET(request): + """Get the specs of a Storage.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'id': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Storage and make sure it exists + + storage = Storage.from_primary_key((request.params_path['id'],)) + + if not storage.exists(): + return Response(404, '{} not found.'.format(storage)) + + # Return this CPU + + return Response( + 200, + 'Successfully retrieved {}.'.format(storage), + storage.to_JSON() + ) diff --git a/opendc/api/v2/tiles/__init__.py b/opendc/api/v2/tiles/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/tiles/tileId/__init__.py b/opendc/api/v2/tiles/tileId/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/tiles/tileId/endpoint.py b/opendc/api/v2/tiles/tileId/endpoint.py new file mode 100644 index 00000000..5ccc9cd7 --- /dev/null +++ b/opendc/api/v2/tiles/tileId/endpoint.py @@ -0,0 +1,85 @@ +from opendc.models.tile import Tile +from opendc.util import 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/v2/tiles/tileId/rack/__init__.py b/opendc/api/v2/tiles/tileId/rack/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/tiles/tileId/rack/endpoint.py b/opendc/api/v2/tiles/tileId/rack/endpoint.py new file mode 100644 index 00000000..64245856 --- /dev/null +++ b/opendc/api/v2/tiles/tileId/rack/endpoint.py @@ -0,0 +1,237 @@ +from opendc.models.rack import Rack +from opendc.models.tile import Tile +from opendc.util import 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/v2/tiles/tileId/rack/machines/__init__.py b/opendc/api/v2/tiles/tileId/rack/machines/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/tiles/tileId/rack/machines/endpoint.py b/opendc/api/v2/tiles/tileId/rack/machines/endpoint.py new file mode 100644 index 00000000..5272c117 --- /dev/null +++ b/opendc/api/v2/tiles/tileId/rack/machines/endpoint.py @@ -0,0 +1,118 @@ +from opendc.models.machine import Machine +from opendc.models.rack import Rack +from opendc.util import 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/v2/tiles/tileId/rack/machines/position/__init__.py b/opendc/api/v2/tiles/tileId/rack/machines/position/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/tiles/tileId/rack/machines/position/endpoint.py b/opendc/api/v2/tiles/tileId/rack/machines/position/endpoint.py new file mode 100644 index 00000000..99011fa4 --- /dev/null +++ b/opendc/api/v2/tiles/tileId/rack/machines/position/endpoint.py @@ -0,0 +1,164 @@ +from opendc.models.machine import Machine +from opendc.models.rack import Rack +from opendc.util import 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.position = 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/v2/traces/__init__.py b/opendc/api/v2/traces/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/traces/endpoint.py b/opendc/api/v2/traces/endpoint.py new file mode 100644 index 00000000..78930b0f --- /dev/null +++ b/opendc/api/v2/traces/endpoint.py @@ -0,0 +1,18 @@ +from opendc.models.trace import Trace +from opendc.util.rest import Response + + +def GET(request): + """Get all available Traces.""" + + # Get the Traces + + traces = Trace.query() + + # Return the Traces + + return Response( + 200, + 'Successfully retrieved Traces', + [x.to_JSON() for x in traces] + ) diff --git a/opendc/api/v2/traces/traceId/__init__.py b/opendc/api/v2/traces/traceId/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/traces/traceId/endpoint.py b/opendc/api/v2/traces/traceId/endpoint.py new file mode 100644 index 00000000..50993c41 --- /dev/null +++ b/opendc/api/v2/traces/traceId/endpoint.py @@ -0,0 +1,34 @@ +from opendc.models.trace import Trace +from opendc.util import exceptions +from opendc.util.rest import Response + + +def GET(request): + """Get this Trace.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'traceId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Trace and make sure it exists + + trace = Trace.from_primary_key((request.params_path['traceId'],)) + + if not trace.exists(): + return Response(404, '{} not found.'.format(trace)) + + # Return this Trace + + return Response( + 200, + 'Successfully retrieved {}.'.format(trace), + trace.to_JSON() + ) diff --git a/opendc/api/v2/traces/traceId/jobs/__init__.py b/opendc/api/v2/traces/traceId/jobs/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/traces/traceId/jobs/endpoint.py b/opendc/api/v2/traces/traceId/jobs/endpoint.py new file mode 100644 index 00000000..bd2c6eb0 --- /dev/null +++ b/opendc/api/v2/traces/traceId/jobs/endpoint.py @@ -0,0 +1,37 @@ +from opendc.models.job import Job +from opendc.models.trace import Trace +from opendc.util import exceptions +from opendc.util.rest import Response + + +def GET(request): + """Get this Trace's Jobs.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'traceId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Trace and make sure it exists + + trace = Trace.from_primary_key((request.params_path['traceId'],)) + + if not trace.exists(): + return Response(404, '{} not found.'.format(trace)) + + # Get and return the Jobs + + jobs = Job.query('trace_id', request.params_path['traceId']) + + return Response( + 200, + 'Successfully retrieved Jobs for {}.'.format(trace), + [x.to_JSON() for x in jobs] + ) diff --git a/opendc/api/v2/users/__init__.py b/opendc/api/v2/users/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/users/endpoint.py b/opendc/api/v2/users/endpoint.py new file mode 100644 index 00000000..abd54f27 --- /dev/null +++ b/opendc/api/v2/users/endpoint.py @@ -0,0 +1,82 @@ +from opendc.models.user import User +from opendc.util import exceptions +from opendc.util.rest import Response + + +def GET(request): + """Search for a User using their email address.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + query={ + 'email': 'string' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate and read a User from the database + + user = User.from_email(request.params_query['email']) + + # Make sure this User exists in the database + + if not user.exists(): + return Response(404, '{} not found'.format(user)) + + # Return this User + + return Response( + 200, + 'Successfully retrieved {}.'.format(user), + user.to_JSON() + ) + + +def POST(request): + """Add a new User.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + body={ + 'user': { + 'email': 'string' + } + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a User + + request.params_body['user']['googleId'] = request.google_id + user = User.from_JSON(request.params_body['user']) + + # Make sure a User with this Google ID does not already exist + + if user.exists('google_id'): + user = user.from_google_id(user.google_id) + return Response(409, '{} already exists.'.format(user)) + + # Make sure this User is authorized to create this User + + if not request.google_id == user.google_id: + return Response(403, 'Forbidden from creating this User.') + + # Insert the User + + user.insert() + + # Return a JSON representation of the User + + return Response( + 200, + 'Successfully created {}'.format(user), + user.to_JSON() + ) diff --git a/opendc/api/v2/users/userId/__init__.py b/opendc/api/v2/users/userId/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/users/userId/authorizations/__init__.py b/opendc/api/v2/users/userId/authorizations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/opendc/api/v2/users/userId/authorizations/endpoint.py b/opendc/api/v2/users/userId/authorizations/endpoint.py new file mode 100644 index 00000000..46ca12ba --- /dev/null +++ b/opendc/api/v2/users/userId/authorizations/endpoint.py @@ -0,0 +1,42 @@ +from opendc.models.authorization import Authorization +from opendc.models.user import User +from opendc.util import exceptions +from opendc.util.rest import Response + + +def GET(request): + """Get this User's Authorizations.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'userId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a User and make sure they exist + + user = User.from_primary_key((request.params_path['userId'],)) + + if not user.exists(): + return Response(404, '{} not found.'.format(user)) + + # Make sure this requester is allowed to retrieve this User's Authorizations + + if not user.google_id_has_at_least(request.google_id, 'OWN'): + return Response(403, 'Forbidden from retrieving Authorizations for {}.'.format(user)) + + # Return this User's Authorizations + + authorizations = Authorization.query('user_id', request.params_path['userId']) + + return Response( + 200, + 'Successfully retrieved Authorizations for {}.'.format(user), + [x.to_JSON() for x in authorizations] + ) diff --git a/opendc/api/v2/users/userId/endpoint.py b/opendc/api/v2/users/userId/endpoint.py new file mode 100644 index 00000000..767c5d13 --- /dev/null +++ b/opendc/api/v2/users/userId/endpoint.py @@ -0,0 +1,123 @@ +from opendc.models.user import User +from opendc.util import exceptions +from opendc.util.rest import Response + + +def DELETE(request): + """Delete this user.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'userId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a User and make sure they exist + + user = User.from_primary_key((request.params_path['userId'],)) + + if not user.exists(): + return Response(404, '{} not found'.format(user)) + + # Make sure this User is allowed to delete this User + + if not user.google_id_has_at_least(request.google_id, 'OWN'): + return Response(403, 'Forbidden from deleting {}.'.format(user)) + + # Delete this User + + user.delete() + + # Return this User + + return Response( + 200, + 'Successfully deleted {}'.format(user), + user.to_JSON() + ) + + +def GET(request): + """Get this User.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path={ + 'userId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a User and make sure they exist + + user = User.from_primary_key((request.params_path['userId'],)) + + if not user.exists(): + return Response(404, '{} not found.'.format(user)) + + # Return this User + + return Response( + 200, + 'Successfully retrieved {}'.format(user), + user.to_JSON(), + ) + + +def PUT(request): + """Update this User's given name and/ or family name.""" + + # Make sure the required parameters are there + + try: + request.check_required_parameters( + body={ + 'user': { + 'givenName': 'string', + 'familyName': 'string' + } + }, + path={ + 'userId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a User and make sure they exist + + user = User.from_primary_key((request.params_path['userId'],)) + + if not user.exists(): + return Response(404, '{} not found.'.format(user)) + + # Make sure this User is allowed to edit this User + + if not user.google_id_has_at_least(request.google_id, 'OWN'): + return Response(403, 'Forbidden from editing {}.'.format(user)) + + # Update this User + + user.given_name = request.params_body['user']['givenName'] + user.family_name = request.params_body['user']['familyName'] + + user.update() + + # Return this User + + return Response( + 200, + 'Successfully updated {}.'.format(user), + user.to_JSON() + ) diff --git a/opendc/models/machine.py b/opendc/models/machine.py index f233d329..e6e49c4b 100644 --- a/opendc/models/machine.py +++ b/opendc/models/machine.py @@ -64,7 +64,7 @@ class Machine(Model): try: statement = 'SELECT id FROM machines WHERE rack_id = %s AND position = %s' - machine_id = database.fetchone(statement, (rack.id, position))[0] + machine_id = database.fetch_one(statement, (rack.id, position))[0] except: return cls(id=-1) @@ -102,7 +102,7 @@ class Machine(Model): for device_table in self.device_table_to_attribute.keys(): statement = 'SELECT * FROM machine_{} WHERE machine_id = %s'.format(device_table) - results = database.fetchall(statement, (self.id,)) + results = database.fetch_all(statement, (self.id,)) device_ids = [] diff --git a/opendc/models/machine_state.py b/opendc/models/machine_state.py index ad295e1a..3e3fdab9 100644 --- a/opendc/models/machine_state.py +++ b/opendc/models/machine_state.py @@ -38,7 +38,7 @@ class MachineState(Model): machine_states = [] statement = 'SELECT * FROM machine_states WHERE experiment_id = %s' - results = database.fetchall(statement, (experiment_id,)) + results = database.fetch_all(statement, (experiment_id,)) for row in results: machine_states.append(cls._from_database_row(row)) @@ -52,7 +52,7 @@ class MachineState(Model): machine_states = [] statement = 'SELECT * FROM machine_states WHERE experiment_id = %s AND machine_states.tick = %s' - results = database.fetchall(statement, (experiment_id, tick)) + results = database.fetch_all(statement, (experiment_id, tick)) for row in results: machine_states.append(cls._from_database_row(row)) @@ -65,7 +65,7 @@ class MachineState(Model): super(MachineState, self).read() statement = 'SELECT tick FROM task_states WHERE id = %s' - result = database.fetchone(statement, (self.task_state_id,)) + result = database.fetch_one(statement, (self.task_state_id,)) self.tick = result[0] diff --git a/opendc/models/model.py b/opendc/models/model.py index 8ab410d4..00ab50d1 100644 --- a/opendc/models/model.py +++ b/opendc/models/model.py @@ -163,7 +163,7 @@ class Model(object): """Initialize a Model by fetching it from the database.""" parameters = {} - model_from_database = database.fetchone(statement, values) + model_from_database = database.fetch_one(statement, values) if model_from_database is None: return None @@ -207,11 +207,11 @@ class Model(object): if column_name is not None and value is not None: statement = 'SELECT * FROM {} WHERE {} = %s'.format(cls.TABLE_NAME, column_name) - database_models = database.fetchall(statement, (value,)) + database_models = database.fetch_all(statement, (value,)) else: statement = 'SELECT * FROM {}'.format(cls.TABLE_NAME) - database_models = database.fetchall(statement) + database_models = database.fetch_all(statement) models = [] @@ -267,7 +267,7 @@ class Model(object): ) values = (getattr(self, column),) - return database.fetchone(query, values)[0] == 1 + return database.fetch_one(query, values)[0] == 1 def insert(self): """Insert this Model into the database.""" diff --git a/opendc/models/rack_state.py b/opendc/models/rack_state.py index c0f0ff6c..440ab293 100644 --- a/opendc/models/rack_state.py +++ b/opendc/models/rack_state.py @@ -35,7 +35,7 @@ class RackState(Model): WHERE machine_states.experiment_id = %s GROUP BY machine_states.tick, racks.id ''' - results = database.fetchall(statement, (experiment_id,)) + results = database.fetch_all(statement, (experiment_id,)) for row in results: rack_states.append(cls._from_database_row(row)) @@ -57,7 +57,7 @@ class RackState(Model): AND machine_states.tick = %s GROUP BY machine_states.tick, racks.id ''' - results = database.fetchall(statement, (experiment_id, tick)) + results = database.fetch_all(statement, (experiment_id, tick)) for row in results: rack_states.append(cls._from_database_row(row)) diff --git a/opendc/models/room_state.py b/opendc/models/room_state.py index 2729407f..2404d86b 100644 --- a/opendc/models/room_state.py +++ b/opendc/models/room_state.py @@ -39,7 +39,7 @@ class RoomState(Model): AND machine_states.experiment_id = %s GROUP BY machine_states.tick, rooms.id ''' - results = database.fetchall(statement, (experiment_id,)) + results = database.fetch_all(statement, (experiment_id,)) for row in results: room_states.append(cls._from_database_row(row)) @@ -65,7 +65,7 @@ class RoomState(Model): AND machine_states.tick = %s GROUP BY rooms.id ''' - results = database.fetchall(statement, (experiment_id, tick)) + results = database.fetch_all(statement, (experiment_id, tick)) for row in results: room_states.append(cls._from_database_row(row)) diff --git a/opendc/models/task_duration.py b/opendc/models/task_duration.py index 1dc3ca01..14cbc31a 100644 --- a/opendc/models/task_duration.py +++ b/opendc/models/task_duration.py @@ -31,7 +31,7 @@ class TaskDuration(Model): GROUP BY task_id ''' - results = database.fetchall(statement, (experiment_id,)) + results = database.fetch_all(statement, (experiment_id,)) for row in results: room_states.append(cls._from_database_row(row)) diff --git a/opendc/models/task_state.py b/opendc/models/task_state.py index 8dca2d03..83cc6a62 100644 --- a/opendc/models/task_state.py +++ b/opendc/models/task_state.py @@ -26,7 +26,7 @@ class TaskState(Model): task_states = [] statement = 'SELECT * FROM task_states WHERE experiment_id = %s AND tick = %s' - results = database.fetchall(statement, (experiment_id, tick)) + results = database.fetch_all(statement, (experiment_id, tick)) for row in results: task_states.append( diff --git a/opendc/util/database.py b/opendc/util/database.py index 92d0feef..67632bd0 100644 --- a/opendc/util/database.py +++ b/opendc/util/database.py @@ -15,68 +15,54 @@ def init_connection_pool(user, password, database, host, port): database = urllib.parse.quote_plus(database) host = urllib.parse.quote_plus(host) - global client global opendcdb - global prefabs_collection - global user_collection - global topologies_collection client = MongoClient('mongodb://%s:%s@%s/default_db?authSource=%s' % (user, password, host, database)) opendcdb = client.opendc - prefabs_collection = opendcdb.prefabs - topologies_collection = opendcdb.topologies - user_collection = opendcdb.users - # global CONNECTION_POOL - # CONNECTION_POOL = MySQLConnectionPool(pool_name="opendcpool", pool_size=5, - # user=user, password=password, database=database, host=host, port=port) +def fetch_one(query, collection): + """Uses existing mongo connection to return a single (the first) document in a collection matching the given + query as a JSON object. -def execute(statement, t): - """Open a database connection and execute the statement.""" + The query needs to be in json format, i.e.: `{'name': prefab_name}`. + """ + bson = getattr(opendcdb, collection).find_one(query) - # Connect to the database - connection = CONNECTION_POOL.get_connection() - cursor = connection.cursor() + return convert_bson_to_json(bson) - # Execute the statement - cursor.execute(statement, t) - # Get the id - cursor.execute('SELECT last_insert_id();') - row_id = cursor.fetchone()[0] +def fetch_all(query, collection): + """Uses existing mongo connection to return all documents matching a given query, as a list of JSON objects. - # Disconnect from the database - connection.commit() - connection.close() + The query needs to be in json format, i.e.: `{'name': prefab_name}`. + """ + results = [] + cursor = getattr(opendcdb, collection).find(query) + for doc in cursor: + results.append(convert_bson_to_json(doc)) + return results - # Return the id - return row_id +def insert(obj, collection): + """Updates an existing object.""" + bson = getattr(opendcdb, collection).insert(obj) -def fetchone(query, collection): - """Uses existing mongo connection to return a single (the first) document in a collection matching the given query as a JSON object""" - # query needs to be in json format, i.e.: {'name': prefab_name} - # TODO: determine which collection to pull from - bson = prefabs_collection.find_one(query) - json_string = dumps(bson) # convert BSON representation to JSON - json_obj = json.loads(json_string) # load as a JSON object - # leave the id field in for now, we can use it later - # json_obj.pop("_id") - return json_obj + return convert_bson_to_json(bson) -def fetchall(query, collection): - """Uses existing mongo connection to return all documents matching a given query, as a list of JSON objects""" - results = [] - cursor = prefabs_collection.find(query) - for doc in cursor: - json_string = dumps(doc) # convert BSON representation to JSON - json_obj = json.loads(json_string) # load as a JSON object - # leave the id field in for now, we can use it later - # json_obj.pop("_id") - results.append(json_obj) - return results +def update(_id, obj, collection): + """Updates an existing object.""" + bson = getattr(opendcdb, collection).update({'_id': _id}, obj) + + return convert_bson_to_json(bson) + + +def convert_bson_to_json(bson): + # Convert BSON representation to JSON + json_string = dumps(bson) + # Load as a JSON object + return json.loads(json_string) def datetime_to_string(datetime_to_convert): -- cgit v1.2.3