Skip to content

Commit

Permalink
Pass along cluster profile properties to create agent request (gocd#5937
Browse files Browse the repository at this point in the history
)

* Modify elastic agent extension v5 create-agent request to pass
  along cluster_profile_properties as part of request body.
  • Loading branch information
GaneshSPatil committed Mar 18, 2019
1 parent da6d73b commit c9a93d4
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.thoughtworks.go.plugin.access.elastic;

import com.thoughtworks.go.config.elastic.ClusterProfile;
import com.thoughtworks.go.domain.JobIdentifier;
import com.thoughtworks.go.plugin.access.ExtensionsRegistry;
import com.thoughtworks.go.plugin.access.PluginRequestHelper;
Expand Down Expand Up @@ -54,8 +55,8 @@ public ElasticAgentExtension(PluginManager pluginManager, ExtensionsRegistry ext
}


public void createAgent(String pluginId, final String autoRegisterKey, final String environment, final Map<String, String> configuration, JobIdentifier jobIdentifier) {
getVersionedElasticAgentExtension(pluginId).createAgent(pluginId, autoRegisterKey, environment, configuration, jobIdentifier);
public void createAgent(String pluginId, final String autoRegisterKey, final String environment, final Map<String, String> configuration, final Map<String, String> clusterProfileConfiguration, JobIdentifier jobIdentifier) {
getVersionedElasticAgentExtension(pluginId).createAgent(pluginId, autoRegisterKey, environment, configuration, clusterProfileConfiguration, jobIdentifier);
}

public void serverPing(final String pluginId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 ThoughtWorks, Inc.
* Copyright 2019 ThoughtWorks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,12 +34,12 @@ public ElasticAgentPluginRegistry(PluginManager pluginManager, ElasticAgentExten
super(pluginManager, elasticAgentExtension);
}

public void createAgent(final String pluginId, String autoRegisterKey, String environment, Map<String, String> configuration, JobIdentifier jobIdentifier) {
public void createAgent(final String pluginId, String autoRegisterKey, String environment, Map<String, String> configuration, Map<String, String> clusterProfileConfiguration, JobIdentifier jobIdentifier) {
PluginDescriptor plugin = findPlugin(pluginId);
if (plugin != null) {
LOGGER.debug("Processing create agent for plugin: {} with environment: {} with configuration: {}", pluginId, environment, configuration);
extension.createAgent(pluginId, autoRegisterKey, environment, configuration, jobIdentifier);
LOGGER.debug("Done processing create agent for plugin: {} with environment: {} with configuration: {}", pluginId, environment, configuration);
LOGGER.debug("Processing create agent for plugin: {} with environment: {} with elastic agent configuration: {} in cluster: {}", pluginId, environment, configuration, clusterProfileConfiguration);
extension.createAgent(pluginId, autoRegisterKey, environment, configuration, clusterProfileConfiguration, jobIdentifier);
LOGGER.debug("Done processing create agent for plugin: {} with environment: {} with elastic agent configuration: {} in cluster: {}", pluginId, environment, configuration, clusterProfileConfiguration);
} else {
LOGGER.warn("Could not find plugin with id: {}", pluginId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface VersionedElasticAgentExtension {

ValidationResult validateClusterProfile(String pluginId, Map<String, String> configuration);

void createAgent(String pluginId, String autoRegisterKey, String environment, Map<String, String> configuration, JobIdentifier jobIdentifier);
void createAgent(String pluginId, String autoRegisterKey, String environment, Map<String, String> configuration, Map<String, String> clusterProfileConfiguration, JobIdentifier jobIdentifier);

void serverPing(String pluginId);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 ThoughtWorks, Inc.
* Copyright 2019 ThoughtWorks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -114,7 +114,7 @@ public ValidationResult validateClusterProfile(String pluginId, Map<String, Stri
}

@Override
public void createAgent(String pluginId, final String autoRegisterKey, final String environment, final Map<String, String> configuration, JobIdentifier jobIdentifier) {
public void createAgent(String pluginId, final String autoRegisterKey, final String environment, final Map<String, String> configuration, Map<String, String> clusterProfileConfiguration, JobIdentifier jobIdentifier) {
pluginRequestHelper.submitRequest(pluginId, REQUEST_CREATE_AGENT, new DefaultPluginInteractionCallback<Void>() {
@Override
public String requestBody(String resolvedExtensionVersion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.thoughtworks.go.config.elastic.ClusterProfile;
import com.thoughtworks.go.domain.JobIdentifier;
import com.thoughtworks.go.plugin.access.common.handler.JSONResultMessageHandler;
import com.thoughtworks.go.plugin.access.common.models.ImageDeserializer;
Expand All @@ -37,10 +38,11 @@ class ElasticAgentExtensionConverterV5 {
private CapabilitiesConverterV5 capabilitiesConverterV5 = new CapabilitiesConverterV5();
private AgentMetadataConverterV5 agentMetadataConverterV5 = new AgentMetadataConverterV5();

String createAgentRequestBody(String autoRegisterKey, String environment, Map<String, String> configuration, JobIdentifier jobIdentifier) {
String createAgentRequestBody(String autoRegisterKey, String environment, Map<String, String> configuration, Map<String, String> clusterProfileProperties, JobIdentifier jobIdentifier) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("auto_register_key", autoRegisterKey);
jsonObject.add("properties", mapToJsonObject(configuration));
jsonObject.add("elastic_agent_profile_properties", mapToJsonObject(configuration));
jsonObject.add("cluster_profile_properties", mapToJsonObject(clusterProfileProperties));
jsonObject.addProperty("environment", environment);
jsonObject.add("job_identifier", jobIdentifierJson(jobIdentifier));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ public ValidationResult onSuccess(String responseBody, Map<String, String> respo
}

@Override
public void createAgent(String pluginId, final String autoRegisterKey, final String environment, final Map<String, String> configuration, JobIdentifier jobIdentifier) {
public void createAgent(String pluginId, final String autoRegisterKey, final String environment, final Map<String, String> configuration, Map<String, String> clusterProfileConfiguration, JobIdentifier jobIdentifier) {
pluginRequestHelper.submitRequest(pluginId, REQUEST_CREATE_AGENT, new DefaultPluginInteractionCallback<Void>() {
@Override
public String requestBody(String resolvedExtensionVersion) {
return elasticAgentExtensionConverterV5.createAgentRequestBody(autoRegisterKey, environment, configuration, jobIdentifier);
return elasticAgentExtensionConverterV5.createAgentRequestBody(autoRegisterKey, environment, configuration, clusterProfileConfiguration, jobIdentifier);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void shouldMakeCreateAgentCall() {
final JobIdentifier jobIdentifier = new JobIdentifier("up42", 2, "Test", "up42_stage", "10", "up42_job");
when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ELASTIC_AGENT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(null));

extensionV4.createAgent(PLUGIN_ID, "auto-registration-key", "test-env", profile, jobIdentifier);
extensionV4.createAgent(PLUGIN_ID, "auto-registration-key", "test-env", profile, null, jobIdentifier);

String expectedRequestBody = "{\n" +
" \"auto_register_key\": \"auto-registration-key\",\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 ThoughtWorks, Inc.
* Copyright 2019 ThoughtWorks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -195,15 +195,19 @@ public void shouldValidateClusterProfile() {

@Test
public void shouldMakeCreateAgentCall() {
final Map<String, String> profile = Collections.singletonMap("ServerURL", "https://example.com/go");
final Map<String, String> profile = Collections.singletonMap("Image", "alpine:latest");
final Map<String, String> clusterProfile = Collections.singletonMap("ServerURL", "https://example.com/go");
final JobIdentifier jobIdentifier = new JobIdentifier("up42", 2, "Test", "up42_stage", "10", "up42_job");
when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ELASTIC_AGENT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(null));

extensionV5.createAgent(PLUGIN_ID, "auto-registration-key", "test-env", profile, jobIdentifier);
extensionV5.createAgent(PLUGIN_ID, "auto-registration-key", "test-env", profile, clusterProfile, jobIdentifier);

String expectedRequestBody = "{\n" +
" \"auto_register_key\": \"auto-registration-key\",\n" +
" \"properties\": {\n" +
" \"elastic_agent_profile_properties\": {\n" +
" \"Image\": \"alpine:latest\"\n" +
" },\n" +
" \"cluster_profile_properties\": {\n" +
" \"ServerURL\": \"https://example.com/go\"\n" +
" },\n" +
" \"environment\": \"test-env\",\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 ThoughtWorks, Inc.
* Copyright 2019 ThoughtWorks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,13 +56,14 @@ public void setUp() throws Exception {
@Test
public void shouldTalkToExtensionToCreateElasticAgent() {
final Map<String, String> configuration = Collections.singletonMap("GoServerURL", "foo");
final Map<String, String> clusterConfiguration = Collections.singletonMap("GoServerURL", "foo");
final JobIdentifier jobIdentifier = new JobIdentifier();
final String autoRegisterKey = "auto-register-key";
final String environment = "test-env";

elasticAgentPluginRegistry.createAgent(PLUGIN_ID, autoRegisterKey, environment, configuration, jobIdentifier);
elasticAgentPluginRegistry.createAgent(PLUGIN_ID, autoRegisterKey, environment, configuration, clusterConfiguration, jobIdentifier);

verify(elasticAgentExtension, times(1)).createAgent(PLUGIN_ID, autoRegisterKey, environment, configuration, jobIdentifier);
verify(elasticAgentExtension, times(1)).createAgent(PLUGIN_ID, autoRegisterKey, environment, configuration, clusterConfiguration, jobIdentifier);
verifyNoMoreInteractions(elasticAgentExtension);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,20 @@ public void shouldJSONizeCreateAgentRequestBody() throws Exception {
Map<String, String> configuration = new HashMap<>();
configuration.put("key1", "value1");
configuration.put("key2", "value2");
String json = new ElasticAgentExtensionConverterV5().createAgentRequestBody("secret-key", "prod", configuration, jobIdentifier);

Map<String, String> clusterProfileConfiguration = new HashMap<>();
clusterProfileConfiguration.put("key1", "value1");
clusterProfileConfiguration.put("key2", "value2");

String json = new ElasticAgentExtensionConverterV5().createAgentRequestBody("secret-key", "prod", configuration, clusterProfileConfiguration, jobIdentifier);

assertThatJson(json).isEqualTo("{" +
" \"auto_register_key\":\"secret-key\"," +
" \"properties\":{" +
" \"elastic_agent_profile_properties\":{" +
" \"key1\":\"value1\"," +
" \"key2\":\"value2\"" +
" }," +
" \"cluster_profile_properties\":{" +
" \"key1\":\"value1\"," +
" \"key2\":\"value2\"" +
" }," +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 ThoughtWorks, Inc.
* Copyright 2019 ThoughtWorks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,6 @@ public CreateAgentListener(ElasticAgentPluginRegistry elasticAgentPluginRegistry

@Override
public void onMessage(CreateAgentMessage message) {
elasticAgentPluginRegistry.createAgent(message.pluginId(), message.autoregisterKey(), message.environment(), message.configuration(), message.jobIdentifier());
elasticAgentPluginRegistry.createAgent(message.pluginId(), message.autoregisterKey(), message.environment(), message.configuration(), message.getClusterProfileConfiguration(), message.jobIdentifier());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 ThoughtWorks, Inc.
* Copyright 2019 ThoughtWorks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,24 +16,28 @@

package com.thoughtworks.go.server.messaging.elasticagents;

import com.thoughtworks.go.config.elastic.ClusterProfile;
import com.thoughtworks.go.config.elastic.ElasticProfile;
import com.thoughtworks.go.domain.JobIdentifier;
import com.thoughtworks.go.server.messaging.PluginAwareMessage;

import java.util.Collections;
import java.util.Map;

public class CreateAgentMessage implements PluginAwareMessage {
private final String autoregisterKey;
private final String environment;
private final Map<String, String> configuration;
private Map<String, String> clusterProfile;
private final JobIdentifier jobIdentifier;
private final String pluginId;

public CreateAgentMessage(String autoregisterKey, String environment, ElasticProfile elasticProfile, JobIdentifier jobIdentifier) {
public CreateAgentMessage(String autoregisterKey, String environment, ElasticProfile elasticProfile, ClusterProfile clusterProfile, JobIdentifier jobIdentifier) {
this.autoregisterKey = autoregisterKey;
this.environment = environment;
this.pluginId = elasticProfile.getPluginId();
this.configuration = elasticProfile.getConfigurationAsMap(true);
this.clusterProfile = clusterProfile != null ? clusterProfile.getConfigurationAsMap(true) : Collections.emptyMap();
this.jobIdentifier = jobIdentifier;
}

Expand All @@ -51,6 +55,8 @@ public String toString() {
"autoregisterKey='" + autoregisterKey + '\'' +
", environment='" + environment + '\'' +
", configuration=" + configuration +
", clusterProfile=" + clusterProfile +
", jobIdentifier=" + jobIdentifier +
", pluginId='" + pluginId + '\'' +
'}';
}
Expand All @@ -66,4 +72,8 @@ public Map<String, String> configuration() {
public JobIdentifier jobIdentifier() {
return jobIdentifier;
}

public Map<String, String> getClusterProfileConfiguration() {
return clusterProfile;
}
}
Loading

0 comments on commit c9a93d4

Please sign in to comment.