-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement patch API for datasources (#2273)
* 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]>
- Loading branch information
Showing
15 changed files
with
580 additions
and
32 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
49 changes: 49 additions & 0 deletions
49
...ain/java/org/opensearch/sql/datasources/model/transport/PatchDataSourceActionRequest.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,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); | ||
} | ||
|
||
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( | ||
"Not allowed to update datasource with name : " + DEFAULT_DATASOURCE_NAME); | ||
return exception; | ||
} else if (this.dataSourceData.get(CONNECTOR_FIELD) != null) { | ||
ActionRequestValidationException exception = new ActionRequestValidationException(); | ||
exception.addValidationError("Not allowed to update connector for datasource"); | ||
return exception; | ||
} else { | ||
return null; | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...in/java/org/opensearch/sql/datasources/model/transport/PatchDataSourceActionResponse.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,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(); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput streamOutput) throws IOException { | ||
streamOutput.writeString(result); | ||
} | ||
} |
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
74 changes: 74 additions & 0 deletions
74
...rc/main/java/org/opensearch/sql/datasources/transport/TransportPatchDataSourceAction.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,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); | ||
} | ||
} | ||
} |
Oops, something went wrong.