2020
2121import org .elasticsearch .ElasticsearchGenerationException ;
2222import org .elasticsearch .ElasticsearchParseException ;
23+ import org .elasticsearch .Version ;
2324import org .elasticsearch .action .ActionRequestValidationException ;
2425import org .elasticsearch .action .IndicesRequest ;
2526import org .elasticsearch .action .admin .indices .alias .Alias ;
3233import org .elasticsearch .common .collect .MapBuilder ;
3334import org .elasticsearch .common .io .stream .StreamInput ;
3435import org .elasticsearch .common .io .stream .StreamOutput ;
36+ import org .elasticsearch .common .logging .DeprecationLogger ;
37+ import org .elasticsearch .common .logging .Loggers ;
3538import org .elasticsearch .common .settings .Settings ;
3639import org .elasticsearch .common .xcontent .XContentBuilder ;
3740import org .elasticsearch .common .xcontent .XContentFactory ;
4144import org .elasticsearch .common .xcontent .support .XContentMapValues ;
4245
4346import java .io .IOException ;
47+ import java .util .Collections ;
4448import java .util .HashMap ;
4549import java .util .HashSet ;
50+ import java .util .List ;
4651import java .util .Map ;
4752import java .util .Set ;
53+ import java .util .stream .Collectors ;
4854
4955import static org .elasticsearch .action .ValidateActions .addValidationError ;
5056import static org .elasticsearch .common .settings .Settings .readSettingsFromStream ;
5662 */
5763public class PutIndexTemplateRequest extends MasterNodeRequest <PutIndexTemplateRequest > implements IndicesRequest {
5864
65+ private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger (Loggers .getLogger (PutIndexTemplateRequest .class ));
66+
67+ public static final Version V_5_1_0 = Version .fromId (5010099 );
68+
5969 private String name ;
6070
6171 private String cause = "" ;
6272
63- private String template ;
73+ private List < String > indexPatterns ;
6474
6575 private int order ;
6676
@@ -92,8 +102,8 @@ public ActionRequestValidationException validate() {
92102 if (name == null ) {
93103 validationException = addValidationError ("name is missing" , validationException );
94104 }
95- if (template == null ) {
96- validationException = addValidationError ("template is missing" , validationException );
105+ if (indexPatterns == null || indexPatterns . size () == 0 ) {
106+ validationException = addValidationError ("pattern is missing" , validationException );
97107 }
98108 return validationException ;
99109 }
@@ -113,13 +123,13 @@ public String name() {
113123 return this .name ;
114124 }
115125
116- public PutIndexTemplateRequest template ( String template ) {
117- this .template = template ;
126+ public PutIndexTemplateRequest patterns ( List < String > indexPatterns ) {
127+ this .indexPatterns = indexPatterns ;
118128 return this ;
119129 }
120130
121- public String template () {
122- return this .template ;
131+ public List < String > patterns () {
132+ return this .indexPatterns ;
123133 }
124134
125135 public PutIndexTemplateRequest order (int order ) {
@@ -286,7 +296,20 @@ public PutIndexTemplateRequest source(Map templateSource) {
286296 for (Map .Entry <String , Object > entry : source .entrySet ()) {
287297 String name = entry .getKey ();
288298 if (name .equals ("template" )) {
289- template (entry .getValue ().toString ());
299+ // This is needed to allow for bwc (beats, logstash) with pre-5.0 templates (#21009)
300+ if (entry .getValue () instanceof String ) {
301+ DEPRECATION_LOGGER .deprecated ("Deprecated field [template] used, replaced by [index_patterns]" );
302+ patterns (Collections .singletonList ((String ) entry .getValue ()));
303+ }
304+ } else if (name .equals ("index_patterns" )) {
305+ if (entry .getValue () instanceof String ) {
306+ patterns (Collections .singletonList ((String ) entry .getValue ()));
307+ } else if (entry .getValue () instanceof List ) {
308+ List <String > elements = ((List <?>) entry .getValue ()).stream ().map (Object ::toString ).collect (Collectors .toList ());
309+ patterns (elements );
310+ } else {
311+ throw new IllegalArgumentException ("Malformed [template] value, should be a string or a list of strings" );
312+ }
290313 } else if (name .equals ("order" )) {
291314 order (XContentMapValues .nodeIntegerValue (entry .getValue (), order ()));
292315 } else if ("version" .equals (name )) {
@@ -295,7 +318,7 @@ public PutIndexTemplateRequest source(Map templateSource) {
295318 }
296319 version ((Integer )entry .getValue ());
297320 } else if (name .equals ("settings" )) {
298- if (! (entry .getValue () instanceof Map )) {
321+ if ((entry .getValue () instanceof Map ) == false ) {
299322 throw new IllegalArgumentException ("Malformed [settings] section, should include an inner object" );
300323 }
301324 settings ((Map <String , Object >) entry .getValue ());
@@ -436,7 +459,7 @@ public PutIndexTemplateRequest alias(Alias alias) {
436459
437460 @ Override
438461 public String [] indices () {
439- return new String []{ template } ;
462+ return indexPatterns . toArray ( new String [indexPatterns . size ()]) ;
440463 }
441464
442465 @ Override
@@ -449,7 +472,12 @@ public void readFrom(StreamInput in) throws IOException {
449472 super .readFrom (in );
450473 cause = in .readString ();
451474 name = in .readString ();
452- template = in .readString ();
475+
476+ if (in .getVersion ().onOrAfter (V_5_1_0 )) {
477+ indexPatterns = in .readList (StreamInput ::readString );
478+ } else {
479+ indexPatterns = Collections .singletonList (in .readString ());
480+ }
453481 order = in .readInt ();
454482 create = in .readBoolean ();
455483 settings = readSettingsFromStream (in );
@@ -475,7 +503,11 @@ public void writeTo(StreamOutput out) throws IOException {
475503 super .writeTo (out );
476504 out .writeString (cause );
477505 out .writeString (name );
478- out .writeString (template );
506+ if (out .getVersion ().onOrAfter (V_5_1_0 )) {
507+ out .writeStringList (indexPatterns );
508+ } else {
509+ out .writeString (indexPatterns .size () > 0 ? indexPatterns .get (0 ) : "" );
510+ }
479511 out .writeInt (order );
480512 out .writeBoolean (create );
481513 writeSettingsToStream (settings , out );
0 commit comments