forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add APIs (GET/PUT) to decommission awareness attribute (opensearch-pr…
…oject#4261) * Add APIs (GET/PUT) to decommission awareness attribute Signed-off-by: Rishab Nahata <rnnahata@amazon.com>
- Loading branch information
Showing
31 changed files
with
1,093 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
rest-api-spec/src/main/resources/rest-api-spec/api/cluster.get_decommission_awareness.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"cluster.get_decommission_awareness": { | ||
"documentation": { | ||
"url": "https://opensearch.org/docs/latest/opensearch/rest-api/decommission/", | ||
"description": "Get details and status of decommissioned attribute" | ||
}, | ||
"stability": "experimental", | ||
"url": { | ||
"paths": [ | ||
{ | ||
"path": "/_cluster/decommission/awareness/_status", | ||
"methods": [ | ||
"GET" | ||
] | ||
} | ||
] | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
rest-api-spec/src/main/resources/rest-api-spec/api/cluster.put_decommission_awareness.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"cluster.put_decommission_awareness": { | ||
"documentation": { | ||
"url": "https://opensearch.org/docs/latest/opensearch/rest-api/decommission/", | ||
"description": "Decommissions an awareness attribute" | ||
}, | ||
"stability": "experimental", | ||
"url": { | ||
"paths": [ | ||
{ | ||
"path": "/_cluster/decommission/awareness/{awareness_attribute_name}/{awareness_attribute_value}", | ||
"methods": [ | ||
"PUT" | ||
], | ||
"parts": { | ||
"awareness_attribute_name": { | ||
"type": "string", | ||
"description": "Awareness attribute name" | ||
}, | ||
"awareness_attribute_value": { | ||
"type": "string", | ||
"description": "Awareness attribute value" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...pensearch/action/admin/cluster/decommission/awareness/get/GetDecommissionStateAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.action.admin.cluster.decommission.awareness.get; | ||
|
||
import org.opensearch.action.ActionType; | ||
|
||
/** | ||
* Get decommission action | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class GetDecommissionStateAction extends ActionType<GetDecommissionStateResponse> { | ||
|
||
public static final GetDecommissionStateAction INSTANCE = new GetDecommissionStateAction(); | ||
public static final String NAME = "cluster:admin/decommission/awareness/get"; | ||
|
||
private GetDecommissionStateAction() { | ||
super(NAME, GetDecommissionStateResponse::new); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...ensearch/action/admin/cluster/decommission/awareness/get/GetDecommissionStateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.action.admin.cluster.decommission.awareness.get; | ||
|
||
import org.opensearch.action.ActionRequestValidationException; | ||
import org.opensearch.action.support.clustermanager.ClusterManagerNodeReadRequest; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
import org.opensearch.common.io.stream.StreamOutput; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Get Decommissioned attribute request | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class GetDecommissionStateRequest extends ClusterManagerNodeReadRequest<GetDecommissionStateRequest> { | ||
|
||
public GetDecommissionStateRequest() {} | ||
|
||
public GetDecommissionStateRequest(StreamInput in) throws IOException { | ||
super(in); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
return null; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...h/action/admin/cluster/decommission/awareness/get/GetDecommissionStateRequestBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.action.admin.cluster.decommission.awareness.get; | ||
|
||
import org.opensearch.action.support.clustermanager.ClusterManagerNodeReadOperationRequestBuilder; | ||
import org.opensearch.client.OpenSearchClient; | ||
|
||
/** | ||
* Get decommission request builder | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class GetDecommissionStateRequestBuilder extends ClusterManagerNodeReadOperationRequestBuilder< | ||
GetDecommissionStateRequest, | ||
GetDecommissionStateResponse, | ||
GetDecommissionStateRequestBuilder> { | ||
|
||
/** | ||
* Creates new get decommissioned attributes request builder | ||
*/ | ||
public GetDecommissionStateRequestBuilder(OpenSearchClient client, GetDecommissionStateAction action) { | ||
super(client, action, new GetDecommissionStateRequest()); | ||
} | ||
} |
166 changes: 166 additions & 0 deletions
166
...nsearch/action/admin/cluster/decommission/awareness/get/GetDecommissionStateResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.action.admin.cluster.decommission.awareness.get; | ||
|
||
import org.opensearch.OpenSearchParseException; | ||
import org.opensearch.action.ActionResponse; | ||
import org.opensearch.cluster.decommission.DecommissionAttribute; | ||
import org.opensearch.cluster.decommission.DecommissionStatus; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
import org.opensearch.common.io.stream.StreamOutput; | ||
import org.opensearch.common.xcontent.ToXContentObject; | ||
import org.opensearch.common.xcontent.XContentBuilder; | ||
import org.opensearch.common.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.util.Locale; | ||
import java.util.Objects; | ||
|
||
import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken; | ||
|
||
/** | ||
* Response for decommission status | ||
* | ||
* @opensearch.internal | ||
*/ | ||
public class GetDecommissionStateResponse extends ActionResponse implements ToXContentObject { | ||
|
||
private DecommissionAttribute decommissionedAttribute; | ||
private DecommissionStatus status; | ||
|
||
GetDecommissionStateResponse() { | ||
this(null, null); | ||
} | ||
|
||
GetDecommissionStateResponse(DecommissionAttribute decommissionedAttribute, DecommissionStatus status) { | ||
this.decommissionedAttribute = decommissionedAttribute; | ||
this.status = status; | ||
} | ||
|
||
GetDecommissionStateResponse(StreamInput in) throws IOException { | ||
// read decommissioned attribute and status only if it is present | ||
if (in.readBoolean()) { | ||
this.decommissionedAttribute = new DecommissionAttribute(in); | ||
} | ||
if (in.readBoolean()) { | ||
this.status = DecommissionStatus.fromString(in.readString()); | ||
} | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
// if decommissioned attribute is null, mark absence of decommissioned attribute | ||
if (decommissionedAttribute == null) { | ||
out.writeBoolean(false); | ||
} else { | ||
out.writeBoolean(true); | ||
decommissionedAttribute.writeTo(out); | ||
} | ||
|
||
// if status is null, mark absence of status | ||
if (status == null) { | ||
out.writeBoolean(false); | ||
} else { | ||
out.writeBoolean(true); | ||
out.writeString(status.status()); | ||
} | ||
} | ||
|
||
public DecommissionAttribute getDecommissionedAttribute() { | ||
return decommissionedAttribute; | ||
} | ||
|
||
public DecommissionStatus getDecommissionStatus() { | ||
return status; | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.startObject("awareness"); | ||
if (decommissionedAttribute != null) { | ||
builder.field(decommissionedAttribute.attributeName(), decommissionedAttribute.attributeValue()); | ||
} | ||
builder.endObject(); | ||
if (status != null) { | ||
builder.field("status", status); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
public static GetDecommissionStateResponse fromXContent(XContentParser parser) throws IOException { | ||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser); | ||
String attributeType = "awareness"; | ||
XContentParser.Token token; | ||
DecommissionAttribute decommissionAttribute = null; | ||
DecommissionStatus status = null; | ||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { | ||
if (token == XContentParser.Token.FIELD_NAME) { | ||
String currentFieldName = parser.currentName(); | ||
if (attributeType.equals(currentFieldName)) { | ||
if (parser.nextToken() != XContentParser.Token.START_OBJECT) { | ||
throw new OpenSearchParseException( | ||
"failed to parse decommission attribute type [{}], expected object", | ||
attributeType | ||
); | ||
} | ||
token = parser.nextToken(); | ||
if (token != XContentParser.Token.END_OBJECT) { | ||
if (token == XContentParser.Token.FIELD_NAME) { | ||
String fieldName = parser.currentName(); | ||
String value; | ||
token = parser.nextToken(); | ||
if (token == XContentParser.Token.VALUE_STRING) { | ||
value = parser.text(); | ||
} else { | ||
throw new OpenSearchParseException( | ||
"failed to parse attribute [{}], expected string for attribute value", | ||
fieldName | ||
); | ||
} | ||
decommissionAttribute = new DecommissionAttribute(fieldName, value); | ||
parser.nextToken(); | ||
} else { | ||
throw new OpenSearchParseException("failed to parse attribute type [{}], unexpected type", attributeType); | ||
} | ||
} else { | ||
throw new OpenSearchParseException("failed to parse attribute type [{}]", attributeType); | ||
} | ||
} else if ("status".equals(currentFieldName)) { | ||
if (parser.nextToken() != XContentParser.Token.VALUE_STRING) { | ||
throw new OpenSearchParseException( | ||
"failed to parse status of decommissioning, expected string but found unknown type" | ||
); | ||
} | ||
status = DecommissionStatus.fromString(parser.text().toLowerCase(Locale.ROOT)); | ||
} else { | ||
throw new OpenSearchParseException( | ||
"unknown field found [{}], failed to parse the decommission attribute", | ||
currentFieldName | ||
); | ||
} | ||
} | ||
} | ||
return new GetDecommissionStateResponse(decommissionAttribute, status); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
GetDecommissionStateResponse that = (GetDecommissionStateResponse) o; | ||
return decommissionedAttribute.equals(that.decommissionedAttribute) && status == that.status; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(decommissionedAttribute, status); | ||
} | ||
} |
Oops, something went wrong.