def change_wt_disk_status(self, wtd_name, enabled): try: wt_disk = self._get_wt_disk(wtd_name) wt_disk.Enabled = enabled wt_disk.put() except exceptions.x_wmi as wmi_exc: err_msg = _('Could not change disk status. WT Disk name: %s') raise exceptions.ISCSITargetWMIException(err_msg % wtd_name, wmi_exc=wmi_exc)
def remove_wt_disk(self, wtd_name): try: wt_disk = self._get_wt_disk(wtd_name, fail_if_not_found=False) if wt_disk: wt_disk.Delete_() except exceptions.x_wmi as wmi_exc: err_msg = _("Failed to remove WT disk: %s.") raise exceptions.ISCSITargetWMIException(err_msg % wtd_name, wmi_exc=wmi_exc)
def import_wt_disk(self, vhd_path, wtd_name): """Import a vhd/x image to be used by Windows iSCSI targets.""" try: self._conn_wmi.WT_Disk.ImportWTDisk(DevicePath=vhd_path, Description=wtd_name) except exceptions.x_wmi as wmi_exc: err_msg = _("Failed to import WT disk: %s.") raise exceptions.ISCSITargetWMIException(err_msg % vhd_path, wmi_exc=wmi_exc)
def extend_wt_disk(self, wtd_name, additional_mb): try: wt_disk = self._get_wt_disk(wtd_name) wt_disk.Extend(additional_mb) except exceptions.x_wmi as wmi_exc: err_msg = _('Could not extend WT Disk %(wtd_name)s ' 'with additional %(additional_mb)s MB.') raise exceptions.ISCSITargetWMIException( err_msg % dict(wtd_name=wtd_name, additional_mb=additional_mb), wmi_exc=wmi_exc)
def delete_snapshot(self, snapshot_name): """Driver entry point for deleting a snapshot.""" try: wt_snapshot = self._get_wt_snapshot(snapshot_name, fail_if_not_found=False) if wt_snapshot: wt_snapshot.Delete_() except exceptions.x_wmi as wmi_exc: err_msg = _('Failed delete snapshot %s.') raise exceptions.ISCSITargetWMIException(err_msg % snapshot_name, wmi_exc=wmi_exc)
def set_chap_credentials(self, target_name, chap_username, chap_password): try: wt_host = self._get_wt_host(target_name) wt_host.EnableCHAP = True wt_host.CHAPUserName = chap_username wt_host.CHAPSecret = chap_password wt_host.put() except exceptions.x_wmi as wmi_exc: err_msg = _('Failed to set CHAP credentials on target %s.') raise exceptions.ISCSITargetWMIException(err_msg % target_name, wmi_exc=wmi_exc)
def deassociate_initiator(self, initiator, target_name): try: wt_idmethod = self._get_wt_idmethod(initiator, target_name) if wt_idmethod: wt_idmethod.Delete_() except exceptions.x_wmi as wmi_exc: err_msg = _('Could not deassociate initiator %(initiator)s from ' 'iSCSI target: %(target_name)s.') raise exceptions.ISCSITargetWMIException( err_msg % dict(initiator=initiator, target_name=target_name), wmi_exc=wmi_exc)
def set_chap_credentials(self, target_name, chap_username, chap_password): try: wt_host = self._get_wt_host(target_name) self._wmi_obj_set_attr(wt_host, 'EnableCHAP', True) self._wmi_obj_set_attr(wt_host, 'CHAPUserName', chap_username) self._wmi_obj_set_attr(wt_host, 'CHAPSecret', chap_password) wt_host.put() except wmi.x_wmi as wmi_exc: err_msg = _('Failed to set CHAP credentials on target %s.') raise exceptions.ISCSITargetWMIException(err_msg % target_name, wmi_exc=wmi_exc)
def add_disk_to_target(self, wtd_name, target_name): """Adds the disk to the target.""" try: wt_disk = self._get_wt_disk(wtd_name) wt_host = self._get_wt_host(target_name) wt_host.AddWTDisk(wt_disk.WTD) except exceptions.x_wmi as wmi_exc: err_msg = _('Could not add WTD Disk %(wtd_name)s to ' 'iSCSI target %(target_name)s.') raise exceptions.ISCSITargetWMIException( err_msg % dict(wtd_name=wtd_name, target_name=target_name), wmi_exc=wmi_exc)
def create_wt_disk(self, vhd_path, wtd_name, size_mb=None): try: self._conn_wmi.WT_Disk.NewWTDisk(DevicePath=vhd_path, Description=wtd_name, SizeInMB=size_mb) except exceptions.x_wmi as wmi_exc: err_msg = _('Failed to create WT Disk. ' 'VHD path: %(vhd_path)s ' 'WT disk name: %(wtd_name)s') raise exceptions.ISCSITargetWMIException( err_msg % dict(vhd_path=vhd_path, wtd_name=wtd_name), wmi_exc=wmi_exc)
def delete_iscsi_target(self, target_name): """Removes ISCSI target.""" try: wt_host = self._get_wt_host(target_name, fail_if_not_found=False) if not wt_host: LOG.debug('Skipping deleting target %s as it does not ' 'exist.', target_name) return wt_host.RemoveAllWTDisks() wt_host.Delete_() except exceptions.x_wmi as wmi_exc: err_msg = _("Failed to delete ISCSI target %s") raise exceptions.ISCSITargetWMIException(err_msg % target_name, wmi_exc=wmi_exc)
def create_iscsi_target(self, target_name, fail_if_exists=False): """Creates ISCSI target.""" try: self._conn_wmi.WT_Host.NewHost(HostName=target_name) except exceptions.x_wmi as wmi_exc: err_code = _utils.get_com_error_code(wmi_exc.com_error) target_exists = err_code == self._ERR_FILE_EXISTS if not target_exists or fail_if_exists: err_msg = _('Failed to create iSCSI target: %s.') raise exceptions.ISCSITargetWMIException(err_msg % target_name, wmi_exc=wmi_exc) else: LOG.info('The iSCSI target %s already exists.', target_name)
def create_snapshot(self, wtd_name, snapshot_name): """Driver entry point for creating a snapshot.""" try: wt_disk = self._get_wt_disk(wtd_name) snap_id = self._conn_wmi.WT_Snapshot.Create(WTD=wt_disk.WTD)[0] wt_snap = self._conn_wmi.WT_Snapshot(Id=snap_id)[0] wt_snap.Description = snapshot_name wt_snap.put() except exceptions.x_wmi as wmi_exc: err_msg = _('Failed to create snapshot. ' 'WT Disk name: %(wtd_name)s ' 'Snapshot name: %(snapshot_name)s') raise exceptions.ISCSITargetWMIException( err_msg % dict(wtd_name=wtd_name, snapshot_name=snapshot_name), wmi_exc=wmi_exc)
def associate_initiator_with_iscsi_target(self, initiator, target_name, id_method=ID_METHOD_IQN): wt_idmethod = self._get_wt_idmethod(initiator, target_name) if wt_idmethod: return try: wt_idmethod = self._conn_wmi.WT_IDMethod.new() wt_idmethod.HostName = target_name wt_idmethod.Method = id_method wt_idmethod.Value = initiator wt_idmethod.put() except exceptions.x_wmi as wmi_exc: err_msg = _('Could not associate initiator %(initiator)s to ' 'iSCSI target: %(target_name)s.') raise exceptions.ISCSITargetWMIException( err_msg % dict(initiator=initiator, target_name=target_name), wmi_exc=wmi_exc)
def export_snapshot(self, snapshot_name, dest_path): """Driver entry point for exporting snapshots as volumes.""" try: wt_snap = self._get_wt_snapshot(snapshot_name) wt_disk_id = wt_snap.Export()[0] # This export is a read-only shadow copy, needing to be copied # to another disk. wt_disk = self._conn_wmi.WT_Disk(WTD=wt_disk_id)[0] wt_disk.Description = '%s-%s-temp' % (snapshot_name, wt_disk_id) wt_disk.put() src_path = wt_disk.DevicePath self._pathutils.copy(src_path, dest_path) wt_disk.Delete_() except exceptions.x_wmi as wmi_exc: err_msg = _('Failed to export snapshot %(snapshot_name)s ' 'to %(dest_path)s.') raise exceptions.ISCSITargetWMIException( err_msg % dict(snapshot_name=snapshot_name, dest_path=dest_path), wmi_exc=wmi_exc)