[CORD-2080] Adding 'tosca_key' support in xproto

Change-Id: Ib01c1cdcb81a51819c9e08703e6c76a4ffa09f17
diff --git a/src/grpc_client/models_accessor.py b/src/grpc_client/models_accessor.py
index b51ee90..918c04d 100644
--- a/src/grpc_client/models_accessor.py
+++ b/src/grpc_client/models_accessor.py
@@ -27,39 +27,46 @@
         Give a Model Class Name and some data, check if that exits or instantiate a new one
         """
 
-        if data.get('name'):
-            used_key = 'name'
-        else:
-            if len(data.keys()) > 0:
-                # FIXME apparently we're not matching model without a name field
-                used_key = data.keys()[0]
-            else:
-                used_key = None
+        # NOTE: we need to import this later as it's generated by main.py
+        from KEYS import TOSCA_KEYS
+
+        # get the key for this model
+        try:
+            filter_keys = TOSCA_KEYS[class_name]
+        except KeyError, e:
+            raise Exception("[XOS-TOSCA] Model %s doesn't have a tosca_key specified" % (class_name))
+
+        if len(filter_keys) == 0:
+            raise Exception("[XOS-TOSCA] Model %s doesn't have a tosca_key specified" % (class_name))
+
+        filter = {}
+        for k in filter_keys:
+            try:
+                filter[k] = data[k]
+            except KeyError, e:
+                raise Exception("[XOS-TOSCA] Model %s doesn't have a property for the specified tosca_key (%s)" % (class_name, e))
 
         key = "%s~%s" % (username, password)
         if not key in RESOURCES:
             raise Exception("[XOS-TOSCA] User '%s' does not have ready resources" % username)
         if class_name not in RESOURCES[key]:
-            raise Exception('[XOS-TOSCA] The model you are trying to create (%s: %s, class: %s) is not know by xos-core' % (used_key, data[used_key], class_name))
+            raise Exception('[XOS-TOSCA] The model you are trying to create (class: %s, properties, %s) is not know by xos-core' % (class_name, str(filter)))
 
         cls = RESOURCES[key][class_name]
-        if used_key:
-            models = cls.objects.filter(**{used_key: data[used_key]})
-        else:
-            # NOTE if we don't have a way to track the model, create a new one
-            models = []
+
+        models = cls.objects.filter(**filter)
 
         if len(models) == 1:
-            print "[XOS-Tosca] Model %s already exist, retrieving instance..." % data[used_key]
+            print "[XOS-Tosca] Model of class %s and properties %s already exist, retrieving instance..." % (class_name, str(filter))
             model = models[0]
         elif len(models) == 0:
 
             if 'must-exist' in data and data['must-exist']:
-                raise Exception("[XOS-TOSCA] Model %s:%s has property 'must-exist' but cannot be found" % (class_name, data[used_key]))
+                raise Exception("[XOS-TOSCA] Model of class %s and properties %s has property 'must-exist' but cannot be found" % (class_name, str(filter)))
 
             model = cls.objects.new()
-            print "[XOS-Tosca] Model %s is new, creating new instance..." % data[used_key] if used_key else class_name
+            print "[XOS-Tosca] Model (%s) is new, creating new instance..." % str(filter)
         else:
-            raise Exception("[XOS-Tosca] Model %s has multiple instances, I can't handle it" % data[used_key])
+            raise Exception("[XOS-Tosca] Model of class %s and properties %s has multiple instances, I can't handle it" % (class_name, str(filter)))
 
         return model
\ No newline at end of file