Skip to content

Commit

Permalink
remove lang url parameter from stored script requests (#25779)
Browse files Browse the repository at this point in the history
Also has updates to ScriptMetaData for allowing the old namespace format to be loaded all the way back through 5.0; however, it will throw an exception if two scripts share the same id but different languages.
  • Loading branch information
jdconrad committed Jul 20, 2017
1 parent 137ab70 commit 9f7463e
Show file tree
Hide file tree
Showing 33 changed files with 283 additions and 591 deletions.
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.action.admin.cluster.storedscripts;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.common.io.stream.StreamInput;
Expand All @@ -31,17 +32,15 @@
public class DeleteStoredScriptRequest extends AcknowledgedRequest<DeleteStoredScriptRequest> {

private String id;
private String lang;

DeleteStoredScriptRequest() {
super();
}

public DeleteStoredScriptRequest(String id, String lang) {
public DeleteStoredScriptRequest(String id) {
super();

this.id = id;
this.lang = lang;
}

@Override
Expand All @@ -54,10 +53,6 @@ public ActionRequestValidationException validate() {
validationException = addValidationError("id cannot contain '#' for stored script", validationException);
}

if (lang != null && lang.contains("#")) {
validationException = addValidationError("lang cannot contain '#' for stored script", validationException);
}

return validationException;
}

Expand All @@ -71,24 +66,12 @@ public DeleteStoredScriptRequest id(String id) {
return this;
}

public String lang() {
return lang;
}

public DeleteStoredScriptRequest lang(String lang) {
this.lang = lang;

return this;
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);

lang = in.readString();

if (lang.isEmpty()) {
lang = null;
if (in.getVersion().before(Version.V_6_0_0_alpha2)) {
in.readString(); // read lang from previous versions
}

id = in.readString();
Expand All @@ -98,12 +81,15 @@ public void readFrom(StreamInput in) throws IOException {
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);

out.writeString(lang == null ? "" : lang);
if (out.getVersion().before(Version.V_6_0_0_alpha2)) {
out.writeString(""); // write an empty lang to previous versions
}

out.writeString(id);
}

@Override
public String toString() {
return "delete stored script {id [" + id + "]" + (lang != null ? ", lang [" + lang + "]" : "") + "}";
return "delete stored script {id [" + id + "]}";
}
}
Expand Up @@ -29,12 +29,6 @@ public DeleteStoredScriptRequestBuilder(ElasticsearchClient client, DeleteStored
super(client, action, new DeleteStoredScriptRequest());
}

public DeleteStoredScriptRequestBuilder setLang(String lang) {
request.lang(lang);

return this;
}

public DeleteStoredScriptRequestBuilder setId(String id) {
request.id(id);

Expand Down
Expand Up @@ -19,10 +19,9 @@

package org.elasticsearch.action.admin.cluster.storedscripts;

import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.ValidateActions;
import org.elasticsearch.action.support.master.MasterNodeReadRequest;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;

Expand All @@ -33,17 +32,15 @@
public class GetStoredScriptRequest extends MasterNodeReadRequest<GetStoredScriptRequest> {

protected String id;
protected String lang;

GetStoredScriptRequest() {
super();
}

public GetStoredScriptRequest(String id, String lang) {
public GetStoredScriptRequest(String id) {
super();

this.id = id;
this.lang = lang;
}

@Override
Expand All @@ -56,10 +53,6 @@ public ActionRequestValidationException validate() {
validationException = addValidationError("id cannot contain '#' for stored script", validationException);
}

if (lang != null && lang.contains("#")) {
validationException = addValidationError("lang cannot contain '#' for stored script", validationException);
}

return validationException;
}

Expand All @@ -73,24 +66,12 @@ public GetStoredScriptRequest id(String id) {
return this;
}

public String lang() {
return lang;
}

public GetStoredScriptRequest lang(String lang) {
this.lang = lang;

return this;
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);

lang = in.readString();

if (lang.isEmpty()) {
lang = null;
if (in.getVersion().before(Version.V_6_0_0_alpha2)) {
in.readString(); // read lang from previous versions
}

id = in.readString();
Expand All @@ -100,12 +81,15 @@ public void readFrom(StreamInput in) throws IOException {
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);

out.writeString(lang == null ? "" : lang);
if (out.getVersion().before(Version.V_6_0_0_alpha2)) {
out.writeString(""); // write an empty lang to previous versions
}

out.writeString(id);
}

@Override
public String toString() {
return "get script [" + lang + "][" + id + "]";
return "get script [" + id + "]";
}
}
Expand Up @@ -21,7 +21,6 @@

import org.elasticsearch.action.support.master.MasterNodeReadOperationRequestBuilder;
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.common.Nullable;

public class GetStoredScriptRequestBuilder extends MasterNodeReadOperationRequestBuilder<GetStoredScriptRequest,
GetStoredScriptResponse, GetStoredScriptRequestBuilder> {
Expand All @@ -31,11 +30,6 @@ public GetStoredScriptRequestBuilder(ElasticsearchClient client, GetStoredScript
super(client, action, new GetStoredScriptRequest());
}

public GetStoredScriptRequestBuilder setLang(@Nullable String lang) {
request.lang(lang);
return this;
}

public GetStoredScriptRequestBuilder setId(String id) {
request.id(id);
return this;
Expand Down
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.script.StoredScriptSource;

import java.io.IOException;
import java.util.Objects;
Expand All @@ -37,22 +38,22 @@
public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptRequest> {

private String id;
private String lang;
private String context;
private BytesReference content;
private XContentType xContentType;
private StoredScriptSource source;

public PutStoredScriptRequest() {
super();
}

public PutStoredScriptRequest(String id, String lang, String context, BytesReference content, XContentType xContentType) {
public PutStoredScriptRequest(String id, String context, BytesReference content, XContentType xContentType, StoredScriptSource source) {
super();
this.id = id;
this.lang = lang;
this.context = context;
this.content = content;
this.xContentType = Objects.requireNonNull(xContentType);
this.source = source;
}

@Override
Expand All @@ -65,10 +66,6 @@ public ActionRequestValidationException validate() {
validationException = addValidationError("id cannot contain '#' for stored script", validationException);
}

if (lang != null && lang.contains("#")) {
validationException = addValidationError("lang cannot contain '#' for stored script", validationException);
}

if (content == null) {
validationException = addValidationError("must specify code for stored script", validationException);
}
Expand All @@ -82,17 +79,6 @@ public String id() {

public PutStoredScriptRequest id(String id) {
this.id = id;

return this;
}

public String lang() {
return lang;
}

public PutStoredScriptRequest lang(String lang) {
this.lang = lang;

return this;
}

Expand All @@ -113,25 +99,27 @@ public XContentType xContentType() {
return xContentType;
}

public StoredScriptSource source() {
return source;
}

/**
* Set the script source and the content type of the bytes.
*/
public PutStoredScriptRequest content(BytesReference content, XContentType xContentType) {
this.content = content;
this.xContentType = Objects.requireNonNull(xContentType);
this.source = StoredScriptSource.parse(content, xContentType);
return this;
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);

lang = in.readString();

if (lang.isEmpty()) {
lang = null;
if (in.getVersion().before(Version.V_6_0_0_alpha2)) {
in.readString(); // read lang from previous versions
}

id = in.readOptionalString();
content = in.readBytesReference();
if (in.getVersion().onOrAfter(Version.V_5_3_0)) {
Expand All @@ -141,21 +129,27 @@ public void readFrom(StreamInput in) throws IOException {
}
if (in.getVersion().onOrAfter(Version.V_6_0_0_alpha2)) {
context = in.readOptionalString();
source = new StoredScriptSource(in);
} else {
source = StoredScriptSource.parse(content, xContentType == null ? XContentType.JSON : xContentType);
}
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);

out.writeString(lang == null ? "" : lang);
if (out.getVersion().before(Version.V_6_0_0_alpha2)) {
out.writeString(source == null ? "" : source.getLang());
}
out.writeOptionalString(id);
out.writeBytesReference(content);
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
xContentType.writeTo(out);
}
if (out.getVersion().onOrAfter(Version.V_6_0_0_alpha2)) {
out.writeOptionalString(context);
source.writeTo(out);
}
}

Expand All @@ -169,6 +163,8 @@ public String toString() {
// ignore
}

return "put stored script {id [" + id + "]" + (lang != null ? ", lang [" + lang + "]" : "") + ", content [" + source + "]}";
return "put stored script {id [" + id + "]" +
(context != null ? ", context [" + context + "]" : "") +
", content [" + source + "]}";
}
}
Expand Up @@ -43,9 +43,4 @@ public PutStoredScriptRequestBuilder setContent(BytesReference source, XContentT
request.content(source, xContentType);
return this;
}

public PutStoredScriptRequestBuilder setLang(String lang) {
request.lang(lang);
return this;
}
}
Expand Up @@ -298,7 +298,7 @@ public interface ClusterAdminClient extends ElasticsearchClient {
/**
* Returns top N hot-threads samples per node. The hot-threads are only
* sampled for the node ids specified in the request.
*
*
*/
ActionFuture<NodesHotThreadsResponse> nodesHotThreads(NodesHotThreadsRequest request);

Expand Down Expand Up @@ -682,7 +682,7 @@ public interface ClusterAdminClient extends ElasticsearchClient {
/**
* Delete a script from the cluster state
*/
DeleteStoredScriptRequestBuilder prepareDeleteStoredScript(String scriptLang, String id);
DeleteStoredScriptRequestBuilder prepareDeleteStoredScript(String id);

/**
* Store a script in the cluster state
Expand All @@ -702,7 +702,7 @@ public interface ClusterAdminClient extends ElasticsearchClient {
/**
* Get a script from the cluster state
*/
GetStoredScriptRequestBuilder prepareGetStoredScript(@Nullable String scriptLang, String id);
GetStoredScriptRequestBuilder prepareGetStoredScript(String id);

/**
* Get a script from the cluster state
Expand Down
Expand Up @@ -1192,8 +1192,8 @@ public GetStoredScriptRequestBuilder prepareGetStoredScript() {
}

@Override
public GetStoredScriptRequestBuilder prepareGetStoredScript(String scriptLang, String id) {
return prepareGetStoredScript().setLang(scriptLang).setId(id);
public GetStoredScriptRequestBuilder prepareGetStoredScript(String id) {
return prepareGetStoredScript().setId(id);
}

@Override
Expand Down Expand Up @@ -1228,8 +1228,8 @@ public DeleteStoredScriptRequestBuilder prepareDeleteStoredScript(){
}

@Override
public DeleteStoredScriptRequestBuilder prepareDeleteStoredScript(@Nullable String scriptLang, String id){
return prepareDeleteStoredScript().setLang(scriptLang).setId(id);
public DeleteStoredScriptRequestBuilder prepareDeleteStoredScript(String id){
return prepareDeleteStoredScript().setId(id);
}
}

Expand Down

0 comments on commit 9f7463e

Please sign in to comment.