Skip to content

Commit

Permalink
core: Check if VM exists with ID of the imported template
Browse files Browse the repository at this point in the history
When importing a template, both templates and VMs should be checked to
ensure that the same ID does not exist in the system. To achieve this,
vm_static table is used.

Change-Id: Ia416f058995ce580708eddcc3fea2040c0b170ea
Bug-Url: https://bugzilla.redhat.com/1853924
Signed-off-by: Shmuel Melamud <smelamud@redhat.com>
  • Loading branch information
smelamud committed Jul 20, 2022
1 parent 39c83f2 commit 9279af9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.ovirt.engine.core.common.businessentities.ArchitectureType;
import org.ovirt.engine.core.common.businessentities.StorageDomain;
import org.ovirt.engine.core.common.businessentities.StorageDomainStatic;
import org.ovirt.engine.core.common.businessentities.VmBase;
import org.ovirt.engine.core.common.businessentities.VmDevice;
import org.ovirt.engine.core.common.businessentities.VmTemplate;
import org.ovirt.engine.core.common.businessentities.VmTemplateStatus;
Expand All @@ -45,6 +46,7 @@
import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.compat.Version;
import org.ovirt.engine.core.dao.ImageDao;
import org.ovirt.engine.core.dao.VmStaticDao;
import org.ovirt.engine.core.dao.network.VmNetworkStatisticsDao;
import org.ovirt.engine.core.utils.transaction.TransactionSupport;
import org.ovirt.engine.core.vdsbroker.vdsbroker.CloudInitHandler;
Expand All @@ -63,6 +65,8 @@ public abstract class ImportVmTemplateCommandBase<T extends ImportVmTemplatePara
@Inject
protected ImageDao imageDao;
@Inject
private VmStaticDao vmStaticDao;
@Inject
private ImportUtils importUtils;
@Inject
private CloudInitHandler cloudInitHandler;
Expand Down Expand Up @@ -168,11 +172,14 @@ protected boolean validate() {
initImportClonedTemplate();
}

VmTemplate duplicateTemplate = vmTemplateDao.get(getParameters().getVmTemplate().getId());
VmBase duplicateTemplateOrVm = vmStaticDao.get(getParameters().getVmTemplate().getId());
if (duplicateTemplateOrVm == null) {
duplicateTemplateOrVm = vmTemplateDao.get(getParameters().getVmTemplate().getId());
}
// check that the template does not exists in the target domain
if (duplicateTemplate != null) {
if (duplicateTemplateOrVm != null) {
return failValidation(EngineMessage.VMT_CANNOT_IMPORT_TEMPLATE_EXISTS,
String.format("$TemplateName %1$s", duplicateTemplate.getName()));
String.format("$TemplateOrVmName %1$s", duplicateTemplateOrVm.getName()));
}
if (getVmTemplate().isBaseTemplate() && isVmTemplateWithSameNameExist()) {
return failValidation(EngineMessage.VM_CANNOT_IMPORT_TEMPLATE_NAME_EXISTS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.ovirt.engine.core.dao.StorageDomainDao;
import org.ovirt.engine.core.dao.StorageDomainStaticDao;
import org.ovirt.engine.core.dao.StoragePoolDao;
import org.ovirt.engine.core.dao.VmStaticDao;
import org.ovirt.engine.core.dao.VmTemplateDao;
import org.ovirt.engine.core.vdsbroker.vdsbroker.CloudInitHandler;

Expand All @@ -80,6 +81,9 @@ public class ImportVmTemplateCommandTest extends BaseCommandTest {
@Mock
private VmTemplateDao vmTemplateDao;

@Mock
private VmStaticDao vmStaticDao;

@Mock
private VmHandler vmHandler;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,7 @@ VMPAYLOAD_CDROM_WITH_CLOUD_INIT=Payload cdrom deivce cannot be used with Cloud-I
VMPAYLOAD_FLOPPY_WITH_SYSPREP=Payload floppy deivce cannot be used with Sysprep via floppy device.
VMT_CANNOT_CREATE_TEMPLATE_FROM_DOWN_VM=Cannot ${action} ${type}. VM is locked or still running, try again once VM is in the Down state.
VMT_CANNOT_EXPORT_BLANK_TEMPLATE=Cannot export Blank Template.
VMT_CANNOT_IMPORT_TEMPLATE_EXISTS=Import Template failed - Template Id already exists in the system. Please remove the Template (${TemplateName}) from the system first
VMT_CANNOT_IMPORT_TEMPLATE_EXISTS=Cannot ${action} ${type}. Template or VM with the same ID already exists in the system. Please remove the Template or VM (${TemplateOrVmName}) from the system first
VMT_CANNOT_IMPORT_TEMPLATE_VERSION_MISSING_BASE=Cannot import Template Version, Base Template for this Version is missing, please first Import Base Template, or Import Version as Base Template using Clone.
VMT_CANNOT_REMOVE_BASE_WITH_VERSIONS=Cannot delete Base Template that has Template Versions, please first remove all Template Versions for this Template\: ${versionsList}.
VMT_CANNOT_REMOVE_BLANK_TEMPLATE=Cannot ${action} ${type}. Removing Blank Template is not allowed.
Expand Down

0 comments on commit 9279af9

Please sign in to comment.