use of com.cloud.network.dao.NetworkDomainVO in project cloudstack by apache.
the class ApiDBUtils method getDomainNetworkDetails.
public static Pair<Long, Boolean> getDomainNetworkDetails(long networkId) {
NetworkDomainVO map = s_networkDomainDao.getDomainNetworkMapByNetworkId(networkId);
boolean subdomainAccess = (map.isSubdomainAccess() != null) ? map.isSubdomainAccess() : s_networkModel.getAllowSubdomainAccessGlobal();
return new Pair<Long, Boolean>(map.getDomainId(), subdomainAccess);
}
use of com.cloud.network.dao.NetworkDomainVO in project cloudstack by apache.
the class ManagementServerImpl method searchForIPAddresses.
@Override
public Pair<List<? extends IpAddress>, Integer> searchForIPAddresses(final ListPublicIpAddressesCmd cmd) {
final Long associatedNetworkId = cmd.getAssociatedNetworkId();
final Long zone = cmd.getZoneId();
final Long vlan = cmd.getVlanId();
final Boolean forVirtualNetwork = cmd.isForVirtualNetwork();
final Long ipId = cmd.getId();
final Long networkId = cmd.getNetworkId();
final Long vpcId = cmd.getVpcId();
final String state = cmd.getState();
Boolean isAllocated = cmd.isAllocatedOnly();
if (isAllocated == null) {
if (state != null && state.equalsIgnoreCase(IpAddress.State.Free.name())) {
isAllocated = Boolean.FALSE;
} else {
// default
isAllocated = Boolean.TRUE;
}
} else {
if (state != null && state.equalsIgnoreCase(IpAddress.State.Free.name())) {
if (isAllocated) {
throw new InvalidParameterValueException("Conflict: allocatedonly is true but state is Free");
}
} else if (state != null && state.equalsIgnoreCase(IpAddress.State.Allocated.name())) {
isAllocated = Boolean.TRUE;
}
}
VlanType vlanType = null;
if (forVirtualNetwork != null) {
vlanType = forVirtualNetwork ? VlanType.VirtualNetwork : VlanType.DirectAttached;
} else {
vlanType = VlanType.VirtualNetwork;
}
final Account caller = getCaller();
List<IPAddressVO> addrs = new ArrayList<>();
if (vlanType == VlanType.DirectAttached && networkId == null && ipId == null) {
// only root admin can list public ips in all shared networks
if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
isAllocated = true;
}
} else if (vlanType == VlanType.DirectAttached) {
// list public ip address on shared network
// access control. admin: all Ips, domain admin/user: all Ips in shared network in the domain/sub-domain/user
NetworkVO network = null;
if (networkId == null) {
IPAddressVO ip = _publicIpAddressDao.findById(ipId);
if (ip == null) {
throw new InvalidParameterValueException("Please specify a valid ipaddress id");
}
network = _networkDao.findById(ip.getSourceNetworkId());
} else {
network = _networkDao.findById(networkId);
}
if (network == null || network.getGuestType() != Network.GuestType.Shared) {
throw new InvalidParameterValueException("Please specify a valid network id");
}
if (network.getAclType() == ControlledEntity.ACLType.Account) {
NetworkAccountVO networkMap = _networkAccountDao.getAccountNetworkMapByNetworkId(network.getId());
if (networkMap == null) {
return new Pair<>(addrs, 0);
}
_accountMgr.checkAccess(caller, null, false, _accountDao.findById(networkMap.getAccountId()));
} else {
// Domain level
NetworkDomainVO networkMap = _networkDomainDao.getDomainNetworkMapByNetworkId(network.getId());
if (networkMap == null) {
return new Pair<>(addrs, 0);
}
if (caller.getType() == Account.ACCOUNT_TYPE_NORMAL || caller.getType() == Account.ACCOUNT_TYPE_PROJECT) {
if (_networkMgr.isNetworkAvailableInDomain(network.getId(), caller.getDomainId())) {
isAllocated = Boolean.TRUE;
} else {
return new Pair<>(addrs, 0);
}
} else if (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) {
if (caller.getDomainId() == networkMap.getDomainId() || _domainDao.isChildDomain(caller.getDomainId(), networkMap.getDomainId())) {
s_logger.debug("Caller " + caller.getUuid() + " has permission to access the network : " + network.getUuid());
} else {
if (_networkMgr.isNetworkAvailableInDomain(network.getId(), caller.getDomainId())) {
isAllocated = Boolean.TRUE;
} else {
return new Pair<>(addrs, 0);
}
}
}
}
}
final Filter searchFilter = new Filter(IPAddressVO.class, "address", false, null, null);
final SearchBuilder<IPAddressVO> sb = _publicIpAddressDao.createSearchBuilder();
Long domainId = null;
Boolean isRecursive = null;
final List<Long> permittedAccounts = new ArrayList<>();
ListProjectResourcesCriteria listProjectResourcesCriteria = null;
if (isAllocated || (vlanType == VlanType.VirtualNetwork && (caller.getType() != Account.ACCOUNT_TYPE_ADMIN || cmd.getDomainId() != null))) {
final Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(cmd.getDomainId(), cmd.isRecursive(), null);
_accountMgr.buildACLSearchParameters(caller, cmd.getId(), cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
domainId = domainIdRecursiveListProject.first();
isRecursive = domainIdRecursiveListProject.second();
listProjectResourcesCriteria = domainIdRecursiveListProject.third();
_accountMgr.buildACLSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
}
buildParameters(sb, cmd, vlanType == VlanType.VirtualNetwork ? true : isAllocated);
SearchCriteria<IPAddressVO> sc = sb.create();
setParameters(sc, cmd, vlanType);
if (isAllocated || (vlanType == VlanType.VirtualNetwork && (caller.getType() != Account.ACCOUNT_TYPE_ADMIN || cmd.getDomainId() != null))) {
_accountMgr.buildACLSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
}
if (associatedNetworkId != null) {
_accountMgr.checkAccess(caller, null, false, _networkDao.findById(associatedNetworkId));
sc.setParameters("associatedNetworkIdEq", associatedNetworkId);
}
if (vpcId != null) {
_accountMgr.checkAccess(caller, null, false, _vpcDao.findById(vpcId));
sc.setParameters("vpcId", vpcId);
}
// Allocated
addrs = _publicIpAddressDao.search(sc, searchFilter);
// Free IP addresses in system IP ranges
List<Long> freeAddrIds = new ArrayList<>();
if (!(isAllocated || vlanType == VlanType.DirectAttached)) {
Long zoneId = zone;
Account owner = _accountMgr.finalizeOwner(CallContext.current().getCallingAccount(), cmd.getAccountName(), cmd.getDomainId(), cmd.getProjectId());
if (associatedNetworkId != null) {
NetworkVO guestNetwork = _networkDao.findById(associatedNetworkId);
if (zoneId == null) {
zoneId = guestNetwork.getDataCenterId();
} else if (zoneId != guestNetwork.getDataCenterId()) {
InvalidParameterValueException ex = new InvalidParameterValueException("Please specify a valid associated network id in the specified zone.");
throw ex;
}
owner = _accountDao.findById(guestNetwork.getAccountId());
}
List<DataCenterVO> dcList = new ArrayList<>();
if (zoneId == null) {
dcList = ApiDBUtils.listZones();
} else {
dcList.add(ApiDBUtils.findZoneById(zoneId));
}
List<Long> vlanDbIds = null;
if (vlan != null) {
vlanDbIds = new ArrayList<>();
vlanDbIds.add(vlan);
}
List<IPAddressVO> freeAddrs = new ArrayList<>();
for (DataCenterVO dc : dcList) {
long dcId = dc.getId();
try {
freeAddrs.addAll(_ipAddressMgr.listAvailablePublicIps(dcId, null, vlanDbIds, owner, VlanType.VirtualNetwork, associatedNetworkId, false, false, false, null, null, false, cmd.getVpcId(), cmd.isDisplay(), false, // Free
false));
} catch (InsufficientAddressCapacityException e) {
s_logger.warn("no free address is found in zone " + dcId);
}
}
for (IPAddressVO addr : freeAddrs) {
freeAddrIds.add(addr.getId());
}
}
if (freeAddrIds.size() > 0) {
final SearchBuilder<IPAddressVO> sb2 = _publicIpAddressDao.createSearchBuilder();
buildParameters(sb2, cmd, false);
sb2.and("ids", sb2.entity().getId(), SearchCriteria.Op.IN);
SearchCriteria<IPAddressVO> sc2 = sb2.create();
setParameters(sc2, cmd, vlanType);
sc2.setParameters("ids", freeAddrIds.toArray());
// Allocated + Free
addrs.addAll(_publicIpAddressDao.search(sc2, searchFilter));
}
Collections.sort(addrs, Comparator.comparing(IPAddressVO::getAddress));
List<? extends IpAddress> wPagination = com.cloud.utils.StringUtils.applyPagination(addrs, cmd.getStartIndex(), cmd.getPageSizeVal());
if (wPagination != null) {
return new Pair<List<? extends IpAddress>, Integer>(wPagination, addrs.size());
}
return new Pair<>(addrs, addrs.size());
}
Aggregations