diff --git a/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestGetSnapshotsAction.java b/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestGetSnapshotsAction.java index f42180b5029b8..160bada6474d0 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestGetSnapshotsAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestGetSnapshotsAction.java @@ -20,18 +20,26 @@ package org.elasticsearch.rest.action.admin.cluster; import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest; +import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.BaseRestHandler; +import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.RestToXContentListener; +import org.elasticsearch.rest.RestResponse; +import org.elasticsearch.rest.action.RestBuilderListener; import java.io.IOException; +import java.util.Collections; +import java.util.Map; import static org.elasticsearch.client.Requests.getSnapshotsRequest; import static org.elasticsearch.rest.RestRequest.Method.GET; +import static org.elasticsearch.rest.RestStatus.OK; /** * Returns information about snapshot @@ -52,10 +60,19 @@ public RestChannelConsumer prepareRequest(final RestRequest request, final NodeC String repository = request.param("repository"); String[] snapshots = request.paramAsStringArray("snapshot", Strings.EMPTY_ARRAY); - GetSnapshotsRequest getSnapshotsRequest = getSnapshotsRequest(repository).snapshots(snapshots); + final GetSnapshotsRequest getSnapshotsRequest = getSnapshotsRequest(repository).snapshots(snapshots); getSnapshotsRequest.ignoreUnavailable(request.paramAsBoolean("ignore_unavailable", getSnapshotsRequest.ignoreUnavailable())); getSnapshotsRequest.verbose(request.paramAsBoolean("verbose", getSnapshotsRequest.verbose())); getSnapshotsRequest.masterNodeTimeout(request.paramAsTime("master_timeout", getSnapshotsRequest.masterNodeTimeout())); - return channel -> client.admin().cluster().getSnapshots(getSnapshotsRequest, new RestToXContentListener<>(channel)); + return channel -> client.admin().cluster().getSnapshots(getSnapshotsRequest, + new RestBuilderListener(channel) { + @Override + public RestResponse buildResponse(GetSnapshotsResponse response, XContentBuilder builder) throws Exception { + Map params = Collections.singletonMap("verbose", Boolean.toString(getSnapshotsRequest.verbose())); + ToXContent.MapParams xContentParams = new ToXContent.MapParams(params); + response.toXContent(builder, xContentParams); + return new BytesRestResponse(OK, builder); + } + }); } } diff --git a/core/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java b/core/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java index a54e72159f8ae..de6a4e2987e25 100644 --- a/core/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java +++ b/core/src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java @@ -346,6 +346,7 @@ public XContentBuilder toXContent(final XContentBuilder builder, final Params pa return toXContentSnapshot(builder, params); } + final boolean verbose = params.paramAsBoolean("verbose", false); // write snapshot info for the API and any other situations builder.startObject(); builder.field(SNAPSHOT, snapshotId.getName()); @@ -359,22 +360,22 @@ public XContentBuilder toXContent(final XContentBuilder builder, final Params pa builder.value(index); } builder.endArray(); - if (state != null) { + if (verbose || state != null) { builder.field(STATE, state); } if (reason != null) { builder.field(REASON, reason); } - if (startTime != 0) { + if (verbose || startTime != 0) { builder.field(START_TIME, DATE_TIME_FORMATTER.printer().print(startTime)); builder.field(START_TIME_IN_MILLIS, startTime); } - if (endTime != 0) { + if (verbose || endTime != 0) { builder.field(END_TIME, DATE_TIME_FORMATTER.printer().print(endTime)); builder.field(END_TIME_IN_MILLIS, endTime); builder.timeValueField(DURATION_IN_MILLIS, DURATION, endTime - startTime); } - if (!shardFailures.isEmpty()) { + if (verbose || !shardFailures.isEmpty()) { builder.startArray(FAILURES); for (SnapshotShardFailure shardFailure : shardFailures) { builder.startObject(); @@ -383,7 +384,7 @@ public XContentBuilder toXContent(final XContentBuilder builder, final Params pa } builder.endArray(); } - if (totalShards != 0) { + if (verbose || totalShards != 0) { builder.startObject(SHARDS); builder.field(TOTAL, totalShards); builder.field(FAILED, failedShards()); diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.get/10_basic.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.get/10_basic.yml index 515662355da3e..70008415122d3 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.get/10_basic.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/snapshot.get/10_basic.yml @@ -32,6 +32,7 @@ setup: snapshot: test_snapshot - is_true: snapshots + - is_true: snapshots.0.failures - do: snapshot.delete: @@ -87,6 +88,8 @@ setup: - is_true: snapshots - match: { snapshots.0.snapshot: test_snapshot } - match: { snapshots.0.state: SUCCESS } + - is_false: snapshots.0.failures + - is_false: snapshots.0.shards - is_false: snapshots.0.version - do: