Skip to content

Commit b3e1027

Browse files
shwstpprdhslove
authored andcommitted
server: consistent domainpath in api responses (apache#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 8be5b61 commit b3e1027

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
@@ -531,6 +531,15 @@ public class ApiResponseHelper implements ResponseGenerator {
531531
@Inject
532532
ResourceIconManager resourceIconManager;
533533

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

842834
List<? extends ResourceTag> tags = _resourceTagDao.listBy(vmSnapshot.getId(), ResourceObjectType.VMSnapshot);
843835
List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
@@ -2359,18 +2351,7 @@ public SecurityGroupResponse createSecurityGroupResponseFromSecurityGroupRule(Li
23592351
response.setName(securityGroup.getName());
23602352
response.setDescription(securityGroup.getDescription());
23612353

2362-
Account account = securiytGroupAccounts.get(securityGroup.getAccountId());
2363-
2364-
if (securityGroup.getAccountType() == Account.Type.PROJECT) {
2365-
response.setProjectId(securityGroup.getProjectUuid());
2366-
response.setProjectName(securityGroup.getProjectName());
2367-
} else {
2368-
response.setAccountName(securityGroup.getAccountName());
2369-
}
2370-
2371-
response.setDomainId(securityGroup.getDomainUuid());
2372-
response.setDomainName(securityGroup.getDomainName());
2373-
response.setDomainPath(securityGroup.getDomainPath());
2354+
populateOwner(response, securityGroup);
23742355

23752356
for (SecurityRule securityRule : securityRules) {
23762357
SecurityGroupRuleResponse securityGroupData = new SecurityGroupRuleResponse();
@@ -2767,32 +2748,18 @@ public NetworkResponse createNetworkResponse(ResponseView view, Network network)
27672748
// get domain from network_domain table
27682749
Pair<Long, Boolean> domainNetworkDetails = ApiDBUtils.getDomainNetworkDetails(network.getId());
27692750
if (domainNetworkDetails.first() != null) {
2770-
Domain domain = ApiDBUtils.findDomainById(domainNetworkDetails.first());
2771-
if (domain != null) {
2772-
response.setDomainId(domain.getUuid());
2773-
2774-
StringBuilder domainPath = new StringBuilder("ROOT");
2775-
(domainPath.append(domain.getPath())).deleteCharAt(domainPath.length() - 1);
2776-
response.setDomainPath(domainPath.toString());
2777-
}
2751+
populateDomain(response, domainNetworkDetails.first());
27782752
}
27792753
response.setSubdomainAccess(domainNetworkDetails.second());
27802754
}
27812755

27822756
Long dedicatedDomainId = ApiDBUtils.getDedicatedNetworkDomain(network.getId());
27832757
if (dedicatedDomainId != null) {
2784-
Domain domain = ApiDBUtils.findDomainById(dedicatedDomainId);
2785-
if (domain != null) {
2786-
response.setDomainId(domain.getUuid());
2787-
response.setDomainName(domain.getName());
2788-
response.setDomainPath(domain.getPath());
2789-
}
2790-
2758+
populateDomain(response, dedicatedDomainId);
27912759
}
27922760

27932761
response.setSpecifyIpRanges(network.getSpecifyIpRanges());
27942762

2795-
27962763
setVpcIdInResponse(network.getVpcId(), response::setVpcId, response::setVpcName);
27972764

27982765
setResponseAssociatedNetworkInformation(response, network.getId());
@@ -3054,14 +3021,10 @@ private void populateOwner(ControlledEntityResponse response, ControlledEntity o
30543021
} else {
30553022
response.setAccountName(account.getAccountName());
30563023
}
3057-
3058-
Domain domain = ApiDBUtils.findDomainById(object.getDomainId());
3059-
response.setDomainId(domain.getUuid());
3060-
response.setDomainName(domain.getName());
3061-
response.setDomainPath(domain.getPath());
3024+
populateDomain(response, object.getDomainId());
30623025
}
30633026

3064-
private void populateOwner(ControlledViewEntityResponse response, ControlledEntity object) {
3027+
public static void populateOwner(ControlledViewEntityResponse response, ControlledEntity object) {
30653028
Account account = ApiDBUtils.findAccountById(object.getAccountId());
30663029

30673030
if (account.getType() == Account.Type.PROJECT) {
@@ -3073,10 +3036,7 @@ private void populateOwner(ControlledViewEntityResponse response, ControlledEnti
30733036
response.setAccountName(account.getAccountName());
30743037
}
30753038

3076-
Domain domain = ApiDBUtils.findDomainById(object.getDomainId());
3077-
response.setDomainId(domain.getUuid());
3078-
response.setDomainName(domain.getName());
3079-
response.setDomainPath(domain.getPath());
3039+
populateDomain(response, object.getDomainId());
30803040
}
30813041

30823042
public static void populateOwner(ControlledViewEntityResponse response, ControlledViewEntity object) {
@@ -3090,7 +3050,7 @@ public static void populateOwner(ControlledViewEntityResponse response, Controll
30903050

30913051
response.setDomainId(object.getDomainUuid());
30923052
response.setDomainName(object.getDomainName());
3093-
response.setDomainPath(object.getDomainPath());
3053+
response.setDomainPath(getPrettyDomainPath(object.getDomainPath()));
30943054
}
30953055

30963056
private void populateAccount(ControlledEntityResponse response, long accountId) {
@@ -3114,10 +3074,22 @@ private void populateAccount(ControlledEntityResponse response, long accountId)
31143074

31153075
private void populateDomain(ControlledEntityResponse response, long domainId) {
31163076
Domain domain = ApiDBUtils.findDomainById(domainId);
3077+
if (domain == null) {
3078+
return;
3079+
}
3080+
response.setDomainId(domain.getUuid());
3081+
response.setDomainName(domain.getName());
3082+
response.setDomainPath(getPrettyDomainPath(domain.getPath()));
3083+
}
31173084

3085+
private static void populateDomain(ControlledViewEntityResponse response, long domainId) {
3086+
Domain domain = ApiDBUtils.findDomainById(domainId);
3087+
if (domain == null) {
3088+
return;
3089+
}
31183090
response.setDomainId(domain.getUuid());
31193091
response.setDomainName(domain.getName());
3120-
response.setDomainPath(domain.getPath());
3092+
response.setDomainPath(getPrettyDomainPath(domain.getPath()));
31213093
}
31223094

31233095
@Override
@@ -4109,12 +4081,7 @@ public UsageRecordResponse createUsageResponse(Usage usageRecord, Map<String, Se
41094081
usageRecResponse.setAccountName(account.getAccountName());
41104082
}
41114083

4112-
Domain domain = ApiDBUtils.findDomainById(usageRecord.getDomainId());
4113-
if (domain != null) {
4114-
usageRecResponse.setDomainId(domain.getUuid());
4115-
usageRecResponse.setDomainName(domain.getName());
4116-
usageRecResponse.setDomainPath(domain.getPath());
4117-
}
4084+
populateDomain(usageRecResponse, account.getDomainId());
41184085

41194086
if (usageRecord.getZoneId() != null) {
41204087
DataCenter zone = ApiDBUtils.findZoneById(usageRecord.getZoneId());
@@ -4901,18 +4868,11 @@ public AffinityGroupResponse createAffinityGroupResponse(AffinityGroup group) {
49014868

49024869
AffinityGroupResponse response = new AffinityGroupResponse();
49034870

4904-
Account account = ApiDBUtils.findAccountById(group.getAccountId());
49054871
response.setId(group.getUuid());
4906-
response.setAccountName(account.getAccountName());
49074872
response.setName(group.getName());
49084873
response.setType(group.getType());
49094874
response.setDescription(group.getDescription());
4910-
Domain domain = ApiDBUtils.findDomainById(account.getDomainId());
4911-
if (domain != null) {
4912-
response.setDomainId(domain.getUuid());
4913-
response.setDomainName(domain.getName());
4914-
response.setDomainPath(domain.getPath());
4915-
}
4875+
populateOwner(response, group);
49164876

49174877
response.setObjectName("affinitygroup");
49184878
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
@@ -208,10 +208,6 @@ public DomainRouterResponse newDomainRouterResponse(DomainRouterJoinVO router, A
208208
// populate owner.
209209
ApiResponseHelper.populateOwner(routerResponse, router);
210210

211-
routerResponse.setDomainId(router.getDomainUuid());
212-
routerResponse.setDomainName(router.getDomainName());
213-
routerResponse.setDomainPath(router.getDomainPath());
214-
215211
routerResponse.setZoneName(router.getDataCenterName());
216212
routerResponse.setDns1(router.getDns1());
217213
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)