Skip to content

Commit 074d466

Browse files
committed
update snapshot policy and backup schedule ownership when VM is moved
1 parent 3882d33 commit 074d466

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

engine/schema/src/main/java/org/apache/cloudstack/backup/BackupScheduleVO.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,12 @@ public long getDomainId() {
189189
public long getAccountId() {
190190
return accountId;
191191
}
192+
193+
public void setAccountId(Long accountId) {
194+
this.accountId = accountId;
195+
}
196+
197+
public void setDomainId(Long domainId) {
198+
this.domainId = domainId;
199+
}
192200
}

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
import javax.xml.parsers.DocumentBuilder;
6161
import javax.xml.parsers.ParserConfigurationException;
6262

63+
import com.cloud.storage.SnapshotPolicyVO;
64+
import com.cloud.storage.dao.SnapshotPolicyDao;
6365
import org.apache.cloudstack.acl.ControlledEntity;
6466
import org.apache.cloudstack.acl.ControlledEntity.ACLType;
6567
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
@@ -103,8 +105,10 @@
103105
import org.apache.cloudstack.api.command.user.volume.ChangeOfferingForVolumeCmd;
104106
import org.apache.cloudstack.api.command.user.volume.ResizeVolumeCmd;
105107
import org.apache.cloudstack.backup.BackupManager;
108+
import org.apache.cloudstack.backup.BackupScheduleVO;
106109
import org.apache.cloudstack.backup.BackupVO;
107110
import org.apache.cloudstack.backup.dao.BackupDao;
111+
import org.apache.cloudstack.backup.dao.BackupScheduleDao;
108112
import org.apache.cloudstack.context.CallContext;
109113
import org.apache.cloudstack.engine.cloud.entity.api.VirtualMachineEntity;
110114
import org.apache.cloudstack.engine.cloud.entity.api.db.dao.VMNetworkMapDao;
@@ -607,6 +611,10 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
607611
ReservationDao reservationDao;
608612
@Inject
609613
ResourceLimitService resourceLimitService;
614+
@Inject
615+
SnapshotPolicyDao snapshotPolicyDao;
616+
@Inject
617+
BackupScheduleDao backupScheduleDao;
610618

611619
@Inject
612620
private StatsCollector statsCollector;
@@ -8043,6 +8051,9 @@ protected void executeStepsToChangeOwnershipOfVm(AssignVMCmd cmd, Account caller
80438051

80448052
updateVolumesOwner(volumes, oldAccount, newAccount, newAccountId);
80458053

8054+
updateSnapshotPolicyOwnership(volumes, newAccount);
8055+
updateBackupScheduleOwnership(vm, newAccount);
8056+
80468057
try {
80478058
updateVmNetwork(cmd, caller, vm, newAccount, template);
80488059
} catch (InsufficientCapacityException | ResourceAllocationException e) {
@@ -8517,6 +8528,36 @@ protected void addAdditionalNetworksToVm(UserVmVO vm, Account newAccount, List<L
85178528
}
85188529
}
85198530

8531+
protected void updateSnapshotPolicyOwnership(List<VolumeVO> volumes, Account newAccount) {
8532+
logger.debug("Updating snapshot policy ownership for volumes of VM being assigned to account [{}]", newAccount);
8533+
8534+
for (VolumeVO volume : volumes) {
8535+
List<SnapshotPolicyVO> snapshotPolicies = snapshotPolicyDao.listByVolumeId(volume.getId());
8536+
for (SnapshotPolicyVO policy : snapshotPolicies) {
8537+
logger.trace("Updating snapshot policy [{}] ownership from account [{}] to account [{}]",
8538+
policy.getId(), policy.getAccountId(), newAccount.getAccountId());
8539+
8540+
policy.setAccountId(newAccount.getAccountId());
8541+
policy.setDomainId(newAccount.getDomainId());
8542+
snapshotPolicyDao.update(policy.getId(), policy);
8543+
}
8544+
}
8545+
}
8546+
8547+
protected void updateBackupScheduleOwnership(UserVmVO vm, Account newAccount) {
8548+
logger.debug("Updating backup schedule ownership for VM [{}] being assigned to account [{}]", vm, newAccount);
8549+
8550+
List<BackupScheduleVO> backupSchedules = backupScheduleDao.listByVM(vm.getId());
8551+
for (BackupScheduleVO schedule : backupSchedules) {
8552+
logger.trace("Updating backup schedule [{}] ownership from account [{}] to account [{}]",
8553+
schedule.getId(), schedule.getAccountId(), newAccount.getAccountId());
8554+
8555+
schedule.setAccountId(newAccount.getAccountId());
8556+
schedule.setDomainId(newAccount.getDomainId());
8557+
backupScheduleDao.update(schedule.getId(), schedule);
8558+
}
8559+
}
8560+
85208561
/**
85218562
* Attempts to create a network suitable for the creation of a VM ({@link NetworkOrchestrationService#createGuestNetwork}).
85228563
* If no physical network is found, throws a {@link InvalidParameterValueException}.

0 commit comments

Comments
 (0)