WorkspaceService extends Service { @DELETE @Path("/{id}") @ApiOperation( value = "Removes the workspace", notes = "This operation can be performed only by the workspace owner") @ApiResponses({ @ApiResponse(code = 204, message = "The workspace successfully removed"), @ApiResponse(code = 403, message = "The user does not have access to remove the workspace"), @ApiResponse(code = 404, message = "The workspace doesn't exist"), @ApiResponse(code = 409, message = "The workspace is not stopped(has runtime)"), @ApiResponse(code = 500, message = "Internal server error occurred") }) public void delete(@ApiParam("The workspace id") @PathParam("id") String id) throws BadRequestException, ServerException, NotFoundException, ConflictException, ForbiddenException { workspaceManager.removeWorkspace(id); } @Inject  WorkspaceService(
      @Named("che.api") String apiEndpoint,
      @Named(CHE_WORKSPACE_AUTO_START) boolean cheWorkspaceAutoStart,
      WorkspaceManager workspaceManager,
      MachineTokenProvider machineTokenProvider,
      WorkspaceLinksGenerator linksGenerator,
      @Named(CHE_WORKSPACE_PLUGIN_REGISTRY_URL_PROPERTY) @Nullable String pluginRegistryUrl,
      @Named(CHE_WORKSPACE_DEVFILE_REGISTRY_URL_PROPERTY) @Nullable String devfileRegistryUrl,
      URLFetcher urlFetcher,
      @Named(DEBUG_WORKSPACE_START_LOG_LIMIT_BYTES) Long logLimitBytes,
      @Named(CHE_WORKSPACE_STORAGE_AVAILABLE_TYPES) String availableStorageTypes,
      @Named(CHE_WORKSPACE_STORAGE_PREFERRED_TYPE) String preferredStorageType); @Path("/devfile") @POST @Consumes({APPLICATION_JSON, "text/yaml", "text/x-yaml"}) @Produces(APPLICATION_JSON) @ApiOperation( value = "Creates a new workspace based on the Devfile.", consumes = "application/json, text/yaml, text/x-yaml", produces = APPLICATION_JSON, nickname = "createFromDevfile", response = WorkspaceDto.class) @ApiResponses({ @ApiResponse(code = 201, message = "The workspace successfully created"), @ApiResponse(code = 400, message = "Missed required parameters, parameters are not valid"), @ApiResponse(code = 403, message = "The user does not have access to create a new workspace"), @ApiResponse( code = 409, message = "Conflict error occurred during the workspace creation" + "(e.g. The workspace with such name already exists)"), @ApiResponse(code = 500, message = "Internal server error occurred") }) Response create(
      @ApiParam(value = "The devfile of the workspace to create", required = true)
          DevfileDto devfile,
      @ApiParam(
              value =
                  "Workspace attribute defined in 'attrName:attrValue' format. "
                      + "The first ':' is considered as attribute name and value separator",
              examples = @Example({@ExampleProperty("attrName:value-with:colon")}))
          @QueryParam("attribute")
          List<String> attrsList,
      @ApiParam(
              value =
                  "The target infrastructure namespace (Kubernetes namespace or OpenShift"
                      + " project) where the workspace should be deployed to when started. This"
                      + " parameter is optional. The workspace creation will fail if the Che server"
                      + " is configured to not allow deploying into that infrastructure namespace.")
          @QueryParam("infrastructure-namespace")
          String infrastructureNamespace,
      @ApiParam(
              "If true then the workspace will be immediately "
                  + "started after it is successfully created")
          @QueryParam("start-after-create")
          @DefaultValue("false")
          Boolean startAfterCreate,
      @ApiParam("Che namespace where workspace should be created") @QueryParam("namespace")
          String namespace,
      @HeaderParam(CONTENT_TYPE) MediaType contentType); @GET @Path("/{key:.*}") @Produces(APPLICATION_JSON) @ApiOperation( value = "Get the workspace by the composite key", notes = "Composite key can be just workspace ID or in the " + "namespace:workspace_name form, where namespace is optional (e.g :workspace_name is valid key too." + "namespace/workspace_name form, where namespace can contain '/' character.") @ApiResponses({ @ApiResponse(code = 200, message = "The response contains requested workspace entity"), @ApiResponse(code = 404, message = "The workspace with specified id does not exist"), @ApiResponse(code = 403, message = "The user is not workspace owner"), @ApiResponse(code = 500, message = "Internal server error occurred") }) WorkspaceDto getByKey(
      @ApiParam(
              value = "Composite key",
              examples =
                  @Example({
                    @ExampleProperty("workspace12345678"),
                    @ExampleProperty("namespace/workspace_name"),
                    @ExampleProperty("namespace_part_1/namespace_part_2/workspace_name")
                  }))
          @PathParam("key")
          String key,
      @ApiParam("Whether to include internal servers into runtime or not")
          @DefaultValue("false")
          @QueryParam("includeInternalServers")
          String includeInternalServers); @GET @Produces(APPLICATION_JSON) @ApiOperation( value = "Get workspaces which user can read", notes = "This operation can be performed only by authorized user", response = WorkspaceDto.class, responseContainer = "List") @ApiResponses({ @ApiResponse(code = 200, message = "The workspaces successfully fetched"), @ApiResponse(code = 500, message = "Internal server error occurred during workspaces fetching") }) Response getWorkspaces(
      @ApiParam("The number of the items to skip") @DefaultValue("0") @QueryParam("skipCount")
          Integer skipCount,
      @ApiParam("The limit of the items in the response, default is 30")
          @DefaultValue("30")
          @QueryParam("maxItems")
          Integer maxItems,
      @ApiParam("Workspace status") @QueryParam("status") String status); @GET @Path("/namespace/{namespace:.*}") @Produces(APPLICATION_JSON) @ApiOperation( value = "Get workspaces by given namespace", notes = "This operation can be performed only by authorized user", response = WorkspaceDto.class, responseContainer = "List") @ApiResponses({ @ApiResponse(code = 200, message = "The workspaces successfully fetched"), @ApiResponse(code = 500, message = "Internal server error occurred during workspaces fetching") }) List<WorkspaceDto> getByNamespace(
      @ApiParam("Workspace status") @QueryParam("status") String status,
      @ApiParam("The namespace") @PathParam("namespace") String namespace); @PUT @Path("/{id}") @Consumes(APPLICATION_JSON) @Produces(APPLICATION_JSON) @ApiOperation( value = "Update the workspace by replacing all the existing data with update", 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 = 409, message = "Conflict error occurred during workspace update" + "(e.g. Workspace with such name already exists)"), @ApiResponse(code = 500, message = "Internal server error occurred") }) WorkspaceDto update(
      @ApiParam("The workspace id") @PathParam("id") String id,
      @ApiParam(value = "The workspace update", required = true) WorkspaceDto update); @DELETE @Path("/{id}") @ApiOperation( value = "Removes the workspace", notes = "This operation can be performed only by the workspace owner") @ApiResponses({ @ApiResponse(code = 204, message = "The workspace successfully removed"), @ApiResponse(code = 403, message = "The user does not have access to remove the workspace"), @ApiResponse(code = 404, message = "The workspace doesn't exist"), @ApiResponse(code = 409, message = "The workspace is not stopped(has runtime)"), @ApiResponse(code = 500, message = "Internal server error occurred") }) void delete(@ApiParam("The workspace id") @PathParam("id") String id); @POST @Path("/{id}/runtime") @Produces(APPLICATION_JSON) @ApiOperation( value = "Start the workspace by the id", notes = "This operation can be performed only by the workspace owner." + "The workspace starts asynchronously") @ApiResponses({ @ApiResponse(code = 200, message = "The workspace is starting"), @ApiResponse(code = 404, message = "The workspace with specified id doesn't exist"), @ApiResponse( code = 403, message = "The user is not workspace owner." + "The operation is not allowed for the user"), @ApiResponse(code = 409, message = "Any conflict occurs during the workspace start"), @ApiResponse(code = 500, message = "Internal server error occurred") }) WorkspaceDto startById(
      @ApiParam("The workspace id") @PathParam("id") String workspaceId,
      @ApiParam("The name of the workspace environment that should be used for start")
          @QueryParam("environment")
          String envName,
      @QueryParam(DEBUG_WORKSPACE_START) @DefaultValue("false") Boolean debugWorkspaceStart); @DELETE @Path("/{id}/runtime") @ApiOperation( value = "Stop the workspace", notes = "This operation can be performed only by the workspace owner." + "The workspace stops asynchronously") @ApiResponses({ @ApiResponse(code = 204, message = "The workspace is stopping"), @ApiResponse(code = 404, message = "The workspace with specified id doesn't exist"), @ApiResponse(code = 403, message = "The user is not workspace owner"), @ApiResponse(code = 500, message = "Internal server error occurred") }) void stop(@ApiParam("The workspace id") @PathParam("id") String id); @GET @Path("/settings") @Produces(APPLICATION_JSON) @ApiOperation(value = "Get workspace server configuration values") @ApiResponses({@ApiResponse(code = 200, message = "The response contains server settings")}) Map<String, String> getSettings();  }
@Test public void shouldDeleteWorkspace() throws Exception { final WorkspaceImpl workspace = createWorkspace(createConfigDto()); when(wsManager.getWorkspace(workspace.getId())).thenReturn(workspace); final Response response = given() .auth() .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD) .when() .delete(SECURE_PATH + "/workspace/" + workspace.getId()); assertEquals(response.getStatusCode(), 204); verify(wsManager).removeWorkspace(workspace.getId()); }
@Test public void shouldStopWorkspace() throws Exception { final WorkspaceImpl workspace = createWorkspace(createConfigDto()); when(wsManager.getWorkspace(workspace.getId())).thenReturn(workspace); final Response response = given() .auth() .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD) .when() .delete(SECURE_PATH + "/workspace/" + workspace.getId() + "/runtime"); assertEquals(response.getStatusCode(), 204); verify(wsManager).stopWorkspace(workspace.getId(), emptyMap()); }