diff --git a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtension.java b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtension.java index 56dbaa8cd7c..714e10cdadb 100644 --- a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtension.java +++ b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtension.java @@ -81,6 +81,18 @@ public ValidationResult validate(final String pluginId, final Map getClusterProfileMetadata(String pluginId) { + return getVersionedElasticAgentExtension(pluginId).getClusterProfileMetadata(pluginId); + } + + String getClusterProfileView(String pluginId) { + return getVersionedElasticAgentExtension(pluginId).getClusterProfileView(pluginId); + } + + public ValidationResult validateClusterProfile(final String pluginId, final Map configuration) { + return getVersionedElasticAgentExtension(pluginId).validateClusterProfile(pluginId, configuration); + } + com.thoughtworks.go.plugin.domain.common.Image getIcon(String pluginId) { return getVersionedElasticAgentExtension(pluginId).getIcon(pluginId); } diff --git a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/VersionedElasticAgentExtension.java b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/VersionedElasticAgentExtension.java index 97b32f5043a..6f65009ff44 100644 --- a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/VersionedElasticAgentExtension.java +++ b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/VersionedElasticAgentExtension.java @@ -18,7 +18,6 @@ import com.thoughtworks.go.domain.JobIdentifier; import com.thoughtworks.go.plugin.access.elastic.models.AgentMetadata; -import com.thoughtworks.go.plugin.access.elastic.v3.ElasticAgentExtensionV3; import com.thoughtworks.go.plugin.api.response.validation.ValidationResult; import com.thoughtworks.go.plugin.domain.common.PluginConfiguration; import com.thoughtworks.go.plugin.domain.elastic.Capabilities; @@ -37,6 +36,12 @@ public interface VersionedElasticAgentExtension { ValidationResult validateElasticProfile(String pluginId, Map configuration); + List getClusterProfileMetadata(String pluginId); + + String getClusterProfileView(String pluginId); + + ValidationResult validateClusterProfile(String pluginId, Map configuration); + void createAgent(String pluginId, String autoRegisterKey, String environment, Map configuration, JobIdentifier jobIdentifier); void serverPing(String pluginId); diff --git a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v3/ElasticAgentExtensionV3.java b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v3/ElasticAgentExtensionV3.java index e6ea1b0e9e5..3ef1de5558f 100644 --- a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v3/ElasticAgentExtensionV3.java +++ b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v3/ElasticAgentExtensionV3.java @@ -158,4 +158,19 @@ public String onSuccess(String responseBody, Map responseHeaders public void jobCompletion(String pluginId, String elasticAgentId, JobIdentifier jobIdentifier) { LOG.debug("Plugin: '{}' uses elastic agent extension v3 and job completion is not supported by elastic agent V3", pluginId); } + + @Override + public List getClusterProfileMetadata(String pluginId) { + throw new UnsupportedOperationException(String.format("Plugin: '%s' uses elastic agent extension v3 and cluster profile extension calls are not supported by elastic agent V3", pluginId)); + } + + @Override + public String getClusterProfileView(String pluginId) { + throw new UnsupportedOperationException(String.format("Plugin: '%s' uses elastic agent extension v3 and cluster profile extension calls are not supported by elastic agent V3", pluginId)); + } + + @Override + public ValidationResult validateClusterProfile(String pluginId, Map configuration) { + throw new UnsupportedOperationException(String.format("Plugin: '%s' uses elastic agent extension v3 and cluster profile extension calls are not supported by elastic agent V3", pluginId)); + } } diff --git a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v4/ElasticAgentExtensionV4.java b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v4/ElasticAgentExtensionV4.java index 9d7b067ec25..4d65e2c88d5 100644 --- a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v4/ElasticAgentExtensionV4.java +++ b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v4/ElasticAgentExtensionV4.java @@ -24,6 +24,8 @@ import com.thoughtworks.go.plugin.api.response.validation.ValidationResult; import com.thoughtworks.go.plugin.domain.common.PluginConfiguration; import com.thoughtworks.go.plugin.domain.elastic.Capabilities; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.List; import java.util.Map; @@ -31,6 +33,7 @@ import static com.thoughtworks.go.plugin.access.elastic.v4.ElasticAgentPluginConstantsV4.*; public class ElasticAgentExtensionV4 implements VersionedElasticAgentExtension { + private static final Logger LOG = LoggerFactory.getLogger(ElasticAgentExtensionV4.class); public static final String VERSION = "4.0"; private final PluginRequestHelper pluginRequestHelper; private final ElasticAgentExtensionConverterV4 elasticAgentExtensionConverterV4; @@ -95,6 +98,20 @@ public ValidationResult onSuccess(String responseBody, Map respo }); } + @Override + public List getClusterProfileMetadata(String pluginId) { + throw new UnsupportedOperationException(String.format("Plugin: '%s' uses elastic agent extension v4 and cluster profile extension calls are not supported by elastic agent V4", pluginId)); + } + + @Override + public String getClusterProfileView(String pluginId) { + throw new UnsupportedOperationException(String.format("Plugin: '%s' uses elastic agent extension v4 and cluster profile extension calls are not supported by elastic agent V4", pluginId)); + } + + @Override + public ValidationResult validateClusterProfile(String pluginId, Map configuration) { + throw new UnsupportedOperationException(String.format("Plugin: '%s' uses elastic agent extension v4 and cluster profile extension calls are not supported by elastic agent V4", pluginId)); + } @Override public void createAgent(String pluginId, final String autoRegisterKey, final String environment, final Map configuration, JobIdentifier jobIdentifier) { diff --git a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentExtensionV5.java b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentExtensionV5.java index 69179bf3a35..8f0d608c3de 100644 --- a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentExtensionV5.java +++ b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentExtensionV5.java @@ -95,6 +95,40 @@ public ValidationResult onSuccess(String responseBody, Map respo }); } + @Override + public List getClusterProfileMetadata(String pluginId) { + return pluginRequestHelper.submitRequest(pluginId, REQUEST_GET_CLUSTER_PROFILE_METADATA, new DefaultPluginInteractionCallback>() { + @Override + public List onSuccess(String responseBody, Map responseHeaders, String resolvedExtensionVersion) { + return elasticAgentExtensionConverterV5.getElasticProfileMetadataResponseFromBody(responseBody); + } + }); + } + + @Override + public String getClusterProfileView(String pluginId) { + return pluginRequestHelper.submitRequest(pluginId, REQUEST_GET_CLUSTER_PROFILE_VIEW, new DefaultPluginInteractionCallback() { + @Override + public String onSuccess(String responseBody, Map responseHeaders, String resolvedExtensionVersion) { + return elasticAgentExtensionConverterV5.getProfileViewResponseFromBody(responseBody); + } + }); + } + + @Override + public ValidationResult validateClusterProfile(String pluginId, Map configuration) { + return pluginRequestHelper.submitRequest(pluginId, REQUEST_VALIDATE_CLUSTER_PROFILE, new DefaultPluginInteractionCallback() { + @Override + public String requestBody(String resolvedExtensionVersion) { + return elasticAgentExtensionConverterV5.validateElasticProfileRequestBody(configuration); + } + + @Override + public ValidationResult onSuccess(String responseBody, Map responseHeaders, String resolvedExtensionVersion) { + return elasticAgentExtensionConverterV5.getElasticProfileValidationResultResponseFromBody(responseBody); + } + }); + } @Override public void createAgent(String pluginId, final String autoRegisterKey, final String environment, final Map configuration, JobIdentifier jobIdentifier) { diff --git a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentPluginConstantsV5.java b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentPluginConstantsV5.java index 2a7b8e2c3d4..ca833017f23 100644 --- a/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentPluginConstantsV5.java +++ b/plugin-infra/go-plugin-access/src/main/java/com/thoughtworks/go/plugin/access/elastic/v5/ElasticAgentPluginConstantsV5.java @@ -28,6 +28,10 @@ public interface ElasticAgentPluginConstantsV5 { String REQUEST_VALIDATE_PROFILE = REQUEST_PREFIX + ".validate-profile"; String REQUEST_GET_PLUGIN_SETTINGS_ICON = REQUEST_PREFIX + ".get-icon"; + String REQUEST_GET_CLUSTER_PROFILE_METADATA = REQUEST_PREFIX + ".get-cluster-profile-metadata"; + String REQUEST_GET_CLUSTER_PROFILE_VIEW = REQUEST_PREFIX + ".get-cluster-profile-view"; + String REQUEST_VALIDATE_CLUSTER_PROFILE = REQUEST_PREFIX + ".validate-cluster-profile"; + String REQUEST_STATUS_REPORT = REQUEST_PREFIX + ".status-report"; String REQUEST_AGENT_STATUS_REPORT = REQUEST_PREFIX + ".agent-status-report"; String REQUEST_CAPABILITIES = REQUEST_PREFIX + ".get-capabilities"; diff --git a/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV3Test.java b/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV3Test.java index 721cda1f3a0..686386f4de7 100644 --- a/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV3Test.java +++ b/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV3Test.java @@ -40,18 +40,13 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mock; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; import static com.thoughtworks.go.plugin.access.elastic.v3.ElasticAgentPluginConstantsV3.*; import static com.thoughtworks.go.plugin.domain.common.PluginConstants.ELASTIC_AGENT_EXTENSION; import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.assertThatJson; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat; -import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.hamcrest.Matchers.hasSize; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.verifyZeroInteractions; @@ -250,6 +245,30 @@ public void shouldGetAgentStatusReport() { assertExtensionRequest("3.0", REQUEST_AGENT_STATUS_REPORT, requestBody); } + @Test + public void shouldNotSupportGetClusterProfileConfigurationCall() { + thrown.expect(UnsupportedOperationException.class); + thrown.expectMessage(String.format("Plugin: '%s' uses elastic agent extension v3 and cluster profile extension calls are not supported by elastic agent V3", PLUGIN_ID)); + + extensionV3.getClusterProfileMetadata(PLUGIN_ID); + } + + @Test + public void shouldNotSupportGetClusterProfileViewCall() { + thrown.expect(UnsupportedOperationException.class); + thrown.expectMessage(String.format("Plugin: '%s' uses elastic agent extension v3 and cluster profile extension calls are not supported by elastic agent V3", PLUGIN_ID)); + + extensionV3.getClusterProfileView(PLUGIN_ID); + } + + @Test + public void shouldNotSupportValidateClusterProfileCall() { + thrown.expect(UnsupportedOperationException.class); + thrown.expectMessage(String.format("Plugin: '%s' uses elastic agent extension v3 and cluster profile extension calls are not supported by elastic agent V3", PLUGIN_ID)); + + extensionV3.validateClusterProfile(PLUGIN_ID, new HashMap<>()); + } + @Test public void shouldDoNothingWhenPluginDoesNotJobCompletionRequest() { String elasticAgentId = "agent1"; diff --git a/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV4Test.java b/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV4Test.java index 8887cfd012d..5a0ce70c6a1 100644 --- a/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV4Test.java +++ b/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV4Test.java @@ -40,18 +40,13 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mock; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; import static com.thoughtworks.go.plugin.access.elastic.v4.ElasticAgentPluginConstantsV4.*; import static com.thoughtworks.go.plugin.domain.common.PluginConstants.ELASTIC_AGENT_EXTENSION; import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.assertThatJson; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat; -import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.hamcrest.Matchers.hasSize; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; @@ -273,6 +268,31 @@ public void shouldGetAgentStatusReport() { assertExtensionRequest("4.0", REQUEST_AGENT_STATUS_REPORT, requestBody); } + + @Test + public void shouldNotSupportGetClusterProfileConfigurationCall() { + thrown.expect(UnsupportedOperationException.class); + thrown.expectMessage(String.format("Plugin: '%s' uses elastic agent extension v4 and cluster profile extension calls are not supported by elastic agent V4", PLUGIN_ID)); + + extensionV4.getClusterProfileMetadata(PLUGIN_ID); + } + + @Test + public void shouldNotSupportGetClusterProfileViewCall() { + thrown.expect(UnsupportedOperationException.class); + thrown.expectMessage(String.format("Plugin: '%s' uses elastic agent extension v4 and cluster profile extension calls are not supported by elastic agent V4", PLUGIN_ID)); + + extensionV4.getClusterProfileView(PLUGIN_ID); + } + + @Test + public void shouldNotSupportValidateClusterProfileCall() { + thrown.expect(UnsupportedOperationException.class); + thrown.expectMessage(String.format("Plugin: '%s' uses elastic agent extension v4 and cluster profile extension calls are not supported by elastic agent V4", PLUGIN_ID)); + + extensionV4.validateClusterProfile(PLUGIN_ID, new HashMap<>()); + } + @Test public void allRequestMustHaveRequestPrefix() { assertThat(REQUEST_PREFIX, is("cd.go.elastic-agent")); diff --git a/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV5Test.java b/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV5Test.java index 05384f455a0..077b81349f1 100644 --- a/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV5Test.java +++ b/plugin-infra/go-plugin-access/src/test/java/com/thoughtworks/go/plugin/access/elastic/ElasticAgentExtensionV5Test.java @@ -45,7 +45,7 @@ import java.util.List; import java.util.Map; -import static com.thoughtworks.go.plugin.access.elastic.v4.ElasticAgentPluginConstantsV4.*; +import static com.thoughtworks.go.plugin.access.elastic.v5.ElasticAgentPluginConstantsV5.*; import static com.thoughtworks.go.plugin.domain.common.PluginConstants.ELASTIC_AGENT_EXTENSION; import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.assertThatJson; import static org.hamcrest.Matchers.*; @@ -148,6 +148,51 @@ public void shouldValidateProfile() { assertExtensionRequest("5.0", REQUEST_VALIDATE_PROFILE, "{}"); } + + @Test + public void shouldGetClusterProfileMetadata() { + String responseBody = "[{\"key\":\"Username\",\"metadata\":{\"required\":true,\"secure\":false}},{\"key\":\"Password\",\"metadata\":{\"required\":true,\"secure\":true}}]"; + when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ELASTIC_AGENT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(responseBody)); + + final List metadata = extensionV5.getClusterProfileMetadata(PLUGIN_ID); + + assertThat(metadata, hasSize(2)); + assertThat(metadata, containsInAnyOrder( + new PluginConfiguration("Username", new Metadata(true, false)), + new PluginConfiguration("Password", new Metadata(true, true)) + )); + + assertExtensionRequest("5.0", REQUEST_GET_CLUSTER_PROFILE_METADATA, null); + } + + @Test + public void shouldGetClusterProfileView() { + String responseBody = "{ \"template\": \"
This is profile view snippet
\" }"; + when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ELASTIC_AGENT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(responseBody)); + + final String view = extensionV5.getClusterProfileView(PLUGIN_ID); + + assertThat(view, is("
This is profile view snippet
")); + + assertExtensionRequest("5.0", REQUEST_GET_CLUSTER_PROFILE_VIEW, null); + } + + @Test + public void shouldValidateClusterProfile() { + String responseBody = "[{\"message\":\"Url must not be blank.\",\"key\":\"Url\"},{\"message\":\"SearchBase must not be blank.\",\"key\":\"SearchBase\"}]"; + when(pluginManager.submitTo(eq(PLUGIN_ID), eq(ELASTIC_AGENT_EXTENSION), requestArgumentCaptor.capture())).thenReturn(DefaultGoPluginApiResponse.success(responseBody)); + + final ValidationResult result = extensionV5.validateClusterProfile(PLUGIN_ID, Collections.emptyMap()); + + assertThat(result.isSuccessful(), is(false)); + assertThat(result.getErrors(), containsInAnyOrder( + new ValidationError("Url", "Url must not be blank."), + new ValidationError("SearchBase", "SearchBase must not be blank.") + )); + + assertExtensionRequest("5.0", REQUEST_VALIDATE_CLUSTER_PROFILE, "{}"); + } + @Test public void shouldMakeCreateAgentCall() { final Map profile = Collections.singletonMap("ServerURL", "https://example.com/go"); diff --git a/server/src/main/java/com/thoughtworks/go/config/update/AddClusterProfileCommand.java b/server/src/main/java/com/thoughtworks/go/config/update/AddClusterProfileCommand.java index 0236d12caaa..a9b7cd33d89 100644 --- a/server/src/main/java/com/thoughtworks/go/config/update/AddClusterProfileCommand.java +++ b/server/src/main/java/com/thoughtworks/go/config/update/AddClusterProfileCommand.java @@ -19,13 +19,14 @@ import com.thoughtworks.go.config.CruiseConfig; import com.thoughtworks.go.config.elastic.ClusterProfile; import com.thoughtworks.go.config.elastic.ClusterProfiles; +import com.thoughtworks.go.plugin.access.elastic.ElasticAgentExtension; import com.thoughtworks.go.server.domain.Username; import com.thoughtworks.go.server.service.GoConfigService; import com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult; public class AddClusterProfileCommand extends ClusterProfileCommand { - public AddClusterProfileCommand(GoConfigService goConfigService, ClusterProfile clusterProfile, Username username, HttpLocalizedOperationResult result) { - super(goConfigService, clusterProfile, username, result); + public AddClusterProfileCommand(ElasticAgentExtension extension, GoConfigService goConfigService, ClusterProfile clusterProfile, Username username, HttpLocalizedOperationResult result) { + super(extension, goConfigService, clusterProfile, username, result); } @Override diff --git a/server/src/main/java/com/thoughtworks/go/config/update/ClusterProfileCommand.java b/server/src/main/java/com/thoughtworks/go/config/update/ClusterProfileCommand.java index c731bfbae43..6dcf8745442 100644 --- a/server/src/main/java/com/thoughtworks/go/config/update/ClusterProfileCommand.java +++ b/server/src/main/java/com/thoughtworks/go/config/update/ClusterProfileCommand.java @@ -20,6 +20,7 @@ import com.thoughtworks.go.config.elastic.ClusterProfile; import com.thoughtworks.go.config.elastic.ClusterProfiles; import com.thoughtworks.go.i18n.LocalizedMessage; +import com.thoughtworks.go.plugin.access.elastic.ElasticAgentExtension; import com.thoughtworks.go.plugin.api.response.validation.ValidationResult; import com.thoughtworks.go.server.domain.Username; import com.thoughtworks.go.server.service.GoConfigService; @@ -29,8 +30,11 @@ import java.util.Map; public abstract class ClusterProfileCommand extends PluginProfileCommand { - public ClusterProfileCommand(GoConfigService goConfigService, ClusterProfile clusterProfile, Username username, HttpLocalizedOperationResult result) { + private final ElasticAgentExtension extension; + + public ClusterProfileCommand(ElasticAgentExtension extension, GoConfigService goConfigService, ClusterProfile clusterProfile, Username username, HttpLocalizedOperationResult result) { super(goConfigService, clusterProfile, username, result); + this.extension = extension; } @Override @@ -40,8 +44,7 @@ protected ClusterProfiles getPluginProfiles(CruiseConfig preprocessedConfig) { @Override public ValidationResult validateUsingExtension(String pluginId, Map configuration) { - //todo: @Vrushali and @Ganesh did this. change this to talk to real extension. - return new ValidationResult(); + return extension.validateClusterProfile(pluginId, configuration); } @Override diff --git a/server/src/main/java/com/thoughtworks/go/config/update/DeleteClusterProfileCommand.java b/server/src/main/java/com/thoughtworks/go/config/update/DeleteClusterProfileCommand.java index ab0cbd4ad10..7ae15900913 100644 --- a/server/src/main/java/com/thoughtworks/go/config/update/DeleteClusterProfileCommand.java +++ b/server/src/main/java/com/thoughtworks/go/config/update/DeleteClusterProfileCommand.java @@ -20,13 +20,14 @@ import com.thoughtworks.go.config.elastic.ClusterProfile; import com.thoughtworks.go.config.elastic.ClusterProfiles; import com.thoughtworks.go.i18n.LocalizedMessage; +import com.thoughtworks.go.plugin.access.elastic.ElasticAgentExtension; import com.thoughtworks.go.server.domain.Username; import com.thoughtworks.go.server.service.GoConfigService; import com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult; public class DeleteClusterProfileCommand extends ClusterProfileCommand { - public DeleteClusterProfileCommand(GoConfigService goConfigService, ClusterProfile clusterProfile, Username currentUser, HttpLocalizedOperationResult result) { - super(goConfigService, clusterProfile, currentUser, result); + public DeleteClusterProfileCommand(ElasticAgentExtension extension, GoConfigService goConfigService, ClusterProfile clusterProfile, Username currentUser, HttpLocalizedOperationResult result) { + super(extension, goConfigService, clusterProfile, currentUser, result); } @Override diff --git a/server/src/main/java/com/thoughtworks/go/config/update/UpdateClusterProfileCommand.java b/server/src/main/java/com/thoughtworks/go/config/update/UpdateClusterProfileCommand.java index 58365ce7082..9135195233d 100644 --- a/server/src/main/java/com/thoughtworks/go/config/update/UpdateClusterProfileCommand.java +++ b/server/src/main/java/com/thoughtworks/go/config/update/UpdateClusterProfileCommand.java @@ -19,13 +19,14 @@ import com.thoughtworks.go.config.CruiseConfig; import com.thoughtworks.go.config.elastic.ClusterProfile; import com.thoughtworks.go.config.elastic.ClusterProfiles; +import com.thoughtworks.go.plugin.access.elastic.ElasticAgentExtension; import com.thoughtworks.go.server.domain.Username; import com.thoughtworks.go.server.service.GoConfigService; import com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult; public class UpdateClusterProfileCommand extends ClusterProfileCommand { - public UpdateClusterProfileCommand(GoConfigService goConfigService, ClusterProfile clusterProfile, Username username, HttpLocalizedOperationResult result) { - super(goConfigService, clusterProfile, username, result); + public UpdateClusterProfileCommand(ElasticAgentExtension extension, GoConfigService goConfigService, ClusterProfile clusterProfile, Username username, HttpLocalizedOperationResult result) { + super(extension, goConfigService, clusterProfile, username, result); } @Override diff --git a/server/src/main/java/com/thoughtworks/go/server/service/ClusterProfilesService.java b/server/src/main/java/com/thoughtworks/go/server/service/ClusterProfilesService.java index 452154d642a..e57ed79a7f8 100644 --- a/server/src/main/java/com/thoughtworks/go/server/service/ClusterProfilesService.java +++ b/server/src/main/java/com/thoughtworks/go/server/service/ClusterProfilesService.java @@ -21,6 +21,7 @@ import com.thoughtworks.go.config.update.AddClusterProfileCommand; import com.thoughtworks.go.config.update.DeleteClusterProfileCommand; import com.thoughtworks.go.config.update.UpdateClusterProfileCommand; +import com.thoughtworks.go.plugin.access.elastic.ElasticAgentExtension; import com.thoughtworks.go.server.domain.Username; import com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult; import org.springframework.beans.factory.annotation.Autowired; @@ -28,9 +29,12 @@ @Component public class ClusterProfilesService extends PluginProfilesService { + private final ElasticAgentExtension extension; + @Autowired - public ClusterProfilesService(GoConfigService goConfigService, EntityHashingService hashingService) { + public ClusterProfilesService(GoConfigService goConfigService, EntityHashingService hashingService, ElasticAgentExtension extension) { super(goConfigService, hashingService); + this.extension = extension; } @Override @@ -39,18 +43,18 @@ public PluginProfiles getPluginProfiles() { } public ClusterProfile create(ClusterProfile clusterProfile, Username currentUser, HttpLocalizedOperationResult result) { - AddClusterProfileCommand addClusterProfileCommand = new AddClusterProfileCommand(goConfigService, clusterProfile, currentUser, result); + AddClusterProfileCommand addClusterProfileCommand = new AddClusterProfileCommand(extension, goConfigService, clusterProfile, currentUser, result); update(currentUser, clusterProfile, result, addClusterProfileCommand); return clusterProfile; } public void delete(ClusterProfile clusterProfile, Username currentUser, HttpLocalizedOperationResult result) { - DeleteClusterProfileCommand deleteClusterProfileCommand = new DeleteClusterProfileCommand(goConfigService, clusterProfile, currentUser, result); + DeleteClusterProfileCommand deleteClusterProfileCommand = new DeleteClusterProfileCommand(extension, goConfigService, clusterProfile, currentUser, result); update(currentUser, clusterProfile, result, deleteClusterProfileCommand); } public ClusterProfile update(ClusterProfile newClusterProfile, Username currentUser, HttpLocalizedOperationResult result) { - UpdateClusterProfileCommand updateClusterProfileCommand = new UpdateClusterProfileCommand(goConfigService, newClusterProfile, currentUser, result); + UpdateClusterProfileCommand updateClusterProfileCommand = new UpdateClusterProfileCommand(extension, goConfigService, newClusterProfile, currentUser, result); update(currentUser, newClusterProfile, result, updateClusterProfileCommand); return newClusterProfile; } diff --git a/server/src/test-fast/java/com/thoughtworks/go/config/update/AddClusterProfileCommandTest.java b/server/src/test-fast/java/com/thoughtworks/go/config/update/AddClusterProfileCommandTest.java index 73c79265a5e..6e10861de43 100644 --- a/server/src/test-fast/java/com/thoughtworks/go/config/update/AddClusterProfileCommandTest.java +++ b/server/src/test-fast/java/com/thoughtworks/go/config/update/AddClusterProfileCommandTest.java @@ -19,6 +19,7 @@ import com.thoughtworks.go.config.BasicCruiseConfig; import com.thoughtworks.go.config.CruiseConfig; import com.thoughtworks.go.config.elastic.ClusterProfile; +import com.thoughtworks.go.plugin.access.elastic.ElasticAgentExtension; import com.thoughtworks.go.server.domain.Username; import com.thoughtworks.go.server.service.GoConfigService; import com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult; @@ -26,13 +27,17 @@ import org.junit.jupiter.api.Test; import org.mockito.Mock; +import java.util.HashMap; + import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import static org.mockito.MockitoAnnotations.initMocks; class AddClusterProfileCommandTest { @Mock private GoConfigService goConfigService; + @Mock + private ElasticAgentExtension extension; private ClusterProfile clusterProfile; private Username username; private HttpLocalizedOperationResult result; @@ -47,7 +52,7 @@ void setUp() { clusterProfile = new ClusterProfile("cluster-id", "plugin-id"); username = new Username("Bob"); result = new HttpLocalizedOperationResult(); - command = new AddClusterProfileCommand(goConfigService, clusterProfile, username, result); + command = new AddClusterProfileCommand(extension, goConfigService, clusterProfile, username, result); } @Test @@ -85,4 +90,13 @@ void shouldReturnWhetherAdditionOfNewClusterProfileIsValid() throws Exception { void shouldSpecifyClusterProfileObjectDescriptor() { assertThat(command.getObjectDescriptor()).isEqualTo("Cluster Profile"); } + + @Test + void shouldMakeACallToExtensionToValidateClusterProfile() { + String pluginId = "plugin-id"; + HashMap configuration = new HashMap<>(); + command.validateUsingExtension(pluginId, configuration); + + verify(extension, times(1)).validateClusterProfile(pluginId, configuration); + } } diff --git a/server/src/test-fast/java/com/thoughtworks/go/config/update/DeleteClusterProfileCommandTest.java b/server/src/test-fast/java/com/thoughtworks/go/config/update/DeleteClusterProfileCommandTest.java index a0e63529430..90336b02b87 100644 --- a/server/src/test-fast/java/com/thoughtworks/go/config/update/DeleteClusterProfileCommandTest.java +++ b/server/src/test-fast/java/com/thoughtworks/go/config/update/DeleteClusterProfileCommandTest.java @@ -19,6 +19,7 @@ import com.thoughtworks.go.config.BasicCruiseConfig; import com.thoughtworks.go.config.CruiseConfig; import com.thoughtworks.go.config.elastic.ClusterProfile; +import com.thoughtworks.go.plugin.access.elastic.ElasticAgentExtension; import com.thoughtworks.go.server.domain.Username; import com.thoughtworks.go.server.service.GoConfigService; import com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult; @@ -33,6 +34,8 @@ class DeleteClusterProfileCommandTest { @Mock private GoConfigService goConfigService; + @Mock + private ElasticAgentExtension extension; private ClusterProfile clusterProfile; private Username username; private HttpLocalizedOperationResult result; @@ -48,7 +51,7 @@ void setUp() { config.getElasticConfig().getClusterProfiles().add(clusterProfile); username = new Username("Bob"); result = new HttpLocalizedOperationResult(); - command = new DeleteClusterProfileCommand(goConfigService, clusterProfile, username, result); + command = new DeleteClusterProfileCommand(extension, goConfigService, clusterProfile, username, result); } @Test diff --git a/server/src/test-fast/java/com/thoughtworks/go/config/update/UpdateClusterProfileCommandTest.java b/server/src/test-fast/java/com/thoughtworks/go/config/update/UpdateClusterProfileCommandTest.java index 7d7dd48ee50..ad88a600eb2 100644 --- a/server/src/test-fast/java/com/thoughtworks/go/config/update/UpdateClusterProfileCommandTest.java +++ b/server/src/test-fast/java/com/thoughtworks/go/config/update/UpdateClusterProfileCommandTest.java @@ -19,6 +19,7 @@ import com.thoughtworks.go.config.BasicCruiseConfig; import com.thoughtworks.go.config.CruiseConfig; import com.thoughtworks.go.config.elastic.ClusterProfile; +import com.thoughtworks.go.plugin.access.elastic.ElasticAgentExtension; import com.thoughtworks.go.server.domain.Username; import com.thoughtworks.go.server.service.GoConfigService; import com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult; @@ -26,13 +27,18 @@ import org.junit.jupiter.api.Test; import org.mockito.Mock; +import java.util.HashMap; + import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import static org.mockito.MockitoAnnotations.initMocks; class UpdateClusterProfileCommandTest { @Mock private GoConfigService goConfigService; + @Mock + private ElasticAgentExtension extension; + private ClusterProfile clusterProfile; private Username username; private HttpLocalizedOperationResult result; @@ -48,7 +54,7 @@ void setUp() { config.getElasticConfig().getClusterProfiles().add(clusterProfile); username = new Username("Bob"); result = new HttpLocalizedOperationResult(); - command = new UpdateClusterProfileCommand(goConfigService, clusterProfile, username, result); + command = new UpdateClusterProfileCommand(extension, goConfigService, clusterProfile, username, result); } @Test @@ -85,4 +91,13 @@ void shouldReturnWhetherRemovalOfExistingClusterProfileIsValid() throws Exceptio void shouldSpecifyClusterProfileObjectDescriptor() { assertThat(command.getObjectDescriptor()).isEqualTo("Cluster Profile"); } + + @Test + void shouldMakeACallToExtensionToValidateClusterProfile() { + String pluginId = "plugin-id"; + HashMap configuration = new HashMap<>(); + command.validateUsingExtension(pluginId, configuration); + + verify(extension, times(1)).validateClusterProfile(pluginId, configuration); + } } diff --git a/server/src/test-fast/java/com/thoughtworks/go/server/service/ClusterProfilesServiceTest.java b/server/src/test-fast/java/com/thoughtworks/go/server/service/ClusterProfilesServiceTest.java index 7671575399a..d9f4d078d83 100644 --- a/server/src/test-fast/java/com/thoughtworks/go/server/service/ClusterProfilesServiceTest.java +++ b/server/src/test-fast/java/com/thoughtworks/go/server/service/ClusterProfilesServiceTest.java @@ -20,12 +20,15 @@ import com.thoughtworks.go.config.elastic.ClusterProfile; import com.thoughtworks.go.config.elastic.ClusterProfiles; import com.thoughtworks.go.config.elastic.ElasticConfig; +import com.thoughtworks.go.plugin.access.elastic.ElasticAgentExtension; +import com.thoughtworks.go.server.domain.Username; +import com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mock; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import static org.mockito.MockitoAnnotations.initMocks; public class ClusterProfilesServiceTest { @@ -33,6 +36,8 @@ public class ClusterProfilesServiceTest { private GoConfigService goConfigService; @Mock private EntityHashingService hashingService; + @Mock + private ElasticAgentExtension extension; private ClusterProfilesService clusterProfilesService; private ClusterProfile clusterProfile; @@ -43,7 +48,7 @@ void setUp() { clusterProfile = new ClusterProfile("prod_cluster", "k8s.ea.plugin"); - clusterProfilesService = new ClusterProfilesService(goConfigService, hashingService); + clusterProfilesService = new ClusterProfilesService(goConfigService, hashingService, extension); } @Test @@ -56,4 +61,18 @@ void shouldFetchClustersDefinedAsPartOfElasticTag() { assertThat(actualClusterProfiles).isEqualTo(elasticConfig.getClusterProfiles()); } + + @Test + void shouldValidateClusterProfileUponClusterProfileCreation() { + clusterProfilesService.create(clusterProfile, new Username("Bob"), new HttpLocalizedOperationResult()); + + verify(extension, times(1)).validateClusterProfile(clusterProfile.getPluginId(), clusterProfile.getConfigurationAsMap(true)); + } + + @Test + void shouldValidateClusterProfileUponClusterProfileUpdate() { + clusterProfilesService.update(clusterProfile, new Username("Bob"), new HttpLocalizedOperationResult()); + + verify(extension, times(1)).validateClusterProfile(clusterProfile.getPluginId(), clusterProfile.getConfigurationAsMap(true)); + } }