Skip to content

Commit 505faf7

Browse files
committed
Fix for Deleted userdata owned by a deleted account remains linked with the template
1 parent f1f779a commit 505faf7

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

engine/schema/src/main/java/com/cloud/user/dao/UserDataDao.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.cloud.user.UserDataVO;
2020
import com.cloud.utils.db.GenericDao;
2121

22+
import java.util.List;
23+
2224
public interface UserDataDao extends GenericDao<UserDataVO, Long> {
2325

2426
public UserDataVO findByUserData(long accountId, long domainId, String userData);
@@ -27,4 +29,6 @@ public interface UserDataDao extends GenericDao<UserDataVO, Long> {
2729

2830
int removeByAccountId(long accountId);
2931

32+
List<UserDataVO> listByAccountId(long accountId);
33+
3034
}

engine/schema/src/main/java/com/cloud/user/dao/UserDataDaoImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.cloud.utils.db.SearchCriteria;
2323
import org.springframework.stereotype.Component;
2424

25+
import java.util.List;
26+
2527
@Component
2628
public class UserDataDaoImpl extends GenericDaoBase<UserDataVO, Long> implements UserDataDao {
2729

@@ -70,4 +72,11 @@ public int removeByAccountId(long accountId) {
7072
sc.setParameters("accountId", accountId);
7173
return remove(sc);
7274
}
75+
76+
@Override
77+
public List<UserDataVO> listByAccountId(long accountId) {
78+
SearchCriteria<UserDataVO> sc = userdataSearch.create();
79+
sc.setParameters("accountId", accountId);
80+
return listBy(sc);
81+
}
7382
}

server/src/main/java/com/cloud/user/AccountManagerImpl.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ public int compare(NetworkVO network1, NetworkVO network2) {
12001200
}
12011201

12021202
// Delete registered UserData
1203-
userDataDao.removeByAccountId(accountId);
1203+
cleanupAccountUserData(account);
12041204

12051205
// Delete Webhooks
12061206
deleteWebhooksForAccount(accountId);
@@ -1221,6 +1221,30 @@ public int compare(NetworkVO network1, NetworkVO network2) {
12211221
}
12221222
}
12231223

1224+
private void cleanupAccountUserData(AccountVO account) {
1225+
long accountId = account.getId();
1226+
List<UserDataVO> userData = userDataDao.listByAccountId(accountId);
1227+
if (CollectionUtils.isEmpty(userData)) {
1228+
return;
1229+
}
1230+
logger.info("Deleting {} registered UserData for Account {}", userData.size(), account);
1231+
for (UserDataVO userdata : userData) {
1232+
List<VMTemplateVO> templatesLinkedToUserdata = _templateDao.findTemplatesLinkedToUserdata(userdata.getId());
1233+
if (CollectionUtils.isEmpty(templatesLinkedToUserdata)) {
1234+
continue;
1235+
}
1236+
for (VMTemplateVO template : templatesLinkedToUserdata) {
1237+
logger.debug("Unregistering UserData {} from Template {}", userdata, template);
1238+
template.setUserDataId(null);
1239+
template.setUserDataLinkPolicy(null);
1240+
_templateDao.update(template.getId(), template);
1241+
}
1242+
userDataDao.remove(userdata.getId());
1243+
}
1244+
int userDataRemoved = userDataDao.removeByAccountId(accountId);
1245+
logger.info("Deleted {} registered UserData for Account {}", userDataRemoved, account);
1246+
}
1247+
12241248
@Override
12251249
public boolean disableAccount(long accountId) throws ConcurrentOperationException, ResourceUnavailableException {
12261250
boolean success = false;

0 commit comments

Comments
 (0)