Towards a1 1.0.0; implement missing GETs
[ric-plt/a1.git] / a1 / data.py
index 17ccf4a..5e2690d 100644 (file)
@@ -23,7 +23,7 @@ Hopefully, the access functions are a good api so nothing else has to change whe
 For now, the database is in memory.
 We use dict data structures (KV) with the expectation of having to move this into Redis
 """
-from a1.exceptions import PolicyTypeNotFound, PolicyInstanceNotFound
+from a1.exceptions import PolicyTypeNotFound, PolicyInstanceNotFound, PolicyTypeAlreadyExists
 from a1 import get_module_logger
 
 logger = get_module_logger(__name__)
@@ -35,14 +35,7 @@ I = "instances"
 H = "handlers"
 D = "data"
 
-
-# TODO: REMOVE THIS!! (should be done via PUT)
-POLICY_DATA[20000] = {}
-POLICY_DATA[20000][D] = {"foo": "bar"}
-POLICY_DATA[20000][I] = {}
-POLICY_DATA[20001] = {}
-POLICY_DATA[20001][D] = {"foo": "bar"}
-POLICY_DATA[20001][I] = {}
+# Types
 
 
 def type_is_valid(policy_type_id):
@@ -54,6 +47,36 @@ def type_is_valid(policy_type_id):
         raise PolicyTypeNotFound()
 
 
+def store_policy_type(policy_type_id, body):
+    """
+    store a policy type if it doesn't already exist
+    """
+    if policy_type_id in POLICY_DATA:
+        raise PolicyTypeAlreadyExists()
+
+    POLICY_DATA[policy_type_id] = {}
+    POLICY_DATA[policy_type_id][D] = body
+    POLICY_DATA[policy_type_id][I] = {}
+
+
+def get_policy_type(policy_type_id):
+    """
+    retrieve a type
+    """
+    type_is_valid(policy_type_id)
+    return POLICY_DATA[policy_type_id][D]
+
+
+def get_type_list():
+    """
+    retrieve all type ids
+    """
+    return list(POLICY_DATA.keys())
+
+
+# Instances
+
+
 def instance_is_valid(policy_type_id, policy_instance_id):
     """
     check that an instance is valid
@@ -103,3 +126,11 @@ def set_policy_instance_status(policy_type_id, policy_instance_id, handler_id, s
     instance_is_valid(policy_type_id, policy_instance_id)
 
     POLICY_DATA[policy_type_id][I][policy_instance_id][H][handler_id] = status
+
+
+def get_instance_list(policy_type_id):
+    """
+    retrieve all instance ids for a type
+    """
+    type_is_valid(policy_type_id)
+    return list(POLICY_DATA[policy_type_id][I].keys())