Skip to content

Commit

Permalink
Adds delete API and refactors package structure
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <[email protected]>
  • Loading branch information
DarshitChanpura committed Dec 5, 2024
1 parent 0460549 commit 46960ea
Show file tree
Hide file tree
Showing 32 changed files with 343 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,24 @@
import org.opensearch.repositories.RepositoriesService;
import org.opensearch.rest.RestController;
import org.opensearch.rest.RestHandler;
import org.opensearch.sample.actions.create.CreateResourceAction;
import org.opensearch.sample.actions.create.CreateResourceRestAction;
import org.opensearch.sample.actions.list.ListAccessibleResourcesAction;
import org.opensearch.sample.actions.list.ListAccessibleResourcesRestAction;
import org.opensearch.sample.actions.revoke.RevokeResourceAccessAction;
import org.opensearch.sample.actions.revoke.RevokeResourceAccessRestAction;
import org.opensearch.sample.actions.share.ShareResourceAction;
import org.opensearch.sample.actions.share.ShareResourceRestAction;
import org.opensearch.sample.actions.verify.VerifyResourceAccessAction;
import org.opensearch.sample.actions.verify.VerifyResourceAccessRestAction;
import org.opensearch.sample.transport.*;
import org.opensearch.sample.actions.access.list.ListAccessibleResourcesAction;
import org.opensearch.sample.actions.access.list.ListAccessibleResourcesRestAction;
import org.opensearch.sample.actions.access.revoke.RevokeResourceAccessAction;
import org.opensearch.sample.actions.access.revoke.RevokeResourceAccessRestAction;
import org.opensearch.sample.actions.access.share.ShareResourceAction;
import org.opensearch.sample.actions.access.share.ShareResourceRestAction;
import org.opensearch.sample.actions.access.verify.VerifyResourceAccessAction;
import org.opensearch.sample.actions.access.verify.VerifyResourceAccessRestAction;
import org.opensearch.sample.actions.resource.create.CreateResourceAction;
import org.opensearch.sample.actions.resource.create.CreateResourceRestAction;
import org.opensearch.sample.actions.resource.delete.DeleteResourceAction;
import org.opensearch.sample.actions.resource.delete.DeleteResourceRestAction;
import org.opensearch.sample.transport.access.ListAccessibleResourcesTransportAction;
import org.opensearch.sample.transport.access.RevokeResourceAccessTransportAction;
import org.opensearch.sample.transport.access.ShareResourceTransportAction;
import org.opensearch.sample.transport.access.VerifyResourceAccessTransportAction;
import org.opensearch.sample.transport.resource.CreateResourceTransportAction;
import org.opensearch.sample.transport.resource.DeleteResourceTransportAction;
import org.opensearch.script.ScriptService;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.watcher.ResourceWatcherService;
Expand Down Expand Up @@ -105,7 +112,8 @@ public List<RestHandler> getRestHandlers(
new ListAccessibleResourcesRestAction(),
new VerifyResourceAccessRestAction(),
new RevokeResourceAccessRestAction(),
new ShareResourceRestAction()
new ShareResourceRestAction(),
new DeleteResourceRestAction()
);
}

Expand All @@ -116,7 +124,8 @@ public List<RestHandler> getRestHandlers(
new ActionHandler<>(ListAccessibleResourcesAction.INSTANCE, ListAccessibleResourcesTransportAction.class),
new ActionHandler<>(ShareResourceAction.INSTANCE, ShareResourceTransportAction.class),
new ActionHandler<>(RevokeResourceAccessAction.INSTANCE, RevokeResourceAccessTransportAction.class),
new ActionHandler<>(VerifyResourceAccessAction.INSTANCE, VerifyResourceAccessTransportAction.class)
new ActionHandler<>(VerifyResourceAccessAction.INSTANCE, VerifyResourceAccessTransportAction.class),
new ActionHandler<>(DeleteResourceAction.INSTANCE, DeleteResourceTransportAction.class)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.list;
package org.opensearch.sample.actions.access.list;

import org.opensearch.action.ActionType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.list;
package org.opensearch.sample.actions.access.list;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.list;
package org.opensearch.sample.actions.access.list;

import java.io.IOException;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.list;
package org.opensearch.sample.actions.access.list;

import java.util.List;

Expand All @@ -33,7 +33,7 @@ public String getName() {
}

@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
final ListAccessibleResourcesRequest listAccessibleResourcesRequest = new ListAccessibleResourcesRequest();
return channel -> client.executeLocally(
ListAccessibleResourcesAction.INSTANCE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.revoke;
package org.opensearch.sample.actions.access.revoke;

import org.opensearch.action.ActionType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.revoke;
package org.opensearch.sample.actions.access.revoke;

import java.io.IOException;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.revoke;
package org.opensearch.sample.actions.access.revoke;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.revoke;
package org.opensearch.sample.actions.access.revoke;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.opensearch.accesscontrol.resources.EntityType;
import org.opensearch.client.node.NodeClient;
Expand All @@ -20,15 +22,15 @@
import org.opensearch.rest.action.RestToXContentListener;

import static java.util.Collections.singletonList;
import static org.opensearch.rest.RestRequest.Method.GET;
import static org.opensearch.rest.RestRequest.Method.POST;

public class RevokeResourceAccessRestAction extends BaseRestHandler {

public RevokeResourceAccessRestAction() {}

@Override
public List<Route> routes() {
return singletonList(new Route(GET, "/_plugins/sample_resource_sharing/revoke"));
return singletonList(new Route(POST, "/_plugins/sample_resource_sharing/revoke"));
}

@Override
Expand All @@ -37,14 +39,25 @@ public String getName() {
}

@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
Map<String, Object> source;
try (XContentParser parser = request.contentParser()) {
source = parser.map();
}

String resourceId = (String) source.get("resource_id");
Map<EntityType, List<String>> revoke = (Map<EntityType, List<String>>) source.get("revoke");
@SuppressWarnings("unchecked")
Map<String, List<String>> revokeSource = (Map<String, List<String>>) source.get("revoke");
Map<EntityType, List<String>> revoke = revokeSource.entrySet().stream().collect(Collectors.toMap(entry -> {
try {
return EntityType.fromValue(entry.getKey());
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(
"Invalid entity type: " + entry.getKey() + ". Valid values are: " + Arrays.toString(EntityType.values())
);
}
}, Map.Entry::getValue));

final RevokeResourceAccessRequest revokeResourceAccessRequest = new RevokeResourceAccessRequest(resourceId, revoke);
return channel -> client.executeLocally(
RevokeResourceAccessAction.INSTANCE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.share;
package org.opensearch.sample.actions.access.share;

import org.opensearch.action.ActionType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.share;
package org.opensearch.sample.actions.access.share;

import java.io.IOException;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.share;
package org.opensearch.sample.actions.access.share;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.share;
package org.opensearch.sample.actions.access.share;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -41,7 +41,7 @@ public String getName() {
}

@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
Map<String, Object> source;
try (XContentParser parser = request.contentParser()) {
source = parser.map();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.verify;
package org.opensearch.sample.actions.access.verify;

import org.opensearch.action.ActionType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.verify;
package org.opensearch.sample.actions.access.verify;

import java.io.IOException;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.verify;
package org.opensearch.sample.actions.access.verify;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.verify;
package org.opensearch.sample.actions.access.verify;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -36,7 +36,7 @@ public String getName() {
}

@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
Map<String, Object> source;
try (XContentParser parser = request.contentParser()) {
source = parser.map();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.create;
package org.opensearch.sample.actions.resource.create;

import org.opensearch.action.ActionType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.create;
package org.opensearch.sample.actions.resource.create;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.create;
package org.opensearch.sample.actions.resource.create;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.sample.actions.create;
package org.opensearch.sample.actions.resource.create;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -36,7 +36,7 @@ public String getName() {
}

@Override
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
Map<String, Object> source;
try (XContentParser parser = request.contentParser()) {
source = parser.map();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* GitHub history for details.
*/

package org.opensearch.sample.actions.create;
package org.opensearch.sample.actions.resource.create;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.sample.actions.resource.delete;

import org.opensearch.action.ActionType;

/**
* Action to create a sample resource
*/
public class DeleteResourceAction extends ActionType<DeleteResourceResponse> {
/**
* Create sample resource action instance
*/
public static final DeleteResourceAction INSTANCE = new DeleteResourceAction();
/**
* Create sample resource action name
*/
public static final String NAME = "cluster:admin/sample-resource-plugin/delete";

private DeleteResourceAction() {
super(NAME, DeleteResourceResponse::new);
}
}
Loading

0 comments on commit 46960ea

Please sign in to comment.