forked from opensearch-project/sql
-
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: Andy Kwok <andy.kwok@improving.com>
- Loading branch information
1 parent
8b3fa5b
commit 9a3a0ad
Showing
9 changed files
with
299 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
plugins { | ||
id 'java' | ||
id 'maven-publish' | ||
} | ||
|
||
group = 'org.opensearch' | ||
version = '3.0.0.0-SNAPSHOT' | ||
|
||
repositories { | ||
mavenLocal() | ||
maven { url "https://aws.oss.sonatype.org/content/repositories/snapshots" } | ||
mavenCentral() | ||
maven { url "https://plugins.gradle.org/m2/" } | ||
} | ||
|
||
dependencies { | ||
compileOnly "org.opensearch:opensearch:${opensearch_version}" | ||
implementation(project(":${rootProject.name}-common")) | ||
|
||
testImplementation platform('org.junit:junit-bom:5.10.0') | ||
testImplementation 'org.junit.jupiter:junit-jupiter' | ||
} | ||
|
||
publishing { | ||
publications { | ||
mavenJava(MavenPublication) { | ||
from components.java | ||
} | ||
} | ||
repositories { | ||
mavenLocal() | ||
} | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
} |
35 changes: 35 additions & 0 deletions
35
client/src/main/java/org/opensearch/IpEnrichmentActionClient.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,35 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch; | ||
|
||
import org.opensearch.client.node.NodeClient; | ||
import org.opensearch.common.action.ActionFuture; | ||
import org.opensearch.core.action.ActionResponse; | ||
import org.opensearch.geospatial.action.IpEnrichmentAction; | ||
import org.opensearch.geospatial.action.IpEnrichmentRequest; | ||
import org.opensearch.geospatial.action.IpEnrichmentResponse; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
|
||
/** | ||
* Proxy for the node client operations. | ||
*/ | ||
public class IpEnrichmentActionClient { | ||
|
||
NodeClient nodeClient; | ||
|
||
public IpEnrichmentActionClient(NodeClient nodeClient) { | ||
this.nodeClient = nodeClient; | ||
} | ||
|
||
public String enrichIp(String ipString) throws ExecutionException, InterruptedException { | ||
ActionFuture<ActionResponse> responseActionFuture = nodeClient.execute(IpEnrichmentAction.INSTANCE, new IpEnrichmentRequest(ipString)); | ||
ActionResponse genericActionResponse = responseActionFuture.get(); | ||
IpEnrichmentResponse enrichmentResponse = IpEnrichmentResponse.fromActionResponse(genericActionResponse); | ||
return enrichmentResponse.getAnswer(); | ||
} | ||
|
||
} |
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
21 changes: 21 additions & 0 deletions
21
common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentAction.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,21 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.geospatial.action; | ||
|
||
import org.opensearch.action.ActionType; | ||
import org.opensearch.core.action.ActionResponse; | ||
|
||
public class IpEnrichmentAction extends ActionType<ActionResponse> { | ||
|
||
|
||
public static final IpEnrichmentAction INSTANCE = new IpEnrichmentAction(); | ||
|
||
public static final String NAME = "cluster:admin/geospatial/ipenrichment/get"; | ||
|
||
public IpEnrichmentAction() { | ||
super(NAME, IpEnrichmentResponse::new); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.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,79 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.geospatial.action; | ||
|
||
import org.opensearch.action.ActionRequest; | ||
import org.opensearch.action.ActionRequestValidationException; | ||
import org.opensearch.core.common.io.stream.InputStreamStreamInput; | ||
import org.opensearch.core.common.io.stream.OutputStreamStreamOutput; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
|
||
public class IpEnrichmentRequest extends ActionRequest { | ||
|
||
private String ipString; | ||
|
||
|
||
public IpEnrichmentRequest() { | ||
} | ||
|
||
public IpEnrichmentRequest(String ipString) { | ||
this.ipString = ipString; | ||
} | ||
|
||
/** | ||
* Constructor for TransportAction. | ||
* @param streamInput | ||
*/ | ||
public IpEnrichmentRequest(StreamInput streamInput) throws IOException { | ||
super(streamInput); | ||
ipString = streamInput.readString(); | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
ActionRequestValidationException errors = null; | ||
if (ipString == null) { | ||
errors = new ActionRequestValidationException(); | ||
errors.addValidationError("ip string should not be null"); | ||
} | ||
return errors; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
out.writeString(ipString); | ||
} | ||
|
||
public String getIpString() { | ||
return ipString; | ||
} | ||
|
||
public static IpEnrichmentRequest fromActionRequest(ActionRequest actionRequest) { | ||
// From the same classloader | ||
if (actionRequest instanceof IpEnrichmentRequest) { | ||
return (IpEnrichmentRequest) actionRequest; | ||
} | ||
|
||
// Or else convert it | ||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) { | ||
actionRequest.writeTo(osso); | ||
try (StreamInput input = | ||
new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) { | ||
return new IpEnrichmentRequest(input); | ||
} | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("failed to parse ActionRequest into IpEnrichmentRequest", e); | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
common/src/main/java/org/opensearch/geospatial/action/IpEnrichmentResponse.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,69 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.geospatial.action; | ||
|
||
import org.opensearch.core.action.ActionResponse; | ||
import org.opensearch.core.common.io.stream.InputStreamStreamInput; | ||
import org.opensearch.core.common.io.stream.OutputStreamStreamOutput; | ||
import org.opensearch.core.common.io.stream.StreamInput; | ||
import org.opensearch.core.common.io.stream.StreamOutput; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.util.UUID; | ||
|
||
|
||
public class IpEnrichmentResponse extends ActionResponse { | ||
|
||
|
||
private String answer; | ||
|
||
public IpEnrichmentResponse(String answer) { | ||
this.answer = answer; | ||
} | ||
|
||
public IpEnrichmentResponse(StreamInput streamInput) throws IOException { | ||
super(streamInput); | ||
answer = streamInput.readString(); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput streamOutput) throws IOException { | ||
streamOutput.writeString(answer); | ||
} | ||
|
||
public String getAnswer() { | ||
return answer; | ||
} | ||
|
||
public static IpEnrichmentResponse fromActionResponse(ActionResponse actionResponse) { | ||
// From the same classloader | ||
if (actionResponse instanceof IpEnrichmentResponse) { | ||
return (IpEnrichmentResponse) actionResponse; | ||
} | ||
|
||
// Or else convert it | ||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) { | ||
actionResponse.writeTo(osso); | ||
try (StreamInput input = | ||
new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) { | ||
return new IpEnrichmentResponse(input); | ||
} | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("failed to parse ActionResponse into IpEnrichmentResponse", e); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "IpEnrichmentResponse{" + | ||
"answer='" + answer + '\'' + | ||
'}'; | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
src/main/java/org/opensearch/geospatial/action/model/IpEnrichmentTransportAction.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,46 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.geospatial.action.model; | ||
|
||
import org.opensearch.action.ActionRequest; | ||
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.core.action.ActionResponse; | ||
import org.opensearch.geospatial.action.IpEnrichmentAction; | ||
import org.opensearch.geospatial.action.IpEnrichmentRequest; | ||
import org.opensearch.geospatial.action.IpEnrichmentResponse; | ||
import org.opensearch.tasks.Task; | ||
import org.opensearch.transport.TransportService; | ||
|
||
public class IpEnrichmentTransportAction extends HandledTransportAction<ActionRequest, | ||
ActionResponse> { | ||
|
||
|
||
@Inject | ||
public IpEnrichmentTransportAction( | ||
TransportService transportService, | ||
ActionFilters actionFilters) { | ||
super(IpEnrichmentAction.NAME, transportService, actionFilters, IpEnrichmentRequest::new); | ||
} | ||
|
||
@Override | ||
protected void doExecute(Task task, ActionRequest request, ActionListener<ActionResponse> listener) { | ||
IpEnrichmentRequest enrichmentRequest = IpEnrichmentRequest.fromActionRequest(request); | ||
listener.onResponse(new IpEnrichmentResponse(enrichmentRequest.getIpString() + " Done!")); | ||
} | ||
|
||
|
||
// @Override | ||
// protected void doExecute(Task task, ActionRequest request, ActionListener<IpEnrichmentResponse> listener) { | ||
// IpEnrichmentRequest enrichmentRequest = IpEnrichmentRequest.fromActionRequest(request); | ||
// listener.onResponse(new IpEnrichmentResponse(enrichmentRequest.getIpString() + " Done!")); | ||
// } | ||
|
||
|
||
|
||
} |
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