Scott Baker | 0f05874 | 2015-09-16 16:14:42 -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 | |
| 8 | from core.models import Node, Site, Deployment, SiteDeployment |
| 9 | |
| 10 | from xosresource import XOSResource |
| 11 | |
| 12 | class XOSNode(XOSResource): |
| 13 | provides = "tosca.nodes.Node" |
| 14 | xos_model = Node |
| 15 | |
| 16 | def get_xos_args(self): |
| 17 | args = {"name": self.nodetemplate.name} |
| 18 | |
| 19 | site = None |
| 20 | siteName = self.get_requirement("tosca.relationships.MemberOfSite", throw_exception=False) |
| 21 | if siteName: |
Scott Baker | bff14a4 | 2015-09-30 21:00:31 -0700 | [diff] [blame] | 22 | site = self.get_xos_object(Site, login_base=siteName) |
Scott Baker | 0f05874 | 2015-09-16 16:14:42 -0700 | [diff] [blame] | 23 | args["site"] = site |
| 24 | |
| 25 | deploymentName = self.get_requirement("tosca.relationships.MemberOfDeployment", throw_exception=False) |
| 26 | if deploymentName: |
| 27 | deployment = self.get_xos_object(Deployment, name=deploymentName) |
| 28 | |
| 29 | if site: |
| 30 | siteDeployment = self.get_xos_object(SiteDeployment, site=site, deployment=deployment, throw_exception=True) |
| 31 | args["site_deployment"] = siteDeployment |
| 32 | |
| 33 | return args |
| 34 | |
| 35 | def create(self): |
| 36 | nodetemplate = self.nodetemplate |
| 37 | sliceName = nodetemplate.name |
| 38 | |
| 39 | xos_args = self.get_xos_args() |
| 40 | |
| 41 | if not xos_args.get("site", None): |
| 42 | raise Exception("Site is a required field of Node") |
| 43 | if not xos_args.get("site_deployment", None): |
| 44 | raise Exception("Deployment is a required field of Node") |
| 45 | |
| 46 | node = Node(**xos_args) |
| 47 | node.caller = self.user |
| 48 | node.save() |
| 49 | |
| 50 | self.postprocess(node) |
| 51 | |
| 52 | self.info("Created Node '%s' on Site '%s' Deployment '%s'" % (str(node), str(node.site), str(node.site_deployment.deployment))) |
| 53 | |
| 54 | def delete(self, obj): |
| 55 | super(XOSNode, self).delete(obj) |
| 56 | |
| 57 | |
| 58 | |