From e02915ed75d5d7cf84e91f69e309d4dabb42cdeb Mon Sep 17 00:00:00 2001
From: David Lehman <dlehman@redhat.com>
Date: Fri, 7 Mar 2014 16:48:19 -0600
Subject: [PATCH 6/6] Add action classes for container member set management.

---
 blivet/deviceaction.py | 152 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 149 insertions(+), 3 deletions(-)

diff --git a/blivet/deviceaction.py b/blivet/deviceaction.py
index cfb8022..8dd14ff 100644
--- a/blivet/deviceaction.py
+++ b/blivet/deviceaction.py
@@ -49,19 +49,25 @@ ACTION_TYPE_NONE = 0
 ACTION_TYPE_DESTROY = 1000
 ACTION_TYPE_RESIZE = 500
 ACTION_TYPE_CREATE = 100
+ACTION_TYPE_ADD = 50
+ACTION_TYPE_REMOVE = 10
 
 action_strings = {ACTION_TYPE_NONE: "None",
                   ACTION_TYPE_DESTROY: "Destroy",
                   ACTION_TYPE_RESIZE: "Resize",
-                  ACTION_TYPE_CREATE: "Create"}
+                  ACTION_TYPE_CREATE: "Create",
+                  ACTION_TYPE_ADD: "Add",
+                  ACTION_TYPE_REMOVE: "Remove"}
 
 ACTION_OBJECT_NONE = 0
 ACTION_OBJECT_FORMAT = 1
 ACTION_OBJECT_DEVICE = 2
+ACTION_OBJECT_CONTAINER = 3
 
 object_strings = {ACTION_OBJECT_NONE: "None",
                   ACTION_OBJECT_FORMAT: "Format",
-                  ACTION_OBJECT_DEVICE: "Device"}
+                  ACTION_OBJECT_DEVICE: "Device",
+                  ACTION_OBJECT_CONTAINER: "Container"}
 
 RESIZE_SHRINK = 88
 RESIZE_GROW = 89
@@ -148,6 +154,7 @@ class DeviceAction(util.ObjectID):
         if not isinstance(device, StorageDevice):
             raise ValueError("arg 1 must be a StorageDevice instance")
         self.device = device
+        self.container = getattr(self.device, "container", None)
 
     def execute(self):
         """ perform the action """
@@ -178,10 +185,22 @@ class DeviceAction(util.ObjectID):
         return (self.type == ACTION_TYPE_RESIZE and self.dir == RESIZE_GROW)
 
     @property
+    def isAdd(self):
+        return self.type == ACTION_TYPE_ADD
+
+    @property
+    def isRemove(self):
+        return self.type == ACTION_TYPE_REMOVE
+
+    @property
     def isDevice(self):
         return self.obj == ACTION_OBJECT_DEVICE
 
     @property
+    def isContainer(self):
+        return self.obj == ACTION_OBJECT_CONTAINER
+
+    @property
     def isFormat(self):
         return self.obj == ACTION_OBJECT_FORMAT
 
@@ -273,6 +292,7 @@ class ActionCreateDevice(DeviceAction):
                 - this action's device depends on the other action's device
                 - both actions are partition create actions on the same disk
                   and this partition has a higher number
+                - the other action adds a member to this device's container
         """
         rc = False
         if self.device.dependsOn(action.device):
@@ -292,6 +312,9 @@ class ActionCreateDevice(DeviceAction):
               self.device.vg == action.device.vg and
               action.device.singlePV and not self.device.singlePV):
             rc = True
+        elif (action.isAdd and action.container == self.container):
+            rc = True
+
         return rc
 
 
@@ -325,6 +348,7 @@ class ActionDestroyDevice(DeviceAction):
                 - the other action's device depends on this action's device
                 - both actions are partition create actions on the same disk
                   and this partition has a lower number
+                - the other action removes this action's device from a container
         """
         rc = False
         if action.device.dependsOn(self.device) and action.isDestroy:
@@ -342,6 +366,8 @@ class ActionDestroyDevice(DeviceAction):
               action.device.id == self.device.id):
             # device destruction comes after destruction of device's format
             rc = True
+        elif (action.isRemove and action.device == self.device):
+            rc = True
         return rc
 
     def obsoletes(self, action):
@@ -352,6 +378,10 @@ class ActionDestroyDevice(DeviceAction):
 
             - obsoletes all but ActionDestroyFormat actions w/ lower id on the
               same device if device exists
+
+            - obsoletes all actions that add a member to this action's
+              (container) device
+
         """
         rc = False
         if action.device.id == self.device.id:
@@ -361,6 +391,10 @@ class ActionDestroyDevice(DeviceAction):
                  self.device.exists and \
                  not (action.isDestroy and action.isFormat):
                 rc = True
+            elif action.isAdd and (action.device == self.device):
+                rc = True
+        elif action.isAdd and (action.container == self.device):
+            rc = True
 
         return rc
 
@@ -413,6 +447,8 @@ class ActionResizeDevice(DeviceAction):
                   this action's device depends on
                 - the other action shrinks a device (or format it contains)
                   that depends on this action's device
+                - the other action removes this action's device from a container
+                - the other action adds a member to this device's container
         """
         retval = False
         if action.isResize:
@@ -424,6 +460,10 @@ class ActionResizeDevice(DeviceAction):
                 retval = True
             elif action.isShrink and action.device.dependsOn(self.device):
                 retval = True
+        elif (action.isRemove and action.device == self.device):
+            rc = True
+        elif (action.isAdd and action.container == self.container):
+            rc = True
 
         return retval
 
@@ -545,8 +585,15 @@ class ActionDestroyFormat(DeviceAction):
 
                 - the other action's device depends on this action's device
                   and the other action is a destroy action
+                - the other action removes this action's device from a container
         """
-        return action.device.dependsOn(self.device) and action.isDestroy
+        retval = False
+        if action.device.dependsOn(self.device) and action.isDestroy:
+            retval = True
+        elif (action.isRemove and action.device == self.device):
+            rc = True
+
+        return retval
 
     def obsoletes(self, action):
         """ Return True if this action obsoletes action.
@@ -611,6 +658,7 @@ class ActionResizeFormat(DeviceAction):
                   that depends on this action's device
                 - the other action grows a device (or format) that this
                   action's device depends on
+                - the other action removes this action's device from a container
         """
         retval = False
         if action.isResize:
@@ -622,5 +670,103 @@ class ActionResizeFormat(DeviceAction):
                 retval = True
             elif action.isGrow and self.device.dependsOn(action.device):
                 retval = True
+        elif (action.isRemove and action.device == self.device):
+            rc = True
+
+        return retval
+
+class ActionRemoveMember(DeviceAction):
+    type = ACTION_TYPE_REMOVE
+    obj = ACTION_OBJECT_CONTAINER
+    typeDescStr = N_("remove container member")
+
+    def __init__(self, device, container):
+        super(ActionRemoveMember, self).__init__(device)
+        self.container = container
+
+    def cancel(self):
+        self.container._addMember(self.device)
+
+    def execute(self):
+        self.container.removeMember(self.device)
+
+    def requires(self, action):
+        """
+            requires
+                - any destroy/shrink action on a device in the same container
+
+            required by
+                - any destroy/resize action on the device
+        """
+        retval = False
+        if ((action.isShrink or action.isDestroy) and
+            action.device.container == self.container):
+            retval = True
+
+        return retval
+
+    def obsoletes(self, action):
+        """
+            obsoletes
+                - add same member to same container w/ lower id
+
+            obsoleted by
+                - add same member to same container w/ higher id
+        """
+        retval = False
+        if (action.isAdd and
+            action.device == self.device and
+            action.container == self.container and
+            action.id < self.id):
+            retval = True
+
+        return retval
+
+class ActionAddMember(DeviceAction):
+    type = ACTION_TYPE_ADD
+    obj = ACTION_OBJECT_CONTAINER
+    typeDescStr = N_("add container member")
+
+    def __init__(self, device, container):
+        super(ActionRemoveMember, self).__init__(device)
+        self.container = container
+
+    def cancel(self):
+        self.container._removeDevice(self.device)
+
+    def execute(self):
+        self.container.addMember(self.device)
+
+    def requires(self, action):
+        """
+            requires
+                - create/resize the same device
+
+            required by
+                - any create/grow action on a device in the same container
+        """
+        retval = False
+        if ((action.isCreate or action.isResize) and
+            action.device == self.device):
+            retval = True
+
+        return retval
+
+    def obsoletes(self, action):
+        """
+            obsoletes
+                - remove same member from same container w/ lower id
+
+            obsoleted by
+                - destroy the container
+                - destroy the device
+                - remove same member from same container w/ higher id
+        """
+        retval = False
+        if (action.isRemove and
+            action.device == self.device and
+            action.container == self.container and
+            action.id < self.id):
+            retval = True
 
         return retval
-- 
1.8.5.3

