Skip to content

Commit 4fdca37

Browse files
committed
Implement migrate config plugin api call
Scenarios: 1. When no plugin settings are configured. => Do not migrate. 2. When plugin settings are configured and no cluster profiles and elastic agent profiles are defined. => Migrate plugin settings configurations to a default cluster. 3. When an empty cluster profile is defined; And is referenced from elastic agent profiles. => Migrate plugin settings to the empty cluster profile. And, if the cluster profile has the no-op cluster profile id, then rename the cluster-profile to a new UUID, and fix all the elastic agent profile references. 4. When an empty cluster profile is defined; And is not referenced from any elastic agent profiles. => Do not migrate such cluster profiles.
1 parent 254a4e4 commit 4fdca37

File tree

2 files changed

+196
-22
lines changed

2 files changed

+196
-22
lines changed

src/main/java/cd/go/contrib/elasticagents/docker/executors/MigrateConfigurationRequestExecutor.java

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.util.Arrays;
2626
import java.util.List;
27+
import java.util.Objects;
2728
import java.util.UUID;
2829
import java.util.stream.Collectors;
2930

@@ -39,36 +40,76 @@ public MigrateConfigurationRequestExecutor(MigrateConfigurationRequest migrateCo
3940

4041
@Override
4142
public GoPluginApiResponse execute() throws Exception {
43+
LOG.info("[Migrate Config] Request for Config Migration Started...");
44+
4245
PluginSettings pluginSettings = migrateConfigurationRequest.getPluginSettings();
4346
List<ClusterProfile> existingClusterProfiles = migrateConfigurationRequest.getClusterProfiles();
4447
List<ElasticAgentProfile> existingElasticAgentProfiles = migrateConfigurationRequest.getElasticAgentProfiles();
4548

4649
if (!arePluginSettingsConfigured(pluginSettings)) {
47-
LOG.info("[Migrate Config] No Plugin Settings are configured. Skipping Config Migration.");
50+
LOG.info("[Migrate Config] No Plugin Settings are configured. Skipping Config Migration...");
4851
return new DefaultGoPluginApiResponse(200, migrateConfigurationRequest.toJSON());
4952
}
5053

51-
if (!existingClusterProfiles.isEmpty()) {
52-
List<String> existingClusterProfileIds = existingClusterProfiles.stream().map(ClusterProfile::getId).collect(Collectors.toList());
53-
LOG.info("[Migrate Config] Found already defined cluster profiles {}. Skipping Config Migration.", existingClusterProfileIds);
54-
return new DefaultGoPluginApiResponse(200, migrateConfigurationRequest.toJSON());
54+
if (existingClusterProfiles.size() == 0) {
55+
LOG.info("[Migrate Config] Did not find any Cluster Profile. Possibly, user just have configured plugin settings and haven't define any elastic agent profiles.");
56+
String newClusterId = UUID.randomUUID().toString();
57+
LOG.info("[Migrate Config] Migrating existing plugin settings to new cluster profile '{}'", newClusterId);
58+
ClusterProfile clusterProfile = new ClusterProfile(newClusterId, Constants.PLUGIN_ID, pluginSettings);
59+
60+
return getGoPluginApiResponse(pluginSettings, Arrays.asList(clusterProfile), existingElasticAgentProfiles);
5561
}
5662

57-
LOG.info("[Migrate Config] No defined cluster profiles found. Running migrations..");
58-
String defaultClusterId = UUID.randomUUID().toString();
59-
ClusterProfile clusterProfile = new ClusterProfile(defaultClusterId, Constants.PLUGIN_ID, pluginSettings);
60-
existingElasticAgentProfiles.forEach(elasticAgentProfile -> {
61-
elasticAgentProfile.setClusterProfileId(defaultClusterId);
62-
});
63+
LOG.info("[Migrate Config] Checking to perform migrations on Cluster Profiles '{}'.", existingClusterProfiles.stream().map(ClusterProfile::getId).collect(Collectors.toList()));
64+
65+
for (ClusterProfile clusterProfile : existingClusterProfiles) {
66+
List<ElasticAgentProfile> associatedElasticAgentProfiles = findAssociatedElasticAgentProfiles(clusterProfile, existingElasticAgentProfiles);
67+
if (associatedElasticAgentProfiles.size() == 0) {
68+
LOG.info("[Migrate Config] Skipping migration for the cluster '{}' as no Elastic Agent Profiles are associated with it.", clusterProfile.getId());
69+
continue;
70+
}
6371

64-
MigrateConfigurationRequest migrateConfigurationRequest = new MigrateConfigurationRequest();
65-
migrateConfigurationRequest.setPluginSettings(pluginSettings);
66-
migrateConfigurationRequest.setClusterProfiles(Arrays.asList(clusterProfile));
67-
migrateConfigurationRequest.setElasticAgentProfiles(existingElasticAgentProfiles);
72+
if (!arePluginSettingsConfigured(clusterProfile.getClusterProfileProperties())) {
73+
List<String> associatedProfileIds = associatedElasticAgentProfiles.stream().map(ElasticAgentProfile::getId).collect(Collectors.toList());
74+
LOG.info("[Migrate Config] Found an empty cluster profile '{}' associated with '{}' elastic agent profiles.", clusterProfile.getId(), associatedProfileIds);
75+
migrateConfigForCluster(pluginSettings, associatedElasticAgentProfiles, clusterProfile);
76+
} else {
77+
LOG.info("[Migrate Config] Skipping migration for the cluster '{}' as cluster has already been configured.", clusterProfile.getId());
78+
}
79+
}
6880

6981
return new DefaultGoPluginApiResponse(200, migrateConfigurationRequest.toJSON());
7082
}
7183

84+
//this is responsible to copy over plugin settings configurations to cluster profile and if required rename no op cluster
85+
private void migrateConfigForCluster(PluginSettings pluginSettings, List<ElasticAgentProfile> associatedElasticAgentProfiles, ClusterProfile clusterProfile) {
86+
LOG.info("[Migrate Config] Coping over existing plugin settings configurations to '{}' cluster profile.", clusterProfile.getId());
87+
clusterProfile.setClusterProfileProperties(pluginSettings);
88+
89+
if (clusterProfile.getId().equals(String.format("no-op-cluster-for-%s", Constants.PLUGIN_ID))) {
90+
String newClusterId = UUID.randomUUID().toString();
91+
LOG.info("[Migrate Config] Renaming dummy cluster profile from '{}' to '{}'.", clusterProfile.getId(), newClusterId);
92+
clusterProfile.setId(newClusterId);
93+
94+
LOG.info("[Migrate Config] Changing all elastic agent profiles to point to '{}' cluster profile.", clusterProfile.getId());
95+
associatedElasticAgentProfiles.forEach(elasticAgentProfile -> elasticAgentProfile.setClusterProfileId(newClusterId));
96+
}
97+
}
98+
99+
private List<ElasticAgentProfile> findAssociatedElasticAgentProfiles(ClusterProfile clusterProfile, List<ElasticAgentProfile> elasticAgentProfiles) {
100+
return elasticAgentProfiles.stream().filter(profile -> Objects.equals(profile.getClusterProfileId(), clusterProfile.getId())).collect(Collectors.toList());
101+
}
102+
103+
private GoPluginApiResponse getGoPluginApiResponse(PluginSettings pluginSettings, List<ClusterProfile> clusterProfiles, List<ElasticAgentProfile> elasticAgentProfiles) {
104+
MigrateConfigurationRequest response = new MigrateConfigurationRequest();
105+
106+
response.setPluginSettings(pluginSettings);
107+
response.setClusterProfiles(clusterProfiles);
108+
response.setElasticAgentProfiles(elasticAgentProfiles);
109+
110+
return new DefaultGoPluginApiResponse(200, response.toJSON());
111+
}
112+
72113
private boolean arePluginSettingsConfigured(PluginSettings pluginSettings) {
73114
return !StringUtils.isBlank(pluginSettings.getGoServerUrl());
74115
}

src/test/java/cd/go/contrib/elasticagents/docker/executors/MigrateConfigurationRequestExecutorTest.java

Lines changed: 140 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@
3131
import java.util.List;
3232

3333
import static org.hamcrest.Matchers.is;
34+
import static org.hamcrest.Matchers.not;
3435
import static org.junit.Assert.assertThat;
3536

3637
public class MigrateConfigurationRequestExecutorTest {
3738

3839
private PluginSettings pluginSettings;
3940
private ClusterProfile clusterProfile;
4041
private ElasticAgentProfile elasticAgentProfile;
42+
private HashMap<String, String> properties;
4143

4244
@Before
4345
public void setUp() throws Exception {
@@ -54,7 +56,7 @@ public void setUp() throws Exception {
5456
elasticAgentProfile.setId("profile_id");
5557
elasticAgentProfile.setPluginId(Constants.PLUGIN_ID);
5658
elasticAgentProfile.setClusterProfileId("cluster_profile_id");
57-
HashMap<String, String> properties = new HashMap<>();
59+
properties = new HashMap<>();
5860
properties.put("some_key", "some_value");
5961
properties.put("some_key2", "some_value2");
6062
elasticAgentProfile.setProperties(properties);
@@ -89,8 +91,10 @@ public void shouldNotMigrateConfigWhenClusterProfileIsAlreadyConfigured() throws
8991
}
9092

9193
@Test
92-
public void shouldDefineANewClusterProfileFromPluginSettings() throws Exception {
93-
MigrateConfigurationRequest request = new MigrateConfigurationRequest(pluginSettings, Collections.emptyList(), Arrays.asList(elasticAgentProfile));
94+
public void shouldPopulateNoOpClusterProfileWithPluginSettingsConfigurations() throws Exception {
95+
ClusterProfile emptyClusterProfile = new ClusterProfile(String.format("no-op-cluster-for-%s", Constants.PLUGIN_ID), Constants.PLUGIN_ID, new PluginSettings());
96+
elasticAgentProfile.setClusterProfileId(emptyClusterProfile.getId());
97+
MigrateConfigurationRequest request = new MigrateConfigurationRequest(pluginSettings, Arrays.asList(emptyClusterProfile), Arrays.asList(elasticAgentProfile));
9498
MigrateConfigurationRequestExecutor executor = new MigrateConfigurationRequestExecutor(request);
9599

96100
GoPluginApiResponse response = executor.execute();
@@ -100,24 +104,153 @@ public void shouldDefineANewClusterProfileFromPluginSettings() throws Exception
100104
assertThat(responseObject.getPluginSettings(), is(pluginSettings));
101105
List<ClusterProfile> actual = responseObject.getClusterProfiles();
102106
ClusterProfile actualClusterProfile = actual.get(0);
107+
108+
assertThat(actualClusterProfile.getId(), is(not(String.format("no-op-cluster-for-%s", Constants.PLUGIN_ID))));
103109
this.clusterProfile.setId(actualClusterProfile.getId());
104110

105111
assertThat(actual, is(Arrays.asList(this.clusterProfile)));
106112
assertThat(responseObject.getElasticAgentProfiles(), is(Arrays.asList(elasticAgentProfile)));
113+
114+
assertThat(elasticAgentProfile.getClusterProfileId(), is(actualClusterProfile.getId()));
107115
}
108116

109117
@Test
110-
public void shouldAssociateExistingElasticAgentProfilesWithNewlyDefinedClusterProfile() throws Exception {
111-
MigrateConfigurationRequest request = new MigrateConfigurationRequest(pluginSettings, Collections.emptyList(), Arrays.asList(elasticAgentProfile));
118+
public void shouldPopulateNoOpClusterProfileWithPluginSettingsConfigurations_WithoutChangingClusterProfileIdIfItsNotNoOp() throws Exception {
119+
String clusterProfileId = "i-renamed-no-op-cluster-to-something-else";
120+
ClusterProfile emptyClusterProfile = new ClusterProfile(clusterProfileId, Constants.PLUGIN_ID, new PluginSettings());
121+
elasticAgentProfile.setClusterProfileId(emptyClusterProfile.getId());
122+
MigrateConfigurationRequest request = new MigrateConfigurationRequest(pluginSettings, Arrays.asList(emptyClusterProfile), Arrays.asList(elasticAgentProfile));
112123
MigrateConfigurationRequestExecutor executor = new MigrateConfigurationRequestExecutor(request);
113124

114125
GoPluginApiResponse response = executor.execute();
115126

116127
MigrateConfigurationRequest responseObject = MigrateConfigurationRequest.fromJSON(response.responseBody());
117128

118-
String newlyDefinedClusterId = responseObject.getClusterProfiles().get(0).getId();
119-
elasticAgentProfile.setClusterProfileId(newlyDefinedClusterId);
129+
assertThat(responseObject.getPluginSettings(), is(pluginSettings));
130+
List<ClusterProfile> actual = responseObject.getClusterProfiles();
131+
ClusterProfile actualClusterProfile = actual.get(0);
120132

133+
assertThat(actualClusterProfile.getId(), is(clusterProfileId));
134+
this.clusterProfile.setId(actualClusterProfile.getId());
135+
136+
assertThat(actual, is(Arrays.asList(this.clusterProfile)));
121137
assertThat(responseObject.getElasticAgentProfiles(), is(Arrays.asList(elasticAgentProfile)));
138+
139+
assertThat(elasticAgentProfile.getClusterProfileId(), is(clusterProfileId));
140+
}
141+
142+
@Test
143+
public void shouldMigratePluginSettingsToClusterProfile_WhenNoElasticAgentProfilesAreConfigured() throws Exception {
144+
MigrateConfigurationRequest request = new MigrateConfigurationRequest(pluginSettings, Collections.emptyList(), Collections.emptyList());
145+
MigrateConfigurationRequestExecutor executor = new MigrateConfigurationRequestExecutor(request);
146+
147+
GoPluginApiResponse response = executor.execute();
148+
149+
MigrateConfigurationRequest responseObject = MigrateConfigurationRequest.fromJSON(response.responseBody());
150+
151+
assertThat(responseObject.getPluginSettings(), is(pluginSettings));
152+
List<ClusterProfile> actual = responseObject.getClusterProfiles();
153+
ClusterProfile actualClusterProfile = actual.get(0);
154+
this.clusterProfile.setId(actualClusterProfile.getId());
155+
156+
assertThat(actual, is(Arrays.asList(this.clusterProfile)));
157+
assertThat(responseObject.getElasticAgentProfiles(), is(Collections.emptyList()));
158+
}
159+
160+
@Test
161+
public void ShouldMigrateEmptyClusterProfiles_WhenMultipleEmptyClusterProfilesExists() throws Exception {
162+
ClusterProfile emptyCluster1 = new ClusterProfile("cluster_profile_1", Constants.PLUGIN_ID, new PluginSettings());
163+
ClusterProfile emptyCluster2 = new ClusterProfile("cluster_profile_2", Constants.PLUGIN_ID, new PluginSettings());
164+
165+
ElasticAgentProfile elasticAgentProfile1 = new ElasticAgentProfile();
166+
elasticAgentProfile1.setId("profile_id_1");
167+
elasticAgentProfile1.setPluginId(Constants.PLUGIN_ID);
168+
elasticAgentProfile1.setClusterProfileId(emptyCluster1.getId());
169+
170+
ElasticAgentProfile elasticAgentProfile2 = new ElasticAgentProfile();
171+
elasticAgentProfile2.setId("profile_id_2");
172+
elasticAgentProfile2.setPluginId(Constants.PLUGIN_ID);
173+
elasticAgentProfile2.setClusterProfileId(emptyCluster2.getId());
174+
175+
MigrateConfigurationRequest request = new MigrateConfigurationRequest(pluginSettings, Arrays.asList(emptyCluster1, emptyCluster2), Arrays.asList(elasticAgentProfile1, elasticAgentProfile2));
176+
MigrateConfigurationRequestExecutor executor = new MigrateConfigurationRequestExecutor(request);
177+
178+
GoPluginApiResponse response = executor.execute();
179+
180+
MigrateConfigurationRequest responseObject = MigrateConfigurationRequest.fromJSON(response.responseBody());
181+
182+
assertThat(responseObject.getPluginSettings(), is(pluginSettings));
183+
184+
this.clusterProfile.setId(responseObject.getClusterProfiles().get(0).getId());
185+
assertThat(responseObject.getClusterProfiles().get(0), is(clusterProfile));
186+
187+
this.clusterProfile.setId(responseObject.getClusterProfiles().get(1).getId());
188+
assertThat(responseObject.getClusterProfiles().get(1), is(clusterProfile));
189+
190+
assertThat(responseObject.getElasticAgentProfiles().get(0).getClusterProfileId(), is(emptyCluster1.getId()));
191+
assertThat(responseObject.getElasticAgentProfiles().get(1).getClusterProfileId(), is(emptyCluster2.getId()));
192+
}
193+
194+
@Test
195+
public void ShouldNotMigrateEmptyAndUnassociatedClusterProfiles() throws Exception {
196+
ClusterProfile emptyCluster1 = new ClusterProfile("cluster_profile_1", Constants.PLUGIN_ID, new PluginSettings());
197+
ClusterProfile emptyCluster2 = new ClusterProfile("cluster_profile_2", Constants.PLUGIN_ID, new PluginSettings());
198+
199+
ElasticAgentProfile elasticAgentProfile1 = new ElasticAgentProfile();
200+
elasticAgentProfile1.setId("profile_id_1");
201+
elasticAgentProfile1.setPluginId(Constants.PLUGIN_ID);
202+
elasticAgentProfile1.setClusterProfileId(emptyCluster1.getId());
203+
204+
ElasticAgentProfile elasticAgentProfile2 = new ElasticAgentProfile();
205+
elasticAgentProfile2.setId("profile_id_2");
206+
elasticAgentProfile2.setPluginId(Constants.PLUGIN_ID);
207+
elasticAgentProfile2.setClusterProfileId(emptyCluster1.getId());
208+
209+
MigrateConfigurationRequest request = new MigrateConfigurationRequest(pluginSettings, Arrays.asList(emptyCluster1, emptyCluster2), Arrays.asList(elasticAgentProfile1, elasticAgentProfile2));
210+
MigrateConfigurationRequestExecutor executor = new MigrateConfigurationRequestExecutor(request);
211+
212+
GoPluginApiResponse response = executor.execute();
213+
214+
MigrateConfigurationRequest responseObject = MigrateConfigurationRequest.fromJSON(response.responseBody());
215+
216+
assertThat(responseObject.getPluginSettings(), is(pluginSettings));
217+
218+
this.clusterProfile.setId(responseObject.getClusterProfiles().get(0).getId());
219+
assertThat(responseObject.getClusterProfiles().get(0), is(clusterProfile));
220+
221+
//verify cluster is empty.. not migrated
222+
assertThat(responseObject.getClusterProfiles().get(1), is(emptyCluster2));
223+
224+
assertThat(responseObject.getElasticAgentProfiles().get(0).getClusterProfileId(), is(emptyCluster1.getId()));
225+
assertThat(responseObject.getElasticAgentProfiles().get(1).getClusterProfileId(), is(emptyCluster1.getId()));
226+
}
227+
228+
@Test
229+
public void shouldNotMigrateConfigWhenMultipleClusterProfilesAreAlreadyMigrated() throws Exception {
230+
ClusterProfile cluster1 = new ClusterProfile("cluster_profile_1", Constants.PLUGIN_ID, pluginSettings);
231+
ClusterProfile cluster2 = new ClusterProfile("cluster_profile_2", Constants.PLUGIN_ID, pluginSettings);
232+
233+
ElasticAgentProfile elasticAgentProfile1 = new ElasticAgentProfile();
234+
elasticAgentProfile1.setId("profile_id_1");
235+
elasticAgentProfile1.setPluginId(Constants.PLUGIN_ID);
236+
elasticAgentProfile1.setClusterProfileId(cluster1.getId());
237+
238+
ElasticAgentProfile elasticAgentProfile2 = new ElasticAgentProfile();
239+
elasticAgentProfile2.setId("profile_id_2");
240+
elasticAgentProfile2.setPluginId(Constants.PLUGIN_ID);
241+
elasticAgentProfile2.setClusterProfileId(cluster2.getId());
242+
243+
MigrateConfigurationRequest request = new MigrateConfigurationRequest(pluginSettings, Arrays.asList(cluster1, cluster2), Arrays.asList(elasticAgentProfile1, elasticAgentProfile2));
244+
MigrateConfigurationRequestExecutor executor = new MigrateConfigurationRequestExecutor(request);
245+
246+
GoPluginApiResponse response = executor.execute();
247+
248+
MigrateConfigurationRequest responseObject = MigrateConfigurationRequest.fromJSON(response.responseBody());
249+
250+
assertThat(responseObject.getPluginSettings(), is(pluginSettings));
251+
252+
assertThat(responseObject.getClusterProfiles(), is(Arrays.asList(cluster1, cluster2)));
253+
254+
assertThat(responseObject.getElasticAgentProfiles(), is(Arrays.asList(elasticAgentProfile1, elasticAgentProfile2)));
122255
}
123256
}

0 commit comments

Comments
 (0)