From 1a335e880a89458a55255a4dc214d7a00f8363d6 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Tue, 6 Apr 2021 12:48:56 +0530 Subject: [PATCH] server: filter null details during volume to template creation (#4794) Fixes #4628 mysql> describe user_vm_details; +---------+-----------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+-----------------+------+-----+---------+----------------+ | id | bigint unsigned | NO | PRI | NULL | auto_increment | | vm_id | bigint unsigned | NO | MUL | NULL | | | name | varchar(255) | NO | | NULL | | | value | varchar(5120) | YES | | NULL | | | display | tinyint(1) | NO | | 1 | | +---------+-----------------+------+-----+---------+----------------+ 5 rows in set (0.00 sec) mysql> describe vm_template_details; +-------------+-----------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+-----------------+------+-----+---------+----------------+ | id | bigint unsigned | NO | PRI | NULL | auto_increment | | template_id | bigint unsigned | NO | MUL | NULL | | | name | varchar(255) | NO | | NULL | | | value | varchar(1024) | NO | | NULL | | | display | tinyint(1) | NO | | 1 | | +-------------+-----------------+------+-----+---------+----------------+ 5 rows in set (0.00 sec) While cloud.user_vm_details allows null values to be added for a detail, cloud.vm_template_details doesn't allow null values. This change filters vm details with null values while creating template from a volume. Signed-off-by: Abhishek Kumar --- .../java/com/cloud/template/TemplateManagerImpl.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/com/cloud/template/TemplateManagerImpl.java b/server/src/main/java/com/cloud/template/TemplateManagerImpl.java index 3bdea7c89c1..fd98615a9e7 100755 --- a/server/src/main/java/com/cloud/template/TemplateManagerImpl.java +++ b/server/src/main/java/com/cloud/template/TemplateManagerImpl.java @@ -28,11 +28,11 @@ import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.stream.Collectors; import javax.inject.Inject; import javax.naming.ConfigurationException; -import com.cloud.agent.api.to.DatadiskTO; import org.apache.cloudstack.acl.SecurityChecker.AccessType; import org.apache.cloudstack.api.ApiConstants; import org.apache.cloudstack.api.BaseListTemplateOrIsoPermissionsCmd; @@ -109,6 +109,7 @@ import com.cloud.agent.api.Command; import com.cloud.agent.api.ComputeChecksumCommand; import com.cloud.agent.api.storage.DestroyCommand; import com.cloud.agent.api.to.DataTO; +import com.cloud.agent.api.to.DatadiskTO; import com.cloud.agent.api.to.DiskTO; import com.cloud.agent.api.to.NfsTO; import com.cloud.api.ApiDBUtils; @@ -1934,7 +1935,12 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager, UserVmVO userVm = _userVmDao.findById(vmId); if (userVm != null) { _userVmDao.loadDetails(userVm); - details.putAll(userVm.getDetails()); + Map vmDetails = userVm.getDetails(); + vmDetails = vmDetails.entrySet() + .stream() + .filter(map -> map.getValue() != null) + .collect(Collectors.toMap(map -> map.getKey(), map -> map.getValue())); + details.putAll(vmDetails); } } }