forked from opensearch-project/security
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Craig Perkins <[email protected]>
- Loading branch information
Showing
7 changed files
with
250 additions
and
0 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
29 changes: 29 additions & 0 deletions
29
...ain/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceAction.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,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.security.sampleextension.actions.get; | ||
|
||
import org.opensearch.action.ActionType; | ||
|
||
/** | ||
* Action to get a sample resource | ||
*/ | ||
public class GetSampleResourceAction extends ActionType<GetSampleResourceResponse> { | ||
/** | ||
* Get sample resource action instance | ||
*/ | ||
public static final GetSampleResourceAction INSTANCE = new GetSampleResourceAction(); | ||
/** | ||
* Get sample resource action name | ||
*/ | ||
public static final String NAME = "cluster:admin/sampleresource/get"; | ||
|
||
private GetSampleResourceAction() { | ||
super(NAME, GetSampleResourceResponse::new); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...in/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceRequest.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,47 @@ | ||
/* | ||
* 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.security.sampleextension.actions.get; | ||
|
||
import java.io.IOException; | ||
|
||
import org.opensearch.action.ActionRequest; | ||
import org.opensearch.action.ActionRequestValidationException; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
|
||
/** | ||
* Request object for GetSampleResource transport action | ||
*/ | ||
public class GetSampleResourceRequest extends ActionRequest { | ||
|
||
private String resourceId; | ||
|
||
public GetSampleResourceRequest(String resourceId) { | ||
this.resourceId = resourceId; | ||
} | ||
|
||
public String getResourceId() { | ||
return resourceId; | ||
} | ||
|
||
/** | ||
* Constructor with stream input | ||
* @param in the stream input | ||
* @throws IOException IOException | ||
*/ | ||
public GetSampleResourceRequest(final StreamInput in) throws IOException {} | ||
|
||
@Override | ||
public void writeTo(final StreamOutput out) throws IOException {} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
return null; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...n/java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceResponse.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,56 @@ | ||
/* | ||
* 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.security.sampleextension.actions.get; | ||
|
||
import java.io.IOException; | ||
|
||
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.ToXContentObject; | ||
import org.opensearch.core.xcontent.XContentBuilder; | ||
import org.opensearch.security.sampleextension.actions.SampleResource; | ||
|
||
/** | ||
* Response to a ListSampleResourceRequest | ||
*/ | ||
public class GetSampleResourceResponse extends ActionResponse implements ToXContentObject { | ||
private final SampleResource resource; | ||
|
||
/** | ||
* Default constructor | ||
* | ||
* @param resource The resource | ||
*/ | ||
public GetSampleResourceResponse(SampleResource resource) { | ||
this.resource = resource; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
resource.writeTo(out); | ||
} | ||
|
||
/** | ||
* Constructor with StreamInput | ||
* | ||
* @param in the stream input | ||
*/ | ||
public GetSampleResourceResponse(final StreamInput in) throws IOException { | ||
resource = SampleResource.from(in); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field("resource", resource); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...java/org/opensearch/security/sampleextension/actions/get/GetSampleResourceRestAction.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,48 @@ | ||
/* | ||
* 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.security.sampleextension.actions.get; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.rest.BaseRestHandler; | ||
import org.opensearch.rest.RestRequest; | ||
import org.opensearch.rest.action.RestToXContentListener; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static org.opensearch.rest.RestRequest.Method.GET; | ||
|
||
public class GetSampleResourceRestAction extends BaseRestHandler { | ||
|
||
public GetSampleResourceRestAction() {} | ||
|
||
@Override | ||
public List<Route> routes() { | ||
return singletonList(new Route(GET, "/_plugins/resource_sharing_example/resource/{id}")); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "get_sample_resource"; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { | ||
String resourceId = request.param("id"); | ||
|
||
final GetSampleResourceRequest getSampleResourceRequest = new GetSampleResourceRequest(resourceId); | ||
return channel -> client.executeLocally( | ||
GetSampleResourceAction.INSTANCE, | ||
getSampleResourceRequest, | ||
new RestToXContentListener<>(channel) | ||
); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...org/opensearch/security/sampleextension/actions/get/GetSampleResourceTransportAction.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,61 @@ | ||
/* | ||
* 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.security.sampleextension.actions.get; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import org.opensearch.action.get.GetRequest; | ||
import org.opensearch.action.get.GetResponse; | ||
import org.opensearch.action.support.ActionFilters; | ||
import org.opensearch.action.support.HandledTransportAction; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.common.inject.Inject; | ||
import org.opensearch.core.action.ActionListener; | ||
import org.opensearch.security.sampleextension.actions.SampleResource; | ||
import org.opensearch.tasks.Task; | ||
import org.opensearch.transport.TransportService; | ||
|
||
import static org.opensearch.security.sampleextension.SampleExtensionPlugin.RESOURCE_INDEX_NAME; | ||
|
||
/** | ||
* Transport action for UpdateSampleResource. | ||
*/ | ||
public class GetSampleResourceTransportAction extends HandledTransportAction<GetSampleResourceRequest, GetSampleResourceResponse> { | ||
private static final Logger log = LogManager.getLogger(GetSampleResourceTransportAction.class); | ||
|
||
private final Client nodeClient; | ||
|
||
@Inject | ||
public GetSampleResourceTransportAction(TransportService transportService, ActionFilters actionFilters, Client nodeClient) { | ||
super(GetSampleResourceAction.NAME, transportService, actionFilters, GetSampleResourceRequest::new); | ||
this.nodeClient = nodeClient; | ||
} | ||
|
||
@Override | ||
protected void doExecute(Task task, GetSampleResourceRequest request, ActionListener<GetSampleResourceResponse> actionListener) { | ||
getResource(request, actionListener); | ||
} | ||
|
||
private void getResource(GetSampleResourceRequest request, ActionListener<GetSampleResourceResponse> listener) { | ||
log.warn("resourceId: " + request.getResourceId()); | ||
GetRequest gr = nodeClient.prepareGet().setIndex(RESOURCE_INDEX_NAME).setId(request.getResourceId()).request(); | ||
|
||
log.warn("GET Request: " + gr.toString()); | ||
|
||
ActionListener<GetResponse> grListener = ActionListener.wrap(getResponse -> { | ||
log.info("Updated resource: " + getResponse.toString()); | ||
getResponse.getSource(); | ||
SampleResource resource = new SampleResource(); | ||
resource.setName(getResponse.getSource().get("name").toString()); | ||
listener.onResponse(new GetSampleResourceResponse(resource)); | ||
}, listener::onFailure); | ||
nodeClient.get(gr, grListener); | ||
} | ||
} |