Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete Rule #2

Open
wants to merge 2 commits into
base: rule
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@
import org.opensearch.plugin.wlm.querygroup.rest.RestGetQueryGroupAction;
import org.opensearch.plugin.wlm.querygroup.rest.RestUpdateQueryGroupAction;
import org.opensearch.plugin.wlm.querygroup.service.QueryGroupPersistenceService;
import org.opensearch.plugin.wlm.rule.action.CreateRuleAction;
import org.opensearch.plugin.wlm.rule.action.GetRuleAction;
import org.opensearch.plugin.wlm.rule.action.TransportCreateRuleAction;
import org.opensearch.plugin.wlm.rule.action.TransportGetRuleAction;
import org.opensearch.plugin.wlm.rule.action.*;
import org.opensearch.plugin.wlm.rule.rest.RestCreateRuleAction;
import org.opensearch.plugin.wlm.rule.rest.RestDeleteRuleAction;
import org.opensearch.plugin.wlm.rule.rest.RestGetRuleAction;
import org.opensearch.plugins.ActionPlugin;
import org.opensearch.plugins.Plugin;
Expand Down Expand Up @@ -68,7 +66,8 @@ public WorkloadManagementPlugin() {}
new ActionPlugin.ActionHandler<>(DeleteQueryGroupAction.INSTANCE, TransportDeleteQueryGroupAction.class),
new ActionPlugin.ActionHandler<>(UpdateQueryGroupAction.INSTANCE, TransportUpdateQueryGroupAction.class),
new ActionPlugin.ActionHandler<>(CreateRuleAction.INSTANCE, TransportCreateRuleAction.class),
new ActionPlugin.ActionHandler<>(GetRuleAction.INSTANCE, TransportGetRuleAction.class)
new ActionPlugin.ActionHandler<>(GetRuleAction.INSTANCE, TransportGetRuleAction.class),
new ActionPlugin.ActionHandler<>(DeleteRuleAction.INSTANCE, TransportDeleteRuleAction.class)
);
}

Expand Down Expand Up @@ -96,7 +95,8 @@ public List<RestHandler> getRestHandlers(
new RestDeleteQueryGroupAction(),
new RestUpdateQueryGroupAction(),
new RestCreateRuleAction(),
new RestGetRuleAction()
new RestGetRuleAction(),
new RestDeleteRuleAction()
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.plugin.wlm.rule.action;

import org.opensearch.action.ActionType;

/**
* Transport action to delete a Rule
*
* @opensearch.experimental
*/
public class DeleteRuleAction extends ActionType<DeleteRuleResponse> {

/**
* An instance of DeleteRuleAction
*/
public static final DeleteRuleAction INSTANCE = new DeleteRuleAction();

/**
* Name for DeleteRuleAction
*/
public static final String NAME = "cluster:admin/opensearch/wlm/rule/delete";

/**
* Default constructor
*/
private DeleteRuleAction() {
super(NAME, DeleteRuleResponse::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.plugin.wlm.rule.action;

import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.action.support.clustermanager.ClusterManagerNodeRequest;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;

import java.io.IOException;

/**
* A request for deleting a Rule
* @opensearch.experimental
*/
public class DeleteRuleRequest extends ClusterManagerNodeRequest<DeleteRuleRequest> {
private final String _id;

/**
* Constructor for DeleteRuleRequest
* @param _id - Rule _id that we want to delete
*/
public DeleteRuleRequest(String _id) {
this._id = _id;
}

/**
* Constructor for DeleteRuleRequest
* @param in - A {@link StreamInput} object
*/
public DeleteRuleRequest(StreamInput in) throws IOException {
super(in);
_id = in.readOptionalString();
}

@Override
public ActionRequestValidationException validate() {
return null;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeOptionalString(_id);
}

/**
* _id getter
*/
public String get_id() {
return _id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.plugin.wlm.rule.action;

import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.action.ActionResponse;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;

import java.io.IOException;

/**
* Response for the delete API for Rule
*
* @opensearch.experimental
*/
public class DeleteRuleResponse extends ActionResponse implements ToXContent, ToXContentObject {
private final boolean acknowledged;

/**
* Constructor for DeleteRuleResponse
* @param acknowledged - Whether the deletion was successful
*/
public DeleteRuleResponse(boolean acknowledged) {
this.acknowledged = acknowledged;
}

/**
* Constructor for DeleteRuleResponse
* @param in - A {@link StreamInput} object
*/
public DeleteRuleResponse(StreamInput in) throws IOException {
this.acknowledged = in.readBoolean();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeBoolean(acknowledged);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("acknowledged", acknowledged);
builder.endObject();
return builder;
}

public String toXContentString() throws IOException {
XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint();
this.toXContent(builder, ToXContent.EMPTY_PARAMS);
return builder.toString();
}


/**
* acknowledged getter
*/
public boolean isAcknowledged() {
return acknowledged;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.plugin.wlm.rule.action;

import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.common.inject.Inject;
import org.opensearch.core.action.ActionListener;
import org.opensearch.plugin.wlm.rule.service.RulePersistenceService;
import org.opensearch.tasks.Task;
import org.opensearch.transport.TransportService;

/**
* Transport action to delete a Rule
*
* @opensearch.experimental
*/
/**
* Transport action to delete a Rule
*
* @opensearch.experimental
*/
public class TransportDeleteRuleAction extends HandledTransportAction<DeleteRuleRequest, DeleteRuleResponse> {

private final RulePersistenceService rulePersistenceService;

/**
* Constructor for TransportDeleteRuleAction
*
* @param transportService - a {@link TransportService} object
* @param actionFilters - a {@link ActionFilters} object
* @param rulePersistenceService - a {@link RulePersistenceService} object
*/
@Inject
public TransportDeleteRuleAction(
TransportService transportService,
ActionFilters actionFilters,
RulePersistenceService rulePersistenceService
) {
super(DeleteRuleAction.NAME, transportService, actionFilters, DeleteRuleRequest::new);
this.rulePersistenceService = rulePersistenceService;
}

@Override
protected void doExecute(Task task, DeleteRuleRequest request, ActionListener<DeleteRuleResponse> listener) {
rulePersistenceService.deleteRule(request.get_id(), ActionListener.wrap(
response -> listener.onResponse(new DeleteRuleResponse(true)),
listener::onFailure
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.plugin.wlm.rule.rest;

import org.opensearch.client.node.NodeClient;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.plugin.wlm.rule.action.*;
import org.opensearch.rest.*;
import org.opensearch.rest.action.RestResponseListener;

import java.io.IOException;
import java.util.List;

import static org.opensearch.rest.RestRequest.Method.DELETE;

/**
* Rest action to delete a Rule
*
* @opensearch.experimental
*/
public class RestDeleteRuleAction extends BaseRestHandler {

/**
* Constructor for RestDeleteRuleAction
*/
public RestDeleteRuleAction() {}

@Override
public String getName() {
return "delete_rule";
}

/**
* The list of {@link Route}s that this RestHandler is responsible for handling.
*/
@Override
public List<Route> routes() {
return List.of(new Route(DELETE, "_wlm/rule/{_id}"));
}

@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
final DeleteRuleRequest deleteRuleRequest = new DeleteRuleRequest(request.param("_id"));
return channel -> client.execute(DeleteRuleAction.INSTANCE, deleteRuleRequest, deleteRuleResponse(channel));
}

private RestResponseListener<DeleteRuleResponse> deleteRuleResponse(final RestChannel channel) {
return new RestResponseListener<>(channel) {
@Override
public RestResponse buildResponse(final DeleteRuleResponse response) throws Exception {
return new BytesRestResponse(RestStatus.OK, response.toXContent(channel.newBuilder(), ToXContent.EMPTY_PARAMS));
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.ResourceNotFoundException;
import org.opensearch.action.delete.DeleteRequest;
import org.opensearch.action.delete.DeleteResponse;
import org.opensearch.action.get.GetRequest;
import org.opensearch.action.get.GetResponse;
import org.opensearch.action.index.IndexRequest;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.plugin.wlm.rule.action.DeleteRuleResponse;
import org.opensearch.wlm.Rule;
import org.opensearch.common.inject.Inject;
import org.opensearch.client.Client;
Expand Down Expand Up @@ -137,4 +141,32 @@ private void handleGetAllRuleResponse(SearchResponse searchResponse, ActionListe

listener.onResponse(new GetRuleResponse(ruleMap, RestStatus.OK));
}

public void deleteRule(String id, ActionListener<DeleteRuleResponse> listener) {
if (id == null) {
listener.onFailure(new IllegalArgumentException("Rule ID must not be null"));
return;
}

// Check if the document exists before deleting
GetRequest getRequest = new GetRequest(RULE_INDEX, id);
client.get(getRequest, ActionListener.wrap(
getResponse -> {
if (!getResponse.isExists()) {
listener.onFailure(new ResourceNotFoundException("The rule with id " + id + " doesn't exist"));
} else {
// Proceed with deletion
DeleteRequest deleteRequest = new DeleteRequest(RULE_INDEX, id);
client.delete(deleteRequest, ActionListener.wrap(
deleteResponse -> {
boolean acknowledged = deleteResponse.getResult() == DeleteResponse.Result.DELETED;
listener.onResponse(new DeleteRuleResponse(acknowledged));
},
listener::onFailure
));
}
},
listener::onFailure
));
}
}
Loading
Loading