[CORD-1528] Allow to reference models already created
Change-Id: Icf1cab69e0939d84f7f9c01d774f4399df51a7a4
diff --git a/src/grpc_client/models_accessor.py b/src/grpc_client/models_accessor.py
index 29c2f11..188a9ce 100644
--- a/src/grpc_client/models_accessor.py
+++ b/src/grpc_client/models_accessor.py
@@ -11,14 +11,13 @@
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:
used_key = data.keys()[0]
if class_name not in RESOURCES:
- raise Exception('[XOS-TOSCA] The model your tring 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 (%s: %s, class: %s) is not know by xos-core' % (used_key, data[used_key], class_name))
cls = RESOURCES[class_name]
models = cls.objects.filter(**{used_key: data[used_key]})
@@ -27,6 +26,10 @@
print "[XOS-Tosca] Model %s already exist, retrieving instance..." % data[used_key]
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]))
+
model = cls.objects.new()
print "[XOS-Tosca] Model %s is new, creating new instance..." % data[used_key]
else: