Skip to content

Commit

Permalink
feat(rest): Create new endpoint for schedule CVE and schedule attachm…
Browse files Browse the repository at this point in the history
…ent deletion.

Signed-off-by: Nikesh kumar <kumar.nikesh@simens.com>
  • Loading branch information
Nikesh kumar committed Jan 4, 2024
1 parent 47d14b1 commit 51f957f
Show file tree
Hide file tree
Showing 6 changed files with 420 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rest/resource-server/src/docs/asciidoc/api-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,4 @@ include::clearingRequests.adoc[]
include::obligations.adoc[]
include::moderationRequests.adoc[]
include::fossology.adoc[]

include::schedule.adoc[]
81 changes: 81 additions & 0 deletions rest/resource-server/src/docs/asciidoc/schedule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// Copyright Siemens AG, 2023. Part of the SW360 Portal Project.
//
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//


[[resources-schedule]]
=== Schedule

The Schedule resource is used to get and list the Schedule requests.

[[schedule-cve]]
==== schedule cve service

A `POST` request will schedule the cve service.

===== Example request
include::{snippets}/should_document_schedule_cve_service/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_schedule_cve_service/http-response.adoc[]

[[unschedule-cve]]
==== unschedule cve search

A `DELETE` request will unschedule the cve search.

===== Example request
include::{snippets}/should_document_unschedule_cve_search/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_unschedule_cve_search/http-response.adoc[]

[[schedule-service]]
==== schedule service for attachment deletion from local fs.

A `POST` request will schedule attachment deletion.

===== Example request
include::{snippets}/should_document_schedule_service_from_local/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_schedule_service_from_local/http-response.adoc[]

[[cancel-schedule]]
==== cancel schedule attachment from local fs

A `DELETE` request will schedule attachment deletion.

===== Example request
include::{snippets}/should_document_cancel_schedule_attachment/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_cancel_schedule_attachment/http-response.adoc[]

[[delete-attachment]]
==== delete old attachment from local fs

A `DELETE` request will schedule attachment deletion.

===== Example request
include::{snippets}/should_document_delete_old_attachment_from_local/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_delete_old_attachment_from_local/http-response.adoc[]

[[cve-search]]
==== schedule cve search

A `POST` request will schedule the cve search.

===== Example request
include::{snippets}/should_document_schedule_cve_search/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_schedule_cve_search/http-response.adoc[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright Siemens AG, 2023-2024.
* Part of the SW360 Portal Project.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.eclipse.sw360.rest.resourceserver.schedule;

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;

import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
import org.eclipse.sw360.datahandler.thrift.RequestSummary;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.rest.resourceserver.core.RestControllerHelper;
import org.eclipse.sw360.rest.resourceserver.license.LicenseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.webmvc.BasePathAwareController;
import org.springframework.data.rest.webmvc.RepositoryLinksResource;
import org.springframework.hateoas.server.RepresentationModelProcessor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@BasePathAwareController
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RestController
@SecurityRequirement(name = "tokenAuth")
public class ScheduleAdminController implements RepresentationModelProcessor<RepositoryLinksResource> {
public static final String SCHEDULE_URL = "/schedule";

@NonNull
private final RestControllerHelper restControllerHelper;

@NonNull
private Sw360ScheduleService scheduleService;


@Override
public RepositoryLinksResource process(RepositoryLinksResource resource) {
resource.add(linkTo(ScheduleAdminController.class).slash("api/schedule").withRel("schedule"));
return resource;
}

@RequestMapping(value = SCHEDULE_URL + "/cveService", method = RequestMethod.POST)
public ResponseEntity<?> scheduleCve()throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
RequestSummary requestSummary = scheduleService.scheduleCveSearch(sw360User);
HttpStatus status = HttpStatus.OK;
return new ResponseEntity<>(requestSummary, status);
}

@RequestMapping(value = SCHEDULE_URL + "/unscheduleCve", method = RequestMethod.DELETE)
public ResponseEntity<?> unscheduleCveSearch()throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
RequestStatus requestStatus = scheduleService.cancleCveSearch(sw360User);
HttpStatus status = HttpStatus.OK;
return new ResponseEntity<>(requestStatus, status);
}

@RequestMapping(value = SCHEDULE_URL + "/deleteAttachment", method = RequestMethod.POST)
public ResponseEntity<?> scheduleDeleteAttachment()throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
RequestSummary requestSummary = scheduleService.deleteAttachmentService(sw360User);
HttpStatus status = HttpStatus.OK;
return new ResponseEntity<>(requestSummary, status);
}

@RequestMapping(value = SCHEDULE_URL + "/unscheduleDelete", method = RequestMethod.DELETE)
public ResponseEntity<?> unscheduleDeleteAttachment()throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
RequestStatus requestStatus = scheduleService.cancleDeleteAttachment(sw360User);
HttpStatus status = HttpStatus.OK;
return new ResponseEntity<>(requestStatus, status);
}

@RequestMapping(value = SCHEDULE_URL + "/attachmentDelete", method = RequestMethod.DELETE)
public ResponseEntity<?> attachmentDeleteLocalFS()throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
RequestStatus requestStatus = scheduleService.canclettachmentDeleteLocalFS(sw360User);
HttpStatus status = HttpStatus.OK;
return new ResponseEntity<>(requestStatus, status);
}

@RequestMapping(value = SCHEDULE_URL + "/cveSearch", method = RequestMethod.POST)
public ResponseEntity<?> CveAllSearch()throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
RequestStatus requestStatus = scheduleService.triggerCveSearch(sw360User);
HttpStatus status = HttpStatus.OK;
return new ResponseEntity<>(requestStatus, status);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright Siemens AG, 2023-2024.
* Part of the SW360 Portal Project.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.eclipse.sw360.rest.resourceserver.schedule;

import org.apache.thrift.TException;
import org.eclipse.sw360.datahandler.permissions.PermissionUtils;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
import org.eclipse.sw360.datahandler.thrift.RequestSummary;
import org.eclipse.sw360.datahandler.thrift.ThriftClients;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.datahandler.thrift.users.UserGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class Sw360ScheduleService {

public RequestSummary scheduleCveSearch(User sw360User) throws TException{
String serviceName = ThriftClients.CVESEARCH_SERVICE;
try {
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
RequestSummary requestSummary = new ThriftClients().makeScheduleClient().scheduleService(serviceName);
return requestSummary;
} else {
throw new HttpMessageNotReadableException("User is not admin");
}
} catch (TException e) {
throw new TException(e.getMessage());
}
}

public RequestStatus cancleCveSearch(User sw360User) throws TException{
String serviceName = ThriftClients.CVESEARCH_SERVICE;
try {
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
RequestStatus requestStatus = new ThriftClients().makeScheduleClient().unscheduleService(serviceName, sw360User);
return requestStatus;
} else {
throw new HttpMessageNotReadableException("User is not admin");
}
} catch (TException e) {
throw new TException(e.getMessage());
}
}

public RequestSummary deleteAttachmentService(User sw360User) throws TException{
try {
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
RequestSummary requestSummary = new ThriftClients().makeScheduleClient().scheduleService(ThriftClients.DELETE_ATTACHMENT_SERVICE);
return requestSummary;
} else {
throw new HttpMessageNotReadableException("User is not admin");
}
} catch (TException e) {
throw new TException(e.getMessage());
}
}

public RequestStatus cancleDeleteAttachment(User sw360User) throws TException{
String serviceName = ThriftClients.DELETE_ATTACHMENT_SERVICE;
try {
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
RequestStatus requestStatus = new ThriftClients().makeScheduleClient().unscheduleService(serviceName, sw360User);
return requestStatus;
} else {
throw new HttpMessageNotReadableException("User is not admin");
}
} catch (TException e) {
throw new TException(e.getMessage());
}
}

public RequestStatus canclettachmentDeleteLocalFS(User sw360User) throws TException {
try {
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
RequestStatus requestStatus = new ThriftClients().makeAttachmentClient().deleteOldAttachmentFromFileSystem();
return requestStatus;
} else {
throw new HttpMessageNotReadableException("User is not admin");
}
} catch (TException e) {
throw new TException(e.getMessage());
}
}

public RequestStatus triggerCveSearch(User sw360User) throws TException {
try {
if (PermissionUtils.isUserAtLeast(UserGroup.ADMIN, sw360User)) {
RequestStatus requestStatus = new ThriftClients().makeCvesearchClient().update();
return requestStatus;
} else {
throw new HttpMessageNotReadableException("User is not admin");
}
} catch (TException e) {
throw new TException(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public void should_document_index() throws Exception {
linkWithRel("sw360:obligations").description("The <<resources-obligations,Obligation resource>>"),
linkWithRel("sw360:moderationRequests").description("The <<resources-moderationRequest,ModerationRequest resource>>"),
linkWithRel("sw360:fossology").description("The <<resources-fossology,Fossology resource>>"),
linkWithRel("sw360:schedule").description("The <<resources-schedule,Schedule resource>>"),
linkWithRel("curies").description("The Curies for documentation"),
linkWithRel("profile").description("The profiles of the REST resources")
),
Expand Down
Loading

0 comments on commit 51f957f

Please sign in to comment.