@Transactional protected WorkspaceImpl doUpdate(WorkspaceImpl update) throws NotFoundException { EntityManager manager = managerProvider.get(); if (manager.find(WorkspaceImpl.class, update.getId()) == null) { throw new NotFoundException(format("Workspace with id '%s' doesn't exist", update.getId())); } if (update.getConfig() != null) { update.getConfig().getProjects().forEach(ProjectConfigImpl::prePersistAttributes); } WorkspaceImpl merged = manager.merge(update); manager.flush(); return merged; }
@Transactional protected WorkspaceImpl doUpdate(WorkspaceImpl update) throws NotFoundException { EntityManager manager = managerProvider.get(); if (manager.find(WorkspaceImpl.class, update.getId()) == null) { throw new NotFoundException(format("Workspace with id '%s' doesn't exist", update.getId())); } if (update.getConfig() != null) { update.getConfig().getProjects().forEach(ProjectConfigImpl::prePersistAttributes); } WorkspaceImpl merged = manager.merge(update); manager.flush(); return merged; }
@DELETE @Path("/{id}/environment/{name}") @ApiOperation( value = "Remove the environment from the workspace", notes = "This operation can be performed only by the workspace owner") @ApiResponses({ @ApiResponse(code = 204, message = "The environment successfully removed"), @ApiResponse(code = 403, message = "The user does not have access remove the environment"), @ApiResponse(code = 404, message = "The workspace not found"), @ApiResponse(code = 500, message = "Internal server error occurred") }) public void deleteEnvironment( @ApiParam("The workspace id") @PathParam("id") String id, @ApiParam("The name of the environment") @PathParam("name") String envName) throws ServerException, BadRequestException, NotFoundException, ConflictException, ForbiddenException { final WorkspaceImpl workspace = workspaceManager.getWorkspace(id); if (workspace.getConfig().getEnvironments().remove(envName) != null) { doUpdate(id, workspace); } }
@DELETE @Path("/{id}/project/{path:.*}") @ApiOperation( value = "Remove the project from the workspace", notes = "This operation can be performed only by the workspace owner") @ApiResponses({ @ApiResponse(code = 204, message = "The project successfully removed"), @ApiResponse(code = 403, message = "The user does not have access remove the project"), @ApiResponse(code = 404, message = "The workspace not found"), @ApiResponse(code = 500, message = "Internal server error occurred") }) public void deleteProject( @ApiParam("The workspace id") @PathParam("id") String id, @ApiParam("The name of the project to remove") @PathParam("path") String path) throws ServerException, BadRequestException, NotFoundException, ConflictException, ForbiddenException { final WorkspaceImpl workspace = workspaceManager.getWorkspace(id); final String normalizedPath = path.startsWith("/") ? path : '/' + path; if (workspace .getConfig() .getProjects() .removeIf(project -> project.getPath().equals(normalizedPath))) { doUpdate(id, workspace); } }
@DELETE @Path("/{id}/command/{name}") @ApiOperation( value = "Remove the command from the workspace", notes = "This operation can be performed only by the workspace owner") @ApiResponses({ @ApiResponse(code = 204, message = "The command successfully removed"), @ApiResponse(code = 403, message = "The user does not have access delete the command"), @ApiResponse(code = 404, message = "The workspace not found"), @ApiResponse(code = 500, message = "Internal server error occurred") }) public void deleteCommand( @ApiParam("The id of the workspace") @PathParam("id") String id, @ApiParam("The name of the command to remove") @PathParam("name") String commandName) throws ServerException, BadRequestException, NotFoundException, ConflictException, ForbiddenException { WorkspaceImpl workspace = workspaceManager.getWorkspace(id); if (workspace .getConfig() .getCommands() .removeIf(command -> command.getName().equals(commandName))) { doUpdate(id, workspace); } }
@POST @Path("/{id}/command") @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) @ApiOperation( value = "Update the workspace by adding a new command to it", notes = "This operation can be performed only by the workspace owner") @ApiResponses({ @ApiResponse(code = 200, message = "The workspace successfully updated"), @ApiResponse(code = 400, message = "Missed required parameters, parameters are not valid"), @ApiResponse(code = 403, message = "The user does not have access to update the workspace"), @ApiResponse(code = 404, message = "The workspace not found"), @ApiResponse(code = 409, message = "The command with such name already exists"), @ApiResponse(code = 500, message = "Internal server error occurred") }) public WorkspaceDto addCommand( @ApiParam("The workspace id") @PathParam("id") String id, @ApiParam(value = "The new workspace command", required = true) CommandDto newCommand) throws ServerException, BadRequestException, NotFoundException, ConflictException, ForbiddenException { requiredNotNull(newCommand, "Command"); WorkspaceImpl workspace = workspaceManager.getWorkspace(id); workspace.getConfig().getCommands().add(new CommandImpl(newCommand)); return asDtoWithLinksAndToken(doUpdate(id, workspace)); }
@POST @Path("/{id}/project") @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) @ApiOperation( value = "Adds a new project to the workspace", notes = "This operation can be performed only by the workspace owner") @ApiResponses({ @ApiResponse(code = 200, message = "The project successfully added to the workspace"), @ApiResponse(code = 400, message = "Missed required parameters, parameters are not valid"), @ApiResponse(code = 403, message = "The user does not have access to add the project"), @ApiResponse(code = 404, message = "The workspace not found"), @ApiResponse(code = 409, message = "Any conflict error occurs"), @ApiResponse(code = 500, message = "Internal server error occurred") }) public WorkspaceDto addProject( @ApiParam("The workspace id") @PathParam("id") String id, @ApiParam(value = "The new project", required = true) ProjectConfigDto newProject) throws ServerException, BadRequestException, NotFoundException, ConflictException, ForbiddenException { requiredNotNull(newProject, "New project config"); final WorkspaceImpl workspace = workspaceManager.getWorkspace(id); workspace.getConfig().getProjects().add(new ProjectConfigImpl(newProject)); return asDtoWithLinksAndToken(doUpdate(id, workspace)); }
@POST @Path("/{id}/environment") @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) @ApiOperation( value = "Add a new environment to the workspace", notes = "This operation can be performed only by the workspace owner") @ApiResponses({ @ApiResponse(code = 200, message = "The workspace successfully updated"), @ApiResponse(code = 400, message = "Missed required parameters, parameters are not valid"), @ApiResponse(code = 403, message = "The user does not have access to add the environment"), @ApiResponse(code = 404, message = "The workspace not found"), @ApiResponse(code = 409, message = "Environment with such name already exists"), @ApiResponse(code = 500, message = "Internal server error occurred") }) public WorkspaceDto addEnvironment( @ApiParam("The workspace id") @PathParam("id") String id, @ApiParam(value = "The new environment", required = true) EnvironmentDto newEnvironment, @ApiParam(value = "The name of the environment", required = true) @QueryParam("name") String envName) throws ServerException, BadRequestException, NotFoundException, ConflictException, ForbiddenException { requiredNotNull(newEnvironment, "New environment"); requiredNotNull(envName, "New environment name"); relativizeRecipeLinks(newEnvironment); WorkspaceImpl workspace = workspaceManager.getWorkspace(id); workspace.getConfig().getEnvironments().put(envName, new EnvironmentImpl(newEnvironment)); return asDtoWithLinksAndToken(doUpdate(id, workspace)); }
private String getValidatedEnvironmentName(WorkspaceImpl workspace, @Nullable String envName) throws NotFoundException, ServerException { if (envName != null && !workspace.getConfig().getEnvironments().containsKey(envName)) { throw new NotFoundException( format( "Workspace '%s:%s' doesn't contain environment '%s'", workspace.getNamespace(), workspace.getConfig().getName(), envName)); } envName = firstNonNull(envName, workspace.getConfig().getDefaultEnv()); if (envName == null && SidecarToolingWorkspaceUtil.isSidecarBasedWorkspace( workspace.getConfig().getAttributes())) { // Sidecar-based workspaces are allowed not to have any environments return null; } // validate environment in advance if (envName == null) { throw new NotFoundException( format( "Workspace %s:%s can't use null environment", workspace.getNamespace(), workspace.getConfig().getName())); } try { runtimes.validate(workspace.getConfig().getEnvironments().get(envName)); } catch (InfrastructureException | ValidationException e) { throw new ServerException(e); } return envName; }
@Override public WorkspaceImpl create(WorkspaceImpl workspace) throws ConflictException, ServerException { requireNonNull(workspace, "Required non-null workspace"); try { doCreate(workspace); } catch (DuplicateKeyException dkEx) { throw new ConflictException( format( "Workspace with id '%s' or name '%s' in namespace '%s' already exists", workspace.getId(), workspace.getConfig().getName(), workspace.getNamespace())); } catch (RuntimeException x) { throw new ServerException(x.getMessage(), x); } return new WorkspaceImpl(workspace); }
@Override public WorkspaceImpl update(WorkspaceImpl update) throws NotFoundException, ConflictException, ServerException { requireNonNull(update, "Required non-null update"); try { return new WorkspaceImpl(doUpdate(update)); } catch (DuplicateKeyException dkEx) { throw new ConflictException( format( "Workspace with name '%s' in namespace '%s' already exists", update.getConfig().getName(), update.getNamespace())); } catch (RuntimeException x) { throw new ServerException(x.getMessage(), x); } }
@Override public WorkspaceImpl update(WorkspaceImpl update) throws NotFoundException, ConflictException, ServerException { requireNonNull(update, "Required non-null update"); try { return new WorkspaceImpl(doUpdate(update)); } catch (DuplicateKeyException dkEx) { throw new ConflictException( format( "Workspace with name '%s' in namespace '%s' already exists", update.getConfig().getName(), update.getNamespace())); } catch (RuntimeException x) { throw new ServerException(x.getMessage(), x); } }
@Override public WorkspaceImpl create(WorkspaceImpl workspace) throws ConflictException, ServerException { requireNonNull(workspace, "Required non-null workspace"); try { doCreate(workspace); } catch (DuplicateKeyException dkEx) { throw new ConflictException( format( "Workspace with id '%s' or name '%s' in namespace '%s' already exists", workspace.getId(), workspace.getConfig().getName(), workspace.getNamespace())); } catch (RuntimeException x) { throw new ServerException(x.getMessage(), x); } return new WorkspaceImpl(workspace); }
/** * 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); }
private void checkWorkspaceIsRunningOrStarting(WorkspaceImpl workspace) throws ConflictException { if (workspace.getStatus() != RUNNING && workspace.getStatus() != STARTING) { throw new ConflictException( format( "Could not stop the workspace '%s/%s' because its status is '%s'.", workspace.getNamespace(), workspace.getConfig().getName(), workspace.getStatus())); } }
@Override public Pair<String, String> get(RuntimeIdentity runtimeIdentity) throws InfrastructureException { try { WorkspaceImpl workspace = workspaceDao.get(runtimeIdentity.getWorkspaceId()); return Pair.of(CHE_WORKSPACE_NAME, workspace.getConfig().getName()); } catch (NotFoundException | ServerException e) { throw new InfrastructureException( "Not able to get workspace name for workspace with id " + runtimeIdentity.getWorkspaceId(), e); } } }
private WorkspaceImpl doCreateWorkspace( WorkspaceConfig config, Account account, Map<String, String> attributes, boolean isTemporary) throws NotFoundException, ServerException, ConflictException { WorkspaceImpl workspace = WorkspaceImpl.builder() .generateId() .setConfig(config) .setAccount(account) .setAttributes(attributes) .setTemporary(isTemporary) .setStatus(STOPPED) .build(); workspace.getAttributes().put(CREATED_ATTRIBUTE_NAME, Long.toString(currentTimeMillis())); workspaceDao.create(workspace); LOG.info( "Workspace '{}/{}' with id '{}' created by user '{}'", account.getName(), workspace.getConfig().getName(), workspace.getId(), sessionUserNameOrUndefined()); eventService.publish(new WorkspaceCreatedEvent(workspace)); return workspace; }
@Override public WorkspaceImpl startWorkspace( String workspaceId, @Nullable String envName, @Nullable Map<String, String> options) throws NotFoundException, ServerException, ConflictException { WorkspaceImpl workspace = this.getWorkspace(workspaceId); String accountId = workspace.getAccount().getId(); try (@SuppressWarnings("unused") Unlocker u = resourcesLocks.lock(accountId)) { checkRuntimeResourceAvailability(accountId); checkRamResourcesAvailability( accountId, workspace.getNamespace(), workspace.getConfig(), envName); return super.startWorkspace(workspaceId, envName, options); } }