summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--opendc/api/v2/datacenters/datacenterId/endpoint.py35
-rw-r--r--opendc/api/v2/topologies/__init__.py (renamed from opendc/api/v2/datacenters/__init__.py)0
-rw-r--r--opendc/api/v2/topologies/topologyId/__init__.py (renamed from opendc/api/v2/datacenters/datacenterId/__init__.py)0
-rw-r--r--opendc/api/v2/topologies/topologyId/endpoint.py34
-rw-r--r--opendc/api/v2/topologies/topologyId/rooms/__init__.py (renamed from opendc/api/v2/datacenters/datacenterId/rooms/__init__.py)0
-rw-r--r--opendc/api/v2/topologies/topologyId/rooms/endpoint.py (renamed from opendc/api/v2/datacenters/datacenterId/rooms/endpoint.py)0
-rw-r--r--opendc/api/v2/topologies/topologyId/test_endpoint.py53
-rw-r--r--opendc/models/topology.py11
8 files changed, 98 insertions, 35 deletions
diff --git a/opendc/api/v2/datacenters/datacenterId/endpoint.py b/opendc/api/v2/datacenters/datacenterId/endpoint.py
deleted file mode 100644
index 67fbfcd8..00000000
--- a/opendc/api/v2/datacenters/datacenterId/endpoint.py
+++ /dev/null
@@ -1,35 +0,0 @@
-from opendc.models_old.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, 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
-
- 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/__init__.py b/opendc/api/v2/topologies/__init__.py
index e69de29b..e69de29b 100644
--- a/opendc/api/v2/datacenters/__init__.py
+++ b/opendc/api/v2/topologies/__init__.py
diff --git a/opendc/api/v2/datacenters/datacenterId/__init__.py b/opendc/api/v2/topologies/topologyId/__init__.py
index e69de29b..e69de29b 100644
--- a/opendc/api/v2/datacenters/datacenterId/__init__.py
+++ 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/datacenters/datacenterId/rooms/__init__.py b/opendc/api/v2/topologies/topologyId/rooms/__init__.py
index e69de29b..e69de29b 100644
--- a/opendc/api/v2/datacenters/datacenterId/rooms/__init__.py
+++ b/opendc/api/v2/topologies/topologyId/rooms/__init__.py
diff --git a/opendc/api/v2/datacenters/datacenterId/rooms/endpoint.py b/opendc/api/v2/topologies/topologyId/rooms/endpoint.py
index 96ee7028..96ee7028 100644
--- a/opendc/api/v2/datacenters/datacenterId/rooms/endpoint.py
+++ b/opendc/api/v2/topologies/topologyId/rooms/endpoint.py
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
diff --git a/opendc/models/topology.py b/opendc/models/topology.py
index a5f53bbb..b01d5f41 100644
--- a/opendc/models/topology.py
+++ b/opendc/models/topology.py
@@ -1,5 +1,16 @@
from opendc.models.model import Model
+from opendc.models.user import User
+from opendc.util.rest import Response
class Topology(Model):
collection_name = 'topologies'
+
+ def validate_user_access(self, google_id, edit_access):
+ user = User.from_google_id(google_id)
+ authorizations = list(
+ filter(lambda x: str(x['topologyId']) == str(self.obj['_id']), user.obj['authorizations']))
+ if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'):
+ return Response(403, "Forbidden from retrieving topology.")
+
+ return None \ No newline at end of file