Scott Baker | 714d524 | 2015-08-05 08:20:12 -0700 | [diff] [blame] | 1 | import pdb |
| 2 | |
Scott Baker | 3841b37 | 2015-08-03 14:20:31 -0700 | [diff] [blame] | 3 | class XOSResource(object): |
| 4 | xos_base_class = "XOSResource" |
Scott Baker | 9fdb39f | 2015-08-04 16:44:18 -0700 | [diff] [blame] | 5 | xos_model = None |
Scott Baker | 3841b37 | 2015-08-03 14:20:31 -0700 | [diff] [blame] | 6 | provides = None |
| 7 | |
| 8 | def __init__(self, user, nodetemplate): |
Scott Baker | 9fce62e | 2015-08-03 15:43:54 -0700 | [diff] [blame] | 9 | self.dirty = False |
Scott Baker | 3841b37 | 2015-08-03 14:20:31 -0700 | [diff] [blame] | 10 | self.user = user |
| 11 | self.nodetemplate = nodetemplate |
Scott Baker | 3841b37 | 2015-08-03 14:20:31 -0700 | [diff] [blame] | 12 | |
Scott Baker | 3279525 | 2015-08-04 23:53:07 -0700 | [diff] [blame] | 13 | 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 Baker | 1415294 | 2015-08-04 10:59:29 -0700 | [diff] [blame] | 20 | def get_requirements(self, relationship_name, throw_exception=False): |
Scott Baker | 9fce62e | 2015-08-03 15:43:54 -0700 | [diff] [blame] | 21 | """ helper to search the list of requirements for a particular relationship |
| 22 | type. |
| 23 | """ |
Scott Baker | 1415294 | 2015-08-04 10:59:29 -0700 | [diff] [blame] | 24 | |
| 25 | results = [] |
Scott Baker | 9fce62e | 2015-08-03 15:43:54 -0700 | [diff] [blame] | 26 | for reqs in self.nodetemplate.requirements: |
| 27 | for (k,v) in reqs.items(): |
| 28 | if (v["relationship"] == relationship_name): |
Scott Baker | 1415294 | 2015-08-04 10:59:29 -0700 | [diff] [blame] | 29 | results.append(v["node"]) |
Scott Baker | 9fce62e | 2015-08-03 15:43:54 -0700 | [diff] [blame] | 30 | |
Scott Baker | 1415294 | 2015-08-04 10:59:29 -0700 | [diff] [blame] | 31 | if (not results) and throw_exception: |
Scott Baker | 9fce62e | 2015-08-03 15:43:54 -0700 | [diff] [blame] | 32 | raise Exception("Failed to find requirement in %s using relationship %s" % (self.nodetemplate.name, relationship_name)) |
| 33 | |
Scott Baker | 1415294 | 2015-08-04 10:59:29 -0700 | [diff] [blame] | 34 | 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 Baker | 9fce62e | 2015-08-03 15:43:54 -0700 | [diff] [blame] | 41 | |
Scott Baker | 714d524 | 2015-08-05 08:20:12 -0700 | [diff] [blame] | 42 | def get_scalable(self): |
Scott Baker | 502400a | 2015-08-05 10:41:51 -0700 | [diff] [blame^] | 43 | 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 Baker | 714d524 | 2015-08-05 08:20:12 -0700 | [diff] [blame] | 48 | else: |
| 49 | return {} |
| 50 | |
Scott Baker | 9fce62e | 2015-08-03 15:43:54 -0700 | [diff] [blame] | 51 | 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 Baker | 9fdb39f | 2015-08-04 16:44:18 -0700 | [diff] [blame] | 57 | 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 Baker | 3841b37 | 2015-08-03 14:20:31 -0700 | [diff] [blame] | 75 | pass |
| 76 | |
Scott Baker | 9fce62e | 2015-08-03 15:43:54 -0700 | [diff] [blame] | 77 | def info(self, s): |
| 78 | print s |
| 79 | |