20
20
21
21
import org .elasticsearch .ElasticsearchGenerationException ;
22
22
import org .elasticsearch .ElasticsearchParseException ;
23
+ import org .elasticsearch .Version ;
23
24
import org .elasticsearch .action .ActionRequestValidationException ;
24
25
import org .elasticsearch .action .IndicesRequest ;
25
26
import org .elasticsearch .action .admin .indices .alias .Alias ;
32
33
import org .elasticsearch .common .collect .MapBuilder ;
33
34
import org .elasticsearch .common .io .stream .StreamInput ;
34
35
import org .elasticsearch .common .io .stream .StreamOutput ;
36
+ import org .elasticsearch .common .logging .DeprecationLogger ;
37
+ import org .elasticsearch .common .logging .Loggers ;
35
38
import org .elasticsearch .common .settings .Settings ;
36
39
import org .elasticsearch .common .xcontent .XContentBuilder ;
37
40
import org .elasticsearch .common .xcontent .XContentFactory ;
41
44
import org .elasticsearch .common .xcontent .support .XContentMapValues ;
42
45
43
46
import java .io .IOException ;
47
+ import java .util .Collections ;
44
48
import java .util .HashMap ;
45
49
import java .util .HashSet ;
50
+ import java .util .List ;
46
51
import java .util .Map ;
47
52
import java .util .Set ;
53
+ import java .util .stream .Collectors ;
48
54
49
55
import static org .elasticsearch .action .ValidateActions .addValidationError ;
50
56
import static org .elasticsearch .common .settings .Settings .readSettingsFromStream ;
56
62
*/
57
63
public class PutIndexTemplateRequest extends MasterNodeRequest <PutIndexTemplateRequest > implements IndicesRequest {
58
64
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
+
59
69
private String name ;
60
70
61
71
private String cause = "" ;
62
72
63
- private String template ;
73
+ private List < String > indexPatterns ;
64
74
65
75
private int order ;
66
76
@@ -92,8 +102,8 @@ public ActionRequestValidationException validate() {
92
102
if (name == null ) {
93
103
validationException = addValidationError ("name is missing" , validationException );
94
104
}
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 );
97
107
}
98
108
return validationException ;
99
109
}
@@ -113,13 +123,13 @@ public String name() {
113
123
return this .name ;
114
124
}
115
125
116
- public PutIndexTemplateRequest template ( String template ) {
117
- this .template = template ;
126
+ public PutIndexTemplateRequest patterns ( List < String > indexPatterns ) {
127
+ this .indexPatterns = indexPatterns ;
118
128
return this ;
119
129
}
120
130
121
- public String template () {
122
- return this .template ;
131
+ public List < String > patterns () {
132
+ return this .indexPatterns ;
123
133
}
124
134
125
135
public PutIndexTemplateRequest order (int order ) {
@@ -286,7 +296,20 @@ public PutIndexTemplateRequest source(Map templateSource) {
286
296
for (Map .Entry <String , Object > entry : source .entrySet ()) {
287
297
String name = entry .getKey ();
288
298
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
+ }
290
313
} else if (name .equals ("order" )) {
291
314
order (XContentMapValues .nodeIntegerValue (entry .getValue (), order ()));
292
315
} else if ("version" .equals (name )) {
@@ -295,7 +318,7 @@ public PutIndexTemplateRequest source(Map templateSource) {
295
318
}
296
319
version ((Integer )entry .getValue ());
297
320
} else if (name .equals ("settings" )) {
298
- if (! (entry .getValue () instanceof Map )) {
321
+ if ((entry .getValue () instanceof Map ) == false ) {
299
322
throw new IllegalArgumentException ("Malformed [settings] section, should include an inner object" );
300
323
}
301
324
settings ((Map <String , Object >) entry .getValue ());
@@ -436,7 +459,7 @@ public PutIndexTemplateRequest alias(Alias alias) {
436
459
437
460
@ Override
438
461
public String [] indices () {
439
- return new String []{ template } ;
462
+ return indexPatterns . toArray ( new String [indexPatterns . size ()]) ;
440
463
}
441
464
442
465
@ Override
@@ -449,7 +472,12 @@ public void readFrom(StreamInput in) throws IOException {
449
472
super .readFrom (in );
450
473
cause = in .readString ();
451
474
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
+ }
453
481
order = in .readInt ();
454
482
create = in .readBoolean ();
455
483
settings = readSettingsFromStream (in );
@@ -475,7 +503,11 @@ public void writeTo(StreamOutput out) throws IOException {
475
503
super .writeTo (out );
476
504
out .writeString (cause );
477
505
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
+ }
479
511
out .writeInt (order );
480
512
out .writeBoolean (create );
481
513
writeSettingsToStream (settings , out );
0 commit comments