Skip to content
Merged
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 @@ -28,7 +28,6 @@
import org.apache.cloudstack.api.response.VpnUsersResponse;
import org.apache.cloudstack.context.CallContext;

import com.cloud.domain.Domain;
import com.cloud.event.EventTypes;
import com.cloud.network.VpnUser;
import com.cloud.user.Account;
Expand Down Expand Up @@ -110,32 +109,17 @@ public String getEventType() {
@Override
public void execute() {
VpnUser vpnUser = _entityMgr.findById(VpnUser.class, getEntityId());
Account account = _entityMgr.findById(Account.class, vpnUser.getAccountId());
try {
if (!_ravService.applyVpnUsers(vpnUser.getAccountId(), userName)) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add vpn user");
}
} catch (Exception ex) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
}

VpnUsersResponse vpnResponse = new VpnUsersResponse();
vpnResponse.setId(vpnUser.getUuid());
vpnResponse.setUserName(vpnUser.getUsername());
vpnResponse.setAccountName(account.getAccountName());
// re-retrieve the vpnuser, as the call to `applyVpnUsers` might have changed the state
vpnUser = _entityMgr.findById(VpnUser.class, getEntityId());
vpnResponse.setState(vpnUser.getState().toString());

Domain domain = _entityMgr.findById(Domain.class, account.getDomainId());
if (domain != null) {
vpnResponse.setDomainId(domain.getUuid());
vpnResponse.setDomainName(domain.getName());
vpnResponse.setDomainPath(domain.getPath());
}

VpnUsersResponse vpnResponse = _responseGenerator.createVpnUserResponse(vpnUser);
vpnResponse.setResponseName(getCommandName());
vpnResponse.setObjectName("vpnuser");
setResponseObject(vpnResponse);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

@SuppressWarnings("unused")
@EntityReference(value = {VirtualMachine.class, UserVm.class, VirtualRouter.class})
public class UserVmResponse extends BaseResponseWithTagInformation implements ControlledEntityResponse, SetResourceIconResponse {
public class UserVmResponse extends BaseResponseWithTagInformation implements ControlledViewEntityResponse, SetResourceIconResponse {
@SerializedName(ApiConstants.ID)
@Param(description = "the ID of the virtual machine")
private String id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
import org.apache.logging.log4j.Level;

import com.cloud.api.ApiDBUtils;
import com.cloud.api.ApiResponseHelper;
import com.cloud.api.query.dao.NetworkOfferingJoinDao;
import com.cloud.api.query.dao.TemplateJoinDao;
import com.cloud.api.query.dao.UserVmJoinDao;
Expand All @@ -135,7 +136,6 @@
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.dc.dao.DedicatedResourceDao;
import com.cloud.deploy.DeployDestination;
import com.cloud.domain.Domain;
import com.cloud.event.ActionEvent;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientCapacityException;
Expand Down Expand Up @@ -796,18 +796,7 @@ public KubernetesClusterResponse createKubernetesClusterResponse(long kubernetes
response.setKubernetesVersionId(version.getUuid());
response.setKubernetesVersionName(version.getName());
}
Account account = ApiDBUtils.findAccountById(kubernetesCluster.getAccountId());
if (account.getType() == Account.Type.PROJECT) {
Project project = ApiDBUtils.findProjectByProjectAccountId(account.getId());
response.setProjectId(project.getUuid());
response.setProjectName(project.getName());
} else {
response.setAccountName(account.getAccountName());
}
Domain domain = ApiDBUtils.findDomainById(kubernetesCluster.getDomainId());
response.setDomainId(domain.getUuid());
response.setDomainName(domain.getName());
response.setDomainPath(domain.getPath());
ApiResponseHelper.populateOwner(response, kubernetesCluster);
response.setKeypair(kubernetesCluster.getKeyPair());
response.setState(kubernetesCluster.getState().toString());
response.setCores(String.valueOf(kubernetesCluster.getCores()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

@SuppressWarnings("unused")
@EntityReference(value = {KubernetesCluster.class})
public class KubernetesClusterResponse extends BaseResponseWithAnnotations implements ControlledEntityResponse {
public class KubernetesClusterResponse extends BaseResponseWithAnnotations implements ControlledViewEntityResponse {
@SerializedName(ApiConstants.ID)
@Param(description = "the id of the Kubernetes cluster")
private String id;
Expand Down
104 changes: 32 additions & 72 deletions server/src/main/java/com/cloud/api/ApiResponseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,15 @@ public class ApiResponseHelper implements ResponseGenerator {
@Inject
ResourceIconManager resourceIconManager;

public static String getPrettyDomainPath(String path) {
if (path == null) {
return null;
}
StringBuilder domainPath = new StringBuilder("ROOT");
(domainPath.append(path)).deleteCharAt(domainPath.length() - 1);
Copy link

Copilot AI Oct 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method assumes path always ends with a character that can be deleted, but if path is empty or doesn't end with a trailing character, deleteCharAt(domainPath.length() - 1) will throw a StringIndexOutOfBoundsException.

Suggested change
(domainPath.append(path)).deleteCharAt(domainPath.length() - 1);
if (path != null && !path.isEmpty()) {
domainPath.append(path);
// Remove trailing '/' if present
if (domainPath.length() > 0 && domainPath.charAt(domainPath.length() - 1) == '/') {
domainPath.deleteCharAt(domainPath.length() - 1);
}
}

Copilot uses AI. Check for mistakes.
return domainPath.toString();
}

@Override
public UserResponse createUserResponse(User user) {
UserAccountJoinVO vUser = ApiDBUtils.newUserView(user);
Expand Down Expand Up @@ -567,9 +576,7 @@ public DomainResponse createDomainResponse(Domain domain) {
if (parentDomain != null) {
domainResponse.setParentDomainId(parentDomain.getUuid());
}
StringBuilder domainPath = new StringBuilder("ROOT");
(domainPath.append(domain.getPath())).deleteCharAt(domainPath.length() - 1);
domainResponse.setPath(domainPath.toString());
domainResponse.setPath(getPrettyDomainPath(domain.getPath()));
if (domain.getParent() != null) {
domainResponse.setParentDomainName(ApiDBUtils.findDomainById(domain.getParent()).getName());
}
Expand Down Expand Up @@ -822,21 +829,6 @@ public VMSnapshotResponse createVMSnapshotResponse(VMSnapshot vmSnapshot) {
}
}
populateOwner(vmSnapshotResponse, vmSnapshot);
Project project = ApiDBUtils.findProjectByProjectAccountId(vmSnapshot.getAccountId());
if (project != null) {
vmSnapshotResponse.setProjectId(project.getUuid());
vmSnapshotResponse.setProjectName(project.getName());
}
Account account = ApiDBUtils.findAccountById(vmSnapshot.getAccountId());
if (account != null) {
vmSnapshotResponse.setAccountName(account.getAccountName());
}
DomainVO domain = ApiDBUtils.findDomainById(vmSnapshot.getDomainId());
if (domain != null) {
vmSnapshotResponse.setDomainId(domain.getUuid());
vmSnapshotResponse.setDomainName(domain.getName());
vmSnapshotResponse.setDomainPath(domain.getPath());
}

List<? extends ResourceTag> tags = _resourceTagDao.listBy(vmSnapshot.getId(), ResourceObjectType.VMSnapshot);
List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
Expand Down Expand Up @@ -2350,18 +2342,7 @@ public SecurityGroupResponse createSecurityGroupResponseFromSecurityGroupRule(Li
response.setName(securityGroup.getName());
response.setDescription(securityGroup.getDescription());

Account account = securiytGroupAccounts.get(securityGroup.getAccountId());

if (securityGroup.getAccountType() == Account.Type.PROJECT) {
response.setProjectId(securityGroup.getProjectUuid());
response.setProjectName(securityGroup.getProjectName());
} else {
response.setAccountName(securityGroup.getAccountName());
}

response.setDomainId(securityGroup.getDomainUuid());
response.setDomainName(securityGroup.getDomainName());
response.setDomainPath(securityGroup.getDomainPath());
populateOwner(response, securityGroup);

for (SecurityRule securityRule : securityRules) {
SecurityGroupRuleResponse securityGroupData = new SecurityGroupRuleResponse();
Expand Down Expand Up @@ -2758,32 +2739,18 @@ public NetworkResponse createNetworkResponse(ResponseView view, Network network)
// get domain from network_domain table
Pair<Long, Boolean> domainNetworkDetails = ApiDBUtils.getDomainNetworkDetails(network.getId());
if (domainNetworkDetails.first() != null) {
Domain domain = ApiDBUtils.findDomainById(domainNetworkDetails.first());
if (domain != null) {
response.setDomainId(domain.getUuid());

StringBuilder domainPath = new StringBuilder("ROOT");
(domainPath.append(domain.getPath())).deleteCharAt(domainPath.length() - 1);
response.setDomainPath(domainPath.toString());
}
populateDomain(response, domainNetworkDetails.first());
}
response.setSubdomainAccess(domainNetworkDetails.second());
}

Long dedicatedDomainId = ApiDBUtils.getDedicatedNetworkDomain(network.getId());
if (dedicatedDomainId != null) {
Domain domain = ApiDBUtils.findDomainById(dedicatedDomainId);
if (domain != null) {
response.setDomainId(domain.getUuid());
response.setDomainName(domain.getName());
response.setDomainPath(domain.getPath());
}

populateDomain(response, dedicatedDomainId);
}

response.setSpecifyIpRanges(network.getSpecifyIpRanges());


setVpcIdInResponse(network.getVpcId(), response::setVpcId, response::setVpcName);

setResponseAssociatedNetworkInformation(response, network.getId());
Expand Down Expand Up @@ -3045,14 +3012,10 @@ private void populateOwner(ControlledEntityResponse response, ControlledEntity o
} else {
response.setAccountName(account.getAccountName());
}

Domain domain = ApiDBUtils.findDomainById(object.getDomainId());
response.setDomainId(domain.getUuid());
response.setDomainName(domain.getName());
response.setDomainPath(domain.getPath());
populateDomain(response, object.getDomainId());
}

private void populateOwner(ControlledViewEntityResponse response, ControlledEntity object) {
public static void populateOwner(ControlledViewEntityResponse response, ControlledEntity object) {
Account account = ApiDBUtils.findAccountById(object.getAccountId());

if (account.getType() == Account.Type.PROJECT) {
Expand All @@ -3064,10 +3027,7 @@ private void populateOwner(ControlledViewEntityResponse response, ControlledEnti
response.setAccountName(account.getAccountName());
}

Domain domain = ApiDBUtils.findDomainById(object.getDomainId());
response.setDomainId(domain.getUuid());
response.setDomainName(domain.getName());
response.setDomainPath(domain.getPath());
populateDomain(response, object.getDomainId());
}

public static void populateOwner(ControlledViewEntityResponse response, ControlledViewEntity object) {
Expand All @@ -3081,7 +3041,7 @@ public static void populateOwner(ControlledViewEntityResponse response, Controll

response.setDomainId(object.getDomainUuid());
response.setDomainName(object.getDomainName());
response.setDomainPath(object.getDomainPath());
response.setDomainPath(getPrettyDomainPath(object.getDomainPath()));
}

private void populateAccount(ControlledEntityResponse response, long accountId) {
Expand All @@ -3105,10 +3065,22 @@ private void populateAccount(ControlledEntityResponse response, long accountId)

private void populateDomain(ControlledEntityResponse response, long domainId) {
Domain domain = ApiDBUtils.findDomainById(domainId);
if (domain == null) {
return;
}
response.setDomainId(domain.getUuid());
response.setDomainName(domain.getName());
response.setDomainPath(getPrettyDomainPath(domain.getPath()));
}

private static void populateDomain(ControlledViewEntityResponse response, long domainId) {
Copy link

Copilot AI Oct 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is declared as private static but needs to be accessible from other classes. It should be public static to match the usage pattern of other helper methods in this class.

Suggested change
private static void populateDomain(ControlledViewEntityResponse response, long domainId) {
public static void populateDomain(ControlledViewEntityResponse response, long domainId) {

Copilot uses AI. Check for mistakes.
Domain domain = ApiDBUtils.findDomainById(domainId);
if (domain == null) {
return;
}
response.setDomainId(domain.getUuid());
response.setDomainName(domain.getName());
response.setDomainPath(domain.getPath());
response.setDomainPath(getPrettyDomainPath(domain.getPath()));
}

@Override
Expand Down Expand Up @@ -4100,12 +4072,7 @@ public UsageRecordResponse createUsageResponse(Usage usageRecord, Map<String, Se
usageRecResponse.setAccountName(account.getAccountName());
}

Domain domain = ApiDBUtils.findDomainById(usageRecord.getDomainId());
if (domain != null) {
usageRecResponse.setDomainId(domain.getUuid());
usageRecResponse.setDomainName(domain.getName());
usageRecResponse.setDomainPath(domain.getPath());
}
populateDomain(usageRecResponse, account.getDomainId());

if (usageRecord.getZoneId() != null) {
DataCenter zone = ApiDBUtils.findZoneById(usageRecord.getZoneId());
Expand Down Expand Up @@ -4892,18 +4859,11 @@ public AffinityGroupResponse createAffinityGroupResponse(AffinityGroup group) {

AffinityGroupResponse response = new AffinityGroupResponse();

Account account = ApiDBUtils.findAccountById(group.getAccountId());
response.setId(group.getUuid());
response.setAccountName(account.getAccountName());
response.setName(group.getName());
response.setType(group.getType());
response.setDescription(group.getDescription());
Domain domain = ApiDBUtils.findDomainById(account.getDomainId());
if (domain != null) {
response.setDomainId(domain.getUuid());
response.setDomainName(domain.getName());
response.setDomainPath(domain.getPath());
}
populateOwner(response, group);

response.setObjectName("affinitygroup");
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.cloudstack.api.response.UserResponse;

import com.cloud.api.ApiDBUtils;
import com.cloud.api.ApiResponseHelper;
import com.cloud.api.query.ViewResponseHelper;
import com.cloud.api.query.vo.AccountJoinVO;
import com.cloud.api.query.vo.UserAccountJoinVO;
Expand Down Expand Up @@ -74,9 +75,7 @@ public AccountResponse newAccountResponse(ResponseView view, EnumSet<DomainDetai
accountResponse.setAccountType(account.getType().ordinal());
accountResponse.setDomainId(account.getDomainUuid());
accountResponse.setDomainName(account.getDomainName());
StringBuilder domainPath = new StringBuilder("ROOT");
(domainPath.append(account.getDomainPath())).deleteCharAt(domainPath.length() - 1);
accountResponse.setDomainPath(domainPath.toString());
accountResponse.setDomainPath(ApiResponseHelper.getPrettyDomainPath(account.getDomainPath()));
accountResponse.setState(account.getState().toString());
accountResponse.setCreated(account.getCreated());
accountResponse.setNetworkDomain(account.getNetworkDomain());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.cloudstack.api.response.AsyncJobResponse;
import org.apache.cloudstack.framework.jobs.AsyncJob;

import com.cloud.api.ApiResponseHelper;
import com.cloud.api.ApiSerializerHelper;
import com.cloud.api.SerializationContext;
import com.cloud.api.query.vo.AsyncJobJoinVO;
Expand Down Expand Up @@ -60,9 +61,7 @@ public AsyncJobResponse newAsyncJobResponse(final AsyncJobJoinVO job) {
jobResponse.setAccountId(job.getAccountUuid());
jobResponse.setAccount(job.getAccountName());
jobResponse.setDomainId(job.getDomainUuid());
StringBuilder domainPath = new StringBuilder("ROOT");
(domainPath.append(job.getDomainPath())).deleteCharAt(domainPath.length() - 1);
jobResponse.setDomainPath(domainPath.toString());
jobResponse.setDomainPath(ApiResponseHelper.getPrettyDomainPath(job.getDomainPath()));
jobResponse.setUserId(job.getUserUuid());
jobResponse.setCmd(job.getCmd());
jobResponse.setCreated(job.getCreated());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.List;


import com.cloud.api.ApiResponseHelper;
import com.cloud.configuration.Resource;
import com.cloud.user.AccountManager;
import org.apache.cloudstack.annotation.AnnotationService;
Expand Down Expand Up @@ -79,9 +80,7 @@ public DomainResponse newDomainResponse(ResponseView view, EnumSet<DomainDetails
if (domain.getParentUuid() != null) {
domainResponse.setParentDomainId(domain.getParentUuid());
}
StringBuilder domainPath = new StringBuilder("ROOT");
(domainPath.append(domain.getPath())).deleteCharAt(domainPath.length() - 1);
domainResponse.setPath(domainPath.toString());
domainResponse.setPath(ApiResponseHelper.getPrettyDomainPath(domain.getPath()));
if (domain.getParent() != null) {
domainResponse.setParentDomainName(domain.getParentName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,6 @@ public DomainRouterResponse newDomainRouterResponse(DomainRouterJoinVO router, A
// populate owner.
ApiResponseHelper.populateOwner(routerResponse, router);

routerResponse.setDomainId(router.getDomainUuid());
routerResponse.setDomainName(router.getDomainName());
routerResponse.setDomainPath(router.getDomainPath());

routerResponse.setZoneName(router.getDataCenterName());
routerResponse.setDns1(router.getDns1());
routerResponse.setDns2(router.getDns2());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.apache.cloudstack.api.response.ProjectAccountResponse;

import com.cloud.api.ApiResponseHelper;
import com.cloud.api.query.vo.ProjectAccountJoinVO;
import com.cloud.projects.ProjectAccount;
import com.cloud.utils.db.GenericDaoBase;
Expand Down Expand Up @@ -59,7 +60,7 @@ public ProjectAccountResponse newProjectAccountResponse(ProjectAccountJoinVO pro
projectAccountResponse.setRole(proj.getAccountRole().toString());
projectAccountResponse.setDomainId(proj.getDomainUuid());
projectAccountResponse.setDomainName(proj.getDomainName());
projectAccountResponse.setDomainPath(proj.getDomainPath());
projectAccountResponse.setDomainPath(ApiResponseHelper.getPrettyDomainPath(proj.getDomainPath()));

projectAccountResponse.setObjectName("projectaccount");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.apache.cloudstack.api.response.ProjectInvitationResponse;

import com.cloud.api.ApiResponseHelper;
import com.cloud.api.query.vo.ProjectInvitationJoinVO;
import com.cloud.projects.ProjectInvitation;
import com.cloud.utils.db.GenericDaoBase;
Expand Down Expand Up @@ -65,7 +66,7 @@ public ProjectInvitationResponse newProjectInvitationResponse(ProjectInvitationJ

response.setDomainId(invite.getDomainUuid());
response.setDomainName(invite.getDomainName());
response.setDomainPath(invite.getDomainPath());
response.setDomainPath(ApiResponseHelper.getPrettyDomainPath(invite.getDomainPath()));

response.setObjectName("projectinvitation");
return response;
Expand Down
Loading
Loading