Skip to content

Commit

Permalink
Fix null config in SnapshotLifecyclePolicy.toRequest (#53328) (#53356)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
probakowski and elasticmachine authored Mar 10, 2020
1 parent b351492 commit 02f5d18
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,10 @@ public String generateSnapshotName(Context context) {
*/
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 02f5d18

Please sign in to comment.