private void validate(APIAddMonToCephBackupStorageMsg msg) { checkMonUrls(msg.getMonUrls()); List<String> hostnames = msg.getMonUrls().stream() .map(MonUri::new) .map(MonUri::getHostname) .collect(Collectors.toList()); if (Q.New(CephBackupStorageMonVO.class).in(CephBackupStorageMonVO_.hostname, hostnames).isExists()){ throw new ApiMessageInterceptionException(argerr("Adding the same Mon node is not allowed")); } } private void validate(APIUpdateCephBackupStorageMonMsg msg) {
private void validate(APIAddMonToCephPrimaryStorageMsg msg) { checkMonUrls(msg.getMonUrls()); List<String> hostnames = msg.getMonUrls().stream() .map(MonUri::new) .map(MonUri::getHostname) .collect(Collectors.toList()); if (Q.New(CephPrimaryStorageMonVO.class).in(CephPrimaryStorageMonVO_.hostname, hostnames).isExists()){ throw new ApiMessageInterceptionException(argerr("Adding the same Mon node is not allowed")); } }
private List<String> getNeedRefreshSizeVolumeUuids(){ Set<String> volUuids = new HashSet<>(); for (RefreshVolumeSizeExtensionPoint ext : pluginRgty.getExtensionList(RefreshVolumeSizeExtensionPoint.class)) { volUuids.addAll(ext.getNeedRefreshVolumeSizeVolume()); } return volUuids.isEmpty() ? new ArrayList<>() : Q.New(VolumeVO.class).in(VolumeVO_.uuid, volUuids) .eq(VolumeVO_.state, VolumeState.Enabled) .eq(VolumeVO_.status, VolumeStatus.Ready) .select(VolumeVO_.uuid) .listValues(); } }
private Map<String, String> getUuidTypeMapByResourceUuids(List<String> resourceUuids) { List<Tuple> ts = Q.New(AccountResourceRefVO.class) .select(AccountResourceRefVO_.resourceUuid, AccountResourceRefVO_.resourceType) .in(AccountResourceRefVO_.resourceUuid, resourceUuids) .listTuple(); Map<String, String> uuidType = new HashMap<String, String>(); for (Tuple t : ts) { String resUuid = t.get(0, String.class); String resType = t.get(1, String.class); uuidType.put(resUuid, resType); } return uuidType; }
@Override protected List<SecurityGroupDeletionMsg> scripts() { List<String> uuids = q(AccountResourceRefVO.class) .select(AccountResourceRefVO_.resourceUuid) .eq(AccountResourceRefVO_.resourceType, SecurityGroupVO.class.getSimpleName()) .in(AccountResourceRefVO_.ownerAccountUuid, accountUuids) .listValues(); if (uuids.isEmpty()) { return null; } return uuids.stream().map(auuid -> { SecurityGroupDeletionMsg msg = new SecurityGroupDeletionMsg(); msg.setUuid(auuid); bus.makeTargetServiceIdByResourceUuid(msg, SecurityGroupConstant.SERVICE_ID, auuid); return msg; }).collect(Collectors.toList()); } }.execute();
@Transactional(readOnly = true) private VirtualRouterVmInventory findVirtualRouterVm(String lbUuid, List<String> vmNics) { String sql = "select vr from VirtualRouterVmVO vr, VirtualRouterLoadBalancerRefVO ref where ref.virtualRouterVmUuid =" + " vr.uuid and ref.loadBalancerUuid = :lbUuid"; TypedQuery<VirtualRouterVmVO> q = dbf.getEntityManager().createQuery(sql, VirtualRouterVmVO.class); q.setParameter("lbUuid", lbUuid); List<VirtualRouterVmVO> vrs = q.getResultList(); if (LoadBalancerSystemTags.SEPARATE_VR.hasTag(lbUuid)) { Optional<VirtualRouterVmVO> vr = vrs.stream() .filter(v -> VirtualRouterSystemTags.DEDICATED_ROLE_VR.hasTag(v.getUuid())) .findFirst(); if (!vr.isPresent()) { return null; } List<String> vmNicL3NetworkUuids = Q.New(VmNicVO.class).select(VmNicVO_.l3NetworkUuid).in(VmNicVO_.uuid, vmNics).listValues(); VirtualRouterVmInventory vrInventory = VirtualRouterVmInventory.valueOf(vr.get()); vmNicL3NetworkUuids.removeAll(vrInventory.getGuestL3Networks()); if (!vmNicL3NetworkUuids.isEmpty()) { logger.debug(String.format("found l3 networks[uuids:%s] not attached to separate vr[uuid:%s] for loadbalancer[uuid:%s]", vmNicL3NetworkUuids, vr.get().getUuid(), lbUuid)); throw new CloudRuntimeException(String.format("not support separate vr with multiple networks vpc!")); } } DebugUtils.Assert(vrs.size() <= 1, String.format("multiple virtual routers[uuids:%s] found", vrs.stream().map(v -> v.getUuid()).collect(Collectors.toList()))); return vrs.isEmpty() ? null : VirtualRouterVmInventory.valueOf(vrs.get(0)); }
private List<EipInventory> eipFromAction(CascadeAction action) { if (VipVO.class.getSimpleName().equals(action.getParentIssuer())) { List<String> vipUuids = CollectionUtils.transformToList((List<VipInventory>) action.getParentIssuerContext(), new Function<String, VipInventory>() { @Override public String call(VipInventory arg) { return arg.getUuid(); } }); if (vipUuids.isEmpty()) { return null; } List<EipVO> eipVOS = Q.New(EipVO.class).in(EipVO_.vipUuid, vipUuids).list(); return EipInventory.valueOf(eipVOS); } else if (EipVO.class.getSimpleName().equals(action.getParentIssuer())) { return action.getParentIssuerContext(); } return null; } }
.in(NetworkServiceL3NetworkRefVO_.l3NetworkUuid, l3Uuids) .eq(NetworkServiceL3NetworkRefVO_.networkServiceType, LoadBalancerConstants.LB_NETWORK_SERVICE_TYPE_STRING) .listValues());
private List<String> getTargetVmUuids() { List<String> result = new ArrayList<>(); List<String> allVmUuids = Q.New(VmInstanceVO.class) .select(VmInstanceVO_.uuid) .eq(VmInstanceVO_.type, VmInstanceConstant.USER_VM_TYPE) .eq(VmInstanceVO_.hypervisorType, VmInstanceConstant.KVM_HYPERVISOR_TYPE) .listValues(); for (String vmUuid : allVmUuids) { if (destinationMaker.isManagedByUs(vmUuid)) { result.add(vmUuid); } } if (result.isEmpty()) { return result; } List<String> hasCdRomVmUuids = Q.New(VmCdRomVO.class) .select(VmCdRomVO_.vmInstanceUuid) .in(VmCdRomVO_.vmInstanceUuid, result) .listValues(); result.removeAll(hasCdRomVmUuids); return result; } }
private List<VtepInventory> vtepFromAction(CascadeAction action) { List<VtepInventory> ret = null; if (L2NetworkVO.class.getSimpleName().equals(action.getParentIssuer())) { List<String> l2uuids = CollectionUtils.transformToList((List<L2NetworkInventory>)action.getParentIssuerContext(), new Function<String, L2NetworkInventory>() { @Override public String call(L2NetworkInventory arg) { return arg.getUuid(); } }); List<VtepVO> vos = Q.New(VtepVO.class).in(VtepVO_.poolUuid, l2uuids).list(); if (!vos.isEmpty()) { ret = VtepInventory.valueOf(vos); } } else if (NAME.equals(action.getParentIssuer())) { ret = action.getParentIssuerContext(); } return ret; }
@Transactional(readOnly = true) private boolean noStorageAccessible(String hostUuid){ // detach ps will delete PrimaryStorageClusterRefVO first. List<String> attachedPsUuids = SQL.New("select distinct ref.primaryStorageUuid" + " from PrimaryStorageClusterRefVO ref, HostVO h" + " where h.uuid =:hostUuid" + " and ref.clusterUuid = h.clusterUuid", String.class) .param("hostUuid", hostUuid) .list(); long attachedPsCount = attachedPsUuids.size(); long inaccessiblePsCount = attachedPsCount == 0 ? 0 : Q.New(PrimaryStorageHostRefVO.class) .eq(PrimaryStorageHostRefVO_.hostUuid, hostUuid) .eq(PrimaryStorageHostRefVO_.status, PrimaryStorageHostStatus.Disconnected) .in(PrimaryStorageHostRefVO_.primaryStorageUuid, attachedPsUuids) .count(); return inaccessiblePsCount == attachedPsCount && attachedPsCount > 0; }
private void checkIfTheAccountOwnTheResource(APIMessage.FieldParam param) throws IllegalAccessException { List<String> uuids = getResourceUuids(param); if (uuids.isEmpty()) { return; } Class resourceType = param.param.resourceType(); List<Tuple> ts = q(AccountResourceRefVO.class).select(AccountResourceRefVO_.accountUuid, AccountResourceRefVO_.resourceUuid) .in(AccountResourceRefVO_.resourceUuid, uuids) //.eq(AccountResourceRefVO_.resourceType, acntMgr.getBaseResourceType(resourceType).getSimpleName()) .listTuple(); ts.addAll( q(SharedResourceVO.class).select(SharedResourceVO_.receiverAccountUuid, SharedResourceVO_.resourceUuid) .in(SharedResourceVO_.resourceUuid, uuids) .eq(SharedResourceVO_.permission, SharedResourceVO.PERMISSION_WRITE) .eq(SharedResourceVO_.receiverAccountUuid, rbacEntity.getApiMessage().getSession().getAccountUuid()) //.eq(SharedResourceVO_.resourceType, acntMgr.getBaseResourceType(resourceType).getSimpleName()) .listTuple() ); Collection<AccountResourceBundle> bundles = toAccountResourceBundles(uuids, ts); uuids.forEach(uuid -> { Optional<AccountResourceBundle> opt = bundles.stream().filter(b -> b.accountUuid.equals(rbacEntity.getApiMessage().getSession().getAccountUuid()) && b.resourceUuid.equals(uuid)).findFirst(); if (!opt.isPresent()) { throw new OperationFailureException(operr("permission denied, the account[uuid:%s] is not the owner of the resource[uuid:%s, type:%s]", rbacEntity.getApiMessage().getSession().getAccountUuid(), uuid, resourceType.getSimpleName())); } }); }
.in(SharedResourceVO_.resourceUuid, checkAccountResourceUuids) .eq(SharedResourceVO_.toPublic, true).listValues(); checkAccountResourceUuids.removeAll(shared);
private void pingAll(PrimaryStorageInventory inv, int limit, final Completion completion){ List<String> huuids = Q.New(HostVO.class).select(HostVO_.uuid) .in(HostVO_.clusterUuid, inv.getAttachedClusterUuids()) .eq(HostVO_.status, HostStatus.Connected) .eq(HostVO_.state, HostState.Enabled) .listValues(); if(huuids.size() == 0){ completion.fail(operr("no host in is Connected or primary storage[uuid:%s] attach no cluster", inv.getUuid())); return; } Collections.shuffle(huuids); doPing(huuids.subList(0, min(limit,huuids.size())), inv, new Completion(completion) { @Override public void success() { completion.success(); } @Override public void fail(ErrorCode errorCode) { completion.fail(errorCode); } }); }
@Transactional(readOnly = true) private boolean noStorageAccessible(){ // detach ps will delete PrimaryStorageClusterRefVO first. List<String> attachedPsUuids = Q.New(PrimaryStorageClusterRefVO.class) .select(PrimaryStorageClusterRefVO_.primaryStorageUuid) .eq(PrimaryStorageClusterRefVO_.clusterUuid, self.getClusterUuid()) .listValues(); long attachedPsCount = attachedPsUuids.size(); long inaccessiblePsCount = attachedPsCount == 0 ? 0 : Q.New(PrimaryStorageHostRefVO.class) .eq(PrimaryStorageHostRefVO_.hostUuid, self.getUuid()) .eq(PrimaryStorageHostRefVO_.status, PrimaryStorageHostStatus.Disconnected) .in(PrimaryStorageHostRefVO_.primaryStorageUuid, attachedPsUuids) .count(); return inaccessiblePsCount == attachedPsCount && attachedPsCount > 0; }
private void addCdRomToHistoricalVm() { if (!VmCdRomGlobalProperty.addCdRomToHistoricalVm) { return; } List<String> vmUuids = getTargetVmUuids(); if (vmUuids.isEmpty()){ return; } List<Tuple> vmUuidPlatforms = Q.New(VmInstanceVO.class) .select(VmInstanceVO_.uuid, VmInstanceVO_.platform) .in(VmInstanceVO_.uuid, vmUuids) .listTuple(); for (Tuple vmTuple : vmUuidPlatforms) { String vmUuid = vmTuple.get(0, String.class); String vmPlatform = vmTuple.get(1, String.class); if (vmPlatform != null && ImagePlatform.Windows.toString().equalsIgnoreCase(vmPlatform)) { addCdRomForWindowsVm(vmUuid); continue; } addCdRomForLinuxAndOthersVm(vmUuid); } }
@Override public void releaseResourceOnDetachingNic(VmInstanceSpec spec, VmNicInventory nic, NoErrorCompletion completion) { // Todo(WeiW): Need to check router rather than not only user vm if (spec.getVmInventory().getType().equals(VmInstanceConstant.USER_VM_TYPE)) { completion.done(); return; } /* FIXME shixin: nic.getL3NetworkUuid() should be change to nic.getUesedIp for appliancevm */ logger.debug(String.format("check detaching nic[uuid:%s] in peer l3 of vip", nic.getUuid())); List<VipPeerL3NetworkRefVO> refVOS = Q.New(VipPeerL3NetworkRefVO.class).eq(VipPeerL3NetworkRefVO_.l3NetworkUuid, nic.getL3NetworkUuid()).list(); if (refVOS == null || refVOS.isEmpty()) { completion.done(); return; } Set<String> refUuids = refVOS.stream().map(r -> r.getVipUuid()).collect(Collectors.toSet()); logger.debug(String.format("release peer l3[uuid:%s] from vips[uuid:%s] for detaching nic[uuid:%s]", nic.getL3NetworkUuid(), refUuids, nic.getUuid())); List<VipVO> vipVOS = Q.New(VipVO.class).in(VipVO_.uuid, refUuids).list(); for (VipVO vipVO : vipVOS) { VipBase v = new VipBase(vipVO); v.deletePeerL3NetworkUuid(nic.getL3NetworkUuid()); } completion.done(); }
private void validate(APIUpdateRoleMsg msg) { if (Q.New(RoleVO.class).in(RoleVO_.type, list(RoleType.Predefined, RoleType.System)).eq(RoleVO_.uuid, msg.getUuid()).isExists()) { throw new ApiMessageInterceptionException(argerr("cannot update a system or predefined role")); } }
private void validate(APIDeleteRoleMsg msg) { if (Q.New(RoleVO.class).in(RoleVO_.type, list(RoleType.Predefined, RoleType.System)).eq(RoleVO_.uuid, msg.getUuid()).isExists()) { throw new ApiMessageInterceptionException(argerr("cannot delete a system or predefined role")); } } }
private void validate(AddVmNicToSecurityGroupMsg msg) { String securityGroupUuid = msg.getSecurityGroupUuid(); List<String> uuids = Q.New(VmNicVO.class) .select(VmNicVO_.uuid) .in(VmNicVO_.uuid, msg.getVmNicUuids()) .listValues(); if (!uuids.containsAll(msg.getVmNicUuids())) { msg.getVmNicUuids().removeAll(uuids); throw new OperationFailureException(err(SysErrors.RESOURCE_NOT_FOUND, "cannot find vm nics[uuids:%s]", msg.getVmNicUuids() )); } List<String> nicUuids = SQL.New("select nic.uuid from SecurityGroupL3NetworkRefVO ref, VmNicVO nic, UsedIpVO ip" + " where ref.l3NetworkUuid = ip.l3NetworkUuid and ip.vmNicUuid = nic.uuid" + " and ref.securityGroupUuid = :sgUuid and nic.uuid in (:nicUuids)") .param("nicUuids", uuids) .param("sgUuid", securityGroupUuid) .list(); List<String> wrongUuids = new ArrayList<>(); for (String uuid : uuids) { if (!nicUuids.contains(uuid)) { wrongUuids.add(uuid); } } if (!wrongUuids.isEmpty()) { throw new OperationFailureException(argerr("VM nics[uuids:%s] are not on L3 networks that have been attached to the security group[uuid:%s]", wrongUuids, securityGroupUuid)); } msg.setVmNicUuids(uuids); }