void checkAccountPermissions(String accountName, AccountOperation operation) throws ForbiddenException, NotFoundException, ServerException { if (accountName == null) { // default namespace will be used return; } final Account account = accountManager.getByName(accountName); AccountPermissionsChecker accountPermissionsChecker = accountTypeToPermissionsChecker.get(account.getType()); if (accountPermissionsChecker == null) { throw new ForbiddenException("User is not authorized to use specified namespace"); } accountPermissionsChecker.checkPermissions(account.getId(), operation); } }
private List<ResourceImpl> getDefaultResources(String accountId) throws NotFoundException, ServerException { List<ResourceImpl> defaultResources = new ArrayList<>(); final Account account = accountManager.getById(accountId); final DefaultResourcesProvider defaultResourcesProvider = defaultResourcesProviders.get(account.getType()); if (defaultResourcesProvider != null) { defaultResources.addAll(defaultResourcesProvider.getResources(accountId)); } return defaultResources; } }
@Transactional(rollbackOn = {RuntimeException.class, ApiException.class}) @Override public void remove(String id) throws ServerException, ConflictException { accountManager.remove(id); super.remove(id); } }
@Transactional(rollbackOn = {RuntimeException.class, ApiException.class}) @Override public User create(User newUser, boolean isTemporary) throws ConflictException, ServerException { User createdUser = super.create(newUser, isTemporary); accountManager.create( new AccountImpl(createdUser.getId(), createdUser.getName(), PERSONAL_ACCOUNT)); return createdUser; }
@Transactional(rollbackOn = {RuntimeException.class, ApiException.class}) @Override public void update(User user) throws NotFoundException, ServerException, ConflictException { User originalUser = getById(user.getId()); if (!originalUser.getName().equals(user.getName())) { accountManager.update(new AccountImpl(user.getId(), user.getName(), PERSONAL_ACCOUNT)); } super.update(user); }
/** * Asynchronously starts workspace from the given configuration. * * @param config workspace configuration from which workspace is created and started * @param namespace workspace name is unique in this namespace * @return starting workspace * @throws NullPointerException when {@code workspaceId} is null * @throws NotFoundException when workspace with given {@code workspaceId} doesn't exist * @throws ServerException when any other error occurs during workspace start */ public WorkspaceImpl startWorkspace( WorkspaceConfig config, String namespace, boolean isTemporary, Map<String, String> options) throws ServerException, NotFoundException, ConflictException, ValidationException { requireNonNull(config, "Required non-null configuration"); requireNonNull(namespace, "Required non-null namespace"); validator.validateConfig(config); final WorkspaceImpl workspace = doCreateWorkspace( config, accountManager.getByName(namespace), Collections.emptyMap(), isTemporary); startAsync(workspace, workspace.getConfig().getDefaultEnv(), options); return normalizeState(workspace, true); }
@Override public Optional<Resource> getUsedResource(String accountId) throws NotFoundException, ServerException { final Account account = accountManager.getById(accountId); final List<WorkspaceImpl> accountWorkspaces = Pages.stream( (maxItems, skipCount) -> workspaceManagerProvider .get() .getByNamespace(account.getName(), false, maxItems, skipCount)) .collect(Collectors.toList()); if (!accountWorkspaces.isEmpty()) { return Optional.of( new ResourceImpl( WorkspaceResourceType.ID, accountWorkspaces.size(), WorkspaceResourceType.UNIT)); } else { return Optional.empty(); } } }
/** * Creates a new {@link Workspace} instance based on the given configuration and the instance * attributes. * * @param config the workspace config to create the new workspace instance * @param namespace workspace name is unique in this namespace * @param attributes workspace instance attributes * @return new workspace instance * @throws NullPointerException when either {@code config} or {@code namespace} is null * @throws NotFoundException when account with given id was not found * @throws ConflictException when any conflict occurs (e.g Workspace with such name already exists * for {@code owner}) * @throws ServerException when any other error occurs * @throws ValidationException when incoming configuration or attributes are not valid */ @Traced public WorkspaceImpl createWorkspace( WorkspaceConfig config, String namespace, @Nullable Map<String, String> attributes) throws ServerException, NotFoundException, ConflictException, ValidationException { requireNonNull(config, "Required non-null config"); requireNonNull(namespace, "Required non-null namespace"); validator.validateConfig(config); if (attributes != null) { validator.validateAttributes(attributes); } return doCreateWorkspace(config, accountManager.getByName(namespace), attributes, false); }
/** * Returns list of resources which are available for usage by given account. * * @param accountId id of account * @return list of resources which are available for usage by given account * @throws NotFoundException when account with specified id was not found * @throws ServerException when some exception occurred while resources fetching */ public List<? extends Resource> getAvailableResources(String accountId) throws NotFoundException, ServerException { final Account account = accountManager.getById(accountId); final AvailableResourcesProvider availableResourcesProvider = accountTypeToAvailableResourcesProvider.get(account.getType()); if (availableResourcesProvider == null) { return defaultAvailableResourcesProvider.getAvailableResources(accountId); } return availableResourcesProvider.getAvailableResources(accountId); }
@Override public WorkspaceImpl createWorkspace( WorkspaceConfig config, String namespace, @Nullable Map<String, String> attributes) throws ServerException, ConflictException, NotFoundException, ValidationException { checkMaxEnvironmentRam(config); String accountId = accountManager.getByName(namespace).getId(); try (@SuppressWarnings("unused") Unlocker u = resourcesLocks.lock(accountId)) { checkWorkspaceResourceAvailability(accountId); return super.createWorkspace(config, namespace, attributes); } }
@Override public Optional<Resource> getUsedResource(String accountId) throws NotFoundException, ServerException { final Account account = accountManager.getById(accountId); final long currentlyUsedRuntimes = Pages.stream( (maxItems, skipCount) -> workspaceManagerProvider .get() .getByNamespace(account.getName(), false, maxItems, skipCount)) .filter(ws -> STOPPED != ws.getStatus()) .count(); if (currentlyUsedRuntimes > 0) { return Optional.of( new ResourceImpl( RuntimeResourceType.ID, currentlyUsedRuntimes, RuntimeResourceType.UNIT)); } else { return Optional.empty(); } } }
@Override public WorkspaceImpl startWorkspace( WorkspaceConfig config, String namespace, boolean isTemporary, Map<String, String> options) throws ServerException, NotFoundException, ConflictException, ValidationException { checkMaxEnvironmentRam(config); String accountId = accountManager.getByName(namespace).getId(); try (@SuppressWarnings("unused") Unlocker u = resourcesLocks.lock(accountId)) { checkWorkspaceResourceAvailability(accountId); checkRuntimeResourceAvailability(accountId); checkRamResourcesAvailability(accountId, namespace, config, null); return super.startWorkspace(config, namespace, isTemporary, options); } }
/** * Acquire resources lock for specified account. * * @param accountId account id to lock resources * @return lock for unlocking resources when resources operation finishes * @throws NotFoundException when account with specified {@code account id} was not found * @throws ServerException when any other error occurs */ public Unlocker lock(String accountId) throws NotFoundException, ServerException { final Account account = accountManager.getById(accountId); final ResourceLockKeyProvider resourceLockKeyProvider = accountTypeToLockProvider.get(account.getType()); String lockKey; if (resourceLockKeyProvider == null) { // this account type doesn't have custom lock provider. // Lock resources by current account lockKey = accountId; } else { lockKey = resourceLockKeyProvider.getLockKey(accountId); } return stripedLocks.writeLock(lockKey); }
@Override public Optional<Resource> getUsedResource(String accountId) throws NotFoundException, ServerException { final Account account = accountManager.getById(accountId); List<WorkspaceImpl> activeWorkspaces = Pages.stream(