Java Code Examples for com.thoughtworks.go.plugin.api.request.DefaultGoApiRequest

The following examples show how to use com.thoughtworks.go.plugin.api.request.DefaultGoApiRequest. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: kubernetes-elastic-agents   Source File: PluginRequest.java    License: Apache License 2.0 6 votes vote down vote up
public void appendToConsoleLog(JobIdentifier jobIdentifier, String text) throws ServerRequestFailedException {
    Map<String, String> requestMap = new HashMap<>();
    requestMap.put("pipelineName", jobIdentifier.getPipelineName());
    requestMap.put("pipelineCounter", String.valueOf(jobIdentifier.getPipelineCounter()));
    requestMap.put("stageName", jobIdentifier.getStageName());
    requestMap.put("stageCounter", jobIdentifier.getStageCounter());
    requestMap.put("jobName", jobIdentifier.getJobName());
    requestMap.put("text", text);

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_APPEND_TO_CONSOLE_LOG, CONSOLE_LOG_API_VERSION, PLUGIN_IDENTIFIER);
    request.setRequestBody(new GsonBuilder().create().toJson(requestMap));

    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        LOG.error("Failed to append to console log for " + jobIdentifier.getRepresentation() + " with text: " + text);
    }
}
 
Example 2
public void appendToConsoleLog(JobIdentifier jobIdentifier, String text) throws ServerRequestFailedException {
    Map<String, String> requestMap = new HashMap<>();
    requestMap.put("pipelineName", jobIdentifier.getPipelineName());
    requestMap.put("pipelineCounter", String.valueOf(jobIdentifier.getPipelineCounter()));
    requestMap.put("stageName", jobIdentifier.getStageName());
    requestMap.put("stageCounter", jobIdentifier.getStageCounter());
    requestMap.put("jobName", jobIdentifier.getJobName());
    requestMap.put("text", text);

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_APPEND_TO_CONSOLE_LOG, CONSOLE_LOG_API_VERSION, PLUGIN_IDENTIFIER);
    request.setRequestBody(new GsonBuilder().create().toJson(requestMap));

    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        LOG.error("Failed to append to console log for " + jobIdentifier.represent() + " with text: " + text);
    }
}
 
Example 3
Source Project: gocd   Source File: ConsoleLogRequestProcessorV1Test.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void shouldRouteMessageToConsoleService() throws IOException, IllegalArtifactLocationException {
    Map<String, String> requestMap = new HashMap<>();
    requestMap.put("pipelineName", "p1");
    requestMap.put("pipelineCounter", "1");
    requestMap.put("stageName", "s1");
    requestMap.put("stageCounter", "2");
    requestMap.put("jobName", "j1");
    requestMap.put("text", "message1");

    DefaultGoApiRequest goApiRequest = new DefaultGoApiRequest(APPEND_TO_CONSOLE_LOG, VERSION_1, null);
    goApiRequest.setRequestBody(new GsonBuilder().create().toJson(requestMap));

    final ConsoleLogRequestProcessor processor = new ConsoleLogRequestProcessor(pluginRequestProcessorRegistry, consoleService);
    final GoApiResponse response = processor.process(pluginDescriptor, goApiRequest);

    assertThat(response.responseCode(), is(DefaultGoApiResponse.SUCCESS_RESPONSE_CODE));

    final JobIdentifier jobIdentifier = new JobIdentifier("p1", 1, null, "s1", "2", "j1");
    verify(consoleService).appendToConsoleLog(jobIdentifier, "message1");
}
 
Example 4
Source Project: gocd   Source File: ConsoleLogRequestProcessorV2Test.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void shouldRouteMessageToConsoleService() throws IOException, IllegalArtifactLocationException {
    Map<String, String> requestMap = new HashMap<>();
    requestMap.put("pipeline_name", "p1");
    requestMap.put("pipeline_counter", "1");
    requestMap.put("stage_name", "s1");
    requestMap.put("stage_counter", "2");
    requestMap.put("job_name", "j1");
    requestMap.put("text", "message1");

    DefaultGoApiRequest goApiRequest = new DefaultGoApiRequest(APPEND_TO_CONSOLE_LOG, VERSION_2, null);
    goApiRequest.setRequestBody(new GsonBuilder().create().toJson(requestMap));

    final ConsoleLogRequestProcessor processor = new ConsoleLogRequestProcessor(pluginRequestProcessorRegistry, consoleService);
    final GoApiResponse response = processor.process(pluginDescriptor, goApiRequest);

    assertThat(response.responseCode(), is(DefaultGoApiResponse.SUCCESS_RESPONSE_CODE));

    final JobIdentifier jobIdentifier = new JobIdentifier("p1", 1, null, "s1", "2", "j1");
    verify(consoleService).appendToConsoleLog(jobIdentifier, "message1");
}
 
Example 5
private void sendLog(ConsoleLogMessage consoleLogMessage) {
    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.SEND_CONSOLE_LOG, CONSOLE_LOG_PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    request.setRequestBody(consoleLogMessage.toJSON());

    GoApiResponse response = accessor.submit(request);
    if (response.responseCode() != DefaultGoApiResponse.SUCCESS_RESPONSE_CODE) {
        LOG.error(String.format("Failed to submit console log: %s", response.responseBody()));
    }
}
 
Example 6
Source Project: kubernetes-elastic-agents   Source File: PluginRequest.java    License: Apache License 2.0 5 votes vote down vote up
public ServerInfo getSeverInfo() throws ServerRequestFailedException {
    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_INFO, SERVER_INFO_API_VERSION, PLUGIN_IDENTIFIER);
    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.serverInfo(response);
    }

    return ServerInfo.fromJSON(response.responseBody());
}
 
Example 7
Source Project: kubernetes-elastic-agents   Source File: PluginRequest.java    License: Apache License 2.0 5 votes vote down vote up
public Agents listAgents() throws ServerRequestFailedException {
    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_LIST_AGENTS, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.listAgents(response);
    }

    return new Agents(Agent.fromJSONArray(response.responseBody()));
}
 
Example 8
Source Project: kubernetes-elastic-agents   Source File: PluginRequest.java    License: Apache License 2.0 5 votes vote down vote up
public void disableAgents(Collection<Agent> toBeDisabled) throws ServerRequestFailedException {
    LOG.debug(format("[Server Ping] Disabling Agents: {0}", toBeDisabled.toString()));
    if (toBeDisabled.isEmpty()) {
        return;
    }

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_DISABLE_AGENT, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    request.setRequestBody(Agent.toJSONArray(toBeDisabled));

    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.disableAgents(response);
    }
}
 
Example 9
Source Project: kubernetes-elastic-agents   Source File: PluginRequest.java    License: Apache License 2.0 5 votes vote down vote up
public void deleteAgents(Collection<Agent> toBeDeleted) throws ServerRequestFailedException {
    LOG.debug(format("[Server Ping] Deleting Agents: {0}", toBeDeleted.toString()));
    if (toBeDeleted.isEmpty()) {
        return;
    }

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_DELETE_AGENT, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    request.setRequestBody(Agent.toJSONArray(toBeDeleted));
    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.deleteAgents(response);
    }
}
 
Example 10
public Agents listAgents() throws ServerRequestFailedException {
    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_LIST_AGENTS, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.listAgents(response);
    }

    return new Agents(Agent.fromJSONArray(response.responseBody()));
}
 
Example 11
public void disableAgents(Collection<Agent> toBeDisabled) throws ServerRequestFailedException {
    if (toBeDisabled.isEmpty()) {
        return;
    }

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_DISABLE_AGENT, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    request.setRequestBody(Agent.toJSONArray(toBeDisabled));

    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.disableAgents(response);
    }
}
 
Example 12
public void deleteAgents(Collection<Agent> toBeDeleted) throws ServerRequestFailedException {
    if (toBeDeleted.isEmpty()) {
        return;
    }

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_DELETE_AGENT, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    request.setRequestBody(Agent.toJSONArray(toBeDeleted));
    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.deleteAgents(response);
    }
}
 
Example 13
public Agents listAgents() throws ServerRequestFailedException {
    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_LIST_AGENTS, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.listAgents(response);
    }

    return new Agents(Agent.fromJSONArray(response.responseBody()));
}
 
Example 14
public void disableAgents(Collection<Agent> toBeDisabled) throws ServerRequestFailedException {
    if (toBeDisabled.isEmpty()) {
        return;
    }

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_DISABLE_AGENT, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    request.setRequestBody(Agent.toJSONArray(toBeDisabled));

    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.disableAgents(response);
    }
}
 
Example 15
public void deleteAgents(Collection<Agent> toBeDeleted) throws ServerRequestFailedException {
    if (toBeDeleted.isEmpty()) {
        return;
    }

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_DELETE_AGENT, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);
    request.setRequestBody(Agent.toJSONArray(toBeDeleted));
    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        throw ServerRequestFailedException.deleteAgents(response);
    }
}
 
Example 16
Source Project: gocd   Source File: ServerInfoRequestProcessorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnAServerIdInJSONForm() {
    DefaultGoApiRequest request = new DefaultGoApiRequest(GET_SERVER_INFO, "1.0", new GoPluginIdentifier("extension1", Arrays.asList("1.0")));


    GoApiResponse response = processor.process(pluginDescriptor, request);

    assertThat(response.responseCode(), is(200));
    assertThat(response.responseBody(),
            is(format("{\"server_id\":\"%s\",\"site_url\":\"%s\",\"secure_site_url\":\"%s\"}",
                    serverConfig.getServerId(), serverConfig.getSiteUrl().getUrl(), serverConfig.getSecureSiteUrl().getUrl())));
}
 
Example 17
Source Project: gocd   Source File: ServerInfoRequestProcessorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnSuccessForServerInfoV2() {
    DefaultGoApiRequest request = new DefaultGoApiRequest(GET_SERVER_INFO, "2.0", new GoPluginIdentifier("extension1", Arrays.asList("1.0")));

    GoApiResponse response = processor.process(pluginDescriptor, request);

    assertThat(response.responseCode(), is(200));
}
 
Example 18
Source Project: gocd   Source File: ServerInfoRequestProcessorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnAErrorResponseIfExtensionDoesNotSupportServerInfo() {
    DefaultGoApiRequest request = new DefaultGoApiRequest(GET_SERVER_INFO, "bad-version", new GoPluginIdentifier("foo", Arrays.asList("1.0")));

    GoApiResponse response = processor.process(pluginDescriptor, request);

    assertThat(response.responseCode(), is(400));
}
 
Example 19
Source Project: gocd   Source File: ConsoleLogRequestProcessorV1Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void shouldRespondWithAMessageIfSomethingGoesWrong() {
    DefaultGoApiRequest goApiRequest = new DefaultGoApiRequest(APPEND_TO_CONSOLE_LOG, VERSION_1, null);
    goApiRequest.setRequestBody("this_is_invalid_JSON");

    final ConsoleLogRequestProcessor processor = new ConsoleLogRequestProcessor(pluginRequestProcessorRegistry, consoleService);
    final GoApiResponse response = processor.process(pluginDescriptor, goApiRequest);

    assertThat(response.responseCode(), is(DefaultGoApiResponse.INTERNAL_ERROR));

    final Map responseContents = new Gson().fromJson(response.responseBody(), Map.class);
    assertThat((String) responseContents.get("message"), containsString("Error:"));
}
 
Example 20
Source Project: gocd   Source File: ConsoleLogRequestProcessorV2Test.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void shouldRespondWithAMessageIfSomethingGoesWrong() {
    DefaultGoApiRequest goApiRequest = new DefaultGoApiRequest(APPEND_TO_CONSOLE_LOG, VERSION_2, null);
    goApiRequest.setRequestBody("this_is_invalid_JSON");

    final ConsoleLogRequestProcessor processor = new ConsoleLogRequestProcessor(pluginRequestProcessorRegistry, consoleService);
    final GoApiResponse response = processor.process(pluginDescriptor, goApiRequest);

    assertThat(response.responseCode(), is(DefaultGoApiResponse.INTERNAL_ERROR));

    final Map responseContents = new Gson().fromJson(response.responseBody(), Map.class);
    assertThat((String) responseContents.get("message"), containsString("Error:"));
}
 
Example 21
Source Project: gocd   Source File: PluginSettingsRequestProcessorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetPluginSettingsForPluginThatExistsInDB() {
    String PLUGIN_ID = "plugin-foo-id";

    when(pluginDescriptor.id()).thenReturn(PLUGIN_ID);
    when(pluginSqlMapDao.findPlugin(PLUGIN_ID)).thenReturn(new Plugin(PLUGIN_ID, "{\"k1\": \"v1\",\"k2\": \"v2\"}"));

    DefaultGoApiRequest apiRequest = new DefaultGoApiRequest(PluginSettingsRequestProcessor.GET_PLUGIN_SETTINGS, "1.0", new GoPluginIdentifier("extension1", Collections.singletonList("1.0")));
    apiRequest.setRequestBody("expected-request");
    GoApiResponse response = processor.process(pluginDescriptor, apiRequest);

    assertThat(response.responseCode(), is(200));
    assertThat(response.responseBody(), is("{\"k1\":\"v1\",\"k2\":\"v2\"}"));
}
 
Example 22
Source Project: gocd   Source File: PluginSettingsRequestProcessorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotGetPluginSettingsForPluginThatDoesNotExistInDB() {
    String PLUGIN_ID = "plugin-foo-id";
    String requestBody = "expected-request";

    when(pluginDescriptor.id()).thenReturn(PLUGIN_ID);
    when(pluginSqlMapDao.findPlugin(PLUGIN_ID)).thenReturn(new NullPlugin());

    DefaultGoApiRequest apiRequest = new DefaultGoApiRequest(PluginSettingsRequestProcessor.GET_PLUGIN_SETTINGS, "1.0", new GoPluginIdentifier("extension1", Collections.singletonList("1.0")));
    apiRequest.setRequestBody(requestBody);
    GoApiResponse response = processor.process(pluginDescriptor, apiRequest);

    assertThat(response.responseCode(), is(200));
    assertThat(response.responseBody(), is(nullValue()));
}
 
Example 23
Source Project: gocd   Source File: PluginSettingsRequestProcessorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRespondWith400InCaseOfErrors() {
    String PLUGIN_ID = "plugin-foo-id";

    when(pluginDescriptor.id()).thenReturn(PLUGIN_ID);
    when(pluginSqlMapDao.findPlugin(PLUGIN_ID)).thenThrow(new RuntimeException());

    DefaultGoApiRequest apiRequest = new DefaultGoApiRequest(PluginSettingsRequestProcessor.GET_PLUGIN_SETTINGS, "1.0", new GoPluginIdentifier("extension1", Collections.singletonList("1.0")));
    apiRequest.setRequestBody("expected-request");

    GoApiResponse response = processor.process(pluginDescriptor, apiRequest);

    assertThat(response.responseCode(), is(400));
}
 
Example 24
Source Project: gocd   Source File: AuthorizationRequestProcessorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldProcessInvalidateCacheRequest() throws Exception {
    PluginRoleService pluginRoleService = mock(PluginRoleService.class);
    when(authorizationExtension.getMessageConverter(AuthorizationMessageConverterV1.VERSION)).thenReturn(new AuthorizationMessageConverterV1());

    GoApiRequest request = new DefaultGoApiRequest(INVALIDATE_CACHE_REQUEST.requestName(), "1.0", null);
    AuthorizationRequestProcessor authorizationRequestProcessor = new AuthorizationRequestProcessor(registry, pluginRoleService);

    GoApiResponse response = authorizationRequestProcessor.process(pluginDescriptor, request);

    assertThat(response.responseCode(), is(200));
    verify(pluginRoleService).invalidateRolesFor("cd.go.authorization.github");
}
 
Example 25
public static GoApiRequest requestWithCredentials(String username, String password) {
    final DefaultGoApiRequest defaultGoApiRequest = new DefaultGoApiRequest(null, null, null);
    defaultGoApiRequest.setRequestBody(requestJson(username, password));
    return defaultGoApiRequest;
}
 
Example 26
public void addServerHealthMessage(List<Map<String, String>> messages) {
    Gson gson = new Gson();

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_SERVER_HEALTH_ADD_MESSAGES, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);

    request.setRequestBody(gson.toJson(messages));

    // submit the request
    GoApiResponse response = accessor.submit(request);

    // check status
    if (response.responseCode() != 200) {
        LOG.error("The server sent an unexpected status code " + response.responseCode() + " with the response body " + response.responseBody());
    }

}
 
Example 27
Source Project: gocd   Source File: ServerInfoRequestProcessorTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void shouldRegisterAPIRequestWithProcessor() {
    DefaultGoApiRequest request = new DefaultGoApiRequest(GET_SERVER_INFO, "1.0", new GoPluginIdentifier("extension1", Collections.singletonList("1.0")));
    assertThat(processorRegistry.canProcess(request), is(true));
}
 
Example 28
Source Project: gocd   Source File: ServerHealthRequestProcessorTest.java    License: Apache License 2.0 4 votes vote down vote up
private DefaultGoApiRequest createRequest(String apiVersion, String requestBody) {
    DefaultGoApiRequest request = new DefaultGoApiRequest(ADD_SERVER_HEALTH_MESSAGES, apiVersion, null);
    request.setRequestBody(requestBody);
    return request;
}
 
Example 29
public void addServerHealthMessage(List<Map<String, String>> messages) {
    Gson gson = new Gson();

    DefaultGoApiRequest request = new DefaultGoApiRequest(Constants.REQUEST_SERVER_SERVER_HEALTH_ADD_MESSAGES, PROCESSOR_API_VERSION, PLUGIN_IDENTIFIER);

    request.setRequestBody(gson.toJson(messages));

    GoApiResponse response = accessor.submit(request);

    if (response.responseCode() != 200) {
        LOG.error("The server sent an unexpected status code " + response.responseCode() + " with the response body " + response.responseBody());
    }
}