From 6099e83532ddadeaf026196a6e27b66788c725df Mon Sep 17 00:00:00 2001 From: Lee Hinman Date: Mon, 7 Oct 2019 19:53:46 -0600 Subject: [PATCH] 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 --- .../rest-api-spec/test/ilm/11_basic_slm.yml | 75 +++++++++++++++++++ .../TransportGetSnapshotLifecycleAction.java | 20 ++++- 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 x-pack/plugin/ilm/qa/rest/src/test/resources/rest-api-spec/test/ilm/11_basic_slm.yml diff --git a/x-pack/plugin/ilm/qa/rest/src/test/resources/rest-api-spec/test/ilm/11_basic_slm.yml b/x-pack/plugin/ilm/qa/rest/src/test/resources/rest-api-spec/test/ilm/11_basic_slm.yml new file mode 100644 index 0000000000000..8ace97277d277 --- /dev/null +++ b/x-pack/plugin/ilm/qa/rest/src/test/resources/rest-api-spec/test/ilm/11_basic_slm.yml @@ -0,0 +1,75 @@ +--- +setup: + - do: + cluster.health: + wait_for_status: yellow + +--- +"Test Basic Policy CRUD": + - do: + catch: missing + slm.get_lifecycle: + policy_id: "daily-snapshots" + + - do: + catch: missing + slm.delete_lifecycle: + policy_id: "daily-snapshots" + + - do: + snapshot.create_repository: + repository: repo + body: + type: fs + settings: + location: "my-snaps" + + - do: + slm.put_lifecycle: + policy_id: "daily-snapshots" + body: | + { + "schedule": "0 1 2 3 4 ?", + "name": "", + "repository": "repo", + "config": { + "indices": ["foo-*", "important"], + "ignore_unavailable": false, + "include_global_state": false + } + } + + - do: + slm.get_lifecycle: + policy_id: "daily-snapshots" + - match: { daily-snapshots.version: 1 } + - match: { daily-snapshots.policy.name: "" } + - is_true: daily-snapshots.next_execution_millis + - match: { daily-snapshots.policy.schedule: "0 1 2 3 4 ?" } + + - do: + slm.put_lifecycle: + policy_id: "daily-snapshots" + body: | + { + "schedule": "1 1 1 1 1 ?", + "name": "", + "repository": "repo", + "config": { + "indices": ["foo-*", "important"], + "ignore_unavailable": false, + "include_global_state": false + } + } + + - do: + catch: missing + slm.get_lifecycle: + policy_id: "doesnt-exist" + + - do: + slm.get_lifecycle: + policy_id: "daily-snapshots" + - match: { daily-snapshots.version: 2 } + - match: { daily-snapshots.policy.schedule: "1 1 1 1 1 ?" } + - is_true: daily-snapshots.next_execution_millis diff --git a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportGetSnapshotLifecycleAction.java b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportGetSnapshotLifecycleAction.java index 98331b9f639ba..cda6902f07af0 100644 --- a/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportGetSnapshotLifecycleAction.java +++ b/x-pack/plugin/ilm/src/main/java/org/elasticsearch/xpack/slm/action/TransportGetSnapshotLifecycleAction.java @@ -8,6 +8,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.elasticsearch.ResourceNotFoundException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.master.TransportMasterNodeAction; @@ -63,7 +64,13 @@ protected void masterOperation(final GetSnapshotLifecycleAction.Request request, final ActionListener listener) { SnapshotLifecycleMetadata snapMeta = state.metaData().custom(SnapshotLifecycleMetadata.TYPE); if (snapMeta == null) { - listener.onResponse(new GetSnapshotLifecycleAction.Response(Collections.emptyList())); + if (request.getLifecycleIds().length == 0) { + listener.onResponse(new GetSnapshotLifecycleAction.Response(Collections.emptyList())); + } else { + listener.onFailure(new ResourceNotFoundException( + "snapshot lifecycle policy or policies {} not found, no policies are configured", + Arrays.toString(request.getLifecycleIds()))); + } } else { final Map inProgress; SnapshotsInProgress sip = state.custom(SnapshotsInProgress.TYPE); @@ -95,7 +102,16 @@ protected void masterOperation(final GetSnapshotLifecycleAction.Request request, }) .map(policyMeta -> new SnapshotLifecyclePolicyItem(policyMeta, inProgress.get(policyMeta.getPolicy().getId()))) .collect(Collectors.toList()); - listener.onResponse(new GetSnapshotLifecycleAction.Response(lifecycles)); + if (lifecycles.size() == 0) { + if (request.getLifecycleIds().length == 0) { + listener.onResponse(new GetSnapshotLifecycleAction.Response(Collections.emptyList())); + } else { + listener.onFailure(new ResourceNotFoundException("snapshot lifecycle policy or policies {} not found", + Arrays.toString(request.getLifecycleIds()))); + } + } else { + listener.onResponse(new GetSnapshotLifecycleAction.Response(lifecycles)); + } } }