ability to create services and CoarseTenants, refactor the save() stuff
diff --git a/xos/tosca/engine.py b/xos/tosca/engine.py
index e33d0f0..2ecf165 100644
--- a/xos/tosca/engine.py
+++ b/xos/tosca/engine.py
@@ -38,7 +38,6 @@
if nodetemplate.type in resources.resources:
cls = resources.resources[nodetemplate.type]
obj = cls(user, nodetemplate)
- obj.save_if_dirty()
diff --git a/xos/tosca/resources/compute.py b/xos/tosca/resources/compute.py
index 71c7926..db65406 100644
--- a/xos/tosca/resources/compute.py
+++ b/xos/tosca/resources/compute.py
@@ -64,9 +64,9 @@
slice = slice,
image = image)
sliver.caller = self.user
+ sliver.save()
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)))
diff --git a/xos/tosca/resources/service.py b/xos/tosca/resources/service.py
new file mode 100644
index 0000000..0ef64ba
--- /dev/null
+++ b/xos/tosca/resources/service.py
@@ -0,0 +1,47 @@
+import os
+import pdb
+import sys
+import tempfile
+sys.path.append("/opt/tosca")
+from translator.toscalib.tosca_template import ToscaTemplate
+
+from core.models import Service,User,CoarseTenant
+
+from xosresource import XOSResource
+
+class XOSService(XOSResource):
+ provides = "tosca.nodes.Service"
+
+ def process_nodetemplate(self):
+ nodetemplate = self.nodetemplate
+ serviceName = nodetemplate.name
+
+ existing_services = Service.objects.filter(name=serviceName)
+ if existing_services:
+ self.info("Service %s already exists" % serviceName)
+ service = existing_services[0]
+ else:
+ service = Service(name = serviceName)
+ service.caller = self.user
+ service.save()
+
+ self.info("Created Service '%s'" % (str(service), ))
+
+ for provider_service_name in self.get_requirements("tosca.relationships.TenantOfService"):
+ provider_service = self.get_xos_object(Service, name=provider_service_name)
+
+ existing_tenancy = CoarseTenant.get_tenant_objects().filter(provider_service = provider_service, subscriber_service = service)
+ if existing_tenancy:
+ self.info("Tenancy relationship from %s to %s already exists" % (str(service), str(provider_service)))
+ else:
+ tenancy = CoarseTenant(provider_service = provider_service,
+ subscriber_service = service)
+ tenancy.save()
+
+ self.info("Created Tenancy relationship from %s to %s" % (str(service), str(provider_service)))
+
+ self.resource = service
+
+ def save(self):
+ self.resource.save()
+
diff --git a/xos/tosca/resources/slice.py b/xos/tosca/resources/slice.py
index 6fdfd35..bbcd861 100644
--- a/xos/tosca/resources/slice.py
+++ b/xos/tosca/resources/slice.py
@@ -19,18 +19,20 @@
existing_slices = Slice.objects.filter(name=sliceName)
if existing_slices:
self.info("Slice %s already exists" % sliceName)
+ slice = existing_slices[0]
+ else:
+ site_name = self.get_requirement("tosca.relationships.MemberOfSite", throw_exception=True)
+ site = self.get_xos_object(Site, login_base=site_name)
- 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
+ slice.save()
- slice = Slice(name = sliceName,
- site = site)
- slice.caller = self.user
+ self.info("Created Slice '%s' on Site '%s'" % (str(slice), str(site)))
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 05adcbe..1bb555c 100644
--- a/xos/tosca/resources/xosresource.py
+++ b/xos/tosca/resources/xosresource.py
@@ -8,19 +8,27 @@
self.nodetemplate = nodetemplate
self.process_nodetemplate()
- def get_requirement(self, relationship_name, throw_exception=False):
+ def get_requirements(self, relationship_name, throw_exception=False):
""" helper to search the list of requirements for a particular relationship
type.
"""
+
+ results = []
for reqs in self.nodetemplate.requirements:
for (k,v) in reqs.items():
if (v["relationship"] == relationship_name):
- return v["node"]
+ results.append(v["node"])
- if throw_exception:
+ if (not results) and throw_exception:
raise Exception("Failed to find requirement in %s using relationship %s" % (self.nodetemplate.name, relationship_name))
- return None
+ return results
+
+ def get_requirement(self, relationship_name, throw_exception=False):
+ reqs = self.get_requirements(relationship_name, throw_exception)
+ if not reqs:
+ return None
+ return reqs[0]
def get_xos_object(self, cls, **kwargs):
objs = cls.objects.filter(**kwargs)
@@ -31,14 +39,6 @@
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/composition.yaml b/xos/tosca/samples/cord.yaml
similarity index 100%
rename from xos/tosca/samples/composition.yaml
rename to xos/tosca/samples/cord.yaml