blob: 573c1676d842b43b49c6aa47e23f70f4cf8f3e64 [file] [log] [blame]
Scott Baker714d5242015-08-05 08:20:12 -07001import pdb
2
Scott Baker3841b372015-08-03 14:20:31 -07003class XOSResource(object):
4 xos_base_class = "XOSResource"
Scott Baker9fdb39f2015-08-04 16:44:18 -07005 xos_model = None
Scott Baker3841b372015-08-03 14:20:31 -07006 provides = None
7
8 def __init__(self, user, nodetemplate):
Scott Baker9fce62e2015-08-03 15:43:54 -07009 self.dirty = False
Scott Baker3841b372015-08-03 14:20:31 -070010 self.user = user
11 self.nodetemplate = nodetemplate
Scott Baker3841b372015-08-03 14:20:31 -070012
Scott Baker32795252015-08-04 23:53:07 -070013 def get_all_required_node_names(self):
14 results = []
15 for reqs in self.nodetemplate.requirements:
16 for (k,v) in reqs.items():
17 results.append(v["node"])
18 return results
19
Scott Baker14152942015-08-04 10:59:29 -070020 def get_requirements(self, relationship_name, throw_exception=False):
Scott Baker9fce62e2015-08-03 15:43:54 -070021 """ helper to search the list of requirements for a particular relationship
22 type.
23 """
Scott Baker14152942015-08-04 10:59:29 -070024
25 results = []
Scott Baker9fce62e2015-08-03 15:43:54 -070026 for reqs in self.nodetemplate.requirements:
27 for (k,v) in reqs.items():
28 if (v["relationship"] == relationship_name):
Scott Baker14152942015-08-04 10:59:29 -070029 results.append(v["node"])
Scott Baker9fce62e2015-08-03 15:43:54 -070030
Scott Baker14152942015-08-04 10:59:29 -070031 if (not results) and throw_exception:
Scott Baker9fce62e2015-08-03 15:43:54 -070032 raise Exception("Failed to find requirement in %s using relationship %s" % (self.nodetemplate.name, relationship_name))
33
Scott Baker14152942015-08-04 10:59:29 -070034 return results
35
36 def get_requirement(self, relationship_name, throw_exception=False):
37 reqs = self.get_requirements(relationship_name, throw_exception)
38 if not reqs:
39 return None
40 return reqs[0]
Scott Baker9fce62e2015-08-03 15:43:54 -070041
Scott Baker714d5242015-08-05 08:20:12 -070042 def get_scalable(self):
Scott Baker502400a2015-08-05 10:41:51 -070043 scalable = self.nodetemplate.get_capabilities().get("scalable", None)
44 if scalable:
45 return {"min_instances": scalable.get_property_value("min_instances"),
46 "max_instances": scalable.get_property_value("max_instances"),
47 "default_instances": scalable.get_property_value("default_instances")}
Scott Baker714d5242015-08-05 08:20:12 -070048 else:
49 return {}
50
Scott Baker9fce62e2015-08-03 15:43:54 -070051 def get_xos_object(self, cls, **kwargs):
52 objs = cls.objects.filter(**kwargs)
53 if not objs:
54 raise Exception("Failed to find %s filtered by %s" % (cls.__name__, str(kwargs)))
55 return objs[0]
56
Scott Baker9fdb39f2015-08-04 16:44:18 -070057 def get_existing_objs(self):
58 return self.xos_model.objects.filter(name = self.nodetemplate.name)
59
60 def get_xos_args(self):
61 return {}
62
63 def create_or_update(self):
64 existing_objs = self.get_existing_objs()
65 if existing_objs:
66 self.info("%s %s already exists" % (self.xos_model.__name__, self.nodetemplate.name))
67 self.update(existing_objs[0])
68 else:
69 self.create()
70
71 def create(self):
72 raise Exception("abstract method -- must override")
73
74 def update(self, obj):
Scott Baker3841b372015-08-03 14:20:31 -070075 pass
76
Scott Baker9fce62e2015-08-03 15:43:54 -070077 def info(self, s):
78 print s
79