org.elasticsearch.xpack.core.transform.action.PreviewTransformAction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of x-pack-core Show documentation
Show all versions of x-pack-core Show documentation
Elasticsearch Expanded Pack Plugin - Core
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.xpack.core.transform.action;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.ActionType;
import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.xpack.core.common.validation.SourceDestValidator;
import org.elasticsearch.xpack.core.transform.TransformField;
import org.elasticsearch.xpack.core.transform.transforms.DestConfig;
import org.elasticsearch.xpack.core.transform.transforms.TransformConfig;
import org.elasticsearch.xpack.core.transform.transforms.TransformDestIndexSettings;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
public class PreviewTransformAction extends ActionType {
public static final PreviewTransformAction INSTANCE = new PreviewTransformAction();
public static final String NAME = "cluster:admin/transform/preview";
private PreviewTransformAction() {
super(NAME, PreviewTransformAction.Response::new);
}
public static class Request extends AcknowledgedRequest implements ToXContentObject {
private final TransformConfig config;
public Request(TransformConfig config) {
this.config = config;
}
public Request(StreamInput in) throws IOException {
super(in);
this.config = new TransformConfig(in);
}
public static Request fromXContent(final XContentParser parser) throws IOException {
Map content = parser.map();
// dest.index and ID are not required for Preview, so we just supply our own
Map tempDestination = new HashMap<>();
tempDestination.put(DestConfig.INDEX.getPreferredName(), "unused-transform-preview-index");
// Users can still provide just dest.pipeline to preview what their data would look like given the pipeline ID
Object providedDestination = content.get(TransformField.DESTINATION.getPreferredName());
if (providedDestination instanceof Map) {
@SuppressWarnings("unchecked")
Map destMap = (Map) providedDestination;
String pipeline = destMap.get(DestConfig.PIPELINE.getPreferredName());
if (pipeline != null) {
tempDestination.put(DestConfig.PIPELINE.getPreferredName(), pipeline);
}
}
content.put(TransformField.DESTINATION.getPreferredName(), tempDestination);
content.putIfAbsent(TransformField.ID.getPreferredName(), "transform-preview");
try (
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().map(content);
XContentParser newParser = XContentType.JSON.xContent()
.createParser(
parser.getXContentRegistry(),
LoggingDeprecationHandler.INSTANCE,
BytesReference.bytes(xContentBuilder).streamInput()
)
) {
return new Request(TransformConfig.fromXContent(newParser, null, false));
}
}
@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
validationException = config.validate(validationException);
validationException = SourceDestValidator.validateRequest(
validationException,
config.getDestination() != null ? config.getDestination().getIndex() : null
);
return validationException;
}
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return this.config.toXContent(builder, params);
}
public TransformConfig getConfig() {
return config;
}
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
this.config.writeTo(out);
}
@Override
public int hashCode() {
return Objects.hash(config);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Request other = (Request) obj;
return Objects.equals(config, other.config);
}
}
public static class Response extends ActionResponse implements ToXContentObject {
public static final ParseField PREVIEW = new ParseField("preview");
public static final ParseField GENERATED_DEST_INDEX_SETTINGS = new ParseField("generated_dest_index");
private final List