Implement functions 'RemoveDisk' and 'DetachInstanceDisk'. The first one
removes a disk from the config file and the second one detaches a disk
from an instance. A wrapper 'RemoveInstanceDisk' is provided in order to
detach a disk from an instance and remove it at once (since Ganeti
doesn't support dangling disks yet).

Signed-off-by: Ilias Tsitsimpis <[email protected]>
---
 lib/config.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/lib/config.py b/lib/config.py
index c0359c5..d59e9a6 100644
--- a/lib/config.py
+++ b/lib/config.py
@@ -455,6 +455,59 @@ class ConfigWriter(object):
     self._UnlockedAddDisk(disk)
     self._UnlockedAttachInstanceDisk(inst_uuid, disk.uuid, idx)
 
+  def _UnlockedDetachInstanceDisk(self, inst_uuid, disk_uuid):
+    """Detach a disk from an instance.
+
+    @type inst_uuid: string
+    @param inst_uuid: The UUID of the instance object
+    @type disk_uuid: string
+    @param disk_uuid: The UUID of the disk object
+
+    """
+    instance = self._UnlockedGetInstanceInfo(inst_uuid)
+    if instance is None:
+      raise errors.ConfigurationError("Instance %s doesn't exist"
+                                      % inst_uuid)
+    if disk_uuid not in self._ConfigData().disks:
+      raise errors.ConfigurationError("Disk %s doesn't exist" % disk_uuid)
+
+    # Check if disk is attached to the instance
+    if disk_uuid not in instance.disks:
+      raise errors.ProgrammerError("Disk %s is not attached to an instance"
+                                   % disk_uuid)
+
+    idx = instance.disks.index(disk_uuid)
+    instance.disks.remove(disk_uuid)
+    instance_disks = self._UnlockedGetInstanceDisks(inst_uuid)
+    _UpdateIvNames(idx, instance_disks[idx:])
+    instance.serial_no += 1
+    instance.mtime = time.time()
+
+  def _UnlockedRemoveDisk(self, disk_uuid):
+    """Remove the disk from the configuration.
+
+    @type disk_uuid: string
+    @param disk_uuid: The UUID of the disk object
+
+    """
+    if disk_uuid not in self._ConfigData().disks:
+      raise errors.ConfigurationError("Disk %s doesn't exist" % disk_uuid)
+
+    # Remove disk from config file
+    del self._ConfigData().disks[disk_uuid]
+    self._ConfigData().cluster.serial_no += 1
+
+  @_ConfigSync()
+  def RemoveInstanceDisk(self, inst_uuid, disk_uuid):
+    """Detach a disk from an instance and remove it from the config.
+
+    This is a simple wrapper over L{_UnlockedDetachInstanceDisk} and
+    L{_UnlockedRemoveDisk}.
+
+    """
+    self._UnlockedDetachInstanceDisk(inst_uuid, disk_uuid)
+    self._UnlockedRemoveDisk(disk_uuid)
+
   def _UnlockedGetDiskInfo(self, disk_uuid):
     """Returns information about a disk.
 
-- 
1.9.1

Reply via email to