Skip to content

Commit 6099e83

Browse files
committed
Throw error retrieving non-existent SLM policy (#47679)
Previously when retrieving an SLM policy it would always return a 200 with `{}` in the body, even if the policy did not exist. This changes that behavior to throw an error (similar to our other APIs) if a policy doesn't exist. This also adds a basic CRUD yml test for the behavior. Resolves #47664
1 parent 1aff080 commit 6099e83

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
setup:
3+
- do:
4+
cluster.health:
5+
wait_for_status: yellow
6+
7+
---
8+
"Test Basic Policy CRUD":
9+
- do:
10+
catch: missing
11+
slm.get_lifecycle:
12+
policy_id: "daily-snapshots"
13+
14+
- do:
15+
catch: missing
16+
slm.delete_lifecycle:
17+
policy_id: "daily-snapshots"
18+
19+
- do:
20+
snapshot.create_repository:
21+
repository: repo
22+
body:
23+
type: fs
24+
settings:
25+
location: "my-snaps"
26+
27+
- do:
28+
slm.put_lifecycle:
29+
policy_id: "daily-snapshots"
30+
body: |
31+
{
32+
"schedule": "0 1 2 3 4 ?",
33+
"name": "<production-snap-{now/d}>",
34+
"repository": "repo",
35+
"config": {
36+
"indices": ["foo-*", "important"],
37+
"ignore_unavailable": false,
38+
"include_global_state": false
39+
}
40+
}
41+
42+
- do:
43+
slm.get_lifecycle:
44+
policy_id: "daily-snapshots"
45+
- match: { daily-snapshots.version: 1 }
46+
- match: { daily-snapshots.policy.name: "<production-snap-{now/d}>" }
47+
- is_true: daily-snapshots.next_execution_millis
48+
- match: { daily-snapshots.policy.schedule: "0 1 2 3 4 ?" }
49+
50+
- do:
51+
slm.put_lifecycle:
52+
policy_id: "daily-snapshots"
53+
body: |
54+
{
55+
"schedule": "1 1 1 1 1 ?",
56+
"name": "<production-snap-{now/d}>",
57+
"repository": "repo",
58+
"config": {
59+
"indices": ["foo-*", "important"],
60+
"ignore_unavailable": false,
61+
"include_global_state": false
62+
}
63+
}
64+
65+
- do:
66+
catch: missing
67+
slm.get_lifecycle:
68+
policy_id: "doesnt-exist"
69+
70+
- do:
71+
slm.get_lifecycle:
72+
policy_id: "daily-snapshots"
73+
- match: { daily-snapshots.version: 2 }
74+
- match: { daily-snapshots.policy.schedule: "1 1 1 1 1 ?" }
75+
- is_true: daily-snapshots.next_execution_millis

x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportGetSnapshotLifecycleAction.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import org.apache.logging.log4j.LogManager;
1010
import org.apache.logging.log4j.Logger;
11+
import org.elasticsearch.ResourceNotFoundException;
1112
import org.elasticsearch.action.ActionListener;
1213
import org.elasticsearch.action.support.ActionFilters;
1314
import org.elasticsearch.action.support.master.TransportMasterNodeAction;
@@ -63,7 +64,13 @@ protected void masterOperation(final GetSnapshotLifecycleAction.Request request,
6364
final ActionListener<GetSnapshotLifecycleAction.Response> listener) {
6465
SnapshotLifecycleMetadata snapMeta = state.metaData().custom(SnapshotLifecycleMetadata.TYPE);
6566
if (snapMeta == null) {
66-
listener.onResponse(new GetSnapshotLifecycleAction.Response(Collections.emptyList()));
67+
if (request.getLifecycleIds().length == 0) {
68+
listener.onResponse(new GetSnapshotLifecycleAction.Response(Collections.emptyList()));
69+
} else {
70+
listener.onFailure(new ResourceNotFoundException(
71+
"snapshot lifecycle policy or policies {} not found, no policies are configured",
72+
Arrays.toString(request.getLifecycleIds())));
73+
}
6774
} else {
6875
final Map<String, SnapshotLifecyclePolicyItem.SnapshotInProgress> inProgress;
6976
SnapshotsInProgress sip = state.custom(SnapshotsInProgress.TYPE);
@@ -95,7 +102,16 @@ protected void masterOperation(final GetSnapshotLifecycleAction.Request request,
95102
})
96103
.map(policyMeta -> new SnapshotLifecyclePolicyItem(policyMeta, inProgress.get(policyMeta.getPolicy().getId())))
97104
.collect(Collectors.toList());
98-
listener.onResponse(new GetSnapshotLifecycleAction.Response(lifecycles));
105+
if (lifecycles.size() == 0) {
106+
if (request.getLifecycleIds().length == 0) {
107+
listener.onResponse(new GetSnapshotLifecycleAction.Response(Collections.emptyList()));
108+
} else {
109+
listener.onFailure(new ResourceNotFoundException("snapshot lifecycle policy or policies {} not found",
110+
Arrays.toString(request.getLifecycleIds())));
111+
}
112+
} else {
113+
listener.onResponse(new GetSnapshotLifecycleAction.Response(lifecycles));
114+
}
99115
}
100116
}
101117

0 commit comments

Comments
 (0)