use of org.apache.cloudstack.affinity.AffinityGroup in project cloudstack by apache.
the class DedicatedResourceManagerImpl method dedicatePod.
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_DEDICATE_RESOURCE, eventDescription = "dedicating a Pod")
public List<DedicatedResourceVO> dedicatePod(final Long podId, final Long domainId, final String accountName) {
Long accountId = null;
if (accountName != null) {
Account caller = CallContext.current().getCallingAccount();
Account owner = _accountMgr.finalizeOwner(caller, accountName, domainId, null);
accountId = owner.getId();
}
List<Long> childDomainIds = getDomainChildIds(domainId);
childDomainIds.add(domainId);
checkAccountAndDomain(accountId, domainId);
HostPodVO pod = _podDao.findById(podId);
List<HostVO> hosts = null;
if (pod == null) {
throw new InvalidParameterValueException("Unable to find pod by id " + podId);
} else {
DedicatedResourceVO dedicatedPod = _dedicatedDao.findByPodId(podId);
DedicatedResourceVO dedicatedZoneOfPod = _dedicatedDao.findByZoneId(pod.getDataCenterId());
//check if pod is dedicated
if (dedicatedPod != null) {
s_logger.error("Pod " + pod.getName() + " is already dedicated");
throw new CloudRuntimeException("Pod " + pod.getName() + " is already dedicated");
}
if (dedicatedZoneOfPod != null) {
boolean domainIdInChildreanList = getDomainChildIds(dedicatedZoneOfPod.getDomainId()).contains(domainId);
//can dedicate a pod to an account/domain if zone is dedicated to parent-domain
if (dedicatedZoneOfPod.getAccountId() != null || (accountId == null && !domainIdInChildreanList) || (accountId != null && !(dedicatedZoneOfPod.getDomainId().equals(domainId) || domainIdInChildreanList))) {
DataCenterVO zone = _zoneDao.findById(pod.getDataCenterId());
s_logger.error("Cannot dedicate Pod. Its zone is already dedicated");
throw new CloudRuntimeException("Pod's Zone " + zone.getName() + " is already dedicated");
}
}
//check if any resource under this pod is dedicated to different account or sub-domain
List<ClusterVO> clusters = _clusterDao.listByPodId(pod.getId());
List<DedicatedResourceVO> clustersToRelease = new ArrayList<DedicatedResourceVO>();
List<DedicatedResourceVO> hostsToRelease = new ArrayList<DedicatedResourceVO>();
for (ClusterVO cluster : clusters) {
DedicatedResourceVO dCluster = _dedicatedDao.findByClusterId(cluster.getId());
if (dCluster != null) {
if (!(childDomainIds.contains(dCluster.getDomainId()))) {
throw new CloudRuntimeException("Cluster " + cluster.getName() + " under this Pod " + pod.getName() + " is dedicated to different account/domain");
}
/*if all dedicated resources belongs to same account and domain then we should release dedication
and make new entry for this Pod*/
if (accountId != null) {
if (dCluster.getAccountId().equals(accountId)) {
clustersToRelease.add(dCluster);
} else {
s_logger.error("Cluster " + cluster.getName() + " under this Pod " + pod.getName() + " is dedicated to different account/domain");
throw new CloudRuntimeException("Cluster " + cluster.getName() + " under this Pod " + pod.getName() + " is dedicated to different account/domain");
}
} else {
if (dCluster.getAccountId() == null && dCluster.getDomainId().equals(domainId)) {
clustersToRelease.add(dCluster);
}
}
}
}
for (DedicatedResourceVO dr : clustersToRelease) {
releaseDedicatedResource(null, null, dr.getClusterId(), null);
}
hosts = _hostDao.findByPodId(pod.getId());
for (HostVO host : hosts) {
DedicatedResourceVO dHost = _dedicatedDao.findByHostId(host.getId());
if (dHost != null) {
if (!(getDomainChildIds(domainId).contains(dHost.getDomainId()))) {
throw new CloudRuntimeException("Host " + host.getName() + " under this Pod " + pod.getName() + " is dedicated to different account/domain");
}
if (accountId != null) {
if (dHost.getAccountId().equals(accountId)) {
hostsToRelease.add(dHost);
} else {
s_logger.error("Host " + host.getName() + " under this Pod " + pod.getName() + " is dedicated to different account/domain");
throw new CloudRuntimeException("Host " + host.getName() + " under this Pod " + pod.getName() + " is dedicated to different account/domain");
}
} else {
if (dHost.getAccountId() == null && dHost.getDomainId().equals(domainId)) {
hostsToRelease.add(dHost);
}
}
}
}
for (DedicatedResourceVO dr : hostsToRelease) {
releaseDedicatedResource(null, null, null, dr.getHostId());
}
}
checkHostsSuitabilityForExplicitDedication(accountId, childDomainIds, hosts);
final Long accountIdFinal = accountId;
return Transaction.execute(new TransactionCallback<List<DedicatedResourceVO>>() {
@Override
public List<DedicatedResourceVO> doInTransaction(TransactionStatus status) {
// find or create the affinity group by name under this account/domain
AffinityGroup group = findOrCreateDedicatedAffinityGroup(domainId, accountIdFinal);
if (group == null) {
s_logger.error("Unable to dedicate zone due to, failed to create dedication affinity group");
throw new CloudRuntimeException("Failed to dedicate zone. Please contact Cloud Support.");
}
DedicatedResourceVO dedicatedResource = new DedicatedResourceVO(null, podId, null, null, null, null, group.getId());
try {
dedicatedResource.setDomainId(domainId);
if (accountIdFinal != null) {
dedicatedResource.setAccountId(accountIdFinal);
}
dedicatedResource = _dedicatedDao.persist(dedicatedResource);
} catch (Exception e) {
s_logger.error("Unable to dedicate pod due to " + e.getMessage(), e);
throw new CloudRuntimeException("Failed to dedicate pod. Please contact Cloud Support.");
}
List<DedicatedResourceVO> result = new ArrayList<DedicatedResourceVO>();
result.add(dedicatedResource);
return result;
}
});
}
use of org.apache.cloudstack.affinity.AffinityGroup in project cloudstack by apache.
the class DedicatedResourceManagerImpl method dedicateCluster.
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_DEDICATE_RESOURCE, eventDescription = "dedicating a Cluster")
public List<DedicatedResourceVO> dedicateCluster(final Long clusterId, final Long domainId, final String accountName) {
Long accountId = null;
List<HostVO> hosts = null;
if (accountName != null) {
Account caller = CallContext.current().getCallingAccount();
Account owner = _accountMgr.finalizeOwner(caller, accountName, domainId, null);
accountId = owner.getId();
}
List<Long> childDomainIds = getDomainChildIds(domainId);
childDomainIds.add(domainId);
checkAccountAndDomain(accountId, domainId);
ClusterVO cluster = _clusterDao.findById(clusterId);
if (cluster == null) {
throw new InvalidParameterValueException("Unable to find cluster by id " + clusterId);
} else {
DedicatedResourceVO dedicatedCluster = _dedicatedDao.findByClusterId(clusterId);
DedicatedResourceVO dedicatedPodOfCluster = _dedicatedDao.findByPodId(cluster.getPodId());
DedicatedResourceVO dedicatedZoneOfCluster = _dedicatedDao.findByZoneId(cluster.getDataCenterId());
//check if cluster is dedicated
if (dedicatedCluster != null) {
s_logger.error("Cluster " + cluster.getName() + " is already dedicated");
throw new CloudRuntimeException("Cluster " + cluster.getName() + " is already dedicated");
}
if (dedicatedPodOfCluster != null) {
boolean domainIdInChildreanList = getDomainChildIds(dedicatedPodOfCluster.getDomainId()).contains(domainId);
//can dedicate a cluster to an account/domain if pod is dedicated to parent-domain
if (dedicatedPodOfCluster.getAccountId() != null || (accountId == null && !domainIdInChildreanList) || (accountId != null && !(dedicatedPodOfCluster.getDomainId().equals(domainId) || domainIdInChildreanList))) {
s_logger.error("Cannot dedicate Cluster. Its Pod is already dedicated");
HostPodVO pod = _podDao.findById(cluster.getPodId());
throw new CloudRuntimeException("Cluster's Pod " + pod.getName() + " is already dedicated");
}
}
if (dedicatedZoneOfCluster != null) {
boolean domainIdInChildreanList = getDomainChildIds(dedicatedZoneOfCluster.getDomainId()).contains(domainId);
//can dedicate a cluster to an account/domain if zone is dedicated to parent-domain
if (dedicatedZoneOfCluster.getAccountId() != null || (accountId == null && !domainIdInChildreanList) || (accountId != null && !(dedicatedZoneOfCluster.getDomainId().equals(domainId) || domainIdInChildreanList))) {
s_logger.error("Cannot dedicate Cluster. Its zone is already dedicated");
DataCenterVO zone = _zoneDao.findById(cluster.getDataCenterId());
throw new CloudRuntimeException("Cluster's Zone " + zone.getName() + " is already dedicated");
}
}
//check if any resource under this cluster is dedicated to different account or sub-domain
hosts = _hostDao.findByClusterId(cluster.getId());
List<DedicatedResourceVO> hostsToRelease = new ArrayList<DedicatedResourceVO>();
for (HostVO host : hosts) {
DedicatedResourceVO dHost = _dedicatedDao.findByHostId(host.getId());
if (dHost != null) {
if (!(childDomainIds.contains(dHost.getDomainId()))) {
throw new CloudRuntimeException("Host " + host.getName() + " under this Cluster " + cluster.getName() + " is dedicated to different account/domain");
}
/*if all dedicated resources belongs to same account and domain then we should release dedication
and make new entry for this cluster */
if (accountId != null) {
if (dHost.getAccountId().equals(accountId)) {
hostsToRelease.add(dHost);
} else {
s_logger.error("Cannot dedicate Cluster " + cluster.getName() + " to account" + accountName);
throw new CloudRuntimeException("Cannot dedicate Cluster " + cluster.getName() + " to account" + accountName);
}
} else {
if (dHost.getAccountId() == null && dHost.getDomainId().equals(domainId)) {
hostsToRelease.add(dHost);
}
}
}
}
for (DedicatedResourceVO dr : hostsToRelease) {
releaseDedicatedResource(null, null, null, dr.getHostId());
}
}
checkHostsSuitabilityForExplicitDedication(accountId, childDomainIds, hosts);
final Long accountIdFinal = accountId;
return Transaction.execute(new TransactionCallback<List<DedicatedResourceVO>>() {
@Override
public List<DedicatedResourceVO> doInTransaction(TransactionStatus status) {
// find or create the affinity group by name under this account/domain
AffinityGroup group = findOrCreateDedicatedAffinityGroup(domainId, accountIdFinal);
if (group == null) {
s_logger.error("Unable to dedicate zone due to, failed to create dedication affinity group");
throw new CloudRuntimeException("Failed to dedicate zone. Please contact Cloud Support.");
}
DedicatedResourceVO dedicatedResource = new DedicatedResourceVO(null, null, clusterId, null, null, null, group.getId());
try {
dedicatedResource.setDomainId(domainId);
if (accountIdFinal != null) {
dedicatedResource.setAccountId(accountIdFinal);
}
dedicatedResource = _dedicatedDao.persist(dedicatedResource);
} catch (Exception e) {
s_logger.error("Unable to dedicate host due to " + e.getMessage(), e);
throw new CloudRuntimeException("Failed to dedicate cluster. Please contact Cloud Support.");
}
List<DedicatedResourceVO> result = new ArrayList<DedicatedResourceVO>();
result.add(dedicatedResource);
return result;
}
});
}
use of org.apache.cloudstack.affinity.AffinityGroup in project cloudstack by apache.
the class DedicatedResourceManagerImpl method dedicateHost.
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_DEDICATE_RESOURCE, eventDescription = "dedicating a Host")
public List<DedicatedResourceVO> dedicateHost(final Long hostId, final Long domainId, final String accountName) {
Long accountId = null;
if (accountName != null) {
Account caller = CallContext.current().getCallingAccount();
Account owner = _accountMgr.finalizeOwner(caller, accountName, domainId, null);
accountId = owner.getId();
}
checkAccountAndDomain(accountId, domainId);
HostVO host = _hostDao.findById(hostId);
if (host == null) {
throw new InvalidParameterValueException("Unable to find host by id " + hostId);
} else {
//check if host is of routing type
if (host.getType() != Host.Type.Routing) {
throw new CloudRuntimeException("Invalid host type for host " + host.getName());
}
DedicatedResourceVO dedicatedHost = _dedicatedDao.findByHostId(hostId);
DedicatedResourceVO dedicatedClusterOfHost = _dedicatedDao.findByClusterId(host.getClusterId());
DedicatedResourceVO dedicatedPodOfHost = _dedicatedDao.findByPodId(host.getPodId());
DedicatedResourceVO dedicatedZoneOfHost = _dedicatedDao.findByZoneId(host.getDataCenterId());
if (dedicatedHost != null) {
s_logger.error("Host " + host.getName() + " is already dedicated");
throw new CloudRuntimeException("Host " + host.getName() + " is already dedicated");
}
if (dedicatedClusterOfHost != null) {
boolean domainIdInChildreanList = getDomainChildIds(dedicatedClusterOfHost.getDomainId()).contains(domainId);
//can dedicate a host to an account/domain if cluster is dedicated to parent-domain
if (dedicatedClusterOfHost.getAccountId() != null || (accountId == null && !domainIdInChildreanList) || (accountId != null && !(dedicatedClusterOfHost.getDomainId().equals(domainId) || domainIdInChildreanList))) {
ClusterVO cluster = _clusterDao.findById(host.getClusterId());
s_logger.error("Host's Cluster " + cluster.getName() + " is already dedicated");
throw new CloudRuntimeException("Host's Cluster " + cluster.getName() + " is already dedicated");
}
}
if (dedicatedPodOfHost != null) {
boolean domainIdInChildreanList = getDomainChildIds(dedicatedPodOfHost.getDomainId()).contains(domainId);
//can dedicate a host to an account/domain if pod is dedicated to parent-domain
if (dedicatedPodOfHost.getAccountId() != null || (accountId == null && !domainIdInChildreanList) || (accountId != null && !(dedicatedPodOfHost.getDomainId().equals(domainId) || domainIdInChildreanList))) {
HostPodVO pod = _podDao.findById(host.getPodId());
s_logger.error("Host's Pod " + pod.getName() + " is already dedicated");
throw new CloudRuntimeException("Host's Pod " + pod.getName() + " is already dedicated");
}
}
if (dedicatedZoneOfHost != null) {
boolean domainIdInChildreanList = getDomainChildIds(dedicatedZoneOfHost.getDomainId()).contains(domainId);
//can dedicate a host to an account/domain if zone is dedicated to parent-domain
if (dedicatedZoneOfHost.getAccountId() != null || (accountId == null && !domainIdInChildreanList) || (accountId != null && !(dedicatedZoneOfHost.getDomainId().equals(domainId) || domainIdInChildreanList))) {
DataCenterVO zone = _zoneDao.findById(host.getDataCenterId());
s_logger.error("Host's Data Center " + zone.getName() + " is already dedicated");
throw new CloudRuntimeException("Host's Data Center " + zone.getName() + " is already dedicated");
}
}
}
List<Long> childDomainIds = getDomainChildIds(domainId);
childDomainIds.add(domainId);
checkHostSuitabilityForExplicitDedication(accountId, childDomainIds, hostId);
final Long accountIdFinal = accountId;
return Transaction.execute(new TransactionCallback<List<DedicatedResourceVO>>() {
@Override
public List<DedicatedResourceVO> doInTransaction(TransactionStatus status) {
// find or create the affinity group by name under this account/domain
AffinityGroup group = findOrCreateDedicatedAffinityGroup(domainId, accountIdFinal);
if (group == null) {
s_logger.error("Unable to dedicate zone due to, failed to create dedication affinity group");
throw new CloudRuntimeException("Failed to dedicate zone. Please contact Cloud Support.");
}
DedicatedResourceVO dedicatedResource = new DedicatedResourceVO(null, null, null, hostId, null, null, group.getId());
try {
dedicatedResource.setDomainId(domainId);
if (accountIdFinal != null) {
dedicatedResource.setAccountId(accountIdFinal);
}
dedicatedResource = _dedicatedDao.persist(dedicatedResource);
} catch (Exception e) {
s_logger.error("Unable to dedicate host due to " + e.getMessage(), e);
throw new CloudRuntimeException("Failed to dedicate host. Please contact Cloud Support.");
}
List<DedicatedResourceVO> result = new ArrayList<DedicatedResourceVO>();
result.add(dedicatedResource);
return result;
}
});
}
use of org.apache.cloudstack.affinity.AffinityGroup in project cloudstack by apache.
the class DomainChecker method checkAccess.
@Override
public boolean checkAccess(Account caller, ControlledEntity entity, AccessType accessType) throws PermissionDeniedException {
if (entity instanceof VirtualMachineTemplate) {
VirtualMachineTemplate template = (VirtualMachineTemplate) entity;
Account owner = _accountDao.findById(template.getAccountId());
// validate that the template is usable by the account
if (!template.isPublicTemplate()) {
if (_accountService.isRootAdmin(caller.getId()) || (owner.getId() == caller.getId())) {
return true;
}
//special handling for the project case
if (owner.getType() == Account.ACCOUNT_TYPE_PROJECT && _projectMgr.canAccessProjectAccount(caller, owner.getId())) {
return true;
}
// since the current account is not the owner of the template, check the launch permissions table to see if the
// account can launch a VM from this template
LaunchPermissionVO permission = _launchPermissionDao.findByTemplateAndAccount(template.getId(), caller.getId());
if (permission == null) {
throw new PermissionDeniedException(caller + " does not have permission to launch instances from " + template);
}
} else {
// Domain admin and regular user can delete/modify only templates created by them
if (accessType != null && accessType == AccessType.OperateEntry) {
if (!_accountService.isRootAdmin(caller.getId()) && owner.getId() != caller.getId()) {
// For projects check if the caller account can access the project account
if (owner.getType() != Account.ACCOUNT_TYPE_PROJECT || !(_projectMgr.canAccessProjectAccount(caller, owner.getId()))) {
throw new PermissionDeniedException("Domain Admin and regular users can modify only their own Public templates");
}
}
}
}
return true;
} else if (entity instanceof Network && accessType != null && accessType == AccessType.UseEntry) {
_networkMgr.checkNetworkPermissions(caller, (Network) entity);
} else if (entity instanceof AffinityGroup) {
return false;
} else {
if (_accountService.isNormalUser(caller.getId())) {
Account account = _accountDao.findById(entity.getAccountId());
if (account != null && account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
//only project owner can delete/modify the project
if (accessType != null && accessType == AccessType.ModifyProject) {
if (!_projectMgr.canModifyProjectAccount(caller, account.getId())) {
throw new PermissionDeniedException(caller + " does not have permission to operate with resource " + entity);
}
} else if (!_projectMgr.canAccessProjectAccount(caller, account.getId())) {
throw new PermissionDeniedException(caller + " does not have permission to operate with resource " + entity);
}
} else {
if (caller.getId() != entity.getAccountId()) {
throw new PermissionDeniedException(caller + " does not have permission to operate with resource " + entity);
}
}
}
}
return true;
}
use of org.apache.cloudstack.affinity.AffinityGroup in project cloudstack by apache.
the class CreateAffinityGroupCmd method execute.
@Override
public void execute() {
AffinityGroup group = _affinityGroupService.getAffinityGroup(getEntityId());
if (group != null) {
AffinityGroupResponse response = _responseGenerator.createAffinityGroupResponse(group);
response.setResponseName(getCommandName());
setResponseObject(response);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create affinity group:" + affinityGroupName);
}
}
Aggregations