-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
* Implement patch API for datasources Signed-off-by: Derek Ho <[email protected]> * Change patch implementation to Map Signed-off-by: Derek Ho <[email protected]> * Fix up, everything complete except unit test Signed-off-by: Derek Ho <[email protected]> * Revise PR to use existing functions Signed-off-by: Derek Ho <[email protected]> * Remove unused utility function Signed-off-by: Derek Ho <[email protected]> * Add tests Signed-off-by: Derek Ho <[email protected]> * Add back line Signed-off-by: Derek Ho <[email protected]> * fix build issue Signed-off-by: Derek Ho <[email protected]> * Fix tests and add in rst Signed-off-by: Derek Ho <[email protected]> * Register patch Signed-off-by: Derek Ho <[email protected]> * Add imports Signed-off-by: Derek Ho <[email protected]> * Patch Signed-off-by: Derek Ho <[email protected]> * Fix integration test Signed-off-by: Derek Ho <[email protected]> * Update IT Signed-off-by: Derek Ho <[email protected]> * Add tests Signed-off-by: Derek Ho <[email protected]> * Fix test Signed-off-by: Derek Ho <[email protected]> * Fix tests and increase code cov Signed-off-by: Derek Ho <[email protected]> * Add more coverage to impl Signed-off-by: Derek Ho <[email protected]> * Fix test and jacoco passing Signed-off-by: Derek Ho <[email protected]> * Test fix Signed-off-by: Derek Ho <[email protected]> * Add docs Signed-off-by: Derek Ho <[email protected]> --------- Signed-off-by: Derek Ho <[email protected]> (cherry picked from commit f835112) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* | ||
* * Copyright OpenSearch Contributors | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.opensearch.sql.datasources.model.transport; | ||
|
||
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; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
import lombok.Getter; | ||
import org.opensearch.action.ActionRequest; | ||
import org.opensearch.action.ActionRequestValidationException; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
|
||
public class PatchDataSourceActionRequest extends ActionRequest { | ||
|
||
@Getter private Map<String, Object> dataSourceData; | ||
|
||
/** Constructor of UpdateDataSourceActionRequest from StreamInput. */ | ||
public PatchDataSourceActionRequest(StreamInput in) throws IOException { | ||
super(in); | ||
} | ||
Check warning on line 28 in datasources/src/main/java/org/opensearch/sql/datasources/model/transport/PatchDataSourceActionRequest.java Codecov / codecov/patchdatasources/src/main/java/org/opensearch/sql/datasources/model/transport/PatchDataSourceActionRequest.java#L27-L28
|
||
|
||
public PatchDataSourceActionRequest(Map<String, Object> dataSourceData) { | ||
this.dataSourceData = dataSourceData; | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
if (this.dataSourceData.get(NAME_FIELD).equals(DEFAULT_DATASOURCE_NAME)) { | ||
ActionRequestValidationException exception = new ActionRequestValidationException(); | ||
exception.addValidationError( | ||
Check warning on line 38 in datasources/src/main/java/org/opensearch/sql/datasources/model/transport/PatchDataSourceActionRequest.java Codecov / codecov/patchdatasources/src/main/java/org/opensearch/sql/datasources/model/transport/PatchDataSourceActionRequest.java#L37-L38
|
||
"Not allowed to update datasource with name : " + DEFAULT_DATASOURCE_NAME); | ||
return exception; | ||
Check warning on line 40 in datasources/src/main/java/org/opensearch/sql/datasources/model/transport/PatchDataSourceActionRequest.java Codecov / codecov/patchdatasources/src/main/java/org/opensearch/sql/datasources/model/transport/PatchDataSourceActionRequest.java#L40
|
||
} else if (this.dataSourceData.get(CONNECTOR_FIELD) != null) { | ||
ActionRequestValidationException exception = new ActionRequestValidationException(); | ||
exception.addValidationError("Not allowed to update connector for datasource"); | ||
return exception; | ||
Check warning on line 44 in datasources/src/main/java/org/opensearch/sql/datasources/model/transport/PatchDataSourceActionRequest.java Codecov / codecov/patchdatasources/src/main/java/org/opensearch/sql/datasources/model/transport/PatchDataSourceActionRequest.java#L42-L44
|
||
} else { | ||
return null; | ||
Check warning on line 46 in datasources/src/main/java/org/opensearch/sql/datasources/model/transport/PatchDataSourceActionRequest.java Codecov / codecov/patchdatasources/src/main/java/org/opensearch/sql/datasources/model/transport/PatchDataSourceActionRequest.java#L46
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* | ||
* * Copyright OpenSearch Contributors | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package org.opensearch.sql.datasources.model.transport; | ||
|
||
import java.io.IOException; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.opensearch.core.action.ActionResponse; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
|
||
@RequiredArgsConstructor | ||
public class PatchDataSourceActionResponse extends ActionResponse { | ||
|
||
@Getter private final String result; | ||
|
||
public PatchDataSourceActionResponse(StreamInput in) throws IOException { | ||
super(in); | ||
result = in.readString(); | ||
} | ||
Check warning on line 25 in datasources/src/main/java/org/opensearch/sql/datasources/model/transport/PatchDataSourceActionResponse.java Codecov / codecov/patchdatasources/src/main/java/org/opensearch/sql/datasources/model/transport/PatchDataSourceActionResponse.java#L23-L25
|
||
|
||
@Override | ||
public void writeTo(StreamOutput streamOutput) throws IOException { | ||
streamOutput.writeString(result); | ||
} | ||
Check warning on line 30 in datasources/src/main/java/org/opensearch/sql/datasources/model/transport/PatchDataSourceActionResponse.java Codecov / codecov/patchdatasources/src/main/java/org/opensearch/sql/datasources/model/transport/PatchDataSourceActionResponse.java#L29-L30
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* | ||
* * 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.PatchDataSourceActionRequest; | ||
import org.opensearch.sql.datasources.model.transport.PatchDataSourceActionResponse; | ||
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 TransportPatchDataSourceAction | ||
extends HandledTransportAction<PatchDataSourceActionRequest, PatchDataSourceActionResponse> { | ||
|
||
public static final String NAME = "cluster:admin/opensearch/ql/datasources/patch"; | ||
public static final ActionType<PatchDataSourceActionResponse> ACTION_TYPE = | ||
new ActionType<>(NAME, PatchDataSourceActionResponse::new); | ||
|
||
private DataSourceService dataSourceService; | ||
|
||
/** | ||
* TransportPatchDataSourceAction action for updating datasource. | ||
* | ||
* @param transportService transportService. | ||
* @param actionFilters actionFilters. | ||
* @param dataSourceService dataSourceService. | ||
*/ | ||
@Inject | ||
public TransportPatchDataSourceAction( | ||
TransportService transportService, | ||
ActionFilters actionFilters, | ||
DataSourceServiceImpl dataSourceService) { | ||
super( | ||
TransportPatchDataSourceAction.NAME, | ||
transportService, | ||
actionFilters, | ||
PatchDataSourceActionRequest::new); | ||
this.dataSourceService = dataSourceService; | ||
} | ||
|
||
@Override | ||
protected void doExecute( | ||
Task task, | ||
PatchDataSourceActionRequest request, | ||
ActionListener<PatchDataSourceActionResponse> actionListener) { | ||
try { | ||
dataSourceService.patchDataSource(request.getDataSourceData()); | ||
String responseContent = | ||
new JsonResponseFormatter<String>(PRETTY) { | ||
@Override | ||
protected Object buildJsonObject(String response) { | ||
return response; | ||
} | ||
}.format("Updated DataSource with name " + request.getDataSourceData().get(NAME_FIELD)); | ||
actionListener.onResponse(new PatchDataSourceActionResponse(responseContent)); | ||
} catch (Exception e) { | ||
actionListener.onFailure(e); | ||
} | ||
} | ||
} |