Skip to content

Commit

Permalink
Introduce elastic agent extension v5 migrate-config call (gocd#5937) (g…
Browse files Browse the repository at this point in the history
…ocd#5538)

* On plugin load, make a config migration call to all elastic agent plugins
  to migrate cluster profile(s) and elastic agent profile(s) providing
  plugin settings, cluster profile(s) and elastic agent profile(s).
* Save received migrated config back to the xml.

* Migrate will always be made to plugin on plugin load even if the latest
  available config has already been migrated.
  • Loading branch information
GaneshSPatil committed Apr 2, 2019
1 parent 10ad950 commit 89066ac
Show file tree
Hide file tree
Showing 27 changed files with 1,209 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.thoughtworks.go.plugin.access.common.AbstractExtension;
import com.thoughtworks.go.plugin.access.common.settings.PluginSettingsJsonMessageHandler1_0;
import com.thoughtworks.go.plugin.access.elastic.models.AgentMetadata;
import com.thoughtworks.go.plugin.access.elastic.models.ElasticAgentInformation;
import com.thoughtworks.go.plugin.access.elastic.v4.ElasticAgentExtensionV4;
import com.thoughtworks.go.plugin.access.elastic.v5.ElasticAgentExtensionV5;
import com.thoughtworks.go.plugin.api.response.validation.ValidationResult;
Expand Down Expand Up @@ -119,4 +120,8 @@ protected VersionedElasticAgentExtension getVersionedElasticAgentExtension(Strin
final String resolvedExtensionVersion = pluginManager.resolveExtensionVersion(pluginId, ELASTIC_AGENT_EXTENSION, goSupportedVersions());
return elasticAgentExtensionMap.get(resolvedExtensionVersion);
}

public ElasticAgentInformation migrateConfig(String pluginId, ElasticAgentInformation elasticAgentInformation) {
return getVersionedElasticAgentExtension(pluginId).migrateConfig(pluginId, elasticAgentInformation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.thoughtworks.go.domain.JobIdentifier;
import com.thoughtworks.go.plugin.access.elastic.models.AgentMetadata;
import com.thoughtworks.go.plugin.access.elastic.models.ElasticAgentInformation;
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;
Expand Down Expand Up @@ -53,4 +54,6 @@ public interface VersionedElasticAgentExtension {
String getAgentStatusReport(String pluginId, JobIdentifier identifier, String elasticAgentId);

void jobCompletion(String pluginId, String elasticAgentId, JobIdentifier jobIdentifier, Map<String, String> elasticProfileConfiguration, Map<String, String> clusterProfileConfiguration);

ElasticAgentInformation migrateConfig(String pluginId, ElasticAgentInformation elasticAgentInformation);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import com.thoughtworks.go.config.elastic.ClusterProfile;
import com.thoughtworks.go.config.elastic.ElasticProfile;

import java.util.List;
import java.util.Map;
import java.util.Objects;

public class ElasticAgentInformation {
private final Map<String, Map<String, Object>> pluginSettings;
private final List<ClusterProfile> clusterProfiles;
private final List<ElasticProfile> elasticAgentProfiles;

public ElasticAgentInformation(Map<String, Map<String, Object>> pluginSettings, List<ClusterProfile> clusterProfiles, List<ElasticProfile> elasticAgentProfiles) {
this.pluginSettings = pluginSettings;
this.clusterProfiles = clusterProfiles;
this.elasticAgentProfiles = elasticAgentProfiles;
}

public Map<String, Map<String, Object>> getPluginSettings() {
return pluginSettings;
}

public List<ClusterProfile> getClusterProfiles() {
return clusterProfiles;
}

public List<ElasticProfile> getElasticAgentProfiles() {
return elasticAgentProfiles;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ElasticAgentInformation that = (ElasticAgentInformation) o;
return Objects.equals(pluginSettings, that.pluginSettings) &&
Objects.equals(clusterProfiles, that.clusterProfiles) &&
Objects.equals(elasticAgentProfiles, that.elasticAgentProfiles);
}

@Override
public int hashCode() {
return Objects.hash(pluginSettings, clusterProfiles, elasticAgentProfiles);
}

@Override
public String toString() {
return "ElasticAgentInformation{" +
"pluginSettings=" + pluginSettings +
", clusterProfiles=" + clusterProfiles +
", elasticAgentProfiles=" + elasticAgentProfiles +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.thoughtworks.go.plugin.access.PluginRequestHelper;
import com.thoughtworks.go.plugin.access.elastic.VersionedElasticAgentExtension;
import com.thoughtworks.go.plugin.access.elastic.models.AgentMetadata;
import com.thoughtworks.go.plugin.access.elastic.models.ElasticAgentInformation;
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;
Expand Down Expand Up @@ -177,4 +178,9 @@ public String requestBody(String resolvedExtensionVersion) {
}
});
}

@Override
public ElasticAgentInformation migrateConfig(String pluginId, ElasticAgentInformation elasticAgentInformation) {
return elasticAgentInformation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.thoughtworks.go.config.builder.ConfigurationPropertyBuilder;
import com.thoughtworks.go.config.elastic.ClusterProfile;
import com.thoughtworks.go.domain.config.ConfigurationProperty;

import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;

public class ClusterProfileDTO {
@Expose
@SerializedName("id")
private String id;

@Expose
@SerializedName("plugin_id")
private String pluginId;

@Expose
@SerializedName("properties")
private Map<String, Map<String, Object>> properties;

public ClusterProfileDTO() {
}

public ClusterProfileDTO(String id, String pluginId, Map<String, Map<String, Object>> properties) {
this.id = id;
this.pluginId = pluginId;
this.properties = properties;
}

public String getId() {
return id;
}

public String getPluginId() {
return pluginId;
}

public Map<String, Map<String, Object>> getProperties() {
return properties;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ClusterProfileDTO that = (ClusterProfileDTO) o;
return Objects.equals(id, that.id) &&
Objects.equals(pluginId, that.pluginId) &&
Objects.equals(properties, that.properties);
}

@Override
public int hashCode() {
return Objects.hash(id, pluginId, properties);
}

@Override
public String toString() {
return "ClusterProfileDTO{" +
"id='" + id + '\'' +
", pluginId='" + pluginId + '\'' +
", properties=" + properties +
'}';
}

public ClusterProfile toDomainModel() {
ArrayList<ConfigurationProperty> configurationProperties = new ArrayList<>();

ConfigurationPropertyBuilder builder = new ConfigurationPropertyBuilder();
this.properties.forEach((key, valueObject) -> {
boolean isSecure = (boolean) valueObject.get("isSecure");
String value = isSecure ? null : (String) valueObject.get("value");
String encryptedValue = isSecure ? (String) valueObject.get("value") : null;

configurationProperties.add(builder.create(key, value, encryptedValue, isSecure));
});

return new ClusterProfile(this.id, this.pluginId, configurationProperties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.thoughtworks.go.config.builder.ConfigurationPropertyBuilder;
import com.thoughtworks.go.config.elastic.ClusterProfile;
import com.thoughtworks.go.domain.config.ConfigurationProperty;

import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;

public class ConfigurationPropertyDTO {
@Expose
@SerializedName("id")
private String id;

@Expose
@SerializedName("plugin_id")
private String pluginId;

@Expose
@SerializedName("properties")
private Map<String, Map<String, Object>> properties;

public ConfigurationPropertyDTO() {
}

public ConfigurationPropertyDTO(String id, String pluginId, Map<String, Map<String, Object>> properties) {
this.id = id;
this.pluginId = pluginId;
this.properties = properties;
}

public String getId() {
return id;
}

public String getPluginId() {
return pluginId;
}

public Map<String, Map<String, Object>> getProperties() {
return properties;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ConfigurationPropertyDTO that = (ConfigurationPropertyDTO) o;
return Objects.equals(id, that.id) &&
Objects.equals(pluginId, that.pluginId) &&
Objects.equals(properties, that.properties);
}

@Override
public int hashCode() {
return Objects.hash(id, pluginId, properties);
}

@Override
public String toString() {
return "ClusterProfileDTO{" +
"id='" + id + '\'' +
", pluginId='" + pluginId + '\'' +
", properties=" + properties +
'}';
}

public ClusterProfile toDomainModel() {
ArrayList<ConfigurationProperty> configurationProperties = new ArrayList<>();

ConfigurationPropertyBuilder builder = new ConfigurationPropertyBuilder();
this.properties.forEach((key, valueObject) -> {
boolean isSecure = (boolean) valueObject.get("isSecure");
String value = isSecure ? null : (String) valueObject.get("value");
String encryptedValue = isSecure ? (String) valueObject.get("value") : null;

configurationProperties.add(builder.create(key, value, encryptedValue, isSecure));
});

return new ClusterProfile(this.id, this.pluginId, configurationProperties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.thoughtworks.go.plugin.access.common.models.ImageDeserializer;
import com.thoughtworks.go.plugin.access.common.models.PluginProfileMetadataKeys;
import com.thoughtworks.go.plugin.access.elastic.models.AgentMetadata;
import com.thoughtworks.go.plugin.access.elastic.models.ElasticAgentInformation;
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;
Expand All @@ -35,6 +36,7 @@

class ElasticAgentExtensionConverterV5 {
private static final Gson GSON = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
private ElasticAgentInformationConverterV5 elasticAgentInformationConverterV5 = new ElasticAgentInformationConverterV5();
private CapabilitiesConverterV5 capabilitiesConverterV5 = new CapabilitiesConverterV5();
private AgentMetadataConverterV5 agentMetadataConverterV5 = new AgentMetadataConverterV5();

Expand Down Expand Up @@ -116,6 +118,11 @@ Capabilities getCapabilitiesFromResponseBody(String responseBody) {
return capabilitiesConverterV5.fromDTO(capabilitiesDTO);
}

public ElasticAgentInformation getElasticAgentInformationFromResponseBody(String responseBody) {
final ElasticAgentInformationDTO elasticAgentInformationDTO = GSON.fromJson(responseBody, ElasticAgentInformationDTO.class);
return elasticAgentInformationConverterV5.fromDTO(elasticAgentInformationDTO);
}

private JsonObject mapToJsonObject(Map<String, String> configuration) {
final JsonObject properties = new JsonObject();
for (Map.Entry<String, String> entry : configuration.entrySet()) {
Expand Down Expand Up @@ -162,5 +169,9 @@ public String serverPingRequestBody(List<Map<String, String>> clusterProfileConf
jsonObject.add("all_cluster_profile_properties", mapToJsonArray(clusterProfileConfigurations));
return GSON.toJson(jsonObject);
}

public ElasticAgentInformationDTO getElasticAgentInformationDTO(ElasticAgentInformation elasticAgentInformation) {
return elasticAgentInformationConverterV5.toDTO(elasticAgentInformation);
}
}

Loading

0 comments on commit 89066ac

Please sign in to comment.