Skip to content

Commit

Permalink
Initial try at using same permissions for patch and put
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Ho <[email protected]>
  • Loading branch information
derek-ho committed Oct 16, 2023
1 parent b98f0b3 commit 0954647
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.opensearch.sql.datasources.model.transport;

import lombok.Getter;
import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.sql.datasource.model.DataSourceMetadata;

import java.io.IOException;
import java.util.Map;

import static org.opensearch.sql.analysis.DataSourceSchemaIdentifierNameResolver.DEFAULT_DATASOURCE_NAME;
import static org.opensearch.sql.datasources.utils.XContentParserUtils.CONNECTOR_FIELD;
import static org.opensearch.sql.datasources.utils.XContentParserUtils.NAME_FIELD;

public class DataSourceActionRequest extends ActionRequest {

public DataSourceActionRequest(StreamInput in) throws IOException {
super(in);
}

Check warning on line 20 in datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DataSourceActionRequest.java

View check run for this annotation

Codecov / codecov/patch

datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DataSourceActionRequest.java#L19-L20

Added lines #L19 - L20 were not covered by tests

@Override
public ActionRequestValidationException validate() {
return null;

Check warning on line 24 in datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DataSourceActionRequest.java

View check run for this annotation

Codecov / codecov/patch

datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DataSourceActionRequest.java#L24

Added line #L24 was not covered by tests
}

public DataSourceActionRequest() {
}

// public DataSourceMetadata getSourceMetadata() {
// return null;
// }
//
// public Map<String, Object> getDataSourceData() {
// return Map.of();
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.opensearch.sql.datasources.model.transport;

import lombok.Getter;
import org.opensearch.core.action.ActionResponse;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;

import java.io.IOException;

public class DataSourceActionResponse extends ActionResponse {
@Getter
String result;

public DataSourceActionResponse() {
}

public DataSourceActionResponse(StreamInput in) throws IOException {
super(in);
}

Check warning on line 19 in datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DataSourceActionResponse.java

View check run for this annotation

Codecov / codecov/patch

datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DataSourceActionResponse.java#L18-L19

Added lines #L18 - L19 were not covered by tests

@Override
public void writeTo(StreamOutput streamOutput) throws IOException {

}

Check warning on line 24 in datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DataSourceActionResponse.java

View check run for this annotation

Codecov / codecov/patch

datasources/src/main/java/org/opensearch/sql/datasources/model/transport/DataSourceActionResponse.java#L24

Added line #L24 was not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.core.common.io.stream.StreamInput;

public class PatchDataSourceActionRequest extends ActionRequest {
public class PatchDataSourceActionRequest extends DataSourceActionRequest {

@Getter private Map<String, Object> dataSourceData;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.opensearch.core.common.io.stream.StreamOutput;

@RequiredArgsConstructor
public class PatchDataSourceActionResponse extends ActionResponse {
public class PatchDataSourceActionResponse extends DataSourceActionResponse {

@Getter private final String result;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.sql.datasource.model.DataSourceMetadata;

public class UpdateDataSourceActionRequest extends ActionRequest {
public class UpdateDataSourceActionRequest extends DataSourceActionRequest {

@Getter private DataSourceMetadata dataSourceMetadata;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.opensearch.core.common.io.stream.StreamOutput;

@RequiredArgsConstructor
public class UpdateDataSourceActionResponse extends ActionResponse {
public class UpdateDataSourceActionResponse extends DataSourceActionResponse {

@Getter private final String result;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,20 @@ private RestChannelConsumer executeUpdateRequest(RestRequest restRequest, NodeCl
nodeClient,
() ->
nodeClient.execute(
TransportUpdateDataSourceAction.ACTION_TYPE,
TransportDataSourceAction.ACTION_TYPE,
new UpdateDataSourceActionRequest(dataSourceMetadata),
new ActionListener<>() {
@Override
public void onResponse(
UpdateDataSourceActionResponse updateDataSourceActionResponse) {
DataSourceActionResponse updateDataSourceActionResponse) {
restChannel.sendResponse(
new BytesRestResponse(
RestStatus.OK,
"application/json; charset=UTF-8",
updateDataSourceActionResponse.getResult()));
}

@Override
@Override
public void onFailure(Exception e) {
handleException(e, restChannel);
}
Expand All @@ -224,13 +224,14 @@ private RestChannelConsumer executePatchRequest(RestRequest restRequest, NodeCli
Scheduler.schedule(

Check warning on line 224 in datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java

View check run for this annotation

Codecov / codecov/patch

datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java#L222-L224

Added lines #L222 - L224 were not covered by tests
nodeClient,
() ->
{
nodeClient.execute(

Check warning on line 228 in datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java

View check run for this annotation

Codecov / codecov/patch

datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java#L228

Added line #L228 was not covered by tests
TransportPatchDataSourceAction.ACTION_TYPE,
TransportDataSourceAction.ACTION_TYPE,
new PatchDataSourceActionRequest(dataSourceData),
new ActionListener<>() {

Check warning on line 231 in datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java

View check run for this annotation

Codecov / codecov/patch

datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java#L231

Added line #L231 was not covered by tests
@Override
public void onResponse(
PatchDataSourceActionResponse patchDataSourceActionResponse) {
DataSourceActionResponse patchDataSourceActionResponse) {
restChannel.sendResponse(

Check warning on line 235 in datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java

View check run for this annotation

Codecov / codecov/patch

datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java#L235

Added line #L235 was not covered by tests
new BytesRestResponse(
RestStatus.OK,
Expand All @@ -242,7 +243,8 @@ public void onResponse(
public void onFailure(Exception e) {
handleException(e, restChannel);
}

Check warning on line 245 in datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java

View check run for this annotation

Codecov / codecov/patch

datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java#L244-L245

Added lines #L244 - L245 were not covered by tests
}));
});
});

Check warning on line 247 in datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java

View check run for this annotation

Codecov / codecov/patch

datasources/src/main/java/org/opensearch/sql/datasources/rest/RestDataSourceQueryAction.java#L247

Added line #L247 was not covered by tests
}

private RestChannelConsumer executeDeleteRequest(RestRequest restRequest, NodeClient nodeClient) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
*
* * Copyright OpenSearch Contributors
* * SPDX-License-Identifier: Apache-2.0
*
*/

package org.opensearch.sql.datasources.transport;

import static org.opensearch.sql.datasources.utils.XContentParserUtils.NAME_FIELD;
import static org.opensearch.sql.protocol.response.format.JsonResponseFormatter.Style.PRETTY;

import org.opensearch.action.ActionType;
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.sql.datasource.DataSourceService;
import org.opensearch.sql.datasources.model.transport.*;
import org.opensearch.sql.datasources.service.DataSourceServiceImpl;
import org.opensearch.sql.protocol.response.format.JsonResponseFormatter;
import org.opensearch.tasks.Task;
import org.opensearch.transport.TransportService;

public class TransportDataSourceAction
extends HandledTransportAction<DataSourceActionRequest, DataSourceActionResponse> {

public static final String NAME = "cluster:admin/opensearch/ql/datasources/update";
public static final ActionType<DataSourceActionResponse> ACTION_TYPE =
new ActionType<>(NAME, DataSourceActionResponse::new);

private DataSourceService dataSourceService;

/**
* TransportUpdateDataSourceAction action for updating datasource.
*
* @param transportService transportService.
* @param actionFilters actionFilters.
* @param dataSourceService dataSourceService.
*/
@Inject
public TransportDataSourceAction(
TransportService transportService,
ActionFilters actionFilters,
DataSourceServiceImpl dataSourceService) {
super(
TransportDataSourceAction.NAME,
transportService,
actionFilters,
DataSourceActionRequest::new);
this.dataSourceService = dataSourceService;
}

@Override
protected void doExecute(
Task task,
DataSourceActionRequest request,
ActionListener<DataSourceActionResponse> actionListener) {
try {
if (request instanceof UpdateDataSourceActionRequest) {
UpdateDataSourceActionRequest request1 = (UpdateDataSourceActionRequest) request;
dataSourceService.updateDataSource(request1.getDataSourceMetadata());
String responseContent =
new JsonResponseFormatter<String>(PRETTY) {
@Override
protected Object buildJsonObject(String response) {
return response;
}
}.format("Updated DataSource with name " + request1.getDataSourceMetadata().getName());
actionListener.onResponse(new UpdateDataSourceActionResponse(responseContent));
} else if (request instanceof PatchDataSourceActionRequest) {
PatchDataSourceActionRequest request2 = (PatchDataSourceActionRequest) request;
dataSourceService.patchDataSource(request2.getDataSourceData());
String responseContent =
new JsonResponseFormatter<String>(PRETTY) {

Check warning on line 75 in datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportDataSourceAction.java

View check run for this annotation

Codecov / codecov/patch

datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportDataSourceAction.java#L72-L75

Added lines #L72 - L75 were not covered by tests
@Override
protected Object buildJsonObject(String response) {
return response;

Check warning on line 78 in datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportDataSourceAction.java

View check run for this annotation

Codecov / codecov/patch

datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportDataSourceAction.java#L78

Added line #L78 was not covered by tests
}
}.format("Updated DataSource with name " + request2.getDataSourceData().get(NAME_FIELD));
actionListener.onResponse(new PatchDataSourceActionResponse(responseContent));
} else {
throw new IllegalArgumentException("Unexpected request type");

Check warning on line 83 in datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportDataSourceAction.java

View check run for this annotation

Codecov / codecov/patch

datasources/src/main/java/org/opensearch/sql/datasources/transport/TransportDataSourceAction.java#L80-L83

Added lines #L80 - L83 were not covered by tests
}
// actionListener.onResponse(new UpdateDataSourceActionResponse(responseContent));
} catch (Exception e) {
actionListener.onFailure(e);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@
import org.opensearch.core.action.ActionListener;
import org.opensearch.sql.datasource.model.DataSourceMetadata;
import org.opensearch.sql.datasource.model.DataSourceType;
import org.opensearch.sql.datasources.model.transport.DataSourceActionResponse;
import org.opensearch.sql.datasources.model.transport.UpdateDataSourceActionRequest;
import org.opensearch.sql.datasources.model.transport.UpdateDataSourceActionResponse;
import org.opensearch.sql.datasources.service.DataSourceServiceImpl;
import org.opensearch.tasks.Task;
import org.opensearch.transport.TransportService;

@ExtendWith(MockitoExtension.class)
public class TransportUpdateDataSourceActionTest {
public class TransportDataSourceActionTest {

@Mock private TransportService transportService;
@Mock private TransportUpdateDataSourceAction action;
@Mock private TransportDataSourceAction action;
@Mock private DataSourceServiceImpl dataSourceService;
@Mock private Task task;
@Mock private ActionListener<UpdateDataSourceActionResponse> actionListener;
@Mock private ActionListener<DataSourceActionResponse> actionListener;

@Captor
private ArgumentCaptor<UpdateDataSourceActionResponse>
Expand All @@ -42,7 +43,7 @@ public class TransportUpdateDataSourceActionTest {
@BeforeEach
public void setUp() {
action =
new TransportUpdateDataSourceAction(
new TransportDataSourceAction(
transportService, new ActionFilters(new HashSet<>()), dataSourceService);
}

Expand Down
12 changes: 6 additions & 6 deletions plugin/src/main/java/org/opensearch/sql/plugin/SQLPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ public List<RestHandler> getRestHandlers(
TransportGetDataSourceAction.class),
new ActionHandler<>(
new ActionType<>(
TransportUpdateDataSourceAction.NAME, UpdateDataSourceActionResponse::new),
TransportUpdateDataSourceAction.class),
new ActionHandler<>(
new ActionType<>(
TransportPatchDataSourceAction.NAME, PatchDataSourceActionResponse::new),
TransportPatchDataSourceAction.class),
TransportDataSourceAction.NAME, DataSourceActionResponse::new),
TransportDataSourceAction.class),
// new ActionHandler<>(
// new ActionType<>(
// TransportPatchDataSourceAction.NAME, PatchDataSourceActionResponse::new),
// TransportPatchDataSourceAction.class),
new ActionHandler<>(
new ActionType<>(
TransportDeleteDataSourceAction.NAME, DeleteDataSourceActionResponse::new),
Expand Down

0 comments on commit 0954647

Please sign in to comment.