add ability to replace objects using tosca
diff --git a/xos/tosca/resources/xosresource.py b/xos/tosca/resources/xosresource.py
index 82514c9..012f814 100644
--- a/xos/tosca/resources/xosresource.py
+++ b/xos/tosca/resources/xosresource.py
@@ -2,6 +2,7 @@
import pdb
import json
import subprocess
+import sys
from core.models import User
@@ -90,6 +91,13 @@
return None
return objs[0]
+ def get_replaces_objs(self):
+ replaces = self.get_property_default("replaces", None)
+ if replaces:
+ return self.xos_model.objects.filter(**{self.name_field: replaces})
+ else:
+ return []
+
def get_existing_objs(self):
return self.xos_model.objects.filter(**{self.name_field: self.obj_name})
@@ -97,7 +105,25 @@
return self.xos_model.__name__
def create_or_update(self):
+ replaces_objs = self.get_replaces_objs()
existing_objs = self.get_existing_objs()
+
+ if (replaces_objs and existing_objs):
+ ro = replaces_objs[0]
+ self.info("deleting %s:%s" % (self.get_model_class_name(), getattr(ro,self.name_field)))
+ ro.delete()
+
+ # in case we wanted to throw an error instead...
+ #self.error("CRITICAL ERROR: Both %s and %s exist!" % (getattr(ro,self.name_field), self.obj_name))
+ #sys.exit(-1)
+
+ if (replaces_objs and not existing_objs):
+ ro = replaces_objs[0]
+ self.info("renaming %s:%s to %s" % (self.get_model_class_name(), getattr(ro,self.name_field), self.obj_name))
+ setattr(ro, self.name_field, self.obj_name)
+ ro.save()
+ existing_objs = self.get_existing_objs()
+
if existing_objs:
if self.get_property_default("no-update", False):
self.info("%s:%s (%s) already exists. Skipping update due to 'no-update' property" % (self.get_model_class_name(), self.obj_name, self.full_name))