generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 49
Implement creation of ip2geo feature #257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
49565b2
Update gradle version to 7.6 (#265)
VijayanB 6d98079
Implement creation of ip2geo feature
heemin32 c98f977
Update based on review comments
heemin32 a99afa1
Renamed a parameter in cluster setting update consumers
heemin32 ddcc0e9
Resolved few comments from PR
heemin32 af33a10
Removed max datasource count setting
heemin32 b505d85
Refactoring code based on review comment
heemin32 e6867b6
Resolved missed comments
heemin32 9fbc3a0
Refactored exception handlings
heemin32 3a2a7fc
Another commit to resolve comments
heemin32 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| distributionBase=GRADLE_USER_HOME | ||
| distributionPath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists |
30 changes: 30 additions & 0 deletions
30
src/main/java/org/opensearch/geospatial/ip2geo/action/PutDatasourceAction.java
This file contains hidden or 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,30 @@ | ||
| /* | ||
| * 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.geospatial.ip2geo.action; | ||
|
|
||
| import org.opensearch.action.ActionType; | ||
| import org.opensearch.action.support.master.AcknowledgedResponse; | ||
|
|
||
| /** | ||
| * Ip2Geo datasource creation action | ||
| */ | ||
| public class PutDatasourceAction extends ActionType<AcknowledgedResponse> { | ||
| /** | ||
| * Put datasource action instance | ||
| */ | ||
| public static final PutDatasourceAction INSTANCE = new PutDatasourceAction(); | ||
navneet1v marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * Put datasource action name | ||
| */ | ||
| public static final String NAME = "cluster:admin/geospatial/datasource/put"; | ||
|
|
||
| private PutDatasourceAction() { | ||
| super(NAME, AcknowledgedResponse::new); | ||
| } | ||
| } | ||
168 changes: 168 additions & 0 deletions
168
src/main/java/org/opensearch/geospatial/ip2geo/action/PutDatasourceRequest.java
This file contains hidden or 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,168 @@ | ||
| /* | ||
| * 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.geospatial.ip2geo.action; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.MalformedURLException; | ||
| import java.net.URISyntaxException; | ||
| import java.net.URL; | ||
| import java.util.Locale; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import lombok.extern.log4j.Log4j2; | ||
|
|
||
| import org.opensearch.action.ActionRequestValidationException; | ||
| import org.opensearch.action.support.master.AcknowledgedRequest; | ||
| import org.opensearch.common.io.stream.StreamInput; | ||
| import org.opensearch.common.io.stream.StreamOutput; | ||
| import org.opensearch.common.unit.TimeValue; | ||
| import org.opensearch.core.ParseField; | ||
| import org.opensearch.core.xcontent.ObjectParser; | ||
| import org.opensearch.geospatial.ip2geo.common.DatasourceManifest; | ||
|
|
||
| /** | ||
| * GeoIP datasource creation request | ||
| */ | ||
| @Getter | ||
| @Setter | ||
| @Log4j2 | ||
| public class PutDatasourceRequest extends AcknowledgedRequest<PutDatasourceRequest> { | ||
| private static final ParseField ENDPOINT_FIELD = new ParseField("endpoint"); | ||
| private static final ParseField UPDATE_INTERVAL_IN_DAYS_FIELD = new ParseField("update_interval_in_days"); | ||
| /** | ||
| * @param datasourceName the datasource name | ||
| * @return the datasource name | ||
| */ | ||
| private String datasourceName; | ||
| /** | ||
| * @param endpoint url to a manifest file for a datasource | ||
| * @return url to a manifest file for a datasource | ||
| */ | ||
| private String endpoint; | ||
| /** | ||
| * @param updateIntervalInDays update interval of a datasource | ||
| * @return update interval of a datasource | ||
| */ | ||
| private TimeValue updateIntervalInDays; | ||
|
|
||
| /** | ||
| * Parser of a datasource | ||
| */ | ||
| public static final ObjectParser<PutDatasourceRequest, Void> PARSER; | ||
heemin32 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| static { | ||
| PARSER = new ObjectParser<>("put_datasource"); | ||
| PARSER.declareString((request, val) -> request.setEndpoint(val), ENDPOINT_FIELD); | ||
| PARSER.declareLong((request, val) -> request.setUpdateIntervalInDays(TimeValue.timeValueDays(val)), UPDATE_INTERVAL_IN_DAYS_FIELD); | ||
| } | ||
|
|
||
| /** | ||
| * Default constructor | ||
| * @param datasourceName name of a datasource | ||
| */ | ||
| public PutDatasourceRequest(final String datasourceName) { | ||
| this.datasourceName = datasourceName; | ||
| } | ||
|
|
||
| /** | ||
| * Constructor with stream input | ||
| * @param in the stream input | ||
| * @throws IOException IOException | ||
| */ | ||
| public PutDatasourceRequest(final StreamInput in) throws IOException { | ||
| super(in); | ||
| this.datasourceName = in.readString(); | ||
| this.endpoint = in.readString(); | ||
| this.updateIntervalInDays = in.readTimeValue(); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(final StreamOutput out) throws IOException { | ||
| super.writeTo(out); | ||
| out.writeString(datasourceName); | ||
| out.writeString(endpoint); | ||
| out.writeTimeValue(updateIntervalInDays); | ||
| } | ||
|
|
||
| @Override | ||
| public ActionRequestValidationException validate() { | ||
| ActionRequestValidationException errors = new ActionRequestValidationException(); | ||
| validateEndpoint(errors); | ||
| validateUpdateInterval(errors); | ||
| return errors.validationErrors().isEmpty() ? null : errors; | ||
| } | ||
|
|
||
| /** | ||
| * Conduct following validation on endpoint | ||
| * 1. endpoint format complies with RFC-2396 | ||
| * 2. validate manifest file from the endpoint | ||
| * | ||
| * @param errors the errors to add error messages | ||
| */ | ||
| private void validateEndpoint(final ActionRequestValidationException errors) { | ||
navneet1v marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try { | ||
| URL url = new URL(endpoint); | ||
| url.toURI(); // Validate URL complies with RFC-2396 | ||
| validateManifestFile(url, errors); | ||
| } catch (MalformedURLException | URISyntaxException e) { | ||
| log.info("Invalid URL[{}] is provided", endpoint, e); | ||
| errors.addValidationError("Invalid URL format is provided"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Conduct following validation on url | ||
| * 1. can read manifest file from the endpoint | ||
| * 2. the url in the manifest file complies with RFC-2396 | ||
| * 3. updateIntervalInDays is less than validForInDays value in the manifest file | ||
| * | ||
| * @param url the url to validate | ||
| * @param errors the errors to add error messages | ||
| */ | ||
| private void validateManifestFile(final URL url, final ActionRequestValidationException errors) { | ||
heemin32 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| DatasourceManifest manifest; | ||
| try { | ||
| manifest = DatasourceManifest.Builder.build(url); | ||
| } catch (Exception e) { | ||
| log.info("Error occurred while reading a file from {}", url, e); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be info or error?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because this is a customer input error not system error. |
||
| errors.addValidationError(String.format(Locale.ROOT, "Error occurred while reading a file from %s", url)); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| new URL(manifest.getUrl()).toURI(); // Validate URL complies with RFC-2396 | ||
| } catch (MalformedURLException | URISyntaxException e) { | ||
| log.info("Invalid URL[{}] is provided for url field in the manifest file", manifest.getUrl(), e); | ||
| errors.addValidationError("Invalid URL format is provided for url field in the manifest file"); | ||
| return; | ||
| } | ||
|
|
||
| if (updateIntervalInDays.days() >= manifest.getValidForInDays()) { | ||
| errors.addValidationError( | ||
| String.format( | ||
| Locale.ROOT, | ||
| "updateInterval %d is should be smaller than %d", | ||
| updateIntervalInDays.days(), | ||
| manifest.getValidForInDays() | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validate updateIntervalInDays is larger than 0 | ||
| * | ||
| * @param errors the errors to add error messages | ||
| */ | ||
| private void validateUpdateInterval(final ActionRequestValidationException errors) { | ||
| if (updateIntervalInDays.compareTo(TimeValue.timeValueDays(1)) > 0) { | ||
| errors.addValidationError("Update interval should be equal to or larger than 1 day"); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.