Search in sources :

Example 1 with CruiseControlEndPoint

use of com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint in project cruise-control by linkedin.

the class Purgatory method submit.

/**
 * Ensure that:
 * <ul>
 *   <li>A request with the given review id exists in the purgatory.</li>
 *   <li>The request with the given review id matches the given request.</li>
 *   <li>The request with the given review id is approved in the purgatory.</li>
 * </ul>
 *
 * Then mark the review status as submitted.
 *
 * @param reviewId The review id for which the corresponding request is requested to be submitted.
 * @param request The request to submit.
 * @return Submitted request info.
 */
public synchronized RequestInfo submit(int reviewId, HttpServletRequest request) {
    RequestInfo requestInfo = _requestInfoById.get(reviewId);
    // 1. Ensure that a request with the given review id exists in the purgatory.
    if (requestInfo == null) {
        throw new UserRequestException(String.format("No request with review id %d exists in purgatory. Please use %s endpoint to check for the " + "current requests awaiting review in purgatory.", reviewId, REVIEW));
    }
    // 2. Ensure that the request with the given review id matches the given request.
    CruiseControlEndPoint endpoint = ParameterUtils.endPoint(request);
    if (requestInfo.endPoint() != endpoint) {
        throw new UserRequestException(String.format("Request with review id %d is associated with %s endpoint, but the given request has %s endpoint." + "Please use %s endpoint to check for the current requests awaiting review in purgatory.", reviewId, requestInfo.endPoint(), endpoint, REVIEW));
    }
    if (requestInfo.status() == ReviewStatus.SUBMITTED) {
        LOG.info("Request {} has already been submitted (review: {}).", requestInfo.endpointWithParams(), reviewId);
        requestInfo.setAccessToAlreadySubmittedRequest();
    } else {
        // 3. Ensure that the request with the given review id is approved in the purgatory, and mark the status as submitted.
        requestInfo.submitReview(reviewId);
        LOG.info("Submitted request {} for execution (review: {}).", requestInfo.endpointWithParams(), reviewId);
    }
    return requestInfo;
}
Also used : CruiseControlEndPoint(com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint) UserRequestException(com.linkedin.kafka.cruisecontrol.servlet.UserRequestException)

Example 2 with CruiseControlEndPoint

use of com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint in project cruise-control by linkedin.

the class RequestParameterTest method setupParameterClasses.

/**
 * Specify endpoints to be tested
 */
@Before
public void setupParameterClasses() throws Exception {
    _endpointToClass = new HashMap<>();
    KafkaCruiseControlConfig defaultConfig = new KafkaCruiseControlConfig(KafkaCruiseControlUnitTestUtils.getKafkaCruiseControlProperties());
    String webserverApiUrlPrefix = KafkaCruiseControlServletTestUtils.getDefaultWebServerApiUrlPrefix();
    for (CruiseControlEndPoint endpoint : CruiseControlEndPoint.cachedValues()) {
        _endpointToClass.put((webserverApiUrlPrefix + endpoint.toString()).toLowerCase(), ((CruiseControlParameters) (defaultConfig.getClass(requestParameterFor(endpoint).parametersClass()).newInstance())));
    }
}
Also used : CruiseControlEndPoint(com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint) KafkaCruiseControlConfig(com.linkedin.kafka.cruisecontrol.config.KafkaCruiseControlConfig) CruiseControlParameters(com.linkedin.cruisecontrol.servlet.parameters.CruiseControlParameters) Before(org.junit.Before)

Example 3 with CruiseControlEndPoint

use of com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint in project cruise-control by linkedin.

the class ParameterUtilsTest method testGetEndpoint.

@Test
public void testGetEndpoint() {
    HttpServletRequest mockRequest = EasyMock.mock(HttpServletRequest.class);
    for (CruiseControlEndPoint getEndPoint : CruiseControlEndPoint.getEndpoints()) {
        EasyMock.expect(mockRequest.getMethod()).andReturn(GET_METHOD).once();
        EasyMock.expect(mockRequest.getPathInfo()).andReturn("/" + getEndPoint).once();
        EasyMock.replay(mockRequest);
        CruiseControlEndPoint endPoint = ParameterUtils.endPoint(mockRequest);
        Assert.assertEquals(getEndPoint, endPoint);
        EasyMock.verify(mockRequest);
        EasyMock.reset(mockRequest);
    }
    for (CruiseControlEndPoint postEndPoint : CruiseControlEndPoint.postEndpoints()) {
        EasyMock.expect(mockRequest.getMethod()).andReturn(POST_METHOD).once();
        EasyMock.expect(mockRequest.getPathInfo()).andReturn("/" + postEndPoint).once();
        EasyMock.replay(mockRequest);
        CruiseControlEndPoint endPoint = ParameterUtils.endPoint(mockRequest);
        Assert.assertEquals(postEndPoint, endPoint);
        EasyMock.verify(mockRequest);
        EasyMock.reset(mockRequest);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) CruiseControlEndPoint(com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint) Test(org.junit.Test)

Example 4 with CruiseControlEndPoint

use of com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint in project cruise-control by linkedin.

the class KafkaCruiseControlServletUtils method getValidEndpoint.

/**
 * Returns the GET or POST endpoint if the request contains a valid one, otherwise (1) writes the error response to
 * the given HTTP response and (2) returns null.
 *
 * @param request HTTP request received by Cruise Control.
 * @param response HTTP response of Cruise Control.
 * @param config The config of Cruise Control.
 * @return The endpoint if the request contains a valid one, otherwise (1) writes the error response to the given HTTP
 * response and (2) returns null.
 */
static CruiseControlEndPoint getValidEndpoint(HttpServletRequest request, HttpServletResponse response, KafkaCruiseControlConfig config) throws IOException {
    CruiseControlEndPoint endPoint = endPoint(request);
    if (endPoint == null) {
        String method = request.getMethod();
        String errorMessage = String.format("Unrecognized endpoint in request '%s'%nSupported %s endpoints: %s", request.getPathInfo(), method, method.equals(GET_METHOD) ? CruiseControlEndPoint.getEndpoints() : CruiseControlEndPoint.postEndpoints());
        writeErrorResponse(response, null, errorMessage, SC_NOT_FOUND, wantJSON(request), wantResponseSchema(request), config);
        return null;
    }
    return endPoint;
}
Also used : CruiseControlEndPoint(com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint)

Example 5 with CruiseControlEndPoint

use of com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint in project cruise-control by linkedin.

the class ParameterUtils method endPoint.

/**
 * @param request The Http request.
 * @return The endpoint specified in the given request.
 */
public static CruiseControlEndPoint endPoint(HttpServletRequest request) {
    List<CruiseControlEndPoint> supportedEndpoints;
    switch(request.getMethod()) {
        case GET_METHOD:
            supportedEndpoints = CruiseControlEndPoint.getEndpoints();
            break;
        case POST_METHOD:
            supportedEndpoints = CruiseControlEndPoint.postEndpoints();
            break;
        default:
            throw new UserRequestException("Unsupported request method: " + request.getMethod() + ".");
    }
    String pathInfo = request.getPathInfo();
    if (pathInfo == null) {
        // URL does not have any extra path information
        return null;
    }
    // Skip the first character '/'
    String path = pathInfo.substring(1);
    for (CruiseControlEndPoint endPoint : supportedEndpoints) {
        if (endPoint.toString().equalsIgnoreCase(path)) {
            return endPoint;
        }
    }
    return null;
}
Also used : CruiseControlEndPoint(com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint) UserRequestException(com.linkedin.kafka.cruisecontrol.servlet.UserRequestException)

Aggregations

CruiseControlEndPoint (com.linkedin.kafka.cruisecontrol.servlet.CruiseControlEndPoint)7 UserRequestException (com.linkedin.kafka.cruisecontrol.servlet.UserRequestException)2 CruiseControlParameters (com.linkedin.cruisecontrol.servlet.parameters.CruiseControlParameters)1 KafkaCruiseControlConfig (com.linkedin.kafka.cruisecontrol.config.KafkaCruiseControlConfig)1 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Before (org.junit.Before)1 Test (org.junit.Test)1