Scott Baker | 8e6647a | 2016-06-20 17:16:20 -0700 | [diff] [blame] | 1 | import os |
| 2 | import pdb |
| 3 | import sys |
| 4 | import tempfile |
| 5 | sys.path.append("/opt/tosca") |
| 6 | from translator.toscalib.tosca_template import ToscaTemplate |
| 7 | import pdb |
| 8 | |
| 9 | from core.models import User |
| 10 | from services.volt.models import CordSubscriberRoot |
| 11 | |
| 12 | from xosresource import XOSResource |
| 13 | |
| 14 | class XOSCORDUser(XOSResource): |
| 15 | provides = "tosca.nodes.CORDUser" |
| 16 | |
| 17 | def get_model_class_name(self): |
| 18 | return "CORDUser" |
| 19 | |
| 20 | def get_subscriber_root(self, throw_exception=True): |
| 21 | sub_name = self.get_requirement("tosca.relationships.SubscriberDevice", throw_exception=throw_exception) |
| 22 | sub = self.get_xos_object(CordSubscriberRoot, name=sub_name, throw_exception=throw_exception) |
| 23 | return sub |
| 24 | |
| 25 | def get_existing_objs(self): |
| 26 | result = [] |
| 27 | sub = self.get_subscriber_root(throw_exception=False) |
| 28 | if not sub: |
| 29 | return [] |
| 30 | for user in sub.devices: |
| 31 | if user["name"] == self.obj_name: |
| 32 | result.append(user) |
| 33 | return result |
| 34 | |
| 35 | def get_xos_args(self): |
| 36 | args = {"name": self.obj_name, |
| 37 | "level": self.get_property("level"), |
| 38 | "mac": self.get_property("mac")} |
| 39 | return args |
| 40 | |
| 41 | |
| 42 | def create(self): |
| 43 | xos_args = self.get_xos_args() |
| 44 | sub = self.get_subscriber_root() |
| 45 | |
| 46 | sub.create_device(**xos_args) |
| 47 | sub.save() |
| 48 | |
| 49 | self.info("Created CORDUser %s for Subscriber %s" % (self.obj_name, sub.name)) |
| 50 | |
| 51 | def update(self, obj): |
| 52 | pass |
| 53 | |
| 54 | def delete(self, obj): |
| 55 | if (self.can_delete(obj)): |
| 56 | self.info("destroying CORDUser %s" % obj["name"]) |
| 57 | sub = self.get_subscriber_root() |
| 58 | sub.delete_user(obj["id"]) |
| 59 | sub.save() |
| 60 | |
| 61 | def can_delete(self, obj): |
| 62 | return True |
| 63 | |