From bba96254f500f90c05c8059dfe7938fb1857fe7f Mon Sep 17 00:00:00 2001 From: Min Chen Date: Mon, 10 Dec 2012 13:52:33 -0800 Subject: [PATCH] Refactoring ListVMGroupsCmd and InstanceGroupResponse. --- .../com/cloud/server/ManagementService.java | 4 +- .../cloudstack/api/ResponseGenerator.java | 3 + .../command/user/vmgroup/ListVMGroupsCmd.java | 16 +- .../api/response/InstanceGroupResponse.java | 20 +- .../api/view/vo/InstanceGroupJoinVO.java | 215 ++++++++++++++++++ server/src/com/cloud/api/ApiDBUtils.java | 13 ++ .../src/com/cloud/api/ApiResponseHelper.java | 20 +- server/src/com/cloud/api/ApiServer.java | 4 +- .../com/cloud/event/dao/EventJoinDaoImpl.java | 1 - .../dao/SecurityGroupJoinDaoImpl.java | 10 +- .../cloud/server/ManagementServerImpl.java | 52 ++--- .../tags/dao/ResourceTagJoinDaoImpl.java | 1 - .../cloud/vm/dao/DomainRouterJoinDaoImpl.java | 10 +- .../cloud/vm/dao/InstanceGroupJoinDao.java | 35 +++ .../vm/dao/InstanceGroupJoinDaoImpl.java | 96 ++++++++ setup/db/create-schema.sql | 24 ++ 16 files changed, 440 insertions(+), 84 deletions(-) create mode 100644 api/src/org/apache/cloudstack/api/view/vo/InstanceGroupJoinVO.java create mode 100644 server/src/com/cloud/vm/dao/InstanceGroupJoinDao.java create mode 100644 server/src/com/cloud/vm/dao/InstanceGroupJoinDaoImpl.java diff --git a/api/src/com/cloud/server/ManagementService.java b/api/src/com/cloud/server/ManagementService.java index 75c042d0dd4..a79dd13469f 100755 --- a/api/src/com/cloud/server/ManagementService.java +++ b/api/src/com/cloud/server/ManagementService.java @@ -64,6 +64,8 @@ import org.apache.cloudstack.api.command.admin.systemvm.UpgradeSystemVMCmd; import org.apache.cloudstack.api.command.admin.resource.UploadCustomCertificateCmd; import org.apache.cloudstack.api.view.vo.DomainRouterJoinVO; import org.apache.cloudstack.api.view.vo.EventJoinVO; +import org.apache.cloudstack.api.view.vo.InstanceGroupJoinVO; + import com.cloud.async.AsyncJob; import com.cloud.capacity.Capacity; import com.cloud.configuration.Configuration; @@ -307,7 +309,7 @@ public interface ManagementService { InstanceGroup updateVmGroup(UpdateVMGroupCmd cmd); - Pair, Integer> searchForVmGroups(ListVMGroupsCmd cmd); + Pair, Integer> searchForVmGroups(ListVMGroupsCmd cmd); Map listCapabilities(ListCapabilitiesCmd cmd); diff --git a/api/src/org/apache/cloudstack/api/ResponseGenerator.java b/api/src/org/apache/cloudstack/api/ResponseGenerator.java index b0a2fafa33c..2ccbec54db6 100644 --- a/api/src/org/apache/cloudstack/api/ResponseGenerator.java +++ b/api/src/org/apache/cloudstack/api/ResponseGenerator.java @@ -152,6 +152,7 @@ import com.cloud.uservm.UserVm; import com.cloud.vm.InstanceGroup; import org.apache.cloudstack.api.view.vo.DomainRouterJoinVO; import org.apache.cloudstack.api.view.vo.EventJoinVO; +import org.apache.cloudstack.api.view.vo.InstanceGroupJoinVO; import org.apache.cloudstack.api.view.vo.ResourceTagJoinVO; import org.apache.cloudstack.api.view.vo.SecurityGroupJoinVO; import org.apache.cloudstack.api.view.vo.UserVmJoinVO; @@ -214,6 +215,8 @@ public interface ResponseGenerator { InstanceGroupResponse createInstanceGroupResponse(InstanceGroup group); + List createInstanceGroupResponse(InstanceGroupJoinVO... groups); + StoragePoolResponse createStoragePoolResponse(StoragePool pool); ClusterResponse createClusterResponse(Cluster cluster, Boolean showCapacities); diff --git a/api/src/org/apache/cloudstack/api/command/user/vmgroup/ListVMGroupsCmd.java b/api/src/org/apache/cloudstack/api/command/user/vmgroup/ListVMGroupsCmd.java index c94571b9cd8..139e0962c46 100644 --- a/api/src/org/apache/cloudstack/api/command/user/vmgroup/ListVMGroupsCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/vmgroup/ListVMGroupsCmd.java @@ -26,8 +26,12 @@ import org.apache.cloudstack.api.BaseListProjectAndAccountResourcesCmd; import org.apache.cloudstack.api.IdentityMapper; import org.apache.cloudstack.api.Implementation; import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.response.EventResponse; import org.apache.cloudstack.api.response.InstanceGroupResponse; import org.apache.cloudstack.api.response.ListResponse; +import org.apache.cloudstack.api.view.vo.EventJoinVO; +import org.apache.cloudstack.api.view.vo.InstanceGroupJoinVO; + import com.cloud.utils.Pair; import com.cloud.vm.InstanceGroup; @@ -71,16 +75,10 @@ public class ListVMGroupsCmd extends BaseListProjectAndAccountResourcesCmd { @Override public void execute(){ - Pair, Integer> groups = _mgr.searchForVmGroups(this); + Pair, Integer> groups = _mgr.searchForVmGroups(this); ListResponse response = new ListResponse(); - List responses = new ArrayList(); - for (InstanceGroup group : groups.first()) { - InstanceGroupResponse groupResponse = _responseGenerator.createInstanceGroupResponse(group); - groupResponse.setObjectName("instancegroup"); - responses.add(groupResponse); - } - - response.setResponses(responses, groups.second()); + List grpResponses = _responseGenerator.createInstanceGroupResponse(groups.first().toArray(new InstanceGroupJoinVO[groups.first().size()])); + response.setResponses(grpResponses, groups.second()); response.setResponseName(getCommandName()); this.setResponseObject(response); } diff --git a/api/src/org/apache/cloudstack/api/response/InstanceGroupResponse.java b/api/src/org/apache/cloudstack/api/response/InstanceGroupResponse.java index ec92397e31e..7231958ea62 100644 --- a/api/src/org/apache/cloudstack/api/response/InstanceGroupResponse.java +++ b/api/src/org/apache/cloudstack/api/response/InstanceGroupResponse.java @@ -28,9 +28,9 @@ import com.google.gson.annotations.SerializedName; @SuppressWarnings("unused") @Entity(value = InstanceGroup.class) -public class InstanceGroupResponse extends BaseResponse implements ControlledEntityResponse{ +public class InstanceGroupResponse extends BaseResponse implements ControlledViewEntityResponse{ @SerializedName(ApiConstants.ID) @Param(description="the id of the instance group") - private IdentityProxy id = new IdentityProxy("instance_group"); + private String id; @SerializedName(ApiConstants.NAME) @Param(description="the name of the instance group") private String name; @@ -42,19 +42,19 @@ public class InstanceGroupResponse extends BaseResponse implements ControlledEnt private String accountName; @SerializedName(ApiConstants.PROJECT_ID) @Param(description="the project id of the group") - private IdentityProxy projectId = new IdentityProxy("projects"); + private String projectId; @SerializedName(ApiConstants.PROJECT) @Param(description="the project name of the group") private String projectName; @SerializedName(ApiConstants.DOMAIN_ID) @Param(description="the domain ID of the instance group") - private IdentityProxy domainId = new IdentityProxy("domain"); + private String domainId; @SerializedName(ApiConstants.DOMAIN) @Param(description="the domain name of the instance group") private String domainName; - public void setId(Long id) { - this.id.setValue(id); + public void setId(String id) { + this.id = id; } public void setName(String name) { @@ -71,8 +71,8 @@ public class InstanceGroupResponse extends BaseResponse implements ControlledEnt } @Override - public void setDomainId(Long domainId) { - this.domainId.setValue(domainId); + public void setDomainId(String domainId) { + this.domainId = domainId; } @Override @@ -81,8 +81,8 @@ public class InstanceGroupResponse extends BaseResponse implements ControlledEnt } @Override - public void setProjectId(Long projectId) { - this.projectId.setValue(projectId); + public void setProjectId(String projectId) { + this.projectId = projectId; } @Override diff --git a/api/src/org/apache/cloudstack/api/view/vo/InstanceGroupJoinVO.java b/api/src/org/apache/cloudstack/api/view/vo/InstanceGroupJoinVO.java new file mode 100644 index 00000000000..09194876f1a --- /dev/null +++ b/api/src/org/apache/cloudstack/api/view/vo/InstanceGroupJoinVO.java @@ -0,0 +1,215 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.api.view.vo; + +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Table; + +import com.cloud.utils.db.GenericDao; + +@Entity +@Table(name="instance_group_view") +public class InstanceGroupJoinVO extends BaseViewVO implements ControlledViewEntity { + + @Column(name="uuid") + private String uuid; + + @Column(name="name") + String name; + + + @Column(name=GenericDao.REMOVED_COLUMN) + private Date removed; + + @Column(name=GenericDao.CREATED_COLUMN) + private Date created; + + + @Column(name="account_id") + private long accountId; + + @Column(name="account_uuid") + private String accountUuid; + + @Column(name="account_name") + private String accountName = null; + + @Column(name="account_type") + private short accountType; + + @Column(name="domain_id") + private long domainId; + + @Column(name="domain_uuid") + private String domainUuid; + + @Column(name="domain_name") + private String domainName = null; + + @Column(name="domain_path") + private String domainPath = null; + + @Column(name="project_id") + private long projectId; + + @Column(name="project_uuid") + private String projectUuid; + + @Column(name="project_name") + private String projectName; + + + + public InstanceGroupJoinVO() { + } + + public String getUuid() { + return uuid; + } + + public void setUuid(String uuid) { + this.uuid = uuid; + } + + + @Override + public long getAccountId() { + return accountId; + } + + public void setAccountId(long accountId) { + this.accountId = accountId; + } + + @Override + public String getAccountUuid() { + return accountUuid; + } + + public void setAccountUuid(String accountUuid) { + this.accountUuid = accountUuid; + } + + @Override + public String getAccountName() { + return accountName; + } + + public void setAccountName(String accountName) { + this.accountName = accountName; + } + + @Override + public short getAccountType() { + return accountType; + } + + public void setAccountType(short accountType) { + this.accountType = accountType; + } + + @Override + public long getDomainId() { + return domainId; + } + + public void setDomainId(long domainId) { + this.domainId = domainId; + } + + @Override + public String getDomainUuid() { + return domainUuid; + } + + public void setDomainUuid(String domainUuid) { + this.domainUuid = domainUuid; + } + + @Override + public String getDomainName() { + return domainName; + } + + public void setDomainName(String domainName) { + this.domainName = domainName; + } + + @Override + public String getDomainPath() { + return domainPath; + } + + public void setDomainPath(String domainPath) { + this.domainPath = domainPath; + } + + public long getProjectId() { + return projectId; + } + + public void setProjectId(long projectId) { + this.projectId = projectId; + } + + @Override + public String getProjectUuid() { + return projectUuid; + } + + public void setProjectUuid(String projectUuid) { + this.projectUuid = projectUuid; + } + + @Override + public String getProjectName() { + return projectName; + } + + public void setProjectName(String projectName) { + this.projectName = projectName; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Date getRemoved() { + return removed; + } + + public void setRemoved(Date removed) { + this.removed = removed; + } + + public Date getCreated() { + return created; + } + + public void setCreated(Date created) { + this.created = created; + } + + +} diff --git a/server/src/com/cloud/api/ApiDBUtils.java b/server/src/com/cloud/api/ApiDBUtils.java index a86f09bf7e6..f3ee0eda7fb 100755 --- a/server/src/com/cloud/api/ApiDBUtils.java +++ b/server/src/com/cloud/api/ApiDBUtils.java @@ -25,11 +25,13 @@ import java.util.Set; import org.apache.cloudstack.api.ApiConstants.VMDetails; import org.apache.cloudstack.api.response.DomainRouterResponse; import org.apache.cloudstack.api.response.EventResponse; +import org.apache.cloudstack.api.response.InstanceGroupResponse; import org.apache.cloudstack.api.response.ResourceTagResponse; import org.apache.cloudstack.api.response.SecurityGroupResponse; import org.apache.cloudstack.api.response.UserVmResponse; import org.apache.cloudstack.api.view.vo.DomainRouterJoinVO; import org.apache.cloudstack.api.view.vo.EventJoinVO; +import org.apache.cloudstack.api.view.vo.InstanceGroupJoinVO; import org.apache.cloudstack.api.view.vo.ResourceTagJoinVO; import org.apache.cloudstack.api.view.vo.SecurityGroupJoinVO; import org.apache.cloudstack.api.view.vo.UserVmJoinVO; @@ -166,6 +168,7 @@ import com.cloud.utils.Pair; import com.cloud.utils.component.ComponentLocator; import com.cloud.vm.ConsoleProxyVO; import com.cloud.vm.DomainRouterVO; +import com.cloud.vm.InstanceGroup; import com.cloud.vm.InstanceGroupVO; import com.cloud.vm.NicProfile; import com.cloud.vm.UserVmDetailVO; @@ -177,6 +180,7 @@ import com.cloud.vm.VmStats; import com.cloud.vm.dao.ConsoleProxyDao; import com.cloud.vm.dao.DomainRouterDao; import com.cloud.vm.dao.DomainRouterJoinDao; +import com.cloud.vm.dao.InstanceGroupJoinDao; import com.cloud.vm.dao.UserVmDao; import com.cloud.vm.dao.UserVmDetailsDao; import com.cloud.vm.dao.UserVmJoinDao; @@ -251,6 +255,7 @@ public class ApiDBUtils { private static CounterDao _counterDao; private static ResourceTagJoinDao _tagJoinDao; private static EventJoinDao _eventJoinDao; + private static InstanceGroupJoinDao _vmGroupJoinDao; static { _ms = (ManagementServer) ComponentLocator.getComponent(ManagementServer.Name); @@ -983,4 +988,12 @@ public class ApiDBUtils { public static EventJoinVO newEventView(Event e){ return _eventJoinDao.newEventView(e); } + + public static InstanceGroupResponse newInstanceGroupResponse(InstanceGroupJoinVO ve) { + return _vmGroupJoinDao.newInstanceGroupResponse(ve); + } + + public static InstanceGroupJoinVO newInstanceGroupView(InstanceGroup e){ + return _vmGroupJoinDao.newInstanceGroupView(e); + } } diff --git a/server/src/com/cloud/api/ApiResponseHelper.java b/server/src/com/cloud/api/ApiResponseHelper.java index 992c0f403a1..04a931447b3 100755 --- a/server/src/com/cloud/api/ApiResponseHelper.java +++ b/server/src/com/cloud/api/ApiResponseHelper.java @@ -115,6 +115,7 @@ import org.apache.cloudstack.api.response.ZoneResponse; import org.apache.cloudstack.api.view.vo.DomainRouterJoinVO; import org.apache.cloudstack.api.view.vo.ControlledViewEntity; import org.apache.cloudstack.api.view.vo.EventJoinVO; +import org.apache.cloudstack.api.view.vo.InstanceGroupJoinVO; import org.apache.cloudstack.api.view.vo.ResourceTagJoinVO; import org.apache.cloudstack.api.view.vo.SecurityGroupJoinVO; import org.apache.cloudstack.api.view.vo.UserVmJoinVO; @@ -1188,15 +1189,20 @@ public class ApiResponseHelper implements ResponseGenerator { @Override public InstanceGroupResponse createInstanceGroupResponse(InstanceGroup group) { - InstanceGroupResponse groupResponse = new InstanceGroupResponse(); - groupResponse.setId(group.getId()); - groupResponse.setName(group.getName()); - groupResponse.setCreated(group.getCreated()); + InstanceGroupJoinVO vgroup = ApiDBUtils.newInstanceGroupView(group); + return ApiDBUtils.newInstanceGroupResponse(vgroup); - populateOwner(groupResponse, group); + } - groupResponse.setObjectName("instancegroup"); - return groupResponse; + + + @Override + public List createInstanceGroupResponse(InstanceGroupJoinVO... groups) { + List respList = new ArrayList(); + for (InstanceGroupJoinVO vt : groups){ + respList.add(ApiDBUtils.newInstanceGroupResponse(vt)); + } + return respList; } @Override diff --git a/server/src/com/cloud/api/ApiServer.java b/server/src/com/cloud/api/ApiServer.java index 183249f5a6d..bb14d4c5dd0 100755 --- a/server/src/com/cloud/api/ApiServer.java +++ b/server/src/com/cloud/api/ApiServer.java @@ -52,6 +52,7 @@ import javax.servlet.http.HttpSession; import org.apache.cloudstack.api.*; import org.apache.cloudstack.api.command.user.event.ListEventsCmd; import org.apache.cloudstack.api.command.user.vm.ListVMsCmd; +import org.apache.cloudstack.api.command.user.vmgroup.ListVMGroupsCmd; import org.apache.commons.codec.binary.Base64; import org.apache.http.ConnectionClosedException; import org.apache.http.HttpException; @@ -443,7 +444,8 @@ public class ApiServer implements HttpRequestHandler { if (cmdObj instanceof BaseListCmd && !(cmdObj instanceof ListVMsCmd) && !(cmdObj instanceof ListRoutersCmd) && !(cmdObj instanceof ListSecurityGroupsCmd) && !(cmdObj instanceof ListTagsCmd) - && !(cmdObj instanceof ListEventsCmd)) { + && !(cmdObj instanceof ListEventsCmd) + && !(cmdObj instanceof ListVMGroupsCmd)) { buildAsyncListResponse((BaseListCmd) cmdObj, caller); } diff --git a/server/src/com/cloud/event/dao/EventJoinDaoImpl.java b/server/src/com/cloud/event/dao/EventJoinDaoImpl.java index 91e7f369cd5..be2a6dc917a 100644 --- a/server/src/com/cloud/event/dao/EventJoinDaoImpl.java +++ b/server/src/com/cloud/event/dao/EventJoinDaoImpl.java @@ -112,7 +112,6 @@ public class EventJoinDaoImpl extends GenericDaoBase implemen @Override public EventJoinVO newEventView(Event vr) { - List uvList = new ArrayList(); SearchCriteria sc = vrIdSearch.create(); sc.setParameters("id", vr.getId()); List vms = searchIncludingRemoved(sc, null, null, false); diff --git a/server/src/com/cloud/network/security/dao/SecurityGroupJoinDaoImpl.java b/server/src/com/cloud/network/security/dao/SecurityGroupJoinDaoImpl.java index 8afb0cd4883..6ecb258b39a 100644 --- a/server/src/com/cloud/network/security/dao/SecurityGroupJoinDaoImpl.java +++ b/server/src/com/cloud/network/security/dao/SecurityGroupJoinDaoImpl.java @@ -162,16 +162,10 @@ public class SecurityGroupJoinDaoImpl extends GenericDaoBase newSecurityGroupView(SecurityGroup sg) { - List uvList = new ArrayList(); + SearchCriteria sc = sgIdSearch.create(); sc.setParameters("id", sg.getId()); - List sgs = searchIncludingRemoved(sc, null, null, false); - if (sgs != null) { - for (SecurityGroupJoinVO uvm : sgs) { - uvList.add(uvm); - } - } - return uvList; + return searchIncludingRemoved(sc, null, null, false); } @Override diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index be6bbefc6c5..a196cd35769 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -100,6 +100,8 @@ import org.apache.cloudstack.api.command.admin.resource.UploadCustomCertificateC import org.apache.cloudstack.api.response.ExtractResponse; import org.apache.cloudstack.api.view.vo.DomainRouterJoinVO; import org.apache.cloudstack.api.view.vo.EventJoinVO; +import org.apache.cloudstack.api.view.vo.InstanceGroupJoinVO; + import com.cloud.async.AsyncJob; import com.cloud.async.AsyncJobExecutor; import com.cloud.async.AsyncJobManager; @@ -262,6 +264,7 @@ import com.cloud.vm.dao.ConsoleProxyDao; import com.cloud.vm.dao.DomainRouterDao; import com.cloud.vm.dao.DomainRouterJoinDao; import com.cloud.vm.dao.InstanceGroupDao; +import com.cloud.vm.dao.InstanceGroupJoinDao; import com.cloud.vm.dao.NicDao; import com.cloud.vm.dao.SecondaryStorageVmDao; import com.cloud.vm.dao.UserVmDao; @@ -317,6 +320,7 @@ public class ManagementServerImpl implements ManagementServer { private final AsyncJobManager _asyncMgr; private final int _purgeDelay; private final InstanceGroupDao _vmGroupDao; + private final InstanceGroupJoinDao _vmGroupJoinDao; private final UploadMonitor _uploadMonitor; private final UploadDao _uploadDao; private final SSHKeyPairDao _sshKeyPairDao; @@ -393,6 +397,7 @@ public class ManagementServerImpl implements ManagementServer { _guestOSCategoryDao = locator.getDao(GuestOSCategoryDao.class); _poolDao = locator.getDao(StoragePoolDao.class); _vmGroupDao = locator.getDao(InstanceGroupDao.class); + _vmGroupJoinDao = locator.getDao(InstanceGroupJoinDao.class); _uploadDao = locator.getDao(UploadDao.class); _configs = _configDao.getConfiguration(); _vmInstanceDao = locator.getDao(VMInstanceDao.class); @@ -3205,7 +3210,7 @@ public class ManagementServerImpl implements ManagementServer { } @Override - public Pair, Integer> searchForVmGroups(ListVMGroupsCmd cmd) { + public Pair, Integer> searchForVmGroups(ListVMGroupsCmd cmd) { Long id = cmd.getId(); String name = cmd.getGroupName(); String keyword = cmd.getKeyword(); @@ -3221,52 +3226,26 @@ public class ManagementServerImpl implements ManagementServer { Boolean isRecursive = domainIdRecursiveListProject.second(); ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third(); - Filter searchFilter = new Filter(InstanceGroupVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal()); + Filter searchFilter = new Filter(InstanceGroupJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal()); - SearchBuilder sb = _vmGroupDao.createSearchBuilder(); + SearchBuilder sb = _vmGroupJoinDao.createSearchBuilder(); + _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria); - sb.and("accountIdIN", sb.entity().getAccountId(), SearchCriteria.Op.IN); - sb.and("domainId", sb.entity().getDomainId(), SearchCriteria.Op.EQ); - if (((permittedAccounts.isEmpty()) && (domainId != null) && isRecursive)) { - // if accountId isn't specified, we can do a domain match for the - // admin case if isRecursive is true - SearchBuilder domainSearch = _domainDao.createSearchBuilder(); - domainSearch.and("path", domainSearch.entity().getPath(), SearchCriteria.Op.LIKE); - sb.join("domainSearch", domainSearch, sb.entity().getDomainId(), domainSearch.entity().getId(), JoinBuilder.JoinType.INNER); - } - - if (listProjectResourcesCriteria != null) { - if (listProjectResourcesCriteria == Project.ListProjectResourcesCriteria.ListProjectResourcesOnly) { - sb.and("accountType", sb.entity().getAccountType(), SearchCriteria.Op.EQ); - } else if (listProjectResourcesCriteria == Project.ListProjectResourcesCriteria.SkipProjectResources) { - sb.and("accountType", sb.entity().getAccountType(), SearchCriteria.Op.NEQ); - } - } sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ); sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE); - SearchCriteria sc = sb.create(); - if (listProjectResourcesCriteria != null) { - sc.setParameters("accountType", Account.ACCOUNT_TYPE_PROJECT); - } - if (!permittedAccounts.isEmpty()) { - sc.setParameters("accountIdIN", permittedAccounts.toArray()); - } else if (domainId != null) { - DomainVO domain = _domainDao.findById(domainId); - if (isRecursive) { - sc.setJoinParameters("domainSearch", "path", domain.getPath() + "%"); - } else { - sc.setParameters("domainId", domainId); - } - } + SearchCriteria sc = sb.create(); + _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria); if (keyword != null) { - SearchCriteria ssc = _vmGroupDao.createSearchCriteria(); + SearchCriteria ssc = _vmGroupJoinDao.createSearchCriteria(); ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%"); + sc.addAnd("name", SearchCriteria.Op.SC, ssc); } + if (id != null) { sc.setParameters("id", id); } @@ -3275,8 +3254,7 @@ public class ManagementServerImpl implements ManagementServer { sc.setParameters("name", "%" + name + "%"); } - Pair, Integer> result = _vmGroupDao.searchAndCount(sc, searchFilter); - return new Pair, Integer>(result.first(), result.second()); + return _vmGroupJoinDao.searchAndCount(sc, searchFilter); } @Override diff --git a/server/src/com/cloud/tags/dao/ResourceTagJoinDaoImpl.java b/server/src/com/cloud/tags/dao/ResourceTagJoinDaoImpl.java index 59f8cad0eaa..3c2dafa03e4 100644 --- a/server/src/com/cloud/tags/dao/ResourceTagJoinDaoImpl.java +++ b/server/src/com/cloud/tags/dao/ResourceTagJoinDaoImpl.java @@ -92,7 +92,6 @@ public class ResourceTagJoinDaoImpl extends GenericDaoBase uvList = new ArrayList(); SearchCriteria sc = vrIdSearch.create(); sc.setParameters("id", vr.getId()); List vms = searchIncludingRemoved(sc, null, null, false); diff --git a/server/src/com/cloud/vm/dao/DomainRouterJoinDaoImpl.java b/server/src/com/cloud/vm/dao/DomainRouterJoinDaoImpl.java index f7a91466d5c..38839e3255c 100644 --- a/server/src/com/cloud/vm/dao/DomainRouterJoinDaoImpl.java +++ b/server/src/com/cloud/vm/dao/DomainRouterJoinDaoImpl.java @@ -194,17 +194,9 @@ public class DomainRouterJoinDaoImpl extends GenericDaoBase newDomainRouterView(VirtualRouter vr) { - List uvList = new ArrayList(); SearchCriteria sc = vrIdSearch.create(); sc.setParameters("id", vr.getId()); - List vms = searchIncludingRemoved(sc, null, null, false); - if (vms != null) { - for (DomainRouterJoinVO uvm : vms) { - uvList.add(uvm); - } - } - return uvList; - + return searchIncludingRemoved(sc, null, null, false); } } diff --git a/server/src/com/cloud/vm/dao/InstanceGroupJoinDao.java b/server/src/com/cloud/vm/dao/InstanceGroupJoinDao.java new file mode 100644 index 00000000000..64599be010e --- /dev/null +++ b/server/src/com/cloud/vm/dao/InstanceGroupJoinDao.java @@ -0,0 +1,35 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.vm.dao; + +import java.util.List; + +import org.apache.cloudstack.api.response.InstanceGroupResponse; +import org.apache.cloudstack.api.view.vo.InstanceGroupJoinVO; + +import com.cloud.utils.db.GenericDao; +import com.cloud.vm.InstanceGroup; + +public interface InstanceGroupJoinDao extends GenericDao { + + InstanceGroupResponse newInstanceGroupResponse(InstanceGroupJoinVO group); + + InstanceGroupJoinVO newInstanceGroupView(InstanceGroup group); + + List searchByIds(Long... ids); + +} diff --git a/server/src/com/cloud/vm/dao/InstanceGroupJoinDaoImpl.java b/server/src/com/cloud/vm/dao/InstanceGroupJoinDaoImpl.java new file mode 100644 index 00000000000..aa20b770dc3 --- /dev/null +++ b/server/src/com/cloud/vm/dao/InstanceGroupJoinDaoImpl.java @@ -0,0 +1,96 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.vm.dao; + +import java.util.List; + +import javax.ejb.Local; + +import org.apache.log4j.Logger; + +import com.cloud.api.ApiResponseHelper; +import org.apache.cloudstack.api.response.InstanceGroupResponse; +import org.apache.cloudstack.api.view.vo.InstanceGroupJoinVO; + +import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.SearchBuilder; +import com.cloud.utils.db.SearchCriteria; +import com.cloud.vm.InstanceGroup; + + +@Local(value={InstanceGroupJoinDao.class}) +public class InstanceGroupJoinDaoImpl extends GenericDaoBase implements InstanceGroupJoinDao { + public static final Logger s_logger = Logger.getLogger(InstanceGroupJoinDaoImpl.class); + + private SearchBuilder vrSearch; + + private SearchBuilder vrIdSearch; + + + protected InstanceGroupJoinDaoImpl() { + + vrSearch = createSearchBuilder(); + vrSearch.and("idIN", vrSearch.entity().getId(), SearchCriteria.Op.IN); + vrSearch.done(); + + vrIdSearch = createSearchBuilder(); + vrIdSearch.and("id", vrIdSearch.entity().getId(), SearchCriteria.Op.EQ); + vrIdSearch.done(); + + this._count = "select count(distinct id) from instance_group_view WHERE "; + } + + + + @Override + public InstanceGroupResponse newInstanceGroupResponse(InstanceGroupJoinVO group) { + InstanceGroupResponse groupResponse = new InstanceGroupResponse(); + groupResponse.setId(group.getUuid()); + groupResponse.setName(group.getName()); + groupResponse.setCreated(group.getCreated()); + + ApiResponseHelper.populateOwner(groupResponse, group); + + groupResponse.setObjectName("instancegroup"); + return groupResponse; + } + + + + @Override + public InstanceGroupJoinVO newInstanceGroupView(InstanceGroup group) { + SearchCriteria sc = vrIdSearch.create(); + sc.setParameters("id", group.getId()); + List grps = searchIncludingRemoved(sc, null, null, false); + assert grps != null && grps.size() == 1 : "No vm group found for group id " + group.getId(); + return grps.get(0); + + } + + + + @Override + public List searchByIds(Long... ids) { + SearchCriteria sc = vrSearch.create(); + sc.setParameters("idIN", ids); + return searchIncludingRemoved(sc, null, null, false); + } + + + + +} diff --git a/setup/db/create-schema.sql b/setup/db/create-schema.sql index d0c49c80f62..406145ce2b3 100755 --- a/setup/db/create-schema.sql +++ b/setup/db/create-schema.sql @@ -2835,3 +2835,27 @@ inner join domain on event.domain_id=domain.id inner join user on event.user_id = user.id left join projects on projects.project_account_id = event.account_id left join event eve on event.start_id = eve.id; + +DROP VIEW IF EXISTS `cloud`.`instance_group_view`; +CREATE VIEW instance_group_view AS +select +instance_group.id, +instance_group.uuid, +instance_group.name, +instance_group.removed, +instance_group.created, +account.id account_id, +account.uuid account_uuid, +account.account_name account_name, +account.type account_type, +domain.id domain_id, +domain.uuid domain_uuid, +domain.name domain_name, +domain.path domain_path, +projects.id project_id, +projects.uuid project_uuid, +projects.name project_name +from instance_group +inner join account on instance_group.account_id=account.id +inner join domain on account.domain_id=domain.id +left join projects on projects.project_account_id = instance_group.account_id;