Skip to content

Commit a6ef24d

Browse files
authored
server: consistent domainpath in api responses (#11589)
* server: consistent domainpath in api responses Currently, some APIs return domainpath as 'ROOT/domain1/domain2' while other return it as '/domain1/domain2'. This PR makes the response consistent like "ROOT/domain1/domain2" Signed-off-by: Abhishek Kumar <[email protected]> * more changes Signed-off-by: Abhishek Kumar <[email protected]> --------- Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 6089c16 commit a6ef24d

File tree

14 files changed

+48
-147
lines changed

14 files changed

+48
-147
lines changed

api/src/main/java/org/apache/cloudstack/api/command/user/vpn/AddVpnUserCmd.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.apache.cloudstack.api.response.VpnUsersResponse;
2929
import org.apache.cloudstack.context.CallContext;
3030

31-
import com.cloud.domain.Domain;
3231
import com.cloud.event.EventTypes;
3332
import com.cloud.network.VpnUser;
3433
import com.cloud.user.Account;
@@ -110,32 +109,17 @@ public String getEventType() {
110109
@Override
111110
public void execute() {
112111
VpnUser vpnUser = _entityMgr.findById(VpnUser.class, getEntityId());
113-
Account account = _entityMgr.findById(Account.class, vpnUser.getAccountId());
114112
try {
115113
if (!_ravService.applyVpnUsers(vpnUser.getAccountId(), userName)) {
116114
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to add vpn user");
117115
}
118116
} catch (Exception ex) {
119117
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, ex.getMessage());
120118
}
121-
122-
VpnUsersResponse vpnResponse = new VpnUsersResponse();
123-
vpnResponse.setId(vpnUser.getUuid());
124-
vpnResponse.setUserName(vpnUser.getUsername());
125-
vpnResponse.setAccountName(account.getAccountName());
126119
// re-retrieve the vpnuser, as the call to `applyVpnUsers` might have changed the state
127120
vpnUser = _entityMgr.findById(VpnUser.class, getEntityId());
128-
vpnResponse.setState(vpnUser.getState().toString());
129-
130-
Domain domain = _entityMgr.findById(Domain.class, account.getDomainId());
131-
if (domain != null) {
132-
vpnResponse.setDomainId(domain.getUuid());
133-
vpnResponse.setDomainName(domain.getName());
134-
vpnResponse.setDomainPath(domain.getPath());
135-
}
136-
121+
VpnUsersResponse vpnResponse = _responseGenerator.createVpnUserResponse(vpnUser);
137122
vpnResponse.setResponseName(getCommandName());
138-
vpnResponse.setObjectName("vpnuser");
139123
setResponseObject(vpnResponse);
140124
}
141125

api/src/main/java/org/apache/cloudstack/api/response/UserVmResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
@SuppressWarnings("unused")
4343
@EntityReference(value = {VirtualMachine.class, UserVm.class, VirtualRouter.class})
44-
public class UserVmResponse extends BaseResponseWithTagInformation implements ControlledEntityResponse, SetResourceIconResponse {
44+
public class UserVmResponse extends BaseResponseWithTagInformation implements ControlledViewEntityResponse, SetResourceIconResponse {
4545
@SerializedName(ApiConstants.ID)
4646
@Param(description = "the ID of the virtual machine")
4747
private String id;

plugins/integrations/kubernetes-service/src/main/java/com/cloud/kubernetes/cluster/KubernetesClusterManagerImpl.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
import org.apache.logging.log4j.Level;
119119

120120
import com.cloud.api.ApiDBUtils;
121+
import com.cloud.api.ApiResponseHelper;
121122
import com.cloud.api.query.dao.NetworkOfferingJoinDao;
122123
import com.cloud.api.query.dao.TemplateJoinDao;
123124
import com.cloud.api.query.dao.UserVmJoinDao;
@@ -135,7 +136,6 @@
135136
import com.cloud.dc.dao.DataCenterDao;
136137
import com.cloud.dc.dao.DedicatedResourceDao;
137138
import com.cloud.deploy.DeployDestination;
138-
import com.cloud.domain.Domain;
139139
import com.cloud.event.ActionEvent;
140140
import com.cloud.exception.ConcurrentOperationException;
141141
import com.cloud.exception.InsufficientCapacityException;
@@ -814,18 +814,7 @@ public KubernetesClusterResponse createKubernetesClusterResponse(long kubernetes
814814
response.setKubernetesVersionId(version.getUuid());
815815
response.setKubernetesVersionName(version.getName());
816816
}
817-
Account account = ApiDBUtils.findAccountById(kubernetesCluster.getAccountId());
818-
if (account.getType() == Account.Type.PROJECT) {
819-
Project project = ApiDBUtils.findProjectByProjectAccountId(account.getId());
820-
response.setProjectId(project.getUuid());
821-
response.setProjectName(project.getName());
822-
} else {
823-
response.setAccountName(account.getAccountName());
824-
}
825-
Domain domain = ApiDBUtils.findDomainById(kubernetesCluster.getDomainId());
826-
response.setDomainId(domain.getUuid());
827-
response.setDomainName(domain.getName());
828-
response.setDomainPath(domain.getPath());
817+
ApiResponseHelper.populateOwner(response, kubernetesCluster);
829818
response.setKeypair(kubernetesCluster.getKeyPair());
830819
response.setState(kubernetesCluster.getState().toString());
831820
response.setCores(String.valueOf(kubernetesCluster.getCores()));

plugins/integrations/kubernetes-service/src/main/java/org/apache/cloudstack/api/response/KubernetesClusterResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
@SuppressWarnings("unused")
3232
@EntityReference(value = {KubernetesCluster.class})
33-
public class KubernetesClusterResponse extends BaseResponseWithAnnotations implements ControlledEntityResponse {
33+
public class KubernetesClusterResponse extends BaseResponseWithAnnotations implements ControlledViewEntityResponse {
3434
@SerializedName(ApiConstants.ID)
3535
@Param(description = "the id of the Kubernetes cluster")
3636
private String id;

server/src/main/java/com/cloud/api/ApiResponseHelper.java

Lines changed: 32 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,15 @@ public class ApiResponseHelper implements ResponseGenerator {
530530
@Inject
531531
ResourceIconManager resourceIconManager;
532532

533+
public static String getPrettyDomainPath(String path) {
534+
if (path == null) {
535+
return null;
536+
}
537+
StringBuilder domainPath = new StringBuilder("ROOT");
538+
(domainPath.append(path)).deleteCharAt(domainPath.length() - 1);
539+
return domainPath.toString();
540+
}
541+
533542
@Override
534543
public UserResponse createUserResponse(User user) {
535544
UserAccountJoinVO vUser = ApiDBUtils.newUserView(user);
@@ -567,9 +576,7 @@ public DomainResponse createDomainResponse(Domain domain) {
567576
if (parentDomain != null) {
568577
domainResponse.setParentDomainId(parentDomain.getUuid());
569578
}
570-
StringBuilder domainPath = new StringBuilder("ROOT");
571-
(domainPath.append(domain.getPath())).deleteCharAt(domainPath.length() - 1);
572-
domainResponse.setPath(domainPath.toString());
579+
domainResponse.setPath(getPrettyDomainPath(domain.getPath()));
573580
if (domain.getParent() != null) {
574581
domainResponse.setParentDomainName(ApiDBUtils.findDomainById(domain.getParent()).getName());
575582
}
@@ -822,21 +829,6 @@ public VMSnapshotResponse createVMSnapshotResponse(VMSnapshot vmSnapshot) {
822829
}
823830
}
824831
populateOwner(vmSnapshotResponse, vmSnapshot);
825-
Project project = ApiDBUtils.findProjectByProjectAccountId(vmSnapshot.getAccountId());
826-
if (project != null) {
827-
vmSnapshotResponse.setProjectId(project.getUuid());
828-
vmSnapshotResponse.setProjectName(project.getName());
829-
}
830-
Account account = ApiDBUtils.findAccountById(vmSnapshot.getAccountId());
831-
if (account != null) {
832-
vmSnapshotResponse.setAccountName(account.getAccountName());
833-
}
834-
DomainVO domain = ApiDBUtils.findDomainById(vmSnapshot.getDomainId());
835-
if (domain != null) {
836-
vmSnapshotResponse.setDomainId(domain.getUuid());
837-
vmSnapshotResponse.setDomainName(domain.getName());
838-
vmSnapshotResponse.setDomainPath(domain.getPath());
839-
}
840832

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

2353-
Account account = securiytGroupAccounts.get(securityGroup.getAccountId());
2354-
2355-
if (securityGroup.getAccountType() == Account.Type.PROJECT) {
2356-
response.setProjectId(securityGroup.getProjectUuid());
2357-
response.setProjectName(securityGroup.getProjectName());
2358-
} else {
2359-
response.setAccountName(securityGroup.getAccountName());
2360-
}
2361-
2362-
response.setDomainId(securityGroup.getDomainUuid());
2363-
response.setDomainName(securityGroup.getDomainName());
2364-
response.setDomainPath(securityGroup.getDomainPath());
2345+
populateOwner(response, securityGroup);
23652346

23662347
for (SecurityRule securityRule : securityRules) {
23672348
SecurityGroupRuleResponse securityGroupData = new SecurityGroupRuleResponse();
@@ -2758,32 +2739,18 @@ public NetworkResponse createNetworkResponse(ResponseView view, Network network)
27582739
// get domain from network_domain table
27592740
Pair<Long, Boolean> domainNetworkDetails = ApiDBUtils.getDomainNetworkDetails(network.getId());
27602741
if (domainNetworkDetails.first() != null) {
2761-
Domain domain = ApiDBUtils.findDomainById(domainNetworkDetails.first());
2762-
if (domain != null) {
2763-
response.setDomainId(domain.getUuid());
2764-
2765-
StringBuilder domainPath = new StringBuilder("ROOT");
2766-
(domainPath.append(domain.getPath())).deleteCharAt(domainPath.length() - 1);
2767-
response.setDomainPath(domainPath.toString());
2768-
}
2742+
populateDomain(response, domainNetworkDetails.first());
27692743
}
27702744
response.setSubdomainAccess(domainNetworkDetails.second());
27712745
}
27722746

27732747
Long dedicatedDomainId = ApiDBUtils.getDedicatedNetworkDomain(network.getId());
27742748
if (dedicatedDomainId != null) {
2775-
Domain domain = ApiDBUtils.findDomainById(dedicatedDomainId);
2776-
if (domain != null) {
2777-
response.setDomainId(domain.getUuid());
2778-
response.setDomainName(domain.getName());
2779-
response.setDomainPath(domain.getPath());
2780-
}
2781-
2749+
populateDomain(response, dedicatedDomainId);
27822750
}
27832751

27842752
response.setSpecifyIpRanges(network.getSpecifyIpRanges());
27852753

2786-
27872754
setVpcIdInResponse(network.getVpcId(), response::setVpcId, response::setVpcName);
27882755

27892756
setResponseAssociatedNetworkInformation(response, network.getId());
@@ -3045,14 +3012,10 @@ private void populateOwner(ControlledEntityResponse response, ControlledEntity o
30453012
} else {
30463013
response.setAccountName(account.getAccountName());
30473014
}
3048-
3049-
Domain domain = ApiDBUtils.findDomainById(object.getDomainId());
3050-
response.setDomainId(domain.getUuid());
3051-
response.setDomainName(domain.getName());
3052-
response.setDomainPath(domain.getPath());
3015+
populateDomain(response, object.getDomainId());
30533016
}
30543017

3055-
private void populateOwner(ControlledViewEntityResponse response, ControlledEntity object) {
3018+
public static void populateOwner(ControlledViewEntityResponse response, ControlledEntity object) {
30563019
Account account = ApiDBUtils.findAccountById(object.getAccountId());
30573020

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

3067-
Domain domain = ApiDBUtils.findDomainById(object.getDomainId());
3068-
response.setDomainId(domain.getUuid());
3069-
response.setDomainName(domain.getName());
3070-
response.setDomainPath(domain.getPath());
3030+
populateDomain(response, object.getDomainId());
30713031
}
30723032

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

30823042
response.setDomainId(object.getDomainUuid());
30833043
response.setDomainName(object.getDomainName());
3084-
response.setDomainPath(object.getDomainPath());
3044+
response.setDomainPath(getPrettyDomainPath(object.getDomainPath()));
30853045
}
30863046

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

31063066
private void populateDomain(ControlledEntityResponse response, long domainId) {
31073067
Domain domain = ApiDBUtils.findDomainById(domainId);
3068+
if (domain == null) {
3069+
return;
3070+
}
3071+
response.setDomainId(domain.getUuid());
3072+
response.setDomainName(domain.getName());
3073+
response.setDomainPath(getPrettyDomainPath(domain.getPath()));
3074+
}
31083075

3076+
private static void populateDomain(ControlledViewEntityResponse response, long domainId) {
3077+
Domain domain = ApiDBUtils.findDomainById(domainId);
3078+
if (domain == null) {
3079+
return;
3080+
}
31093081
response.setDomainId(domain.getUuid());
31103082
response.setDomainName(domain.getName());
3111-
response.setDomainPath(domain.getPath());
3083+
response.setDomainPath(getPrettyDomainPath(domain.getPath()));
31123084
}
31133085

31143086
@Override
@@ -4100,12 +4072,7 @@ public UsageRecordResponse createUsageResponse(Usage usageRecord, Map<String, Se
41004072
usageRecResponse.setAccountName(account.getAccountName());
41014073
}
41024074

4103-
Domain domain = ApiDBUtils.findDomainById(usageRecord.getDomainId());
4104-
if (domain != null) {
4105-
usageRecResponse.setDomainId(domain.getUuid());
4106-
usageRecResponse.setDomainName(domain.getName());
4107-
usageRecResponse.setDomainPath(domain.getPath());
4108-
}
4075+
populateDomain(usageRecResponse, account.getDomainId());
41094076

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

48934860
AffinityGroupResponse response = new AffinityGroupResponse();
48944861

4895-
Account account = ApiDBUtils.findAccountById(group.getAccountId());
48964862
response.setId(group.getUuid());
4897-
response.setAccountName(account.getAccountName());
48984863
response.setName(group.getName());
48994864
response.setType(group.getType());
49004865
response.setDescription(group.getDescription());
4901-
Domain domain = ApiDBUtils.findDomainById(account.getDomainId());
4902-
if (domain != null) {
4903-
response.setDomainId(domain.getUuid());
4904-
response.setDomainName(domain.getName());
4905-
response.setDomainPath(domain.getPath());
4906-
}
4866+
populateOwner(response, group);
49074867

49084868
response.setObjectName("affinitygroup");
49094869
return response;

server/src/main/java/com/cloud/api/query/dao/AccountJoinDaoImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.cloudstack.api.response.UserResponse;
3333

3434
import com.cloud.api.ApiDBUtils;
35+
import com.cloud.api.ApiResponseHelper;
3536
import com.cloud.api.query.ViewResponseHelper;
3637
import com.cloud.api.query.vo.AccountJoinVO;
3738
import com.cloud.api.query.vo.UserAccountJoinVO;
@@ -74,9 +75,7 @@ public AccountResponse newAccountResponse(ResponseView view, EnumSet<DomainDetai
7475
accountResponse.setAccountType(account.getType().ordinal());
7576
accountResponse.setDomainId(account.getDomainUuid());
7677
accountResponse.setDomainName(account.getDomainName());
77-
StringBuilder domainPath = new StringBuilder("ROOT");
78-
(domainPath.append(account.getDomainPath())).deleteCharAt(domainPath.length() - 1);
79-
accountResponse.setDomainPath(domainPath.toString());
78+
accountResponse.setDomainPath(ApiResponseHelper.getPrettyDomainPath(account.getDomainPath()));
8079
accountResponse.setState(account.getState().toString());
8180
accountResponse.setCreated(account.getCreated());
8281
accountResponse.setNetworkDomain(account.getNetworkDomain());

server/src/main/java/com/cloud/api/query/dao/AsyncJobJoinDaoImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.cloudstack.api.response.AsyncJobResponse;
2929
import org.apache.cloudstack.framework.jobs.AsyncJob;
3030

31+
import com.cloud.api.ApiResponseHelper;
3132
import com.cloud.api.ApiSerializerHelper;
3233
import com.cloud.api.SerializationContext;
3334
import com.cloud.api.query.vo.AsyncJobJoinVO;
@@ -60,9 +61,7 @@ public AsyncJobResponse newAsyncJobResponse(final AsyncJobJoinVO job) {
6061
jobResponse.setAccountId(job.getAccountUuid());
6162
jobResponse.setAccount(job.getAccountName());
6263
jobResponse.setDomainId(job.getDomainUuid());
63-
StringBuilder domainPath = new StringBuilder("ROOT");
64-
(domainPath.append(job.getDomainPath())).deleteCharAt(domainPath.length() - 1);
65-
jobResponse.setDomainPath(domainPath.toString());
64+
jobResponse.setDomainPath(ApiResponseHelper.getPrettyDomainPath(job.getDomainPath()));
6665
jobResponse.setUserId(job.getUserUuid());
6766
jobResponse.setCmd(job.getCmd());
6867
jobResponse.setCreated(job.getCreated());

server/src/main/java/com/cloud/api/query/dao/DomainJoinDaoImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222

2323

24+
import com.cloud.api.ApiResponseHelper;
2425
import com.cloud.configuration.Resource;
2526
import com.cloud.user.AccountManager;
2627
import org.apache.cloudstack.annotation.AnnotationService;
@@ -79,9 +80,7 @@ public DomainResponse newDomainResponse(ResponseView view, EnumSet<DomainDetails
7980
if (domain.getParentUuid() != null) {
8081
domainResponse.setParentDomainId(domain.getParentUuid());
8182
}
82-
StringBuilder domainPath = new StringBuilder("ROOT");
83-
(domainPath.append(domain.getPath())).deleteCharAt(domainPath.length() - 1);
84-
domainResponse.setPath(domainPath.toString());
83+
domainResponse.setPath(ApiResponseHelper.getPrettyDomainPath(domain.getPath()));
8584
if (domain.getParent() != null) {
8685
domainResponse.setParentDomainName(domain.getParentName());
8786
}

server/src/main/java/com/cloud/api/query/dao/DomainRouterJoinDaoImpl.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,6 @@ public DomainRouterResponse newDomainRouterResponse(DomainRouterJoinVO router, A
207207
// populate owner.
208208
ApiResponseHelper.populateOwner(routerResponse, router);
209209

210-
routerResponse.setDomainId(router.getDomainUuid());
211-
routerResponse.setDomainName(router.getDomainName());
212-
routerResponse.setDomainPath(router.getDomainPath());
213-
214210
routerResponse.setZoneName(router.getDataCenterName());
215211
routerResponse.setDns1(router.getDns1());
216212
routerResponse.setDns2(router.getDns2());

server/src/main/java/com/cloud/api/query/dao/ProjectAccountJoinDaoImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

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

26+
import com.cloud.api.ApiResponseHelper;
2627
import com.cloud.api.query.vo.ProjectAccountJoinVO;
2728
import com.cloud.projects.ProjectAccount;
2829
import com.cloud.utils.db.GenericDaoBase;
@@ -59,7 +60,7 @@ public ProjectAccountResponse newProjectAccountResponse(ProjectAccountJoinVO pro
5960
projectAccountResponse.setRole(proj.getAccountRole().toString());
6061
projectAccountResponse.setDomainId(proj.getDomainUuid());
6162
projectAccountResponse.setDomainName(proj.getDomainName());
62-
projectAccountResponse.setDomainPath(proj.getDomainPath());
63+
projectAccountResponse.setDomainPath(ApiResponseHelper.getPrettyDomainPath(proj.getDomainPath()));
6364

6465
projectAccountResponse.setObjectName("projectaccount");
6566

0 commit comments

Comments
 (0)