Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NAS-133544 / 25.04 / Delete fcport target mapping on target delete #15391

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/middlewared/middlewared/plugins/iscsi_/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ async def __validate(self, verrors, data, schema_name, old=None):
f'Auth network "{network}" is not a valid IPv4 or IPv6 network'
)

async def __remove_target_fcport(self, id_):
for fcport in await self.middleware.call('fcport.query', [['target.id', '=', id_]]):
await self.middleware.call('fcport.delete', fcport['id'])

@api_method(
IscsiTargetValidateNameArgs,
IscsiTargetValidateNameResult,
Expand Down Expand Up @@ -376,6 +380,11 @@ async def do_delete(self, audit_callback, id_, force, delete_extents):
if delete_extents:
await self.middleware.call('iscsi.extent.delete', target_to_extent['extent'], False, force)

# If the target was being used for FC then we may also need to clear the
# Fibre Channel port mapping
if target['mode'] in ['FC', 'BOTH']:
await self.__remove_target_fcport(id_)

await self.middleware.call(
'datastore.delete', 'services.iscsitargetgroups', [['iscsi_target', '=', id_]]
)
Expand Down
31 changes: 28 additions & 3 deletions tests/api2/test_fibre_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from assets.websocket.pool import zvol
from auto_config import ha, pool_name

from middlewared.service_exception import ValidationError, ValidationErrors
from middlewared.service_exception import InstanceNotFound, ValidationError, ValidationErrors
from middlewared.test.integration.utils import call, mock, ssh

NODE_A_0_WWPN = '0x210000aaaaaaaa01'
Expand Down Expand Up @@ -182,12 +182,18 @@ def node_b_hardware(remote=False):


@contextlib.contextmanager
def fcport_create(alias, target_id):
def fcport_create(alias, target_id, allow_deleted=False):
config = call('fcport.create', {'port': alias, 'target_id': target_id})
try:
yield config
finally:
call('fcport.delete', config['id'])
if allow_deleted:
try:
call('fcport.delete', config['id'])
except InstanceNotFound:
pass
else:
call('fcport.delete', config['id'])


class TestFixtureFibreChannel:
Expand Down Expand Up @@ -411,6 +417,25 @@ def test_target(self, fc_hosts):
# Then put is back
assert call('fcport.update', map0['id'], {'port': fc_hosts[0]['alias']})['port'] == fc_hosts[0]['alias']

def test_target_delete(self, fc_hosts):
"""Ensure that we can delete a mapped FC target."""
with target_lun_zero('fctarget0', 'fcextent0', 100) as config:
target_id = config['target']['id']

# Change the mode of the target
call('iscsi.target.update', target_id, {'mode': 'FC'})

# Now we should be able to successfully map the target
with fcport_create(fc_hosts[0]['alias'], target_id, True):
# Make sure we have a mapping
assert len(call('fcport.query', [['target.id', '=', target_id]])) == 1

# Delete the target
call('iscsi.target.delete', target_id, True, True)

# Make sure we DON'T have a mapping
assert len(call('fcport.query', [['target.id', '=', target_id]])) == 0

def test_npiv_setting(self, fc_hosts):
# Try to set NPIV to -1
with pytest.raises(ValidationErrors) as ve:
Expand Down
Loading