blob: 566e205c09736fb32cfd9d6270dd5059e14f8627 [file] [log] [blame]
Scott Bakerc0581502015-09-01 22:57:46 -07001import os
2import pdb
3import sys
4import tempfile
5sys.path.append("/opt/tosca")
6from translator.toscalib.tosca_template import ToscaTemplate
7import pdb
8
9from core.models import User
Scott Baker98f51552015-12-31 10:18:28 -080010from services.cord.models import CordSubscriberRoot
Scott Bakerc0581502015-09-01 22:57:46 -070011
12from xosresource import XOSResource
13
14class 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.users:
31 if user["name"] == self.nodetemplate.name:
32 result.append(user)
33 return result
34
35 def get_xos_args(self):
36 args = {"name": self.nodetemplate.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_user(**xos_args)
47 sub.save()
48
49 self.info("Created CORDUser %s for Subscriber %s" % (self.nodetemplate.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