-
Notifications
You must be signed in to change notification settings - Fork 3k
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
feat(ingest): add preset source #10954
Merged
Merged
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
9bc43b1
datahub metadata-ingestion support for preset
llance d677b06
cleanup unused import
llance 29da83b
Add preset tests
hwmarkcheng 13c8c29
Add back test respone
hwmarkcheng ab34cc7
fix unit tests
hwmarkcheng 8a23dd5
Add versioning to integration test
hwmarkcheng 06b045d
Merge branch 'datahub-project:master' into master
llance 4e03a23
Merge pull request #1 from llance/add-preset-tests
hwmarkcheng 8f7bfed
Merge branch 'datahub-project:master' into master
llance 23d88e7
remove preset specific config keys
llance 5c4211c
Merge pull request #2 from llance/shuixi/cleanup-superset
llance 9e9b79a
Merge branch 'datahub-project:master' into master
llance 8708be6
Merge branch 'datahub-project:master' into master
llance a9a9ecc
Merge branch 'datahub-project:master' into master
llance 6741c51
working state
llance 36cffd9
Address PR comments
hwmarkcheng 1e9c3c9
Fix session
hwmarkcheng a566d88
Merge pull request #5 from llance/fix-preset-ingestion
hwmarkcheng a823082
Merge branch 'master' into master
hwmarkcheng dd10b4b
Fix Linting Issues
hwmarkcheng a3ab921
Merge pull request #6 from llance/fix-lint-issues
hwmarkcheng 6258bd9
Update Preset Tests
hwmarkcheng 088af77
Merge pull request #7 from llance/fix-preset-tests
hwmarkcheng 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 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
121 changes: 121 additions & 0 deletions
121
metadata-ingestion/src/datahub/ingestion/source/preset.py
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,121 @@ | ||
import logging | ||
from typing import Dict, Optional | ||
|
||
import requests | ||
from pydantic.class_validators import root_validator, validator | ||
from pydantic.fields import Field | ||
|
||
from datahub.configuration import ConfigModel | ||
|
||
from datahub.emitter.mce_builder import DEFAULT_ENV | ||
from datahub.ingestion.api.common import PipelineContext | ||
from datahub.ingestion.api.decorators import ( | ||
SourceCapability, | ||
SupportStatus, | ||
capability, | ||
config_class, | ||
platform_name, | ||
support_status, | ||
) | ||
|
||
from datahub.ingestion.source.state.stale_entity_removal_handler import ( | ||
StaleEntityRemovalSourceReport, | ||
StatefulStaleMetadataRemovalConfig, | ||
) | ||
from datahub.ingestion.source.state.stateful_ingestion_base import ( | ||
StatefulIngestionConfigBase, | ||
) | ||
|
||
from datahub.utilities import config_clean\ | ||
|
||
from datahub.ingestion.source.superset import SupersetSource | ||
|
||
logger = logging.getLogger(__name__) | ||
class PresetConfig(StatefulIngestionConfigBase, ConfigModel): | ||
manager_uri: str = Field( | ||
default="https://api.app.preset.io", description="Preset.io API URL" | ||
) | ||
connect_uri: str = Field(default=None, description="Preset workspace URL.") | ||
display_uri: Optional[str] = Field( | ||
default=None, | ||
description="optional URL to use in links (if `connect_uri` is only for ingestion)", | ||
) | ||
api_key: Optional[str] = Field(default=None, description="Preset.io API key.") | ||
api_secret: Optional[str] = Field(default=None, description="Preset.io API secret.") | ||
|
||
# Configuration for stateful ingestion | ||
stateful_ingestion: Optional[StatefulStaleMetadataRemovalConfig] = Field( | ||
default=None, description="Preset Stateful Ingestion Config." | ||
) | ||
|
||
options: Dict = Field(default={}, description="") | ||
env: str = Field( | ||
default=DEFAULT_ENV, | ||
description="Environment to use in namespace when constructing URNs", | ||
) | ||
database_alias: Dict[str, str] = Field( | ||
default={}, | ||
description="Can be used to change mapping for database names in superset to what you have in datahub", | ||
) | ||
|
||
@validator("connect_uri", "display_uri") | ||
def remove_trailing_slash(cls, v): | ||
return config_clean.remove_trailing_slashes(v) | ||
|
||
@root_validator | ||
def default_display_uri_to_connect_uri(cls, values): | ||
base = values.get("display_uri") | ||
if base is None: | ||
values["display_uri"] = values.get("connect_uri") | ||
return values | ||
|
||
|
||
@platform_name("Preset") | ||
@config_class(PresetConfig) | ||
@support_status(SupportStatus.TESTING) | ||
@capability( | ||
SourceCapability.DELETION_DETECTION, "Optionally enabled via stateful_ingestion" | ||
) | ||
class PresetSource(SupersetSource): | ||
""" | ||
Variation of the Superset plugin that works with Preset.io (Apache Superset SaaS). | ||
""" | ||
|
||
config: PresetConfig | ||
report: StaleEntityRemovalSourceReport | ||
platform = "preset" | ||
|
||
def __init__(self, ctx: PipelineContext, config: PresetConfig): | ||
logger.info(f"ctx is {ctx}") | ||
|
||
super().__init__(ctx, config) | ||
self.config = config | ||
self.report = StaleEntityRemovalSourceReport() | ||
self.session = self.login() | ||
|
||
def login(self): | ||
try: | ||
login_response = requests.post( | ||
f"{self.config.manager_uri}/v1/auth/", | ||
json={"name": self.config.api_key, "secret": self.config.api_secret}, | ||
) | ||
except requests.exceptions.RequestException as e: | ||
logger.error(f"Failed to authenticate with Preset: {e}") | ||
raise e | ||
|
||
self.access_token = login_response.json()["payload"]["access_token"] | ||
logger.debug("Got access token from Preset") | ||
|
||
requests_session = requests.Session() | ||
requests_session.headers.update( | ||
{ | ||
"Authorization": f"Bearer {self.access_token}", | ||
"Content-Type": "application/json", | ||
"Accept": "*/*", | ||
} | ||
) | ||
# Test the connection | ||
test_response = requests_session.get(f"{self.config.connect_uri}/version") | ||
if not test_response.ok: | ||
logger.error("Unable to connect to workspace") | ||
return requests_session |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because
login()
is called from superset's init, and we callsuper().__init__(...)
here, I don't think we actually need this lineThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overall I don't want to make the login API call twice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey Harshal, the login here is called twice since the authentication method is different (For Preset, which is why we redefined the method below & called it). Do you have any suggestions on a better way to handle this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the self.login in the superset class will call login() from the preset method already - that's the behavior of method overrides with python
So I believe things should "just work" without the explicit calls