Skip to content

Commit

Permalink
Store cluster profile settings as part of elastic agent plugin infos (g…
Browse files Browse the repository at this point in the history
  • Loading branch information
GaneshSPatil committed Apr 1, 2019
1 parent 5fa735d commit 9de4ebb
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ClusterProfileRepresenterTest {
def elasticAgentMetadataStore = ElasticAgentMetadataStore.instance()
PluggableInstanceSettings pluggableInstanceSettings = new PluggableInstanceSettings(Arrays.asList(
new PluginConfiguration("Password", new Metadata(true, true))))
elasticAgentMetadataStore.setPluginInfo(new ElasticAgentPluginInfo(pluginDescriptor(), pluggableInstanceSettings, null, null, null))
elasticAgentMetadataStore.setPluginInfo(new ElasticAgentPluginInfo(pluginDescriptor(), pluggableInstanceSettings, pluggableInstanceSettings, null, null, null))
def jsonReader = GsonTransformer.instance.jsonReaderFrom(clusterProfile)

def object = ClusterProfileRepresenter.fromJSON(jsonReader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ElasticProfileRepresenterTest {
def elasticAgentMetadataStore = ElasticAgentMetadataStore.instance()
PluggableInstanceSettings pluggableInstanceSettings = new PluggableInstanceSettings(Arrays.asList(
new PluginConfiguration("Password", new Metadata(true, true))))
elasticAgentMetadataStore.setPluginInfo(new ElasticAgentPluginInfo(pluginDescriptor(), pluggableInstanceSettings, null, null, null))
elasticAgentMetadataStore.setPluginInfo(new ElasticAgentPluginInfo(pluginDescriptor(), pluggableInstanceSettings, pluggableInstanceSettings, null, null, null))
def jsonReader = GsonTransformer.instance.jsonReaderFrom(elasticProfile)

def object = ElasticProfileRepresenter.fromJSON(jsonReader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class ElasticProfileRepresenterTest {
def elasticAgentMetadataStore = ElasticAgentMetadataStore.instance()
PluggableInstanceSettings pluggableInstanceSettings = new PluggableInstanceSettings(Arrays.asList(
new PluginConfiguration("Password", new Metadata(true, true))))
elasticAgentMetadataStore.setPluginInfo(new ElasticAgentPluginInfo(pluginDescriptor(), pluggableInstanceSettings, null, null, null))
elasticAgentMetadataStore.setPluginInfo(new ElasticAgentPluginInfo(pluginDescriptor(), pluggableInstanceSettings, pluggableInstanceSettings, null, null, null))
def jsonReader = GsonTransformer.instance.jsonReaderFrom(elasticProfile)

def object = ElasticProfileRepresenter.fromJSON(jsonReader)
Expand Down Expand Up @@ -183,7 +183,7 @@ class ElasticProfileRepresenterTest {
def elasticAgentMetadataStore = ElasticAgentMetadataStore.instance()
PluggableInstanceSettings pluggableInstanceSettings = new PluggableInstanceSettings(Arrays.asList(
new PluginConfiguration("Password", new Metadata(true, true))))
elasticAgentMetadataStore.setPluginInfo(new ElasticAgentPluginInfo(pluginDescriptor(), pluggableInstanceSettings, null, null, null))
elasticAgentMetadataStore.setPluginInfo(new ElasticAgentPluginInfo(pluginDescriptor(), pluggableInstanceSettings, pluggableInstanceSettings, null, null, null))
def jsonReader = GsonTransformer.instance.jsonReaderFrom(elasticProfile)

def object = ElasticProfileRepresenter.fromJSON(jsonReader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ protected boolean isSecure(String key) {
ElasticAgentPluginInfo pluginInfo = this.metadataStore().getPluginInfo(getPluginId());

if (pluginInfo == null
|| pluginInfo.getProfileSettings() == null
|| pluginInfo.getProfileSettings().getConfiguration(key) == null) {
|| pluginInfo.getClusterProfileSettings() == null
|| pluginInfo.getClusterProfileSettings().getConfiguration(key) == null) {
return false;
}

return pluginInfo.getProfileSettings().getConfiguration(key).isSecure();
return pluginInfo.getClusterProfileSettings().getConfiguration(key).isSecure();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ protected boolean isSecure(String key) {
ElasticAgentPluginInfo pluginInfo = this.metadataStore().getPluginInfo(getPluginId());

if (pluginInfo == null
|| pluginInfo.getProfileSettings() == null
|| pluginInfo.getProfileSettings().getConfiguration(key) == null) {
|| pluginInfo.getElasticAgentProfileSettings() == null
|| pluginInfo.getElasticAgentProfileSettings().getConfiguration(key) == null) {
return false;
}

return pluginInfo.getProfileSettings().getConfiguration(key).isSecure();
return pluginInfo.getElasticAgentProfileSettings().getConfiguration(key).isSecure();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void addConfigurations_shouldAddConfigurationsWithEncryptedValue() throws
@Test
public void addConfiguration_shouldEncryptASecureVariable() throws Exception {
PluggableInstanceSettings profileSettings = new PluggableInstanceSettings(Arrays.asList(new PluginConfiguration("password", new Metadata(true, true))));
ElasticAgentPluginInfo pluginInfo = new ElasticAgentPluginInfo(pluginDescriptor("plugin_id"), profileSettings, null, null, null);
ElasticAgentPluginInfo pluginInfo = new ElasticAgentPluginInfo(pluginDescriptor("plugin_id"), profileSettings, profileSettings, null, null, null);

store.setPluginInfo(pluginInfo);
ElasticProfile profile = new ElasticProfile("id", "plugin_id");
Expand All @@ -118,7 +118,7 @@ public void addConfiguration_shouldEncryptASecureVariable() throws Exception {

@Test
public void addConfiguration_shouldIgnoreEncryptionInAbsenceOfCorrespondingConfigurationInStore() throws Exception {
ElasticAgentPluginInfo pluginInfo = new ElasticAgentPluginInfo(pluginDescriptor("plugin_id"), new PluggableInstanceSettings(new ArrayList<>()), null, null, null);
ElasticAgentPluginInfo pluginInfo = new ElasticAgentPluginInfo(pluginDescriptor("plugin_id"), new PluggableInstanceSettings(new ArrayList<>()), null, null, null, null);

store.setPluginInfo(pluginInfo);
ElasticProfile profile = new ElasticProfile("id", "plugin_id");
Expand All @@ -132,7 +132,7 @@ public void addConfiguration_shouldIgnoreEncryptionInAbsenceOfCorrespondingConfi
@Test
public void postConstruct_shouldEncryptSecureConfigurations() {
PluggableInstanceSettings profileSettings = new PluggableInstanceSettings(Arrays.asList(new PluginConfiguration("password", new Metadata(true, true))));
ElasticAgentPluginInfo pluginInfo = new ElasticAgentPluginInfo(pluginDescriptor("plugin_id"), profileSettings, null, null, null);
ElasticAgentPluginInfo pluginInfo = new ElasticAgentPluginInfo(pluginDescriptor("plugin_id"), profileSettings, profileSettings, null, null, null);

store.setPluginInfo(pluginInfo);
ElasticProfile profile = new ElasticProfile("id", "plugin_id", new ConfigurationProperty(new ConfigurationKey("password"), new ConfigurationValue("pass")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,30 @@ public ElasticAgentPluginInfoBuilder(ElasticAgentExtension extension) {
public ElasticAgentPluginInfo pluginInfoFor(GoPluginDescriptor descriptor) {
PluggableInstanceSettings pluggableInstanceSettings = getPluginSettingsAndView(descriptor, extension);

return new ElasticAgentPluginInfo(descriptor, elasticProfileSettings(descriptor.id()), image(descriptor.id()),
pluggableInstanceSettings, capabilities(descriptor.id()));
return new ElasticAgentPluginInfo(descriptor,
elasticElasticAgentProfileSettings(descriptor.id()),
elasticClusterProfileSettings(descriptor.id()),
image(descriptor.id()),
pluggableInstanceSettings,
capabilities(descriptor.id()));
}

private com.thoughtworks.go.plugin.domain.common.Image image(String pluginId) {
return extension.getIcon(pluginId);
}

private PluggableInstanceSettings elasticProfileSettings(String pluginId) {
private PluggableInstanceSettings elasticElasticAgentProfileSettings(String pluginId) {
List<PluginConfiguration> profileMetadata = extension.getProfileMetadata(pluginId);
String profileView = extension.getProfileView(pluginId);
return new PluggableInstanceSettings(profileMetadata, new PluginView(profileView));
}

private PluggableInstanceSettings elasticClusterProfileSettings(String pluginId) {
List<PluginConfiguration> profileMetadata = extension.getClusterProfileMetadata(pluginId);
String profileView = extension.getClusterProfileView(pluginId);
return new PluggableInstanceSettings(profileMetadata, new PluginView(profileView));
}

private Capabilities capabilities(String pluginId) {
return extension.getCapabilities(pluginId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.junit.Before;
import org.junit.Test;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;

Expand Down Expand Up @@ -51,7 +50,7 @@ public void shouldBeAPluginChangeListener() throws Exception {
public void onPluginLoaded_shouldAddPluginInfoToMetadataStore() throws Exception {
GoPluginDescriptor descriptor = new GoPluginDescriptor("plugin1", null, null, null, null, false);
ElasticAgentMetadataLoader metadataLoader = new ElasticAgentMetadataLoader(pluginManager, metadataStore, infoBuilder, extension);
ElasticAgentPluginInfo pluginInfo = new ElasticAgentPluginInfo(descriptor, null, null, null, null);
ElasticAgentPluginInfo pluginInfo = new ElasticAgentPluginInfo(descriptor, null, null, null, null, null);

when(extension.canHandlePlugin(descriptor.id())).thenReturn(true);
when(infoBuilder.pluginInfoFor(descriptor)).thenReturn(pluginInfo);
Expand All @@ -78,7 +77,7 @@ public void onPluginLoaded_shouldIgnoreNonAuthorizationPlugins() throws Exceptio
public void onPluginUnloded_shouldRemoveTheCorrespondingPluginInfoFromStore() throws Exception {
GoPluginDescriptor descriptor = new GoPluginDescriptor("plugin1", null, null, null, null, false);
ElasticAgentMetadataLoader metadataLoader = new ElasticAgentMetadataLoader(pluginManager, metadataStore, infoBuilder, extension);
ElasticAgentPluginInfo pluginInfo = new ElasticAgentPluginInfo(descriptor, null, null, null, null);
ElasticAgentPluginInfo pluginInfo = new ElasticAgentPluginInfo(descriptor, null, null, null, null, null);

metadataStore.setPluginInfo(pluginInfo);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,42 @@ public void shouldBuildPluginInfoWithProfileSettings() {
assertThat(pluginInfo.getExtensionName(), is("elastic-agent"));

assertThat(pluginInfo.getImage(), is(icon));
assertThat(pluginInfo.getProfileSettings(), is(new PluggableInstanceSettings(pluginConfigurations, new PluginView("profile_view"))));
assertThat(pluginInfo.getElasticAgentProfileSettings(), is(new PluggableInstanceSettings(pluginConfigurations, new PluginView("profile_view"))));
assertThat(pluginInfo.getPluginSettings(), is(new PluggableInstanceSettings(builder.configurations(pluginSettingsConfiguration), new PluginView("some html"))));
assertFalse(pluginInfo.supportsStatusReport());
}

@Test
public void shouldBuildPluginInfoWithClusterProfileSettings() {
GoPluginDescriptor descriptor = new GoPluginDescriptor("plugin1", null, null, null, null, false);
List<PluginConfiguration> elasticAgentProfileConfigurations = Arrays.asList(new PluginConfiguration("aws_password", new Metadata(true, false)));
List<PluginConfiguration> clusterProfileConfigurations = Arrays.asList(new PluginConfiguration("aws_url", new Metadata(true, false)));
PluginSettingsProperty property = new PluginSettingsProperty("ami-id", "ami-123");
PluginSettingsConfiguration pluginSettingsConfiguration = new PluginSettingsConfiguration();
pluginSettingsConfiguration.add(property);
Image icon = new Image("content_type", "data", "hash");

when(pluginManager.resolveExtensionVersion("plugin1", ELASTIC_AGENT_EXTENSION, SUPPORTED_VERSIONS)).thenReturn("1.0");
when(extension.getPluginSettingsConfiguration(descriptor.id())).thenReturn(pluginSettingsConfiguration);
when(extension.getPluginSettingsView(descriptor.id())).thenReturn("some html");

when(extension.getIcon(descriptor.id())).thenReturn(icon);

when(extension.getClusterProfileMetadata(descriptor.id())).thenReturn(clusterProfileConfigurations);
when(extension.getClusterProfileView(descriptor.id())).thenReturn("cluster_profile_view");

when(extension.getProfileMetadata(descriptor.id())).thenReturn(elasticAgentProfileConfigurations);
when(extension.getProfileView(descriptor.id())).thenReturn("elastic_agent_profile_view");

ElasticAgentPluginInfoBuilder builder = new ElasticAgentPluginInfoBuilder(extension);
ElasticAgentPluginInfo pluginInfo = builder.pluginInfoFor(descriptor);

assertThat(pluginInfo.getDescriptor(), is(descriptor));
assertThat(pluginInfo.getExtensionName(), is("elastic-agent"));

assertThat(pluginInfo.getImage(), is(icon));
assertThat(pluginInfo.getElasticAgentProfileSettings(), is(new PluggableInstanceSettings(elasticAgentProfileConfigurations, new PluginView("elastic_agent_profile_view"))));
assertThat(pluginInfo.getClusterProfileSettings(), is(new PluggableInstanceSettings(clusterProfileConfigurations, new PluginView("cluster_profile_view"))));
assertThat(pluginInfo.getPluginSettings(), is(new PluggableInstanceSettings(builder.configurations(pluginSettingsConfiguration), new PluginView("some html"))));
assertFalse(pluginInfo.supportsStatusReport());
}
Expand All @@ -96,7 +131,7 @@ public void shouldContinueWithBuildingPluginInfoIfPluginSettingsIsNotProvidedByT
assertThat(pluginInfo.getExtensionName(), is("elastic-agent"));

assertThat(pluginInfo.getImage(), is(icon));
assertThat(pluginInfo.getProfileSettings(), is(new PluggableInstanceSettings(pluginConfigurations, new PluginView("profile_view"))));
assertThat(pluginInfo.getElasticAgentProfileSettings(), is(new PluggableInstanceSettings(pluginConfigurations, new PluginView("profile_view"))));
assertNull(pluginInfo.getPluginSettings());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,29 @@
import com.thoughtworks.go.plugin.domain.common.PluginConstants;
import com.thoughtworks.go.plugin.domain.common.PluginInfo;

import java.util.Objects;

public class ElasticAgentPluginInfo extends PluginInfo {
private final PluggableInstanceSettings profileSettings;
private final PluggableInstanceSettings clusterProfileSettings;
private final Capabilities capabilities;

public ElasticAgentPluginInfo(PluginDescriptor descriptor, PluggableInstanceSettings profileSettings, Image image,
public ElasticAgentPluginInfo(PluginDescriptor descriptor, PluggableInstanceSettings profileSettings, PluggableInstanceSettings clusterProfileSettings, Image image,
PluggableInstanceSettings pluginSettings, Capabilities capabilities) {
super(descriptor, PluginConstants.ELASTIC_AGENT_EXTENSION, pluginSettings, image);
super(descriptor, PluginConstants.ELASTIC_AGENT_EXTENSION, pluginSettings, image);
this.profileSettings = profileSettings;
this.clusterProfileSettings = clusterProfileSettings;
this.capabilities = capabilities;
}

public PluggableInstanceSettings getProfileSettings() {
public PluggableInstanceSettings getElasticAgentProfileSettings() {
return profileSettings;
}

public PluggableInstanceSettings getClusterProfileSettings() {
return clusterProfileSettings;
}

public Capabilities getCapabilities() {
return capabilities;
}
Expand All @@ -50,22 +58,14 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;

ElasticAgentPluginInfo that = (ElasticAgentPluginInfo) o;

if (profileSettings != null ? !profileSettings.equals(that.profileSettings) : that.profileSettings != null)
return false;
if (image != null ? !image.equals(that.image) : that.image != null) return false;
return capabilities != null ? capabilities.equals(that.capabilities) : that.capabilities == null;

return Objects.equals(profileSettings, that.profileSettings) &&
Objects.equals(clusterProfileSettings, that.clusterProfileSettings) &&
Objects.equals(capabilities, that.capabilities);
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (profileSettings != null ? profileSettings.hashCode() : 0);
result = 31 * result + (image != null ? image.hashCode() : 0);
result = 31 * result + (capabilities != null ? capabilities.hashCode() : 0);
return result;
return Objects.hash(super.hashCode(), profileSettings, clusterProfileSettings, capabilities);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void shouldFindFirstExtensionWithImageIfPluginImplementsAtleastOneExtensi
Image image2 = new Image("c2", "d2", "hash2");
Image image3 = new Image("c3", "d3", "hash3");

ElasticAgentPluginInfo elasticAgentPluginInfo = new ElasticAgentPluginInfo(null, null, image1, null, null);
ElasticAgentPluginInfo elasticAgentPluginInfo = new ElasticAgentPluginInfo(null, null, elasticClusterProfileSettings(descriptor.id()), image1, null, null);
AuthorizationPluginInfo authorizationPluginInfo = new AuthorizationPluginInfo(null, null, null, image2, null);
AnalyticsPluginInfo analyticsPluginInfo = new AnalyticsPluginInfo(null, image3, null, null);

Expand Down
Loading

0 comments on commit 9de4ebb

Please sign in to comment.