From de266a4cb85f27ac958cd12d3fb7d40dcb6b47f4 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Mon, 24 Jun 2024 14:41:01 +0530 Subject: [PATCH] api,server: allow updating hypervisor capabilities with hypervisor and version (#8475) * api,server: allow updating hypervisor capabilities with hypervisor and version hypervisor and hypervisorversion parameter added to the updateHypervisorCapabilities API. * param description * Update server/src/main/java/com/cloud/server/ManagementServerImpl.java Signed-off-by: Abhishek Kumar Co-authored-by: dahn Co-authored-by: Henrique Sato Co-authored-by: Vishesh --- .../UpdateHypervisorCapabilitiesCmd.java | 14 +++++ .../hypervisor/HypervisorCapabilitiesVO.java | 12 +++++ .../cloud/server/ManagementServerImpl.java | 52 ++++++++++++++++--- 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/api/src/main/java/org/apache/cloudstack/api/command/admin/config/UpdateHypervisorCapabilitiesCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/admin/config/UpdateHypervisorCapabilitiesCmd.java index 02cdf1a0717..6c70b24653d 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/admin/config/UpdateHypervisorCapabilitiesCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/admin/config/UpdateHypervisorCapabilitiesCmd.java @@ -45,6 +45,12 @@ public class UpdateHypervisorCapabilitiesCmd extends BaseCmd { @Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = HypervisorCapabilitiesResponse.class, description = "ID of the hypervisor capability") private Long id; + @Parameter(name = ApiConstants.HYPERVISOR, type = CommandType.STRING, description = "the hypervisor for which the hypervisor capabilities are to be updated", since = "4.19.1") + private String hypervisor; + + @Parameter(name = ApiConstants.HYPERVISOR_VERSION, type = CommandType.STRING, description = "the hypervisor version for which the hypervisor capabilities are to be updated", since = "4.19.1") + private String hypervisorVersion; + @Parameter(name = ApiConstants.SECURITY_GROUP_EANBLED, type = CommandType.BOOLEAN, description = "set true to enable security group for this hypervisor.") private Boolean securityGroupEnabled; @@ -75,6 +81,14 @@ public class UpdateHypervisorCapabilitiesCmd extends BaseCmd { return id; } + public String getHypervisor() { + return hypervisor; + } + + public String getHypervisorVersion() { + return hypervisorVersion; + } + public Long getMaxGuestsLimit() { return maxGuestsLimit; } diff --git a/engine/schema/src/main/java/com/cloud/hypervisor/HypervisorCapabilitiesVO.java b/engine/schema/src/main/java/com/cloud/hypervisor/HypervisorCapabilitiesVO.java index 5ab684c1100..118699c1f84 100644 --- a/engine/schema/src/main/java/com/cloud/hypervisor/HypervisorCapabilitiesVO.java +++ b/engine/schema/src/main/java/com/cloud/hypervisor/HypervisorCapabilitiesVO.java @@ -80,6 +80,18 @@ public class HypervisorCapabilitiesVO implements HypervisorCapabilities { this.uuid = UUID.randomUUID().toString(); } + public HypervisorCapabilitiesVO(HypervisorCapabilitiesVO source) { + this.hypervisorType = source.getHypervisorType(); + this.hypervisorVersion = source.getHypervisorVersion(); + this.maxGuestsLimit = source.getMaxGuestsLimit(); + this.maxDataVolumesLimit = source.getMaxDataVolumesLimit(); + this.maxHostsPerCluster = source.getMaxHostsPerCluster(); + this.securityGroupEnabled = source.isSecurityGroupEnabled(); + this.storageMotionSupported = source.isStorageMotionSupported(); + this.vmSnapshotEnabled = source.isVmSnapshotEnabled(); + this.uuid = UUID.randomUUID().toString(); + } + /** * @param hypervisorType the hypervisorType to set */ diff --git a/server/src/main/java/com/cloud/server/ManagementServerImpl.java b/server/src/main/java/com/cloud/server/ManagementServerImpl.java index 28140d843ad..14afcc71245 100644 --- a/server/src/main/java/com/cloud/server/ManagementServerImpl.java +++ b/server/src/main/java/com/cloud/server/ManagementServerImpl.java @@ -5065,22 +5065,51 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe return new Pair, Integer>(result.first(), result.second()); } + protected HypervisorCapabilitiesVO getHypervisorCapabilitiesForUpdate(final Long id, final String hypervisorStr, final String hypervisorVersion) { + if (id == null && StringUtils.isAllEmpty(hypervisorStr, hypervisorVersion)) { + throw new InvalidParameterValueException("Either ID or hypervisor and hypervisor version must be specified"); + } + if (id != null) { + if (!StringUtils.isAllBlank(hypervisorStr, hypervisorVersion)) { + throw new InvalidParameterValueException("ID can not be specified together with hypervisor and hypervisor version"); + } + HypervisorCapabilitiesVO hpvCapabilities = _hypervisorCapabilitiesDao.findById(id, true); + if (hpvCapabilities == null) { + final InvalidParameterValueException ex = new InvalidParameterValueException("unable to find the hypervisor capabilities for specified id"); + ex.addProxyObject(id.toString(), "Id"); + throw ex; + } + return hpvCapabilities; + } + if (StringUtils.isAnyBlank(hypervisorStr, hypervisorVersion)) { + throw new InvalidParameterValueException("Hypervisor and hypervisor version must be specified together"); + } + HypervisorType hypervisorType = HypervisorType.getType(hypervisorStr); + if (hypervisorType == HypervisorType.None) { + throw new InvalidParameterValueException("Invalid hypervisor specified"); + } + HypervisorCapabilitiesVO hpvCapabilities = _hypervisorCapabilitiesDao.findByHypervisorTypeAndVersion(hypervisorType, hypervisorVersion); + if (hpvCapabilities == null) { + final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find the hypervisor capabilities for specified hypervisor and hypervisor version"); + ex.addProxyObject(hypervisorStr, "hypervisor"); + ex.addProxyObject(hypervisorVersion, "hypervisorVersion"); + throw ex; + } + return hpvCapabilities; + } + @Override public HypervisorCapabilities updateHypervisorCapabilities(UpdateHypervisorCapabilitiesCmd cmd) { - final Long id = cmd.getId(); + Long id = cmd.getId(); + final String hypervisorStr = cmd.getHypervisor(); + final String hypervisorVersion = cmd.getHypervisorVersion(); final Boolean securityGroupEnabled = cmd.getSecurityGroupEnabled(); final Long maxGuestsLimit = cmd.getMaxGuestsLimit(); final Integer maxDataVolumesLimit = cmd.getMaxDataVolumesLimit(); final Boolean storageMotionSupported = cmd.getStorageMotionSupported(); final Integer maxHostsPerClusterLimit = cmd.getMaxHostsPerClusterLimit(); final Boolean vmSnapshotEnabled = cmd.getVmSnapshotEnabled(); - HypervisorCapabilitiesVO hpvCapabilities = _hypervisorCapabilitiesDao.findById(id, true); - - if (hpvCapabilities == null) { - final InvalidParameterValueException ex = new InvalidParameterValueException("unable to find the hypervisor capabilities for specified id"); - ex.addProxyObject(id.toString(), "Id"); - throw ex; - } + HypervisorCapabilitiesVO hpvCapabilities = getHypervisorCapabilitiesForUpdate(id, hypervisorStr, hypervisorVersion); final boolean updateNeeded = securityGroupEnabled != null || maxGuestsLimit != null || maxDataVolumesLimit != null || storageMotionSupported != null || maxHostsPerClusterLimit != null || @@ -5088,7 +5117,14 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe if (!updateNeeded) { return hpvCapabilities; } + if (StringUtils.isNotBlank(hypervisorVersion) && !hpvCapabilities.getHypervisorVersion().equals(hypervisorVersion)) { + s_logger.debug(String.format("Hypervisor capabilities for hypervisor: %s and version: %s does not exist, creating a copy from the parent version: %s for update.", hypervisorStr, hypervisorVersion, hpvCapabilities.getHypervisorVersion())); + HypervisorCapabilitiesVO copy = new HypervisorCapabilitiesVO(hpvCapabilities); + copy.setHypervisorVersion(hypervisorVersion); + hpvCapabilities = _hypervisorCapabilitiesDao.persist(copy); + } + id = hpvCapabilities.getId(); hpvCapabilities = _hypervisorCapabilitiesDao.createForUpdate(id); if (securityGroupEnabled != null) {