11
11
import org .opensearch .action .ActionRequest ;
12
12
import org .opensearch .action .ActionRequestValidationException ;
13
13
import org .opensearch .common .io .stream .StreamInput ;
14
+ import org .opensearch .common .io .stream .StreamOutput ;
14
15
import org .opensearch .common .unit .TimeValue ;
15
16
import org .opensearch .common .xcontent .ToXContent ;
16
17
import org .opensearch .common .xcontent .ToXContentObject ;
25
26
import static org .opensearch .action .ValidateActions .addValidationError ;
26
27
27
28
public class UpdatePitRequest extends ActionRequest implements ToXContentObject {
28
- // TODO: update the pit reqyest to handle not just array
29
+ // TODO: update the pit request to handle not just array
29
30
private final List <UpdatePitRequestInfo > updatePitRequests ;
30
31
31
- public UpdatePitRequest (StreamInput in ) throws IOException {
32
- super (in );
33
- int size = in .readVInt ();
34
- updatePitRequests = new ArrayList <>();
35
- for (int i =0 ;i <size ;i ++){
36
- updatePitRequests .add (new UpdatePitRequestInfo (in ));
37
- }
38
- }
39
32
40
33
public List <UpdatePitRequestInfo > getUpdatePitRequests () {
41
34
return updatePitRequests ;
@@ -49,39 +42,103 @@ public UpdatePitRequest(List<UpdatePitRequestInfo> updatePitRequests){
49
42
this .updatePitRequests = updatePitRequests ;
50
43
}
51
44
52
- public UpdatePitRequest () {}
45
+ public UpdatePitRequest () {
46
+ this .updatePitRequests = new ArrayList <>();
47
+ }
53
48
54
49
55
50
@ Override
56
51
public ActionRequestValidationException validate () {
57
52
ActionRequestValidationException validationException = null ;
58
- if (keepAlive == null ) {
59
- validationException = addValidationError ("keep alive not specified" , validationException );
53
+ if (updatePitRequests == null || updatePitRequests . isEmpty () ) {
54
+ validationException = addValidationError ("No pit ids specified" , validationException );
60
55
}
61
56
return validationException ;
62
57
}
63
58
59
+ public UpdatePitRequest (StreamInput in ) throws IOException {
60
+ super (in );
61
+ int size = in .readVInt ();
62
+ updatePitRequests = new ArrayList <>();
63
+ for (int i =0 ;i <size ;i ++){
64
+ updatePitRequests .add (new UpdatePitRequestInfo (in ));
65
+ }
66
+ }
67
+
68
+ @ Override
69
+ public void writeTo (StreamOutput out ) throws IOException {
70
+ super .writeTo (out );
71
+ out .writeVInt (updatePitRequests .size ());
72
+ for (UpdatePitRequestInfo updatePitRequest : updatePitRequests ) {
73
+ updatePitRequest .writeTo (out );
74
+ }
75
+ }
76
+
64
77
@ Override
65
78
public XContentBuilder toXContent (XContentBuilder builder , ToXContent .Params params ) throws IOException {
66
- builder .field ("keep_alive" , keepAlive );
67
- builder .field ("pit_id" , pit_id );
79
+ builder .startObject ();
80
+ builder .startArray ("pits" );
81
+ for (UpdatePitRequestInfo requestInfo : updatePitRequests ){
82
+ requestInfo .toXContent (builder ,params );
83
+ }
84
+ builder .endArray ();
85
+ builder .endObject ();
68
86
return builder ;
69
87
}
70
88
89
+ // Req body
90
+ // {
91
+ // pits: [
92
+ // {
93
+ // pit_id: <>
94
+ // keep_alive: <>
95
+ // }
96
+ // ]
97
+ // }
71
98
public void fromXContent (XContentParser parser ) throws IOException {
99
+ updatePitRequests .clear ();
72
100
if (parser .nextToken () != XContentParser .Token .START_OBJECT ){
73
101
throw new IllegalArgumentException ("Malformed content, must start with an object" );
74
102
} else {
75
103
XContentParser .Token token ;
76
104
String currentFieldName = null ;
105
+ String currentFieldName1 = null ;
77
106
while ((token = parser .nextToken ()) != XContentParser .Token .END_OBJECT ){
78
107
if (token == XContentParser .Token .FIELD_NAME ){
79
108
currentFieldName = parser .currentName ();
80
- } else if ("keep_alive" .equals (currentFieldName )){
81
- if (token .isValue () == false ){
82
- throw new IllegalArgumentException ("keep_alive should only contain a time value" );
109
+ } else if ("pits" .equals (currentFieldName )){
110
+ if (token == XContentParser .Token .START_ARRAY ){
111
+ while (parser .nextToken () != XContentParser .Token .START_OBJECT ) {
112
+ String pit_id =null ;
113
+ String keep_alive = null ;
114
+ if (parser .nextToken () == XContentParser .Token .FIELD_NAME ){
115
+ currentFieldName1 = parser .currentName ();
116
+ }
117
+ if ("pit_id" .equals (currentFieldName1 )){
118
+ pit_id = parser .text ();
119
+ } else {
120
+ throw new IllegalArgumentException ("pit_id array element should only contain pit_id " + currentFieldName1 );
121
+ }
122
+
123
+ if (parser .nextToken () == XContentParser .Token .FIELD_NAME ){
124
+ currentFieldName1 = parser .currentName ();
125
+ }
126
+ if ("keep_alive" .equals (currentFieldName1 )){
127
+ keep_alive = parser .text ();
128
+ } else {
129
+ throw new IllegalArgumentException ("pit_id array element should only contain pit_id " + currentFieldName1 );
130
+ }
131
+ updatePitRequests .add (new UpdatePitRequestInfo (pit_id , keep_alive ));
132
+
133
+
134
+ if (parser .nextToken () !=XContentParser .Token .END_OBJECT ){
135
+ throw new IllegalArgumentException ("pit_id array element should only contain pit_id " + currentFieldName1 );
136
+ }
137
+ }
138
+ } else {
139
+ throw new IllegalArgumentException ("pit_id array element should only contain pit_id" );
140
+
83
141
}
84
- keepAlive = TimeValue .parseTimeValue (parser .text (),"keep_alive" );
85
142
}
86
143
87
144
}
0 commit comments