add ability to create slices
diff --git a/xos/tosca/custom_types/xos.yaml b/xos/tosca/custom_types/xos.yaml
index 87c96b4..b1f6873 100644
--- a/xos/tosca/custom_types/xos.yaml
+++ b/xos/tosca/custom_types/xos.yaml
@@ -3,7 +3,10 @@
node_types:
tosca.nodes.Service:
derived_from: tosca.nodes.Root
-
+
+ tosca.nodes.Site:
+ derived_from: tosca.nodes.Root
+
tosca.nodes.Slice:
derived_from: tosca.nodes.Root
properties:
@@ -17,5 +20,8 @@
tosca.relationships.MemberOfService:
derived_from: tosca.relationships.Root
+ tosca.relationships.MemberOfSite:
+ derived_from: tosca.relationships.Root
+
tosca.relationships.TenantOfService:
derived_from: tosca.relationships.Root
diff --git a/xos/tosca/engine.py b/xos/tosca/engine.py
index f4c2a86..e33d0f0 100644
--- a/xos/tosca/engine.py
+++ b/xos/tosca/engine.py
@@ -38,7 +38,7 @@
if nodetemplate.type in resources.resources:
cls = resources.resources[nodetemplate.type]
obj = cls(user, nodetemplate)
- print "XXX created", obj.resource
+ obj.save_if_dirty()
diff --git a/xos/tosca/resources/compute.py b/xos/tosca/resources/compute.py
index e292f64..71c7926 100644
--- a/xos/tosca/resources/compute.py
+++ b/xos/tosca/resources/compute.py
@@ -41,19 +41,8 @@
flavor=None
image=None
- sliceName = None
- for reqs in nodetemplate.requirements:
- for (k,v) in reqs.items():
- print v
- if (v["relationship"] == "tosca.relationships.MemberOfSlice"):
- sliceName = v["node"]
- if not sliceName:
- raise Exception("No slice requirement for node %s" % nodetemplate.name)
-
- slice = Slice.objects.filter(name=sliceName)
- if not slice:
- raise Exception("Could not find slice %s" % sliceName)
- slice = slice[0]
+ sliceName = self.get_requirement("tosca.relationships.MemberOfSlice", throw_exception=True)
+ slice = self.get_xos_object(Slice, name=sliceName)
capabilities = nodetemplate.get_capabilities()
for (k,v) in capabilities.items():
@@ -77,6 +66,10 @@
sliver.caller = self.user
self.resource = sliver
+ self.dirty = True
+
+ self.info("Created Sliver '%s' on node '%s' using flavor '%s' and image '%s'" %
+ (str(sliver), str(compute_node), str(flavor), str(image)))
def save(self):
self.resource.save()
diff --git a/xos/tosca/resources/slice.py b/xos/tosca/resources/slice.py
new file mode 100644
index 0000000..6fdfd35
--- /dev/null
+++ b/xos/tosca/resources/slice.py
@@ -0,0 +1,37 @@
+import os
+import pdb
+import sys
+import tempfile
+sys.path.append("/opt/tosca")
+from translator.toscalib.tosca_template import ToscaTemplate
+
+from core.models import Slice,User,Site
+
+from xosresource import XOSResource
+
+class XOSSlice(XOSResource):
+ provides = "tosca.nodes.Slice"
+
+ def process_nodetemplate(self):
+ nodetemplate = self.nodetemplate
+ sliceName = nodetemplate.name
+
+ existing_slices = Slice.objects.filter(name=sliceName)
+ if existing_slices:
+ self.info("Slice %s already exists" % sliceName)
+
+ site_name = self.get_requirement("tosca.relationships.MemberOfSite", throw_exception=True)
+ site = self.get_xos_object(Site, login_base=site_name)
+
+ slice = Slice(name = sliceName,
+ site = site)
+ slice.caller = self.user
+
+ self.resource = slice
+ self.dirty = True
+
+ self.info("Created Slice '%s' on Site '%s'" % (str(slice), str(site)))
+
+ def save(self):
+ self.resource.save()
+
diff --git a/xos/tosca/resources/xosresource.py b/xos/tosca/resources/xosresource.py
index 890f965..05adcbe 100644
--- a/xos/tosca/resources/xosresource.py
+++ b/xos/tosca/resources/xosresource.py
@@ -3,13 +3,42 @@
provides = None
def __init__(self, user, nodetemplate):
+ self.dirty = False
self.user = user
self.nodetemplate = nodetemplate
self.process_nodetemplate()
+ def get_requirement(self, relationship_name, throw_exception=False):
+ """ helper to search the list of requirements for a particular relationship
+ type.
+ """
+ for reqs in self.nodetemplate.requirements:
+ for (k,v) in reqs.items():
+ if (v["relationship"] == relationship_name):
+ return v["node"]
+
+ if throw_exception:
+ raise Exception("Failed to find requirement in %s using relationship %s" % (self.nodetemplate.name, relationship_name))
+
+ return None
+
+ def get_xos_object(self, cls, **kwargs):
+ objs = cls.objects.filter(**kwargs)
+ if not objs:
+ raise Exception("Failed to find %s filtered by %s" % (cls.__name__, str(kwargs)))
+ return objs[0]
+
def process_nodetemplate(self):
pass
def save(self):
pass
+ def save_if_dirty(self):
+ if self.dirty:
+ self.save()
+ self.dirty=False
+
+ def info(self, s):
+ print s
+
diff --git a/xos/tosca/samples/one_instance.yaml b/xos/tosca/samples/one_instance.yaml
index 968c999..f8919ed 100644
--- a/xos/tosca/samples/one_instance.yaml
+++ b/xos/tosca/samples/one_instance.yaml
@@ -7,8 +7,15 @@
topology_template:
node_templates:
+ mysite:
+ type: tosca.nodes.Site
+
mysite_tosca:
type: tosca.nodes.Slice
+ requirements:
+ - slice:
+ node: mysite
+ relationship: tosca.relationships.MemberOfSite
my_server:
type: tosca.nodes.Compute