diff options
| author | jc0b <j@jc0b.computer> | 2020-06-25 22:11:25 +0200 |
|---|---|---|
| committer | jc0b <j@jc0b.computer> | 2020-06-25 22:11:25 +0200 |
| commit | 61cf27a9529e503d3c56854c9d664f2d9520213a (patch) | |
| tree | 77b32f3dfd56af806520ed481450f6d3c2f33412 /opendc/api/v2/topologies | |
| parent | 00597ec99f587557b88b9982a2c41a2cb8db8112 (diff) | |
GET for topologies with tests
Diffstat (limited to 'opendc/api/v2/topologies')
| -rw-r--r-- | opendc/api/v2/topologies/__init__.py | 0 | ||||
| -rw-r--r-- | opendc/api/v2/topologies/topologyId/__init__.py | 0 | ||||
| -rw-r--r-- | opendc/api/v2/topologies/topologyId/endpoint.py | 34 | ||||
| -rw-r--r-- | opendc/api/v2/topologies/topologyId/rooms/__init__.py | 0 | ||||
| -rw-r--r-- | opendc/api/v2/topologies/topologyId/rooms/endpoint.py | 93 | ||||
| -rw-r--r-- | opendc/api/v2/topologies/topologyId/test_endpoint.py | 53 |
6 files changed, 180 insertions, 0 deletions
diff --git a/opendc/api/v2/topologies/__init__.py b/opendc/api/v2/topologies/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/opendc/api/v2/topologies/__init__.py diff --git a/opendc/api/v2/topologies/topologyId/__init__.py b/opendc/api/v2/topologies/topologyId/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/opendc/api/v2/topologies/topologyId/__init__.py diff --git a/opendc/api/v2/topologies/topologyId/endpoint.py b/opendc/api/v2/topologies/topologyId/endpoint.py new file mode 100644 index 00000000..ef541daa --- /dev/null +++ b/opendc/api/v2/topologies/topologyId/endpoint.py @@ -0,0 +1,34 @@ +from opendc.models.topology import Topology +from opendc.util import exceptions +from opendc.util.rest import Response + + +def GET(request): + """Get this Topology.""" + + # Make sure required parameters are there + + try: + request.check_required_parameters(path={'topologyId': 'int'}) + except exceptions.ParameterError as e: + return Response(400, str(e)) + + # Instantiate a Topology from the database + + topology = Topology.from_id(request.params_path['topologyId']) + + # Make sure this Topology exists + + validation_error = topology.validate() + if validation_error is not None: + return validation_error + + # Make sure this user is authorized to view this Topology + + access_error = topology.validate_user_access(request.google_id, False) + if access_error is not None: + return access_error + + # Return this Topology + + return Response(200, 'Successfully retrieved topology.', topology.obj) diff --git a/opendc/api/v2/topologies/topologyId/rooms/__init__.py b/opendc/api/v2/topologies/topologyId/rooms/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/opendc/api/v2/topologies/topologyId/rooms/__init__.py diff --git a/opendc/api/v2/topologies/topologyId/rooms/endpoint.py b/opendc/api/v2/topologies/topologyId/rooms/endpoint.py new file mode 100644 index 00000000..96ee7028 --- /dev/null +++ b/opendc/api/v2/topologies/topologyId/rooms/endpoint.py @@ -0,0 +1,93 @@ +from opendc.models_old.datacenter import Datacenter +from opendc.models_old.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, str(e)) + + # 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, str(e)) + + # 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/topologies/topologyId/test_endpoint.py b/opendc/api/v2/topologies/topologyId/test_endpoint.py new file mode 100644 index 00000000..0e264270 --- /dev/null +++ b/opendc/api/v2/topologies/topologyId/test_endpoint.py @@ -0,0 +1,53 @@ +from opendc.util.database import DB + +''' +POST /topologies +''' + + + + +''' +GET /topologies/{topologyId} +''' + +def test_get_topology(client, mocker): + mocker.patch.object(DB, 'fetch_one', return_value={ + '_id': '1', + 'authorizations': [{ + 'topologyId': '1', + 'authorizationLevel': 'EDIT' + }] + }) + res = client.get('/api/v2/topologies/1') + assert '200' in res.status + +def test_get_topology_non_existing(client, mocker): + mocker.patch.object(DB, 'fetch_one', return_value=None) + assert '404' in client.get('/api/v2/topologies/1').status + +def test_get_topology_not_authorized(client, mocker): + mocker.patch.object(DB, 'fetch_one', return_value={ + '_id': '1', + 'authorizations': [{ + 'topologyId': '2', + 'authorizationLevel': 'OWN' + }] + }) + res = client.get('/api/v2/topologies/1') + assert '403' in res.status + +def test_get_topology_no_authorizations(client, mocker): + mocker.patch.object(DB, 'fetch_one', return_value={'authorizations': []}) + res = client.get('/api/v2/topologies/1') + assert '403' in res.status + + +''' +PUT /topologies/{topologyId} +''' + + +''' +DELETE /topologies/{topologyId} +'''
\ No newline at end of file |
