server: Check for null poolid (#6879)

Extract retrieveDatastore method

Add unit test for null poolId

Fixes #6878

Co-authored-by: Craig Squire <craig.squire@ticketmaster.com>
Co-authored-by: Stephan Krug <stekrug@icloud.com>
This commit is contained in:
Craig Squire 2022-11-30 01:15:35 -06:00 committed by GitHub
parent 6c436ec90e
commit dbc2032077
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -318,8 +318,10 @@ public class TaggedResourceManagerImpl extends ManagerBase implements TaggedReso
private void informStoragePoolForVmTags(long vmId, String key, String value) {
List<VolumeVO> volumeVos = volumeDao.findByInstance(vmId);
for (VolumeVO volume : volumeVos) {
DataStore dataStore = dataStoreMgr.getDataStore(volume.getPoolId(), DataStoreRole.Primary);
Long poolId = volume.getPoolId();
DataStore dataStore = retrieveDatastore(poolId);
if (dataStore == null || !(dataStore.getDriver() instanceof PrimaryDataStoreDriver)) {
s_logger.info(String.format("No data store found for VM %d with pool ID %d.", vmId, poolId));
continue;
}
PrimaryDataStoreDriver dataStoreDriver = (PrimaryDataStoreDriver) dataStore.getDriver();
@ -328,4 +330,11 @@ public class TaggedResourceManagerImpl extends ManagerBase implements TaggedReso
}
}
}
protected DataStore retrieveDatastore(Long poolId) {
if (poolId == null) {
return null;
}
return dataStoreMgr.getDataStore(poolId, DataStoreRole.Primary);
}
}

View File

@ -25,6 +25,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -123,4 +124,10 @@ public class TaggedResourceManagerImplTest extends TestCase {
Mockito.doThrow(PermissionDeniedException.class).when(accountManager).checkAccess(caller, null, false, owner);
taggedResourceManagerImplSpy.checkTagsDeletePermission(List.of(resourceTag1, resourceTag2), caller);
}
@Test
public void testRetrieveDataStoreNullPoolId() {
DataStore dataStore = taggedResourceManagerImplSpy.retrieveDatastore(null);
Assert.assertNull(dataStore);
}
}