extension: remove resourceId and resourceType from listExtensions

This commit is contained in:
Wei Zhou 2026-04-30 17:24:58 +02:00
parent 5674d8cbe7
commit b6500ca7e3
5 changed files with 14 additions and 132 deletions

View File

@ -70,17 +70,11 @@ public class ListExtensionsCmd extends BaseListCmd {
+ " When no parameters are passed, all the details are returned.")
private List<String> details;
@Parameter(name = ApiConstants.TYPE, type = CommandType.STRING, description = "Type of the extension (e.g. Orchestrator, NetworkOrchestrator)")
@Parameter(name = ApiConstants.TYPE, type = CommandType.STRING,
description = "Type of the extension (e.g. Orchestrator, NetworkOrchestrator)",
since = "4.23.0")
private String type;
@Parameter(name = ApiConstants.RESOURCE_ID, type = CommandType.STRING,
description = "ID of the resource to list registered extensions for (e.g. cluster UUID, physical network UUID)")
private String resourceId;
@Parameter(name = ApiConstants.RESOURCE_TYPE, type = CommandType.STRING,
description = "Type of the resource (e.g. Cluster, PhysicalNetwork)")
private String resourceType;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
@ -97,14 +91,6 @@ public class ListExtensionsCmd extends BaseListCmd {
return type;
}
public String getResourceId() {
return resourceId;
}
public String getResourceType() {
return resourceType;
}
public EnumSet<ApiConstants.ExtensionDetails> getDetails() throws InvalidParameterValueException {
if (CollectionUtils.isEmpty(details)) {
return EnumSet.of(ApiConstants.ExtensionDetails.all);

View File

@ -794,40 +794,6 @@ public class ExtensionsManagerImpl extends ManagerBase implements ExtensionsMana
String name = cmd.getName();
String keyword = cmd.getKeyword();
String typeStr = cmd.getType();
String resourceIdStr = cmd.getResourceId();
String resourceTypeStr = cmd.getResourceType();
// If resourceId + resourceType are specified, return only extensions registered to that resource
if (StringUtils.isNotBlank(resourceIdStr) && StringUtils.isNotBlank(resourceTypeStr)) {
if (!EnumUtils.isValidEnum(ExtensionResourceMap.ResourceType.class, resourceTypeStr)) {
throw new InvalidParameterValueException("Invalid resourcetype: " + resourceTypeStr);
}
ExtensionResourceMap.ResourceType resType = ExtensionResourceMap.ResourceType.valueOf(resourceTypeStr);
// Resolve resourceId to a DB id
long resolvedResourceId;
if (ExtensionResourceMap.ResourceType.PhysicalNetwork.equals(resType)) {
PhysicalNetworkVO pn = physicalNetworkDao.findByUuid(resourceIdStr);
if (pn == null) {
try { pn = physicalNetworkDao.findById(Long.parseLong(resourceIdStr)); } catch (NumberFormatException ignored) {}
}
if (pn == null) throw new InvalidParameterValueException("Invalid physical network ID: " + resourceIdStr);
resolvedResourceId = pn.getId();
} else {
try { resolvedResourceId = Long.parseLong(resourceIdStr); } catch (NumberFormatException e) {
throw new InvalidParameterValueException("Invalid resource ID: " + resourceIdStr);
}
}
List<ExtensionResourceMapVO> maps = extensionResourceMapDao.listByResourceIdAndType(resolvedResourceId, resType);
List<ExtensionResponse> responses = new ArrayList<>();
for (ExtensionResourceMapVO map : maps) {
ExtensionVO ext = extensionDao.findById(map.getExtensionId());
if (ext == null) continue;
if (typeStr != null && !typeStr.equalsIgnoreCase(ext.getType().name())) continue;
if (name != null && !name.equalsIgnoreCase(ext.getName())) continue;
responses.add(createExtensionResponse(ext, cmd.getDetails()));
}
return responses;
}
final SearchBuilder<ExtensionVO> sb = extensionDao.createSearchBuilder();
final Filter searchFilter = new Filter(ExtensionVO.class, "id", false, cmd.getStartIndex(), cmd.getPageSizeVal());
@ -1032,21 +998,21 @@ public class ExtensionsManagerImpl extends ManagerBase implements ExtensionsMana
ExtensionResourceMap.ResourceType resType = ExtensionResourceMap.ResourceType.valueOf(resourceType);
if (ExtensionResourceMap.ResourceType.PhysicalNetwork.equals(resType)) {
PhysicalNetworkVO physicalNetwork = physicalNetworkDao.findByUuid(resourceId);
if (physicalNetwork == null) {
physicalNetwork = physicalNetworkDao.findById(Long.parseLong(resourceId));
}
if (physicalNetwork == null) {
throw new InvalidParameterValueException("Invalid physical network ID specified");
}
ExtensionResourceMap extensionResourceMap = registerExtensionWithPhysicalNetwork(physicalNetwork, extension, cmd.getDetails());
return extensionDao.findById(extensionResourceMap.getExtensionId());
} else if (ExtensionResourceMap.ResourceType.Cluster.equals(resType)) {
ClusterVO clusterVO = clusterDao.findByUuid(resourceId);
if (clusterVO == null) {
throw new InvalidParameterValueException("Invalid cluster ID specified");
}
ExtensionResourceMap extensionResourceMap = registerExtensionWithCluster(clusterVO, extension, cmd.getDetails());
return extensionDao.findById(extensionResourceMap.getExtensionId());
} else {
throw new InvalidParameterValueException("Unsupported resource type specified");
}
ClusterVO clusterVO = clusterDao.findByUuid(resourceId);
if (clusterVO == null) {
throw new InvalidParameterValueException("Invalid cluster ID specified");
}
ExtensionResourceMap extensionResourceMap = registerExtensionWithCluster(clusterVO, extension, cmd.getDetails());
return extensionDao.findById(extensionResourceMap.getExtensionId());
}
@Override

View File

@ -92,7 +92,7 @@ public class ListExtensionsCmdTest {
}
// -----------------------------------------------------------------------
// Tests for new getters: type, resourceId, resourceType
// Tests for type getter
// -----------------------------------------------------------------------
@Test
@ -107,27 +107,4 @@ public class ListExtensionsCmdTest {
assertNull(cmd.getType());
}
@Test
public void testGetResourceIdReturnsValueWhenSet() {
setPrivateField("resourceId", "pnet-uuid-123");
assertEquals("pnet-uuid-123", cmd.getResourceId());
}
@Test
public void testGetResourceIdReturnsNullWhenUnset() {
setPrivateField("resourceId", null);
assertNull(cmd.getResourceId());
}
@Test
public void testGetResourceTypeReturnsValueWhenSet() {
setPrivateField("resourceType", "PhysicalNetwork");
assertEquals("PhysicalNetwork", cmd.getResourceType());
}
@Test
public void testGetResourceTypeReturnsNullWhenUnset() {
setPrivateField("resourceType", null);
assertNull(cmd.getResourceType());
}
}

View File

@ -2743,52 +2743,6 @@ public class ExtensionsManagerImplTest {
assertNull(result);
}
// -----------------------------------------------------------------------
// Tests for listExtensions with resourceId + resourceType (PhysicalNetwork)
// -----------------------------------------------------------------------
@Test
public void listExtensionsWithPhysicalNetworkResourceReturnsFilteredExtensions() {
ListExtensionsCmd cmd = mock(ListExtensionsCmd.class);
when(cmd.getResourceId()).thenReturn("pnet-uuid");
when(cmd.getResourceType()).thenReturn(ExtensionResourceMap.ResourceType.PhysicalNetwork.name());
when(cmd.getExtensionId()).thenReturn(null);
when(cmd.getName()).thenReturn(null);
when(cmd.getType()).thenReturn(null);
when(cmd.getDetails()).thenReturn(null);
PhysicalNetworkVO physNet = mock(PhysicalNetworkVO.class);
when(physNet.getId()).thenReturn(42L);
when(physicalNetworkDao.findByUuid("pnet-uuid")).thenReturn(physNet);
ExtensionResourceMapVO mapVO = mock(ExtensionResourceMapVO.class);
when(mapVO.getExtensionId()).thenReturn(100L);
when(extensionResourceMapDao.listByResourceIdAndType(42L, ExtensionResourceMap.ResourceType.PhysicalNetwork))
.thenReturn(List.of(mapVO));
ExtensionVO ext = mock(ExtensionVO.class);
when(ext.getType()).thenReturn(Extension.Type.NetworkOrchestrator);
when(extensionDao.findById(100L)).thenReturn(ext);
ExtensionResponse resp = mock(ExtensionResponse.class);
doReturn(resp).when(extensionsManager).createExtensionResponse(eq(ext), any());
List<ExtensionResponse> result = extensionsManager.listExtensions(cmd);
assertEquals(1, result.size());
assertEquals(resp, result.get(0));
}
@Test(expected = InvalidParameterValueException.class)
public void listExtensionsWithInvalidResourceTypeThrows() {
ListExtensionsCmd cmd = mock(ListExtensionsCmd.class);
when(cmd.getResourceId()).thenReturn("resource-uuid");
when(cmd.getResourceType()).thenReturn("InvalidType");
when(cmd.getExtensionId()).thenReturn(null);
when(cmd.getName()).thenReturn(null);
when(cmd.getType()).thenReturn(null);
extensionsManager.listExtensions(cmd);
}
// -----------------------------------------------------------------------
// Tests for registerExtensionWithPhysicalNetwork

View File

@ -1325,8 +1325,7 @@ public class NetworkModelImpl extends ManagerBase implements NetworkModel, Confi
// Use a set to avoid adding the same provider name twice (multiple phys-nets)
Set<String> addedExtProviders = new HashSet<>();
for (PhysicalNetworkVO physNet : physNets) {
List<com.cloud.network.dao.PhysicalNetworkServiceProviderVO> nsps =
_pNSPDao.listBy(physNet.getId());
List<PhysicalNetworkServiceProviderVO> nsps = _pNSPDao.listBy(physNet.getId());
if (nsps == null) continue;
for (PhysicalNetworkServiceProviderVO nsp : nsps) {
String provName = nsp.getProviderName();