diff options
Diffstat (limited to 'opendc/api/v1/tiles')
| -rw-r--r-- | opendc/api/v1/tiles/__init__.py | 0 | ||||
| -rw-r--r-- | opendc/api/v1/tiles/tileId/__init__.py | 0 | ||||
| -rw-r--r-- | opendc/api/v1/tiles/tileId/endpoint.py | 83 | ||||
| -rw-r--r-- | opendc/api/v1/tiles/tileId/rack/__init__.py | 0 | ||||
| -rw-r--r-- | opendc/api/v1/tiles/tileId/rack/endpoint.py | 233 | ||||
| -rw-r--r-- | opendc/api/v1/tiles/tileId/rack/machines/__init__.py | 0 | ||||
| -rw-r--r-- | opendc/api/v1/tiles/tileId/rack/machines/endpoint.py | 116 | ||||
| -rw-r--r-- | opendc/api/v1/tiles/tileId/rack/machines/position/__init__.py | 0 | ||||
| -rw-r--r-- | opendc/api/v1/tiles/tileId/rack/machines/position/endpoint.py | 161 |
9 files changed, 593 insertions, 0 deletions
diff --git a/opendc/api/v1/tiles/__init__.py b/opendc/api/v1/tiles/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/opendc/api/v1/tiles/__init__.py diff --git a/opendc/api/v1/tiles/tileId/__init__.py b/opendc/api/v1/tiles/tileId/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/opendc/api/v1/tiles/tileId/__init__.py diff --git a/opendc/api/v1/tiles/tileId/endpoint.py b/opendc/api/v1/tiles/tileId/endpoint.py new file mode 100644 index 00000000..deb76195 --- /dev/null +++ b/opendc/api/v1/tiles/tileId/endpoint.py @@ -0,0 +1,83 @@ +from opendc.models.tile import Tile +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Tile.""" + + # Make sure request parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Tile from the database + + tile = Tile.from_primary_key((request.params_path['tileId'],)) + + # Make sure this Tile exists + + if not tile.exists(): + return Response(404, '{} not found.'.format(tile)) + + # Make sure this user is authorized to view this Tile + + if not tile.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from retrieving {}.'.format(tile)) + + # Return this Tile + + tile.read() + + return Response( + 200, + 'Successfully retrieved {}.'.format(tile), + tile.to_JSON() + ) + +def DELETE(request): + """Delete this Tile.""" + + # Make sure request parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Tile from the database + + tile = Tile.from_primary_key((request.params_path['tileId'],)) + + # Make sure this Tile exists + + if not tile.exists(): + return Response(404, '{} not found.'.format(tile)) + + # Make sure this user is authorized to edit this Tile + + if not tile.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from deleting {}.'.format(tile)) + + # Delete this Tile + + tile.delete() + + # Return this Tile + + return Response( + 200, + 'Successfully deleted {}.'.format(tile), + tile.to_JSON() + ) diff --git a/opendc/api/v1/tiles/tileId/rack/__init__.py b/opendc/api/v1/tiles/tileId/rack/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/opendc/api/v1/tiles/tileId/rack/__init__.py diff --git a/opendc/api/v1/tiles/tileId/rack/endpoint.py b/opendc/api/v1/tiles/tileId/rack/endpoint.py new file mode 100644 index 00000000..59338e9a --- /dev/null +++ b/opendc/api/v1/tiles/tileId/rack/endpoint.py @@ -0,0 +1,233 @@ +from opendc.models.rack import Rack +from opendc.models.tile import Tile +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Tile's Rack.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int' + }, + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Tile from the database + + tile = Tile.from_primary_key((request.params_path['tileId'],)) + + # Make sure this Tile exists + + if not tile.exists(): + return Response(404, '{} not found.'.format(tile)) + + # Make sure this user is authorized to view this Tile + + if not tile.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from editing {}'.format(tile)) + + # Instantiate a Rack from the database + + rack = Rack.from_primary_key((tile.object_id,)) + + # Make sure this Rack exists + + if not rack.exists(): + return Response(404, '{} not found'.format(rack)) + + # Return the Rack + + rack.read() + + return Response( + 200, + 'Successfully retrieved {}.'.format(rack), + rack.to_JSON() + ) + +def POST(request): + """Add a Rack to this Tile if it is empty.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int' + }, + body = { + 'rack': { + 'name': 'string', + 'capacity': 'int', + 'powerCapacityW': 'int' + } + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Tile from the database + + tile = Tile.from_primary_key((request.params_path['tileId'],)) + + # Make sure this Tile exists + + if not tile.exists(): + return Response(404, '{} not found.'.format(tile)) + + # Make sure this user is authorized to edit this Tile + + if not tile.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from editing {}'.format(tile)) + + # Make sure this Tile isn't occupied + + if tile.object_id is not None: + return Response(409, '{} occupied.'.format(tile)) + + # Instantiate a Rack and insert it into the database + + rack = Rack.from_JSON(request.params_body['rack']) + rack.insert() + + # Try to add this Rack to this Tile + + tile.object_id = rack.id + tile.object_type = 'RACK' + tile.update() + + # Return this Rack + + rack.read() + + return Response( + 200, + 'Successfully added {}.'.format(rack), + rack.to_JSON() + ) + +def PUT(request): + """Update the Rack on this Tile.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int' + }, + body = { + 'rack': { + 'name': 'string', + 'capacity': 'int', + 'powerCapacityW': 'int' + } + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Tile from the database + + tile = Tile.from_primary_key((request.params_path['tileId'],)) + + # Make sure this Tile exists + + if not tile.exists(): + return Response(404, '{} not found.'.format(tile)) + + # Make sure this user is authorized to edit this Tile + + if not tile.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from editing {}'.format(tile)) + + # Instantiate a Rack from the database + + rack = Rack.from_primary_key((tile.object_id,)) + + # Make sure this Rack exists + + if not rack.exists(): + return Response(404, '{} not found'.format(rack)) + + # Update this Rack + + rack.name = request.params_body['rack']['name'] + rack.capacity = request.params_body['rack']['capacity'] + + rack.update() + + # Return this Rack + + rack.read() + + return Response( + 200, + 'Successfully updated {}.'.format(rack), + rack.to_JSON() + ) + +def DELETE(request): + """Delete this Tile's Rack.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int' + }, + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Tile from the database + + tile = Tile.from_primary_key((request.params_path['tileId'],)) + + # Make sure this Tile exists + + if not tile.exists(): + return Response(404, '{} not found.'.format(tile)) + + # Make sure this user is authorized to edit this Tile + + if not tile.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from editing {}'.format(tile)) + + # Instantiate a Rack from the database + + rack = Rack.from_primary_key((tile.object_id,)) + + # Make sure this Rack exists + + if not rack.exists(): + return Response(404, '{} not found'.format(rack)) + + # Remove this Rack from this Tile + + tile.object_id = None + tile.object_type = None + + tile.update() + + # Delete this Rack + + rack.delete() + + # Return this Rack + + return Response( + 200, + 'Successfully deleted {}.'.format(rack), + rack.to_JSON() + ) diff --git a/opendc/api/v1/tiles/tileId/rack/machines/__init__.py b/opendc/api/v1/tiles/tileId/rack/machines/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/opendc/api/v1/tiles/tileId/rack/machines/__init__.py diff --git a/opendc/api/v1/tiles/tileId/rack/machines/endpoint.py b/opendc/api/v1/tiles/tileId/rack/machines/endpoint.py new file mode 100644 index 00000000..2d160713 --- /dev/null +++ b/opendc/api/v1/tiles/tileId/rack/machines/endpoint.py @@ -0,0 +1,116 @@ +from opendc.models.machine import Machine +from opendc.models.rack import Rack +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get this Rack's Machines.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Rack from the database + + rack = Rack.from_tile_id(request.params_path['tileId']) + + # Make sure this Rack exists + + if not rack.exists(): + return Response(404, '{} not found.'.format(rack)) + + # Make sure this user is authorized to view this Rack's Machines + + if not rack.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from viewing {}.'.format(rack)) + + # Get and return the Machines + + machines = Machine.query('rack_id', rack.id) + + for machine in machines: + machine.read() + + return Response( + 200, + 'Successfully retrieved Machines for {}.'.format(rack), + [x.to_JSON() for x in machines] + ) + +def POST(request): + """Add a Machine to this rack.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int' + }, + body = { + 'machine': { + 'rackId': 'int', + 'position': 'int', + 'tags': 'list-string', + 'cpuIds': 'list-int', + 'gpuIds': 'list-int', + 'memoryIds': 'list-int', + 'storageIds': 'list-int' + } + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Rack from the database + + rack = Rack.from_tile_id(request.params_path['tileId']) + + # Make sure this Rack exists + + if not rack.exists(): + return Response(404, '{} not found.'.format(rack)) + + # Make sure this Rack's ID matches the given rack ID + + if rack.id != request.params_body['machine']['rackId']: + return Response(400, 'Rack ID in `machine` and path do not match.') + + # Make sure this user is authorized to edit this Rack's Machines + + if not rack.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from viewing {}.'.format(rack)) + + # Instantiate a Machine + + machine = Machine.from_JSON(request.params_body['machine']) + + # Try to insert this Machine + + try: + machine.insert() + + except exceptions.ForeignKeyError: + return Response(409, 'Rack position occupied.') + + except: + return Response(400, 'Invalid Machine.') + + # Return this Machine + + machine.read() + + return Response( + 200, + 'Successfully added {}.'.format(machine), + machine.to_JSON() + ) diff --git a/opendc/api/v1/tiles/tileId/rack/machines/position/__init__.py b/opendc/api/v1/tiles/tileId/rack/machines/position/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/opendc/api/v1/tiles/tileId/rack/machines/position/__init__.py diff --git a/opendc/api/v1/tiles/tileId/rack/machines/position/endpoint.py b/opendc/api/v1/tiles/tileId/rack/machines/position/endpoint.py new file mode 100644 index 00000000..15e6cf96 --- /dev/null +++ b/opendc/api/v1/tiles/tileId/rack/machines/position/endpoint.py @@ -0,0 +1,161 @@ +from opendc.models.machine import Machine +from opendc.models.rack import Rack +from opendc.util import database, exceptions +from opendc.util.rest import Response + +def GET(request): + """Get the Machine at this location in this Rack.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int', + 'position': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Machine from the database + + machine = Machine.from_tile_id_and_rack_position(request.params_path['tileId'], request.params_path['position']) + + # Make sure this Machine exists + + if not machine.exists(): + return Response(404, '{} not found.'.format(machine)) + + # Make sure this user is authorized to view this Machine + + if not machine.google_id_has_at_least(request.google_id, 'VIEW'): + return Response(403, 'Forbidden from retrieving {}.'.format(machine)) + + # Return this Machine + + machine.read() + + return Response( + 200, + 'Successfully retrieved {}.'.format(machine), + machine.to_JSON() + ) + +def PUT(request): + """Update the Machine at this location in this Rack.""" + + try: + request.check_required_parameters( + path = { + 'tileId': 'int', + 'position': 'int' + }, + body = { + 'machine': { + 'rackId': 'int', + 'position': 'int', + 'tags': 'list-string', + 'cpuIds': 'list-int', + 'gpuIds': 'list-int', + 'memoryIds': 'list-int', + 'storageIds': 'list-int' + } + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Machine from the database + + machine = Machine.from_tile_id_and_rack_position(request.params_path['tileId'], request.params_path['position']) + + # Make sure this Machine exists + + if not machine.exists(): + return Response(404, '{} not found.'.format(machine)) + + # Make sure this Machine's rack ID is right + + rack = Rack.from_tile_id(request.params_path['tileId']) + + if not rack.exists() or rack.id != request.params_body['machine']['rackId']: + return Response(400, 'Mismatch in Rack IDs.') + + # Make sure this user is authorized to edit this Machine + + if not machine.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from retrieving {}.'.format(machine)) + + # Update this Machine + + machine.positoin = request.params_body['machine']['position'] + machine.tags = request.params_body['machine']['tags'] + machine.cpu_ids = request.params_body['machine']['cpuIds'] + machine.gpu_ids = request.params_body['machine']['gpuIds'] + machine.memory_ids = request.params_body['machine']['memoryIds'] + machine.storage_ids = request.params_body['machine']['storageIds'] + + try: + machine.update() + + except exceptions.ForeignKeyError: + return Response(409, 'Rack position occupied.') + + except Exception as e: + print e + return Response(400, 'Invalid Machine.') + + # Return this Machine + + machine.read() + + return Response( + 200, + 'Successfully updated {}.'.format(machine), + machine.to_JSON() + ) + +def DELETE(request): + """Delete the Machine at this location in this Rack.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters( + path = { + 'tileId': 'int', + 'position': 'int' + } + ) + + except exceptions.ParameterError as e: + return Response(400, e.message) + + # Instantiate a Machine from the database + + machine = Machine.from_tile_id_and_rack_position(request.params_path['tileId'], request.params_path['position']) + + # Make sure this Machine exists + + if not machine.exists(): + return Response(404, '{} not found.'.format(machine)) + + # Make sure this user is authorized to edit this Machine + + if not machine.google_id_has_at_least(request.google_id, 'EDIT'): + return Response(403, 'Forbidden from retrieving {}.'.format(machine)) + + # Delete this Machine + + machine.delete() + + # Return this Machine + + return Response( + 200, + 'Successfully deleted {}.'.format(machine), + machine.to_JSON() + ) |
