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
280 additions
and
34 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
29 changes: 29 additions & 0 deletions
29
...main/java/org/opensearch/security/sampleextension/actions/UpdateSampleResourceAction.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; | ||
|
||
import org.opensearch.action.ActionType; | ||
|
||
/** | ||
* Action to create a sample resource | ||
*/ | ||
public class UpdateSampleResourceAction extends ActionType<UpdateSampleResourceResponse> { | ||
/** | ||
* Create sample resource action instance | ||
*/ | ||
public static final UpdateSampleResourceAction INSTANCE = new UpdateSampleResourceAction(); | ||
/** | ||
* Create sample resource action name | ||
*/ | ||
public static final String NAME = "cluster:admin/sampleresource/update"; | ||
|
||
private UpdateSampleResourceAction() { | ||
super(NAME, UpdateSampleResourceResponse::new); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...ain/java/org/opensearch/security/sampleextension/actions/UpdateSampleResourceRequest.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,53 @@ | ||
/* | ||
* 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; | ||
|
||
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 ListSampleResource transport action | ||
*/ | ||
public class UpdateSampleResourceRequest extends ActionRequest { | ||
|
||
private String resourceId; | ||
private String name; | ||
|
||
public UpdateSampleResourceRequest(String resourceId, String name) { | ||
this.resourceId = resourceId; | ||
this.name = name; | ||
} | ||
|
||
public String getResourceId() { | ||
return resourceId; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* Constructor with stream input | ||
* @param in the stream input | ||
* @throws IOException IOException | ||
*/ | ||
public UpdateSampleResourceRequest(final StreamInput in) throws IOException {} | ||
|
||
@Override | ||
public void writeTo(final StreamOutput out) throws IOException {} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
return null; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...in/java/org/opensearch/security/sampleextension/actions/UpdateSampleResourceResponse.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,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.security.sampleextension.actions; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
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.spi.AbstractResource; | ||
|
||
/** | ||
* Response to a ListSampleResourceRequest | ||
*/ | ||
public class UpdateSampleResourceResponse extends ActionResponse implements ToXContentObject { | ||
private final List<SampleResource> resources; | ||
|
||
/** | ||
* Default constructor | ||
* | ||
* @param resources The resources | ||
*/ | ||
public UpdateSampleResourceResponse(List<SampleResource> resources) { | ||
this.resources = resources; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeList(resources); | ||
} | ||
|
||
/** | ||
* Constructor with StreamInput | ||
* | ||
* @param in the stream input | ||
*/ | ||
public UpdateSampleResourceResponse(final StreamInput in) throws IOException { | ||
resources = in.readList(SampleResource::new); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.array("resources", (Object[]) resources.toArray(new AbstractResource[0])); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
.../java/org/opensearch/security/sampleextension/actions/UpdateSampleResourceRestAction.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,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.security.sampleextension.actions; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.core.xcontent.XContentParser; | ||
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.PUT; | ||
|
||
public class UpdateSampleResourceRestAction extends BaseRestHandler { | ||
|
||
public UpdateSampleResourceRestAction() {} | ||
|
||
@Override | ||
public List<Route> routes() { | ||
return singletonList(new Route(PUT, "/_plugins/resource_sharing_example/resource/update/{id}")); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "update_sample_resource"; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException { | ||
String resourceId = request.param("id"); | ||
Map<String, Object> source; | ||
try (XContentParser parser = request.contentParser()) { | ||
source = parser.map(); | ||
} | ||
|
||
String name = (String) source.get("name"); | ||
|
||
// TODO Update the request obj | ||
final UpdateSampleResourceRequest updateSampleResourceRequest = new UpdateSampleResourceRequest(resourceId, name); | ||
return channel -> client.executeLocally( | ||
UpdateSampleResourceAction.INSTANCE, | ||
updateSampleResourceRequest, | ||
new RestToXContentListener<>(channel) | ||
); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
.../org/opensearch/security/sampleextension/actions/UpdateSampleResourceTransportAction.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,80 @@ | ||
/* | ||
* 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; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import org.opensearch.action.index.IndexRequest; | ||
import org.opensearch.action.index.IndexResponse; | ||
import org.opensearch.action.support.ActionFilters; | ||
import org.opensearch.action.support.HandledTransportAction; | ||
import org.opensearch.action.support.WriteRequest; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.common.inject.Inject; | ||
import org.opensearch.core.action.ActionListener; | ||
import org.opensearch.core.xcontent.ToXContent; | ||
import org.opensearch.tasks.Task; | ||
import org.opensearch.transport.TransportService; | ||
|
||
import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder; | ||
|
||
/** | ||
* Transport action for UpdateSampleResource. | ||
*/ | ||
public class UpdateSampleResourceTransportAction extends HandledTransportAction<UpdateSampleResourceRequest, UpdateSampleResourceResponse> { | ||
private static final Logger log = LogManager.getLogger(UpdateSampleResourceTransportAction.class); | ||
|
||
private final Client nodeClient; | ||
private final String resourceIndex; | ||
|
||
@Inject | ||
public UpdateSampleResourceTransportAction( | ||
TransportService transportService, | ||
ActionFilters actionFilters, | ||
Client nodeClient, | ||
String resourceIndex | ||
) { | ||
super(UpdateSampleResourceAction.NAME, transportService, actionFilters, UpdateSampleResourceRequest::new); | ||
this.nodeClient = nodeClient; | ||
this.resourceIndex = resourceIndex; | ||
} | ||
|
||
@Override | ||
protected void doExecute(Task task, UpdateSampleResourceRequest request, ActionListener<UpdateSampleResourceResponse> actionListener) { | ||
indexResource(request, actionListener); | ||
} | ||
|
||
private void indexResource(UpdateSampleResourceRequest request, ActionListener<UpdateSampleResourceResponse> listener) { | ||
log.warn("resourceId: " + request.getResourceId()); | ||
String name = request.getName(); | ||
SampleResource updatedResource = new SampleResource(); | ||
updatedResource.setName(name); | ||
try { | ||
IndexRequest ir = nodeClient.prepareIndex(resourceIndex) | ||
.setId(request.getResourceId()) | ||
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) | ||
.setSource(updatedResource.toXContent(jsonBuilder(), ToXContent.EMPTY_PARAMS)) | ||
.request(); | ||
|
||
log.warn("Index Request: " + ir.toString()); | ||
|
||
ActionListener<IndexResponse> irListener = ActionListener.wrap(idxResponse -> { | ||
log.info("Updated resource: " + idxResponse.toString()); | ||
listener.onResponse(new UpdateSampleResourceResponse(List.of(updatedResource))); | ||
}, listener::onFailure); | ||
nodeClient.index(ir, irListener); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
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