Change signature of Get Repositories Response (#30333)

The Get Repositories response object held a list of RepositoryMetaData
entries. This object does not have the from/toXContent methods that are
needed to expose this to the high level REST client. The
RepositoriesMetaData, however, does, and it also contains a list of
RepositoryMetaData objects within it. So rather than duplicate this
logic or move it (RepositoriesMetaData is a fragment object used by
cluster state), the object holding state in the Response was changed to
use the RepositoriesMetaData instead. This also cleans up the read/write
methods in the response, as they can now use the same read/write in
RepositoriesMetaData, which also were not present in the singular class.
This commit is contained in:
Michael Basnight 2018-05-03 07:22:59 -05:00 committed by GitHub
parent 18394540ab
commit bdd43fa69f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 54 deletions

View File

@ -20,13 +20,14 @@
package org.elasticsearch.action.admin.cluster.repositories.get;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.cluster.metadata.RepositoriesMetaData;
import org.elasticsearch.cluster.metadata.RepositoryMetaData;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@ -34,15 +35,15 @@ import java.util.List;
/**
* Get repositories response
*/
public class GetRepositoriesResponse extends ActionResponse implements Iterable<RepositoryMetaData> {
private List<RepositoryMetaData> repositories = Collections.emptyList();
public class GetRepositoriesResponse extends ActionResponse implements ToXContentObject {
private RepositoriesMetaData repositories;
GetRepositoriesResponse() {
repositories = new RepositoriesMetaData(Collections.emptyList());
}
GetRepositoriesResponse(List<RepositoryMetaData> repositories) {
GetRepositoriesResponse(RepositoriesMetaData repositories) {
this.repositories = repositories;
}
@ -52,43 +53,25 @@ public class GetRepositoriesResponse extends ActionResponse implements Iterable<
* @return list or repositories
*/
public List<RepositoryMetaData> repositories() {
return repositories;
return repositories.repositories();
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
int size = in.readVInt();
List<RepositoryMetaData> repositoryListBuilder = new ArrayList<>(size);
for (int j = 0; j < size; j++) {
repositoryListBuilder.add(new RepositoryMetaData(
in.readString(),
in.readString(),
Settings.readSettingsFromStream(in))
);
}
repositories = Collections.unmodifiableList(repositoryListBuilder);
repositories = new RepositoriesMetaData(in);
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeVInt(repositories.size());
for (RepositoryMetaData repository : repositories) {
out.writeString(repository.name());
out.writeString(repository.type());
Settings.writeSettingsToStream(repository.settings(), out);
}
repositories.writeTo(out);
}
/**
* Iterator over the repositories data
*
* @return iterator over the repositories data
*/
@Override
public Iterator<RepositoryMetaData> iterator() {
return repositories.iterator();
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
repositories.toXContent(builder, params);
builder.endObject();
return builder;
}
}

View File

@ -75,9 +75,9 @@ public class TransportGetRepositoriesAction extends TransportMasterNodeReadActio
RepositoriesMetaData repositories = metaData.custom(RepositoriesMetaData.TYPE);
if (request.repositories().length == 0 || (request.repositories().length == 1 && "_all".equals(request.repositories()[0]))) {
if (repositories != null) {
listener.onResponse(new GetRepositoriesResponse(repositories.repositories()));
listener.onResponse(new GetRepositoriesResponse(repositories));
} else {
listener.onResponse(new GetRepositoriesResponse(Collections.<RepositoryMetaData>emptyList()));
listener.onResponse(new GetRepositoriesResponse(new RepositoriesMetaData(Collections.emptyList())));
}
} else {
if (repositories != null) {
@ -102,7 +102,7 @@ public class TransportGetRepositoriesAction extends TransportMasterNodeReadActio
}
repositoryListBuilder.add(repositoryMetaData);
}
listener.onResponse(new GetRepositoriesResponse(Collections.unmodifiableList(repositoryListBuilder)));
listener.onResponse(new GetRepositoriesResponse(new RepositoriesMetaData(repositoryListBuilder)));
} else {
listener.onFailure(new RepositoryMissingException(request.repositories()[0]));
}

View File

@ -22,25 +22,19 @@ package org.elasticsearch.rest.action.admin.cluster;
import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesRequest;
import org.elasticsearch.action.admin.cluster.repositories.get.GetRepositoriesResponse;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.cluster.metadata.RepositoriesMetaData;
import org.elasticsearch.cluster.metadata.RepositoryMetaData;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsFilter;
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.RestResponse;
import org.elasticsearch.rest.action.RestBuilderListener;
import org.elasticsearch.rest.action.RestToXContentListener;
import java.io.IOException;
import java.util.Set;
import static org.elasticsearch.client.Requests.getRepositoryRequest;
import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestStatus.OK;
/**
* Returns repository information
@ -69,18 +63,8 @@ public class RestGetRepositoriesAction extends BaseRestHandler {
getRepositoriesRequest.local(request.paramAsBoolean("local", getRepositoriesRequest.local()));
settingsFilter.addFilterSettingParams(request);
return channel ->
client.admin().cluster().getRepositories(getRepositoriesRequest, new RestBuilderListener<GetRepositoriesResponse>(channel) {
@Override
public RestResponse buildResponse(GetRepositoriesResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
for (RepositoryMetaData repositoryMetaData : response.repositories()) {
RepositoriesMetaData.toXContent(repositoryMetaData, builder, request);
}
builder.endObject();
return new BytesRestResponse(OK, builder);
}
});
client.admin().cluster().getRepositories(getRepositoriesRequest,
new RestToXContentListener<>(channel));
}
@Override