blob: 29c2f11d0846b812b540a38faece481b9b548f45 [file] [log] [blame]
Matteo Scandolo485b7132017-06-30 11:46:47 -07001from resources import RESOURCES
2
3class GRPCModelsAccessor:
4 """
5 This class provide the glue between the models managed by TOSCA and the ones living in xos-core
6 """
7
8 @staticmethod
9 def get_model_from_classname(class_name, data):
10 """
11 Give a Model Class Name and some data, check if that exits or instantiate a new one
12 """
13
14
15 if data.get('name'):
16 used_key = 'name'
17 else:
18 used_key = data.keys()[0]
19
20 if class_name not in RESOURCES:
21 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))
22
23 cls = RESOURCES[class_name]
24 models = cls.objects.filter(**{used_key: data[used_key]})
25
26 if len(models) == 1:
27 print "[XOS-Tosca] Model %s already exist, retrieving instance..." % data[used_key]
28 model = models[0]
29 elif len(models) == 0:
30 model = cls.objects.new()
31 print "[XOS-Tosca] Model %s is new, creating new instance..." % data[used_key]
32 else:
33 raise Exception("[XOS-Tosca] Model %s has multiple instances, I can't handle it" % data[used_key])
34 return model