def validate_disk(self, key, value): # pylint: disable=W0613 Disk.polymorphic_subclass(value, "Invalid disk type") return value
def render(self, session, logger, machine, disk, controller, share, filesystem, resourcegroup, address, comments, size, boot, snapshot, rename_to, **kw): dbmachine = Machine.get_unique(session, machine, compel=True) dbdisk = Disk.get_unique(session, device_name=disk, machine=dbmachine, compel=True) plenaries = PlenaryCollection(logger=logger) plenaries.append(Plenary.get_plenary(dbmachine)) if rename_to: Disk.get_unique(session, device_name=rename_to, machine=dbmachine, preclude=True) dbdisk.device_name = rename_to if comments is not None: dbdisk.comments = comments if size is not None: dbdisk.capacity = size if controller: if controller not in controller_types: raise ArgumentError("%s is not a valid controller type, use " "one of: %s." % (controller, ", ".join(controller_types))) dbdisk.controller_type = controller if boot is not None: dbdisk.bootable = boot # There should be just one boot disk. We may need to re-think this # if we want to model software RAID in the database. for disk in dbmachine.disks: if disk == dbdisk: continue if boot and disk.bootable: disk.bootable = False if address: # TODO: do we really care? Bus address makes sense for physical # disks as well, even if we cannot use that information today. if not isinstance(dbdisk, VirtualDisk): raise ArgumentError("Bus address can only be set for virtual " "disks.") dbdisk.address = address if snapshot is not None: if not isinstance(dbdisk, VirtualDisk): raise ArgumentError("Snapshot capability can only be set for " "virtual disks.") dbdisk.snapshotable = snapshot if share or filesystem: if isinstance(dbdisk, VirtualNasDisk): old_share = dbdisk.share old_share.disks.remove(dbdisk) elif isinstance(dbdisk, VirtualLocalDisk): old_fs = dbdisk.filesystem old_fs.disks.remove(dbdisk) else: raise ArgumentError("Disk {0!s} of {1:l} is not a virtual " "disk, changing the backend store is not " "possible.".format(dbdisk, dbmachine)) if share: if not isinstance(dbdisk, VirtualNasDisk): new_dbdisk = copy_virt_disk(session, VirtualNasDisk, dbdisk) session.delete(dbdisk) session.flush() session.add(new_dbdisk) dbdisk = new_dbdisk new_share = find_resource(Share, dbmachine.vm_container.holder.holder_object, resourcegroup, share) new_share.disks.append(dbdisk) if filesystem: if not isinstance(dbdisk, VirtualLocalDisk): new_dbdisk = copy_virt_disk(session, VirtualLocalDisk, dbdisk) session.delete(dbdisk) session.flush() session.add(new_dbdisk) dbdisk = new_dbdisk new_fs = find_resource(Filesystem, dbmachine.vm_container.holder.holder_object, resourcegroup, filesystem) new_fs.disks.append(dbdisk) session.flush() plenaries.write() return