diff --git a/server/src/main/java/com/thoughtworks/go/server/service/ElasticAgentPluginService.java b/server/src/main/java/com/thoughtworks/go/server/service/ElasticAgentPluginService.java index bcb79109783..f3fc15bfe64 100644 --- a/server/src/main/java/com/thoughtworks/go/server/service/ElasticAgentPluginService.java +++ b/server/src/main/java/com/thoughtworks/go/server/service/ElasticAgentPluginService.java @@ -19,6 +19,7 @@ import com.google.common.collect.Sets; import com.thoughtworks.go.config.elastic.ClusterProfile; import com.thoughtworks.go.config.elastic.ElasticProfile; +import com.thoughtworks.go.config.exceptions.RecordNotFoundException; import com.thoughtworks.go.domain.AgentInstance; import com.thoughtworks.go.domain.JobIdentifier; import com.thoughtworks.go.domain.JobInstance; @@ -231,6 +232,9 @@ public String getClusterStatusReport(String pluginId, String clusterProfileId) { final ElasticAgentPluginInfo pluginInfo = elasticAgentMetadataStore.getPluginInfo(pluginId); if (pluginInfo.getCapabilities().supportsClusterStatusReport()) { ClusterProfile clusterProfile = clusterProfilesService.getPluginProfiles().findByPluginIdAndProfileId(pluginId, clusterProfileId); + if (clusterProfile == null) { + throw new RecordNotFoundException(String.format("Cluster profile with id: '%s' is not found.", clusterProfileId)); + } return elasticAgentPluginRegistry.getClusterStatusReport(pluginId, clusterProfile.getConfigurationAsMap(true)); } diff --git a/server/src/test-fast/java/com/thoughtworks/go/server/service/ElasticAgentPluginServiceTest.java b/server/src/test-fast/java/com/thoughtworks/go/server/service/ElasticAgentPluginServiceTest.java index 044b0ef2218..8a35d3b8d62 100644 --- a/server/src/test-fast/java/com/thoughtworks/go/server/service/ElasticAgentPluginServiceTest.java +++ b/server/src/test-fast/java/com/thoughtworks/go/server/service/ElasticAgentPluginServiceTest.java @@ -21,6 +21,7 @@ import com.thoughtworks.go.config.elastic.ClusterProfile; import com.thoughtworks.go.config.elastic.ClusterProfiles; import com.thoughtworks.go.config.elastic.ElasticProfile; +import com.thoughtworks.go.config.exceptions.RecordNotFoundException; import com.thoughtworks.go.domain.*; import com.thoughtworks.go.domain.config.ConfigurationKey; import com.thoughtworks.go.domain.config.ConfigurationProperty; @@ -422,6 +423,20 @@ void shouldErrorOutWhenPluginDoesNotClusterSupportStatusReport() { } + @Test + void shouldErrorOutWhenClusterProfileNotFound() { + final Capabilities capabilities = new Capabilities(true, true, false); + final GoPluginDescriptor descriptor = new GoPluginDescriptor("cd.go.example.plugin", null, null, null, null, false); + elasticAgentMetadataStore.setPluginInfo(new ElasticAgentPluginInfo(descriptor, null, null, null, null, capabilities)); + ClusterProfile clusterProfile = new ClusterProfile("cluster-profile-id", "cd.go.example.plugin"); + clusterProfile.addNewConfigurationWithValue("go-server-url", "server-url", false); + PluginProfiles clusterProfiles = new ClusterProfiles(clusterProfile); + when(clusterProfilesService.getPluginProfiles()).thenReturn(clusterProfiles); + + final RecordNotFoundException exception = assertThrows(RecordNotFoundException.class, () -> service.getClusterStatusReport("cd.go.example.plugin", "test")); + assertThat(exception.getMessage()).isEqualTo("Cluster profile with id: 'test' is not found."); + } + @Nested class JobCompleted {