|
| 1 | +/** |
| 2 | + * ShinyProxy |
| 3 | + * |
| 4 | + * Copyright (C) 2016-2024 Open Analytics |
| 5 | + * |
| 6 | + * =========================================================================== |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the Apache License as published by |
| 10 | + * The Apache Software Foundation, either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * Apache License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the Apache License |
| 19 | + * along with this program. If not, see <http://www.apache.org/licenses/> |
| 20 | + */ |
| 21 | +package eu.openanalytics.shinyproxy.controllers; |
| 22 | + |
| 23 | +import eu.openanalytics.containerproxy.api.dto.ApiResponse; |
| 24 | +import eu.openanalytics.containerproxy.event.RemoveDelegateProxiesEvent; |
| 25 | +import io.swagger.v3.oas.annotations.Operation; |
| 26 | +import io.swagger.v3.oas.annotations.Parameter; |
| 27 | +import io.swagger.v3.oas.annotations.media.Content; |
| 28 | +import io.swagger.v3.oas.annotations.media.ExampleObject; |
| 29 | +import io.swagger.v3.oas.annotations.responses.ApiResponses; |
| 30 | +import org.springframework.context.ApplicationEventPublisher; |
| 31 | +import org.springframework.http.MediaType; |
| 32 | +import org.springframework.http.ResponseEntity; |
| 33 | +import org.springframework.stereotype.Controller; |
| 34 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 35 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 36 | +import org.springframework.web.bind.annotation.RequestParam; |
| 37 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 38 | + |
| 39 | +import javax.inject.Inject; |
| 40 | + |
| 41 | +@Controller |
| 42 | +public class DelegateProxyAdminController extends BaseController { |
| 43 | + |
| 44 | + @Inject |
| 45 | + private ApplicationEventPublisher applicationEventPublisher; |
| 46 | + |
| 47 | + @Operation(summary = "Stops DelegateProxies. Can only be used by admins. If no parameters are specified, all DelegateProxies (of all specs) are stopped. " + |
| 48 | + "DelegateProxies that have claimed seats will be stopped as soon as all seats are released. " + |
| 49 | + "New DelegateProxies are automatically created to meet the minimum number of seats.", |
| 50 | + tags = "ShinyProxy" |
| 51 | + ) |
| 52 | + @ApiResponses(value = { |
| 53 | + @io.swagger.v3.oas.annotations.responses.ApiResponse( |
| 54 | + responseCode = "200", |
| 55 | + description = "The DelegateProxies are being stopped.", |
| 56 | + content = { |
| 57 | + @Content( |
| 58 | + mediaType = "application/json", |
| 59 | + examples = { |
| 60 | + @ExampleObject(value = "{\"status\":\"success\", \"data\": null}") |
| 61 | + } |
| 62 | + ) |
| 63 | + }), |
| 64 | + @io.swagger.v3.oas.annotations.responses.ApiResponse( |
| 65 | + responseCode = "400", |
| 66 | + description = "Invalid request, no DelegateProxies are being stopped.", |
| 67 | + content = { |
| 68 | + @Content( |
| 69 | + mediaType = "application/json", |
| 70 | + examples = { |
| 71 | + @ExampleObject(name = "Both id and specId are specified, provide only a single parameter.", value = "{\"status\":\"fail\",\"data\":\"Id and specId cannot be specified at the same time\"}"), |
| 72 | + } |
| 73 | + ) |
| 74 | + }), |
| 75 | + @io.swagger.v3.oas.annotations.responses.ApiResponse( |
| 76 | + responseCode = "403", |
| 77 | + description = "Forbidden, you are not an admin user.", |
| 78 | + content = { |
| 79 | + @Content( |
| 80 | + mediaType = "application/json", |
| 81 | + examples = {@ExampleObject(value = "{\"status\": \"fail\", \"data\": \"forbidden\"}")} |
| 82 | + ) |
| 83 | + }), |
| 84 | + }) |
| 85 | + |
| 86 | + @RequestMapping(value = "/admin/delegate-proxy", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.DELETE) |
| 87 | + @ResponseBody |
| 88 | + public ResponseEntity<ApiResponse<Object>> stopDelegateProxies( |
| 89 | + @Parameter(description = "If specified stops the DelegateProxy with this id") @RequestParam(required = false) String id, |
| 90 | + @Parameter(description = "If specified stops all DelegateProxies of this specId ") @RequestParam(required = false) String specId |
| 91 | + ) { |
| 92 | + |
| 93 | + if (id != null && specId != null) { |
| 94 | + return ApiResponse.fail("Id and specId cannot be specified at the same time"); |
| 95 | + } |
| 96 | + |
| 97 | + applicationEventPublisher.publishEvent(new RemoveDelegateProxiesEvent(id, specId)); |
| 98 | + |
| 99 | + return ApiResponse.success(); |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments