Skip to content

Commit 9aa6685

Browse files
committed
add update rule api logic
Signed-off-by: Ruirui Zhang <[email protected]>
1 parent cf31931 commit 9aa6685

File tree

68 files changed

+2040
-112
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2040
-112
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.rule.action;
10+
11+
import org.opensearch.autotagging.Rule;
12+
import org.opensearch.core.action.ActionResponse;
13+
import org.opensearch.core.common.io.stream.StreamInput;
14+
import org.opensearch.core.common.io.stream.StreamOutput;
15+
import org.opensearch.core.rest.RestStatus;
16+
import org.opensearch.core.xcontent.ToXContent;
17+
import org.opensearch.core.xcontent.ToXContentObject;
18+
import org.opensearch.core.xcontent.XContentBuilder;
19+
20+
import java.io.IOException;
21+
import java.util.Map;
22+
23+
import static org.opensearch.autotagging.Rule._ID_STRING;
24+
25+
/**
26+
* Response for the create API for Rule
27+
* @opensearch.experimental
28+
*/
29+
public class CreateRuleResponse extends ActionResponse implements ToXContent, ToXContentObject {
30+
private final String _id;
31+
private final Rule rule;
32+
private final RestStatus restStatus;
33+
34+
/**
35+
* contructor for CreateRuleResponse
36+
* @param id - the id for the rule created
37+
* @param rule - the rule created
38+
* @param restStatus - the status of CreateRuleResponse
39+
*/
40+
public CreateRuleResponse(String id, final Rule rule, RestStatus restStatus) {
41+
this._id = id;
42+
this.rule = rule;
43+
this.restStatus = restStatus;
44+
}
45+
46+
/**
47+
* Constructs a CreateRuleResponse from a StreamInput for deserialization
48+
* @param in - The {@link StreamInput} instance to read from.
49+
*/
50+
public CreateRuleResponse(StreamInput in) throws IOException {
51+
_id = in.readString();
52+
rule = new Rule(in);
53+
restStatus = RestStatus.readFrom(in);
54+
}
55+
56+
@Override
57+
public void writeTo(StreamOutput out) throws IOException {
58+
out.writeString(_id);
59+
rule.writeTo(out);
60+
RestStatus.writeTo(out, restStatus);
61+
}
62+
63+
@Override
64+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
65+
return rule.toXContent(builder, new MapParams(Map.of(_ID_STRING, _id)));
66+
}
67+
68+
/**
69+
* rule getter
70+
*/
71+
public Rule getRule() {
72+
return rule;
73+
}
74+
75+
/**
76+
* restStatus getter
77+
*/
78+
public RestStatus getRestStatus() {
79+
return restStatus;
80+
}
81+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.rule.action;
10+
11+
import org.opensearch.autotagging.Rule;
12+
import org.opensearch.core.action.ActionResponse;
13+
import org.opensearch.core.common.io.stream.StreamInput;
14+
import org.opensearch.core.common.io.stream.StreamOutput;
15+
import org.opensearch.core.rest.RestStatus;
16+
import org.opensearch.core.xcontent.ToXContent;
17+
import org.opensearch.core.xcontent.ToXContentObject;
18+
import org.opensearch.core.xcontent.XContentBuilder;
19+
20+
import java.io.IOException;
21+
import java.util.Map;
22+
23+
import static org.opensearch.autotagging.Rule._ID_STRING;
24+
25+
/**
26+
* Response for the get API for Rule.
27+
* Example response:
28+
* {
29+
* "rules": [
30+
* {
31+
* "_id": "z1MJApUB0zgMcDmz-UQq",
32+
* "description": "Rule for tagging query_group_id to index123"
33+
* "index_pattern": ["index123"],
34+
* "query_group": "query_group_id",
35+
* "updated_at": "2025-02-14T01:19:22.589Z"
36+
* },
37+
* ...
38+
* ],
39+
* "search_after": ["z1MJApUB0zgMcDmz-UQq"]
40+
* }
41+
* @opensearch.experimental
42+
*/
43+
public class GetRuleResponse extends ActionResponse implements ToXContent, ToXContentObject {
44+
private final Map<String, Rule> rules;
45+
private final String searchAfter;
46+
private final RestStatus restStatus;
47+
48+
/**
49+
* Constructor for GetRuleResponse
50+
* @param rules - Rules get from the request
51+
* @param searchAfter - The sort value used for pagination.
52+
* @param restStatus - Status of the GetRuleResponse
53+
*/
54+
public GetRuleResponse(final Map<String, Rule> rules, String searchAfter, RestStatus restStatus) {
55+
this.rules = rules;
56+
this.searchAfter = searchAfter;
57+
this.restStatus = restStatus;
58+
}
59+
60+
/**
61+
* Constructs a GetRuleResponse from a StreamInput for deserialization
62+
* @param in - The {@link StreamInput} instance to read from.
63+
*/
64+
public GetRuleResponse(StreamInput in) throws IOException {
65+
this(in.readMap(StreamInput::readString, Rule::new), in.readOptionalString(), RestStatus.readFrom(in));
66+
}
67+
68+
@Override
69+
public void writeTo(StreamOutput out) throws IOException {
70+
out.writeMap(rules, StreamOutput::writeString, (outStream, rule) -> rule.writeTo(outStream));
71+
out.writeOptionalString(searchAfter);
72+
RestStatus.writeTo(out, restStatus);
73+
}
74+
75+
@Override
76+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
77+
builder.startObject();
78+
builder.startArray("rules");
79+
for (Map.Entry<String, Rule> entry : rules.entrySet()) {
80+
entry.getValue().toXContent(builder, new MapParams(Map.of(_ID_STRING, entry.getKey())));
81+
}
82+
builder.endArray();
83+
if (searchAfter != null && !searchAfter.isEmpty()) {
84+
builder.field("search_after", new Object[] { searchAfter });
85+
}
86+
builder.endObject();
87+
return builder;
88+
}
89+
90+
/**
91+
* rules getter
92+
*/
93+
public Map<String, Rule> getRules() {
94+
return rules;
95+
}
96+
97+
/**
98+
* restStatus getter
99+
*/
100+
public RestStatus getRestStatus() {
101+
return restStatus;
102+
}
103+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.rule.action;
10+
11+
import org.opensearch.action.ActionRequest;
12+
import org.opensearch.action.ActionRequestValidationException;
13+
import org.opensearch.autotagging.Attribute;
14+
import org.opensearch.autotagging.FeatureType;
15+
import org.opensearch.autotagging.RuleValidator;
16+
import org.opensearch.core.common.io.stream.StreamInput;
17+
import org.opensearch.core.common.io.stream.StreamOutput;
18+
19+
import java.io.IOException;
20+
import java.util.HashSet;
21+
import java.util.Map;
22+
import java.util.Set;
23+
24+
/**
25+
* A request for update Rule
26+
* @opensearch.experimental
27+
*/
28+
public class UpdateRuleRequest extends ActionRequest {
29+
private final String _id;
30+
private final String description;
31+
private final Map<Attribute, Set<String>> attributeMap;
32+
private final String featureValue;
33+
private final FeatureType featureType;
34+
35+
/**
36+
* constructor for UpdateRuleRequest
37+
* @param _id - the rule id to update
38+
* @param description - the description to update
39+
* @param attributeMap - the attribute values to update
40+
* @param featureValue - the feature value to update
41+
* @param featureType - the feature type for the rule
42+
*/
43+
public UpdateRuleRequest(
44+
String _id,
45+
String description,
46+
Map<Attribute, Set<String>> attributeMap,
47+
String featureValue,
48+
FeatureType featureType
49+
) {
50+
this._id = _id;
51+
this.description = description;
52+
this.attributeMap = attributeMap;
53+
this.featureValue = featureValue;
54+
this.featureType = featureType;
55+
}
56+
57+
/**
58+
* Constructs a UpdateRuleRequest from a StreamInput for deserialization
59+
* @param in - The {@link StreamInput} instance to read from.
60+
*/
61+
public UpdateRuleRequest(StreamInput in) throws IOException {
62+
super(in);
63+
_id = in.readString();
64+
description = in.readOptionalString();
65+
featureType = FeatureType.from(in);
66+
attributeMap = in.readMap(i -> Attribute.from(i, featureType), i -> new HashSet<>(i.readStringList()));
67+
featureValue = in.readOptionalString();
68+
}
69+
70+
@Override
71+
public ActionRequestValidationException validate() {
72+
RuleValidator validator = new RuleValidator(description, attributeMap, featureValue, null, featureType);
73+
validator.validateUpdatingRuleParams();
74+
return null;
75+
}
76+
77+
@Override
78+
public void writeTo(StreamOutput out) throws IOException {
79+
super.writeTo(out);
80+
out.writeString(_id);
81+
out.writeOptionalString(description);
82+
featureType.writeTo(out);
83+
out.writeMap(attributeMap, (o, a) -> a.writeTo(o), StreamOutput::writeStringCollection);
84+
out.writeOptionalString(featureValue);
85+
}
86+
87+
/**
88+
* id getter
89+
*/
90+
public String get_id() {
91+
return _id;
92+
}
93+
94+
/**
95+
* description getter
96+
*/
97+
public String getDescription() {
98+
return description;
99+
}
100+
101+
/**
102+
* attributeMap getter
103+
*/
104+
public Map<Attribute, Set<String>> getAttributeMap() {
105+
return attributeMap;
106+
}
107+
108+
/**
109+
* featureValue getter
110+
*/
111+
public String getFeatureValue() {
112+
return featureValue;
113+
}
114+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.rule.action;
10+
11+
import org.opensearch.autotagging.Rule;
12+
import org.opensearch.core.action.ActionResponse;
13+
import org.opensearch.core.common.io.stream.StreamInput;
14+
import org.opensearch.core.common.io.stream.StreamOutput;
15+
import org.opensearch.core.rest.RestStatus;
16+
import org.opensearch.core.xcontent.ToXContent;
17+
import org.opensearch.core.xcontent.ToXContentObject;
18+
import org.opensearch.core.xcontent.XContentBuilder;
19+
20+
import java.io.IOException;
21+
import java.util.Map;
22+
23+
import static org.opensearch.autotagging.Rule._ID_STRING;
24+
25+
/**
26+
* Response for the update API for Rule
27+
* @opensearch.experimental
28+
*/
29+
public class UpdateRuleResponse extends ActionResponse implements ToXContent, ToXContentObject {
30+
private final String _id;
31+
private final Rule rule;
32+
private final RestStatus restStatus;
33+
34+
/**
35+
* constructor for UpdateRuleResponse
36+
* @param _id - rule id updated
37+
* @param rule - the updated rule
38+
* @param restStatus - the status of UpdateRuleResponse
39+
*/
40+
public UpdateRuleResponse(String _id, final Rule rule, RestStatus restStatus) {
41+
this._id = _id;
42+
this.rule = rule;
43+
this.restStatus = restStatus;
44+
}
45+
46+
/**
47+
* Constructs a UpdateRuleResponse from a StreamInput for deserialization
48+
* @param in - The {@link StreamInput} instance to read from.
49+
*/
50+
public UpdateRuleResponse(StreamInput in) throws IOException {
51+
this(in.readString(), new Rule(in), RestStatus.readFrom(in));
52+
}
53+
54+
@Override
55+
public void writeTo(StreamOutput out) throws IOException {
56+
out.writeString(_id);
57+
rule.writeTo(out);
58+
RestStatus.writeTo(out, restStatus);
59+
}
60+
61+
@Override
62+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
63+
return rule.toXContent(builder, new MapParams(Map.of(_ID_STRING, _id)));
64+
}
65+
66+
/**
67+
* id getter
68+
*/
69+
public String get_id() {
70+
return _id;
71+
}
72+
73+
/**
74+
* rule getter
75+
*/
76+
public Rule getRule() {
77+
return rule;
78+
}
79+
80+
/**
81+
* restStatus getter
82+
*/
83+
public RestStatus getRestStatus() {
84+
return restStatus;
85+
}
86+
}

0 commit comments

Comments
 (0)