Skip to content

Commit ead99f1

Browse files
authored
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 e0c2ac1 commit ead99f1

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
"retention": {
41+
"expire_after": "30d",
42+
"min_count": 1,
43+
"max_count": 50
44+
}
45+
}
46+
47+
- do:
48+
slm.get_lifecycle:
49+
policy_id: "daily-snapshots"
50+
- match: { daily-snapshots.version: 1 }
51+
- match: { daily-snapshots.policy.name: "<production-snap-{now/d}>" }
52+
- is_true: daily-snapshots.next_execution_millis
53+
- is_true: daily-snapshots.stats
54+
- match: { daily-snapshots.policy.schedule: "0 1 2 3 4 ?" }
55+
56+
- do:
57+
slm.put_lifecycle:
58+
policy_id: "daily-snapshots"
59+
body: |
60+
{
61+
"schedule": "1 1 1 1 1 ?",
62+
"name": "<production-snap-{now/d}>",
63+
"repository": "repo",
64+
"config": {
65+
"indices": ["foo-*", "important"],
66+
"ignore_unavailable": false,
67+
"include_global_state": false
68+
},
69+
"retention": {
70+
"expire_after": "30d",
71+
"min_count": 1,
72+
"max_count": 50
73+
}
74+
}
75+
76+
- do:
77+
catch: missing
78+
slm.get_lifecycle:
79+
policy_id: "doesnt-exist"
80+
81+
- do:
82+
slm.get_lifecycle:
83+
policy_id: "daily-snapshots"
84+
- match: { daily-snapshots.version: 2 }
85+
- match: { daily-snapshots.policy.schedule: "1 1 1 1 1 ?" }
86+
- is_true: daily-snapshots.next_execution_millis
87+
- is_true: daily-snapshots.stats

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;
@@ -65,7 +66,13 @@ protected void masterOperation(final Task task, final GetSnapshotLifecycleAction
6566
final ActionListener<GetSnapshotLifecycleAction.Response> listener) {
6667
SnapshotLifecycleMetadata snapMeta = state.metaData().custom(SnapshotLifecycleMetadata.TYPE);
6768
if (snapMeta == null) {
68-
listener.onResponse(new GetSnapshotLifecycleAction.Response(Collections.emptyList()));
69+
if (request.getLifecycleIds().length == 0) {
70+
listener.onResponse(new GetSnapshotLifecycleAction.Response(Collections.emptyList()));
71+
} else {
72+
listener.onFailure(new ResourceNotFoundException(
73+
"snapshot lifecycle policy or policies {} not found, no policies are configured",
74+
Arrays.toString(request.getLifecycleIds())));
75+
}
6976
} else {
7077
final Map<String, SnapshotLifecyclePolicyItem.SnapshotInProgress> inProgress;
7178
SnapshotsInProgress sip = state.custom(SnapshotsInProgress.TYPE);
@@ -100,7 +107,16 @@ protected void masterOperation(final Task task, final GetSnapshotLifecycleAction
100107
new SnapshotLifecyclePolicyItem(policyMeta, inProgress.get(policyMeta.getPolicy().getId()),
101108
slmStats.getMetrics().get(policyMeta.getPolicy().getId())))
102109
.collect(Collectors.toList());
103-
listener.onResponse(new GetSnapshotLifecycleAction.Response(lifecycles));
110+
if (lifecycles.size() == 0) {
111+
if (request.getLifecycleIds().length == 0) {
112+
listener.onResponse(new GetSnapshotLifecycleAction.Response(Collections.emptyList()));
113+
} else {
114+
listener.onFailure(new ResourceNotFoundException("snapshot lifecycle policy or policies {} not found",
115+
Arrays.toString(request.getLifecycleIds())));
116+
}
117+
} else {
118+
listener.onResponse(new GetSnapshotLifecycleAction.Response(lifecycles));
119+
}
104120
}
105121
}
106122

0 commit comments

Comments
 (0)