Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,6 @@ public interface TemplateManager {
static Boolean getValidateUrlIsResolvableBeforeRegisteringTemplateValue() {
return ValidateUrlIsResolvableBeforeRegisteringTemplate.value();
}

void unlinkAllAndCleanupUserDataFromAccount(long accountId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,6 @@ public interface VMTemplateDao extends GenericDao<VMTemplateVO, Long>, StateDao<
List<Long> listIdsByTemplateTag(String tag);

List<Long> listIdsByExtensionId(long extensionId);

void unlinkUserdataFromTemplate(List<Long> userdataIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -917,4 +917,20 @@ public boolean updateState(
}
return rows > 0;
}

@Override
public void unlinkUserdataFromTemplate(List<Long> userdataIds) {
if (CollectionUtils.isEmpty(userdataIds)) {
return;
}
SearchBuilder<VMTemplateVO> sb = createSearchBuilder();
sb.and("userDataId", sb.entity().getUserDataId(), SearchCriteria.Op.IN);
sb.done();
SearchCriteria<VMTemplateVO> sc = sb.create();
sc.setParameters("userDataId", userdataIds.toArray());
VMTemplateVO vo = createForUpdate();
vo.setUserDataId(null);
vo.setUserDataLinkPolicy(null);
update(vo, sc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
// under the License.
package com.cloud.user.dao;

import java.util.List;

import com.cloud.user.UserDataVO;
import com.cloud.utils.db.GenericDao;

Expand All @@ -25,6 +27,8 @@ public interface UserDataDao extends GenericDao<UserDataVO, Long> {

public UserDataVO findByName(long accountId, long domainId, String name);

int removeByAccountId(long accountId);
List<Long> listIdsByAccountId(long accountId);

int removeAllIds(List<Long> ids);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
// under the License.
package com.cloud.user.dao;

import java.util.List;

import com.cloud.user.UserDataVO;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.GenericSearchBuilder;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

@Component
Expand Down Expand Up @@ -65,9 +70,26 @@ public UserDataVO findByName(long accountId, long domainId, String name) {
}

@Override
public int removeByAccountId(long accountId) {
SearchCriteria<UserDataVO> sc = userdataSearch.create();
public List<Long> listIdsByAccountId(long accountId) {
GenericSearchBuilder<UserDataVO, Long> sb = createSearchBuilder(Long.class);
sb.selectFields(sb.entity().getId());
sb.and("accountId", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
sb.done();
SearchCriteria<Long> sc = sb.create();
sc.setParameters("accountId", accountId);
return customSearch(sc, null);
}

@Override
public int removeAllIds(List<Long> ids) {
if (CollectionUtils.isEmpty(ids)) {
return 0;
}
SearchBuilder<UserDataVO> sb = createSearchBuilder();
sb.and("idIn", sb.entity().getId(), SearchCriteria.Op.IN);
sb.done();
SearchCriteria<UserDataVO> sc = sb.create();
sc.setParameters("idIn", ids.toArray());
return remove(sc);
}
}
13 changes: 13 additions & 0 deletions server/src/main/java/com/cloud/template/TemplateManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
import com.cloud.user.User;
import com.cloud.user.UserData;
import com.cloud.user.dao.AccountDao;
import com.cloud.user.dao.UserDataDao;
import com.cloud.uservm.UserVm;
import com.cloud.utils.DateUtil;
import com.cloud.utils.EncryptionUtil;
Expand Down Expand Up @@ -301,6 +302,8 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
private VMTemplateDetailsDao _tmpltDetailsDao;
@Inject
private HypervisorGuruManager _hvGuruMgr;
@Inject
UserDataDao userDataDao;

private List<TemplateAdapter> _adapters;

Expand Down Expand Up @@ -2495,4 +2498,14 @@ public VirtualMachineTemplate linkUserDataToTemplate(LinkUserDataToTemplateCmd c

return _tmpltDao.findById(template.getId());
}

@Override
public void unlinkAllAndCleanupUserDataFromAccount(long accountId) {
List<Long> userDataIds = userDataDao.listIdsByAccountId(accountId);
if (CollectionUtils.isEmpty(userDataIds)) {
return;
}
_tmpltDao.unlinkUserdataFromTemplate(userDataIds);
userDataDao.removeAllIds(userDataIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ public int compare(NetworkVO network1, NetworkVO network2) {
}

// Delete registered UserData
userDataDao.removeByAccountId(accountId);
_tmpltMgr.unlinkAllAndCleanupUserDataFromAccount(accountId);

// Delete Webhooks
deleteWebhooksForAccount(accountId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import com.cloud.user.UserData;
import com.cloud.user.UserVO;
import com.cloud.user.dao.AccountDao;
import com.cloud.user.dao.UserDataDao;
import com.cloud.utils.component.ComponentContext;
import com.cloud.utils.concurrency.NamedThreadFactory;
import com.cloud.utils.exception.CloudRuntimeException;
Expand Down Expand Up @@ -203,14 +204,19 @@ public class TemplateManagerImplTest {

@Inject
AccountManager _accountMgr;

@Inject
VnfTemplateManager vnfTemplateManager;

@Inject
SnapshotJoinDao snapshotJoinDao;

@Inject
HeuristicRuleHelper heuristicRuleHelperMock;

@Inject
UserDataDao userDataDao;

public class CustomThreadPoolExecutor extends ThreadPoolExecutor {
AtomicInteger ai = new AtomicInteger(0);
public CustomThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
Expand Down Expand Up @@ -978,11 +984,17 @@ public SecondaryStorageHeuristicDao secondaryStorageHeuristicDao() {
public HeuristicRuleHelper heuristicRuleHelper() {
return Mockito.mock(HeuristicRuleHelper.class);
}

@Bean
public SnapshotJoinDao snapshotJoinDao() {
return Mockito.mock(SnapshotJoinDao.class);
}

@Bean
public UserDataDao userDataDao() {
return Mockito.mock(UserDataDao.class);
}


public static class Library implements TypeFilter {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ public void deleteUserAccount() {
sshkeyList.add(sshkey);
Mockito.when(_sshKeyPairDao.listKeyPairs(Mockito.anyLong(), Mockito.anyLong())).thenReturn(sshkeyList);
Mockito.when(_sshKeyPairDao.remove(Mockito.anyLong())).thenReturn(true);
Mockito.when(userDataDao.removeByAccountId(Mockito.anyLong())).thenReturn(222);
Mockito.doNothing().when(accountManagerImpl).deleteWebhooksForAccount(Mockito.anyLong());
Mockito.doNothing().when(accountManagerImpl).verifyCallerPrivilegeForUserOrAccountOperations((Account) any());

Expand Down
Loading