refactor
Change-Id: I833f751ab3f307f6996d0822bef7a749dc6f6d2f
diff --git a/src/grpc_client/models_accessor.py b/src/grpc_client/models_accessor.py
new file mode 100644
index 0000000..29c2f11
--- /dev/null
+++ b/src/grpc_client/models_accessor.py
@@ -0,0 +1,34 @@
+from resources import RESOURCES
+
+class GRPCModelsAccessor:
+ """
+ This class provide the glue between the models managed by TOSCA and the ones living in xos-core
+ """
+
+ @staticmethod
+ def get_model_from_classname(class_name, data):
+ """
+ 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))
+
+ cls = RESOURCES[class_name]
+ models = cls.objects.filter(**{used_key: data[used_key]})
+
+ if len(models) == 1:
+ print "[XOS-Tosca] Model %s already exist, retrieving instance..." % data[used_key]
+ model = models[0]
+ elif len(models) == 0:
+ model = cls.objects.new()
+ print "[XOS-Tosca] Model %s is new, creating new instance..." % data[used_key]
+ else:
+ raise Exception("[XOS-Tosca] Model %s has multiple instances, I can't handle it" % data[used_key])
+ return model
\ No newline at end of file