fix error when creating slice using sliceplus object
[plstackapi.git] / planetstack / core / xoslib / objects / sliceplus.py
index 4fdc824..7e9836c 100644 (file)
@@ -8,9 +8,11 @@ class SlicePlus(Slice, PlusObjectMixin):
 
     def __init__(self, *args, **kwargs):
         super(SlicePlus, self).__init__(*args, **kwargs)
-        self._update_site_allocation = None
         self._update_users = None
         self._sliceInfo = None
+        self.getSliceInfo()
+        self._site_allocation = self._sliceInfo["sitesUsed"]
+        self._initial_site_allocation = self._site_allocation
 
     def getSliceInfo(self, user=None):
         if not self._sliceInfo:
@@ -46,17 +48,20 @@ class SlicePlus(Slice, PlusObjectMixin):
 
     @property
     def site_allocation(self):
-        return self.getSliceInfo()["sitesUsed"]
+        return self._site_allocation
 
     @site_allocation.setter
     def site_allocation(self, value):
-        self._update_site_allocation = value
-        #print "XXX set sitesUsed to", value
+        self._site_allocation = value
 
     @property
     def user_names(self):
         return [user["name"] for user in self.getSliceInfo()["users"].values()]
 
+    @user_names.setter
+    def user_names(self, value):
+        pass # it's read-only
+
     @property
     def users(self):
         return [user["id"] for user in self.getSliceInfo()["users"].values()]
@@ -105,26 +110,35 @@ class SlicePlus(Slice, PlusObjectMixin):
         return nodeList
 
     def save(self, *args, **kwargs):
+        updated_image = self.has_field_changed("default_image")
+        updated_flavor = self.has_field_changed("default_flavor")
+
         super(SlicePlus, self).save(*args, **kwargs)
 
-        if self._update_site_allocation:
-            self.save_site_allocation(noAct=True)
+        updated_sites = (self._site_allocation != self._initial_site_allocation) or updated_image or updated_flavor
+        if updated_sites:
+            self.save_site_allocation(noAct=True, reset=(updated_image or updated_flavor))
 
         if self._update_users:
             self.save_users(noAct=True)
 
-        if self._update_site_allocation:
-            self.save_site_allocation()
+        if updated_sites:
+            self.save_site_allocation(reset=(updated_image or updated_flavor))
 
         if self._update_users:
             self.save_users()
 
-    def save_site_allocation(self, noAct = False):
-        new_site_allocation = self._update_site_allocation
+    def save_site_allocation(self, noAct = False, reset=False):
+        print "save_site_allocation, reset=",reset
+
+        if (not self._site_allocation):
+            # Must be a sliver that was just created, and has not site_allocation
+            # field.
+            return
 
         all_slice_slivers = self.slivers.all()
-        for site_name in new_site_allocation.keys():
-            desired_allocation = new_site_allocation[site_name]
+        for site_name in self._site_allocation.keys():
+            desired_allocation = self._site_allocation[site_name]
 
             # make a list of the slivers for this site
             slivers = []
@@ -133,11 +147,13 @@ class SlicePlus(Slice, PlusObjectMixin):
                     slivers.append(sliver)
 
             # delete extra slivers
-            while (len(slivers) > desired_allocation):
+            while (reset and len(slivers)>0) or (len(slivers) > desired_allocation):
                 sliver = slivers.pop()
-                print "deleting sliver", sliver
                 if (not noAct):
+                    print "deleting sliver", sliver
                     sliver.delete()
+                else:
+                    print "would delete sliver", sliver
 
             # add more slivers
             if (len(slivers) < desired_allocation):
@@ -161,9 +177,10 @@ class SlicePlus(Slice, PlusObjectMixin):
                             deployment = node.site_deployment.deployment)
                     slivers.append(sliver)
                     if (not noAct):
+                        print "added sliver", sliver
                         sliver.save()
-
-                    print "added sliver", sliver
+                    else:
+                        print "would add sliver", sliver
 
                     node.sliverCount = node.sliverCount + 1