org.elasticsearch.action.admin.indices.template.get.GetComponentTemplateAction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch Show documentation
Show all versions of elasticsearch Show documentation
Elasticsearch subproject :server
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.action.admin.indices.template.get;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.ActionType;
import org.elasticsearch.action.support.master.MasterNodeReadRequest;
import org.elasticsearch.cluster.metadata.ComponentTemplate;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
/**
* Action to retrieve one or more component templates
*/
public class GetComponentTemplateAction extends ActionType {
public static final GetComponentTemplateAction INSTANCE = new GetComponentTemplateAction();
public static final String NAME = "cluster:admin/component_template/get";
private GetComponentTemplateAction() {
super(NAME, GetComponentTemplateAction.Response::new);
}
/**
* Request that to retrieve one or more component templates
*/
public static class Request extends MasterNodeReadRequest {
@Nullable
private String name;
public Request() {}
public Request(String name) {
this.name = name;
}
public Request(StreamInput in) throws IOException {
super(in);
name = in.readOptionalString();
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeOptionalString(name);
}
@Override
public ActionRequestValidationException validate() {
return null;
}
/**
* Sets the name of the component templates.
*/
public Request name(String name) {
this.name = name;
return this;
}
/**
* The name of the component templates.
*/
public String name() {
return this.name;
}
}
public static class Response extends ActionResponse implements ToXContentObject {
public static final ParseField NAME = new ParseField("name");
public static final ParseField COMPONENT_TEMPLATES = new ParseField("component_templates");
public static final ParseField COMPONENT_TEMPLATE = new ParseField("component_template");
private final Map componentTemplates;
public Response(StreamInput in) throws IOException {
super(in);
componentTemplates = in.readMap(StreamInput::readString, ComponentTemplate::new);
}
public Response(Map componentTemplates) {
this.componentTemplates = componentTemplates;
}
public Map getComponentTemplates() {
return componentTemplates;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeMap(componentTemplates, StreamOutput::writeString, (o, v) -> v.writeTo(o));
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Response that = (Response) o;
return Objects.equals(componentTemplates, that.componentTemplates);
}
@Override
public int hashCode() {
return Objects.hash(componentTemplates);
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.startArray(COMPONENT_TEMPLATES.getPreferredName());
for (Map.Entry componentTemplate : this.componentTemplates.entrySet()) {
builder.startObject();
builder.field(NAME.getPreferredName(), componentTemplate.getKey());
builder.field(COMPONENT_TEMPLATE.getPreferredName(), componentTemplate.getValue());
builder.endObject();
}
builder.endArray();
builder.endObject();
return builder;
}
}
}