protected static String getAccountUuidOfResource(String resourceUuid) { if (acntMgr == null) { acntMgr = Platform.getComponentLoader().getComponent(AccountManager.class); } return acntMgr.getOwnerAccountUuidOfResource(resourceUuid); }
@Transactional private void changeEipOwner(AccountResourceRefInventory ref, String newOwnerUuid) { String sql = "select eip.uuid" + " from VmInstanceVO vm, VmNicVO nic, EipVO eip" + " where vm.uuid = nic.vmInstanceUuid" + " and nic.uuid = eip.vmNicUuid" + " and vm.uuid = :uuid"; TypedQuery<String> q = dbf.getEntityManager().createQuery(sql, String.class); q.setParameter("uuid", ref.getResourceUuid()); List<String> eipUuids = q.getResultList(); if (eipUuids.isEmpty()) { logger.debug(String.format("Vm[uuid:%s] doesn't have any eip, there is no need to change owner of eip.", ref.getResourceUuid())); return; } for (String uuid : eipUuids) { acntMgr.changeResourceOwner(uuid, newOwnerUuid); } }
@Transactional(readOnly = true) private List<VmNicVO> getCandidateVmNic(String sgId, String accountUuid) { List<String> nicUuidsToInclude = acntMgr.getResourceUuidsCanAccessByAccount(accountUuid, VmNicVO.class); if (nicUuidsToInclude != null && nicUuidsToInclude.isEmpty()) { return new ArrayList<VmNicVO>();
@Override public String makeSubquery(APIQueryMessage msg, Class inventoryClass) { if (AccountConstant.INITIAL_SYSTEM_ADMIN_UUID.equals(msg.getSession().getAccountUuid()) && IdentityGlobalConfig.SHOW_ALL_RESOURCE_TO_ADMIN.value(Boolean.class)) { return null; } Class entityClass = QueryUtils.getEntityClassFromInventoryClass(inventoryClass); if (!acntMgr.isResourceHavingAccountReference(entityClass)) { return null; } String priKey = QueryUtils.getPrimaryKeyNameFromEntityClass(entityClass); String resourceType = acntMgr.getBaseResourceType(entityClass).getSimpleName(); return String.format("(%s.%s in (select accountresourcerefvo.resourceUuid from AccountResourceRefVO accountresourcerefvo where accountresourcerefvo.ownerAccountUuid = '%s'" + " and accountresourcerefvo.resourceType = '%s') or %s.%s in (select sharedresourcevo.resourceUuid from SharedResourceVO sharedresourcevo where" + " (sharedresourcevo.receiverAccountUuid = '%s' or sharedresourcevo.toPublic = 1) and sharedresourcevo.resourceType = '%s'))", inventoryClass.getSimpleName().toLowerCase(), priKey, msg.getSession().getAccountUuid(), resourceType, inventoryClass.getSimpleName().toLowerCase(), priKey, msg.getSession().getAccountUuid(), resourceType); } }
@Override public void check(RBACEntity entity) { if (acntMgr.isAdmin(entity.getApiMessage().getSession())) { return; } List<Quota> quotas = acntMgr.getMessageQuotaMap().get(entity.getApiMessage().getClass()); if (quotas == null || quotas.isEmpty()) { return; } quotas.forEach(quota -> { Map<String, Quota.QuotaPair> pairs = new QuotaUtil().makeQuotaPairs(entity.getApiMessage().getSession().getAccountUuid()); quota.getOperator().checkQuota(entity.getApiMessage(), pairs); if (quota.getQuotaValidators() != null) { for (Quota.QuotaValidator q : quota.getQuotaValidators()) { q.checkQuota(entity.getApiMessage(), pairs); } } }); } }
@Override public void beforeDeliveryMessage(Message msg) { if (msg instanceof NeedQuotaCheckMessage) { if (((NeedQuotaCheckMessage) msg).getAccountUuid() == null || ((NeedQuotaCheckMessage) msg).getAccountUuid().equals("")) { // skip admin scheduler return; } List<Quota> quotas = acntMgr.getMessageQuotaMap().get(msg.getClass()); if (quotas == null || quotas.size() == 0) { return; } Map<String, Quota.QuotaPair> pairs = new QuotaUtil(). makeQuotaPairs(((NeedQuotaCheckMessage) msg).getAccountUuid()); for (Quota quota : quotas) { quota.getOperator().checkQuota((NeedQuotaCheckMessage) msg, pairs); } } } }, VolumeCreateSnapshotMsg.class, CreateVolumeSnapshotMsg.class);
private void handle(APIGetAccountQuotaUsageMsg msg) { APIGetAccountQuotaUsageReply reply = new APIGetAccountQuotaUsageReply(); List<Quota> quotas = acntMgr.getQuotas(); List<Quota.QuotaUsage> usages = new ArrayList<Quota.QuotaUsage>();
@Override public APIMessage intercept(APIMessage msg) throws ApiMessageInterceptionException { // login, logout if (msg.getSession() == null) { return msg; } // skip admin. if needed, another quota check will be issued in AccountManagerImpl if (new QuotaUtil().isAdminAccount(msg.getSession().getAccountUuid())) { return msg; } List<Quota> quotas = acntMgr.getMessageQuotaMap().get(msg.getClass()); if (quotas == null || quotas.size() == 0) { return msg; } for (Quota q : quotas) { check(msg, q); } return msg; }
private static String getAccountUuid(String resourceUuid) { AccountManager acntMgr = Platform.getComponentLoader().getComponent(AccountManager.class); String accountUuid = acntMgr.getOwnerAccountUuidOfResource(resourceUuid); DebugUtils.Assert(accountUuid!=null, String.format("cannot find account uuid for resource[uuid:%s]", resourceUuid)); return accountUuid; }
private void changeVmCdRomsOwner(String vmInstanceUuid, String newOwnerUuid) { List<String> vmCdRomUuids = Q.New(VmCdRomVO.class) .select(VmCdRomVO_.uuid) .eq(VmCdRomVO_.vmInstanceUuid, vmInstanceUuid) .listValues(); if (vmCdRomUuids.isEmpty()) { return; } for (String uuid :vmCdRomUuids) { acntMgr.changeResourceOwner(uuid, newOwnerUuid); } }
@Transactional(readOnly = true) private List<L3NetworkInventory> getAttachableL3Network(String accountUuid) { List<String> l3Uuids = acntMgr.getResourceUuidsCanAccessByAccount(accountUuid, L3NetworkVO.class); if (l3Uuids != null && l3Uuids.isEmpty()) { return new ArrayList<L3NetworkInventory>();
public static String makeImageInstallPath(String imageUuid, String mediaType) { String root; String suffix; if (mediaType.equals(ImageMediaType.RootVolumeTemplate.toString())) { root = ROOT_VOLUME_TEMPLATE_FOLDER; suffix = "template"; } else if (mediaType.equals(ImageMediaType.ISO.toString())) { root = ISO_FOLDER; suffix = "iso"; } else if (mediaType.equals(ImageMediaType.DataVolumeTemplate.toString())) { root = DATA_VOLUME_TEMPLATE_FOLDER; suffix = "volume"; } else if (mediaType.equals(VolumeSnapshotVO.class.getSimpleName())) { root = VOLUME_SNAPSHOT_FOLDER; suffix = "snapshot"; } else { throw new CloudRuntimeException(String.format("unknown image mediaType[%s]", mediaType)); } String accountUuid = getAccountManager().getOwnerAccountUuidOfResource(imageUuid); String filename = String.format("%s.%s", imageUuid, suffix); return PathUtil.join(root, accountFolder(accountUuid), imageUuid, filename); }
private void changeVolumeSnapshotOwner(AccountResourceRefInventory ref, String newOwnerUuid) { SimpleQuery<VolumeSnapshotVO> q = dbf.createQuery(VolumeSnapshotVO.class); q.select(VolumeSnapshotVO_.uuid); q.add(VolumeSnapshotVO_.volumeUuid, Op.EQ, ref.getResourceUuid()); List<String> spUuids = q.listValue(); for (String spUuid : spUuids) { acntMgr.changeResourceOwner(spUuid, newOwnerUuid); } }
List<String> l3sFromAccount = acntMgr.getResourceUuidsCanAccessByAccount(accountUuid, L3NetworkVO.class); if (l3sFromAccount == null) { reply.setInventories(L3NetworkInventory.valueOf(l3s));
public static String makeVolumeInstallPath(String url, VolumeInventory vol) { String accountUuid = getAccountManager().getOwnerAccountUuidOfResource(vol.getUuid()); String fileName = makeFilename(url, "volume"); return PathUtil.join("volumes", accountFolder(accountUuid), vol.getUuid(), fileName); }
private void changeVolumeOwner(AccountResourceRefInventory ref, String newOwnerUuid) { SimpleQuery<VolumeVO> q = dbf.createQuery(VolumeVO.class); q.select(VolumeVO_.uuid); q.add(VolumeVO_.vmInstanceUuid, Op.EQ, ref.getResourceUuid()); List<String> uuids = q.listValue(); for (String uuid : uuids) { acntMgr.changeResourceOwner(uuid, newOwnerUuid); } }
return Collections.emptyList(); List<String> volUuids = acntMgr.getResourceUuidsCanAccessByAccount(accountUuid, VolumeVO.class); if (volUuids != null && volUuids.isEmpty()) { return Collections.emptyList();
public static String makeImageInstallPath(ImageInventory iminv) { String root; if (iminv.getMediaType().equals(ImageMediaType.RootVolumeTemplate.toString())) { root = ROOT_VOLUME_TEMPLATE_FOLDER; } else if (iminv.getMediaType().equals(ImageMediaType.ISO.toString())) { root = ISO_FOLDER; } else if (iminv.getMediaType().equals(ImageMediaType.DataVolumeTemplate.toString())) { root = DATA_VOLUME_TEMPLATE_FOLDER; } else { throw new CloudRuntimeException(String.format("unknown image mediaType[%s]", iminv.getMediaType())); } String accountUuid = getAccountManager().getOwnerAccountUuidOfResource(iminv.getUuid()); String filename = makeFilename(iminv.getUrl(), iminv.getMediaType()); return PathUtil.join(root, accountFolder(accountUuid), iminv.getUuid(), filename); }
@Override public void resourceOwnerAfterChange(AccountResourceRefInventory ref, String newOwnerUuid) { if (!VmInstanceVO.class.getSimpleName().equals(ref.getResourceType())) { return; } // change root volume SimpleQuery<VmInstanceVO> q = dbf.createQuery(VmInstanceVO.class); q.select(VmInstanceVO_.rootVolumeUuid); q.add(VmInstanceVO_.uuid, Op.EQ, ref.getResourceUuid()); String rootVolumeUuid = q.findValue(); if (rootVolumeUuid == null) { return; } acntMgr.changeResourceOwner(rootVolumeUuid, newOwnerUuid); // change vmnic(s) SimpleQuery<VmNicVO> sq = dbf.createQuery(VmNicVO.class); sq.select(VmNicVO_.uuid); sq.add(VmNicVO_.vmInstanceUuid, Op.EQ, ref.getResourceUuid()); List<String> vmnics = sq.listValue(); if (vmnics.isEmpty()) { return; } for (String vmnicUuid : vmnics) { acntMgr.changeResourceOwner(vmnicUuid, newOwnerUuid); } changeVmCdRomsOwner(ref.getResourceUuid(), newOwnerUuid); }
@Transactional private List<VmInstanceVO> getCandidateVmForAttaching(String accountUuid) { List<String> vmUuids = acntMgr.getResourceUuidsCanAccessByAccount(accountUuid, VmInstanceVO.class); if (vmUuids != null && vmUuids.isEmpty()) { return new ArrayList<>();