6
6
*/
7
7
package org .elasticsearch .xpack .core .ilm ;
8
8
9
+ import org .elasticsearch .Version ;
9
10
import org .elasticsearch .client .Client ;
10
11
import org .elasticsearch .cluster .AbstractDiffable ;
11
12
import org .elasticsearch .cluster .Diffable ;
13
+ import org .elasticsearch .common .Nullable ;
12
14
import org .elasticsearch .common .ParseField ;
13
15
import org .elasticsearch .common .Strings ;
14
16
import org .elasticsearch .common .io .stream .StreamInput ;
@@ -43,33 +45,51 @@ public class LifecyclePolicy extends AbstractDiffable<LifecyclePolicy>
43
45
private static final int MAX_INDEX_NAME_BYTES = 255 ;
44
46
45
47
public static final ParseField PHASES_FIELD = new ParseField ("phases" );
48
+ private static final ParseField METADATA = new ParseField ("_meta" );
46
49
47
50
@ SuppressWarnings ("unchecked" )
48
51
public static final ConstructingObjectParser <LifecyclePolicy , String > PARSER = new ConstructingObjectParser <>("lifecycle_policy" , false ,
49
52
(a , name ) -> {
50
53
List <Phase > phases = (List <Phase >) a [0 ];
51
54
Map <String , Phase > phaseMap = phases .stream ().collect (Collectors .toMap (Phase ::getName , Function .identity ()));
52
- return new LifecyclePolicy (TimeseriesLifecycleType .INSTANCE , name , phaseMap );
55
+ return new LifecyclePolicy (TimeseriesLifecycleType .INSTANCE , name , phaseMap , ( Map < String , Object >) a [ 1 ] );
53
56
});
54
57
static {
55
58
PARSER .declareNamedObjects (ConstructingObjectParser .constructorArg (), (p , c , n ) -> Phase .parse (p , n ), v -> {
56
59
throw new IllegalArgumentException ("ordered " + PHASES_FIELD .getPreferredName () + " are not supported" );
57
60
}, PHASES_FIELD );
61
+ PARSER .declareObject (ConstructingObjectParser .optionalConstructorArg (), (p , c ) -> p .map (), METADATA );
58
62
}
59
63
60
64
private final String name ;
61
65
private final LifecycleType type ;
62
66
private final Map <String , Phase > phases ;
67
+ @ Nullable
68
+ private final Map <String , Object > metadata ;
63
69
64
70
/**
65
71
* @param name
66
72
* the name of this {@link LifecyclePolicy}
67
73
* @param phases
68
74
* a {@link Map} of {@link Phase}s which make up this
69
75
* {@link LifecyclePolicy}.
76
+ *
70
77
*/
71
78
public LifecyclePolicy (String name , Map <String , Phase > phases ) {
72
- this (TimeseriesLifecycleType .INSTANCE , name , phases );
79
+ this (TimeseriesLifecycleType .INSTANCE , name , phases , null );
80
+ }
81
+
82
+ /**
83
+ * @param name
84
+ * the name of this {@link LifecyclePolicy}
85
+ * @param phases
86
+ * a {@link Map} of {@link Phase}s which make up this
87
+ * {@link LifecyclePolicy}.
88
+ * @param metadata
89
+ * the custom metadata of this {@link LifecyclePolicy}
90
+ */
91
+ public LifecyclePolicy (String name , Map <String , Phase > phases , @ Nullable Map <String , Object > metadata ) {
92
+ this (TimeseriesLifecycleType .INSTANCE , name , phases , metadata );
73
93
}
74
94
75
95
/**
@@ -79,6 +99,11 @@ public LifecyclePolicy(StreamInput in) throws IOException {
79
99
type = in .readNamedWriteable (LifecycleType .class );
80
100
name = in .readString ();
81
101
phases = Collections .unmodifiableMap (in .readMap (StreamInput ::readString , Phase ::new ));
102
+ if (in .getVersion ().onOrAfter (Version .V_8_0_0 )) {
103
+ this .metadata = in .readMap ();
104
+ } else {
105
+ this .metadata = null ;
106
+ }
82
107
}
83
108
84
109
/**
@@ -89,11 +114,14 @@ public LifecyclePolicy(StreamInput in) throws IOException {
89
114
* @param phases
90
115
* a {@link Map} of {@link Phase}s which make up this
91
116
* {@link LifecyclePolicy}.
117
+ * @param metadata
118
+ * the custom metadata of this {@link LifecyclePolicy}
92
119
*/
93
- public LifecyclePolicy (LifecycleType type , String name , Map <String , Phase > phases ) {
120
+ public LifecyclePolicy (LifecycleType type , String name , Map <String , Phase > phases , @ Nullable Map < String , Object > metadata ) {
94
121
this .name = name ;
95
122
this .phases = phases ;
96
123
this .type = type ;
124
+ this .metadata = metadata ;
97
125
this .type .validate (phases .values ());
98
126
}
99
127
@@ -106,6 +134,9 @@ public void writeTo(StreamOutput out) throws IOException {
106
134
out .writeNamedWriteable (type );
107
135
out .writeString (name );
108
136
out .writeMap (phases , StreamOutput ::writeString , (o , val ) -> val .writeTo (o ));
137
+ if (out .getVersion ().onOrAfter (Version .V_8_0_0 )) {
138
+ out .writeMap (this .metadata );
139
+ }
109
140
}
110
141
111
142
/**
@@ -130,6 +161,13 @@ public Map<String, Phase> getPhases() {
130
161
return phases ;
131
162
}
132
163
164
+ /**
165
+ * @return the custom metadata of this {@link LifecyclePolicy}
166
+ */
167
+ public Map <String , Object > getMetadata () {
168
+ return metadata ;
169
+ }
170
+
133
171
@ Override
134
172
public XContentBuilder toXContent (XContentBuilder builder , Params params ) throws IOException {
135
173
builder .startObject ();
@@ -138,6 +176,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
138
176
builder .field (phase .getName (), phase );
139
177
}
140
178
builder .endObject ();
179
+ if (this .metadata != null ) {
180
+ builder .field (METADATA .getPreferredName (), this .metadata );
181
+ }
141
182
builder .endObject ();
142
183
return builder ;
143
184
}
@@ -264,7 +305,7 @@ public static void validatePolicyName(String policy) {
264
305
265
306
@ Override
266
307
public int hashCode () {
267
- return Objects .hash (name , phases );
308
+ return Objects .hash (name , phases , metadata );
268
309
}
269
310
270
311
@ Override
@@ -277,7 +318,8 @@ public boolean equals(Object obj) {
277
318
}
278
319
LifecyclePolicy other = (LifecyclePolicy ) obj ;
279
320
return Objects .equals (name , other .name ) &&
280
- Objects .equals (phases , other .phases );
321
+ Objects .equals (phases , other .phases ) &&
322
+ Objects .equals (metadata , other .metadata );
281
323
}
282
324
283
325
@ Override
0 commit comments