Fix null config in SnapshotLifecyclePolicy.toRequest (#53328) (#53355)

This avoids NPE when executing SLM policy when no config was provided.

Related to #44465

Closes #53171

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Przemko Robakowski 2020-03-10 20:44:30 +01:00 committed by GitHub
parent 20bbe5bae4
commit 847ac9c7d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -254,10 +254,10 @@ public class SnapshotLifecyclePolicy extends AbstractDiffable<SnapshotLifecycleP
*/
public CreateSnapshotRequest toRequest() {
CreateSnapshotRequest req = new CreateSnapshotRequest(repository, generateSnapshotName(new ResolverContext()));
Map<String, Object> mergedConfiguration = configuration == null ? new HashMap<>() : new HashMap<>(configuration);
@SuppressWarnings("unchecked")
Map<String, Object> metadata = (Map<String, Object>) configuration.get("metadata");
Map<String, Object> metadata = (Map<String, Object>) mergedConfiguration.get("metadata");
Map<String, Object> metadataWithAddedPolicyName = addPolicyNameToMetadata(metadata);
Map<String, Object> mergedConfiguration = new HashMap<>(configuration);
mergedConfiguration.put("metadata", metadataWithAddedPolicyName);
req.source(mergedConfiguration);
req.waitForCompletion(true);

View File

@ -6,6 +6,7 @@
package org.elasticsearch.xpack.slm;
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest;
import org.elasticsearch.common.ValidationException;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.XContentParser;
@ -30,6 +31,18 @@ public class SnapshotLifecyclePolicyTests extends AbstractSerializingTestCase<Sn
private String id;
public void testToRequest() {
SnapshotLifecyclePolicy p = new SnapshotLifecyclePolicy("id", "name", "0 1 2 3 4 ? 2099", "repo", Collections.emptyMap(),
SnapshotRetentionConfiguration.EMPTY);
CreateSnapshotRequest request = p.toRequest();
CreateSnapshotRequest expected = new CreateSnapshotRequest().userMetadata(Collections.singletonMap("policy", "id"));
p = new SnapshotLifecyclePolicy("id", "name", "0 1 2 3 4 ? 2099", "repo", null, null);
request = p.toRequest();
expected.waitForCompletion(true).snapshot(request.snapshot()).repository("repo");
assertEquals(expected, request);
}
public void testNameGeneration() {
long time = 1552684146542L; // Fri Mar 15 2019 21:09:06 UTC
SnapshotLifecyclePolicy.ResolverContext context = new SnapshotLifecyclePolicy.ResolverContext(time);