Skip to content

Commit 5b6bfbc

Browse files
Add check for host NQN before un mapping it from Subsystem
1 parent 57499ff commit 5b6bfbc

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

storage_drivers/ontap/api/abstraction_rest.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3451,23 +3451,36 @@ func (d OntapAPIREST) NVMeEnsureNamespaceUnmapped(ctx context.Context, hostNQN,
34513451
return false, fmt.Errorf("error getting hosts mapped to subsystem with UUID %s; %v", subsystemUUID, err)
34523452
}
34533453

3454-
if subsystemHosts == nil {
3455-
return false, fmt.Errorf("error getting hosts attached to subsystem %v", subsystemUUID)
3454+
hostFound := false
3455+
for _, host := range subsystemHosts {
3456+
if host != nil && *host.Nqn == hostNQN {
3457+
hostFound = true
3458+
break
3459+
}
34563460
}
34573461

34583462
// In case of multiple hosts attached to a subsystem (e.g. in RWX case), do not delete the namespace,
34593463
// subsystem or the published info
34603464
if len(subsystemHosts) > 1 {
3461-
Logc(ctx).Infof("Multiple hosts are attached to this subsystem %v. Do not delete namespace or subsystem",
3462-
subsystemUUID)
3463-
// Remove HostNQN from the subsystem using api call
3464-
if err := d.api.NVMeRemoveHostFromSubsystem(ctx, hostNQN, subsystemUUID); err != nil {
3465-
Logc(ctx).Errorf("Remove host from subsystem failed; %v", err)
3466-
return false, err
3465+
if hostFound {
3466+
Logc(ctx).Infof("Multiple hosts are attached to this subsystem %v. Do not delete namespace or subsystem",
3467+
subsystemUUID)
3468+
if err = d.api.NVMeRemoveHostFromSubsystem(ctx, hostNQN, subsystemUUID); err != nil {
3469+
Logc(ctx).Errorf("Remove host from subsystem failed; %v", err)
3470+
return false, err
3471+
}
34673472
}
34683473
return false, nil
34693474
}
34703475

3476+
// If there is only one host attached to the subsystem, check if it is the host which is being asked to be unmapped
3477+
if len(subsystemHosts) == 1 && !hostFound {
3478+
Logc(ctx).Infof("Hosts are attached to this subsystem %v. Do not delete namespace or subsystem",
3479+
subsystemUUID)
3480+
return false, nil
3481+
}
3482+
3483+
// Proceed further as there are no hosts or only the specified host is present that needs to be unmapped.
34713484
// Unmap the namespace from the subsystem
34723485
err = d.api.NVMeSubsystemRemoveNamespace(ctx, subsystemUUID, namespaceUUID)
34733486
if err != nil {

storage_drivers/ontap/api/abstraction_rest_test.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,8 @@ func TestNVMeNamespaceUnmapped(t *testing.T) {
865865
nsUUID := "fakeNsUUID"
866866
hostNQN := "fakeHostNQN"
867867
host2NQN := "fakeHost2NQN"
868+
nonExistentHostNQN := "fakeHostNQNNonExisting"
869+
868870
host1 := &models.NvmeSubsystemHost{Nqn: &hostNQN}
869871
host2 := &models.NvmeSubsystemHost{Nqn: &host2NQN}
870872
var removePublishInfo bool
@@ -901,11 +903,14 @@ func TestNVMeNamespaceUnmapped(t *testing.T) {
901903
mock.EXPECT().ClientConfig().Return(clientConfig).AnyTimes()
902904
mock.EXPECT().NVMeIsNamespaceMapped(ctx, subsystemUUID, nsUUID).Return(true, nil).Times(1)
903905
mock.EXPECT().NVMeGetHostsOfSubsystem(ctx, subsystemUUID).Return(nil, nil).Times(1)
906+
mock.EXPECT().NVMeSubsystemRemoveNamespace(ctx, subsystemUUID, nsUUID).AnyTimes().Return(nil).Times(1)
907+
mock.EXPECT().NVMeNamespaceCount(ctx, subsystemUUID).Return(int64(0), nil).Times(1)
908+
mock.EXPECT().NVMeSubsystemDelete(ctx, subsystemUUID).Return(nil).Times(1)
904909

905910
removePublishInfo, err = oapi.NVMeEnsureNamespaceUnmapped(ctx, hostNQN, subsystemUUID, nsUUID)
906911

907-
assert.Equal(t, false, removePublishInfo, "subsystem removed")
908-
assert.Error(t, err)
912+
assert.Equal(t, true, removePublishInfo, "subsystem removed")
913+
assert.NoError(t, err)
909914

910915
// case 5: multiple hosts of the subsystem returned but error while removing host from subsystem
911916
mock.EXPECT().ClientConfig().Return(clientConfig).AnyTimes()
@@ -977,6 +982,28 @@ func TestNVMeNamespaceUnmapped(t *testing.T) {
977982

978983
assert.Equal(t, true, removePublishInfo, "subsystem is not removed")
979984
assert.NoError(t, err)
985+
986+
// case 11: Success when the nqn is already removed from subsystem
987+
mock.EXPECT().ClientConfig().Return(clientConfig).AnyTimes()
988+
mock.EXPECT().NVMeIsNamespaceMapped(ctx, subsystemUUID, nsUUID).Return(true, nil).Times(1)
989+
mock.EXPECT().NVMeGetHostsOfSubsystem(ctx, subsystemUUID).Return([]*models.NvmeSubsystemHost{host1},
990+
nil).Times(1)
991+
992+
removePublishInfo, err = oapi.NVMeEnsureNamespaceUnmapped(ctx, nonExistentHostNQN, subsystemUUID, nsUUID)
993+
994+
assert.Equal(t, false, removePublishInfo, "nqn is unmapped")
995+
assert.NoError(t, err)
996+
997+
// case 12: Success when the nqn is already removed from subsystem with multiple hosts
998+
mock.EXPECT().ClientConfig().Return(clientConfig).AnyTimes()
999+
mock.EXPECT().NVMeIsNamespaceMapped(ctx, subsystemUUID, nsUUID).Return(true, nil).Times(1)
1000+
mock.EXPECT().NVMeGetHostsOfSubsystem(ctx, subsystemUUID).Return([]*models.NvmeSubsystemHost{host1, host2},
1001+
nil).Times(1)
1002+
1003+
removePublishInfo, err = oapi.NVMeEnsureNamespaceUnmapped(ctx, nonExistentHostNQN, subsystemUUID, nsUUID)
1004+
1005+
assert.Equal(t, false, removePublishInfo, "nqn is unmapped")
1006+
assert.NoError(t, err)
9801007
}
9811008

9821009
func TestNVMeIsNamespaceMapped(t *testing.T) {

0 commit comments

Comments
 (0)