-
Notifications
You must be signed in to change notification settings - Fork 4
Add dbt-cloud integration command to dp cli #99
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
Open
rdziadosz
wants to merge
10
commits into
getindata:develop
Choose a base branch
from
rdziadosz:dbt-cloud
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a0e7d6d
Add dbt-cloud integration command to dp cli
21132c1
Add dbt-cloud integration command to dp cli
e2427e6
Add dbt-cloud integration command to dp cli
4a4835f
Add dbt-cloud integration command to dp cli
1ddbb09
Add dbt-cloud integration command to dp cli
ad56045
Add dbt-cloud integration command to dp cli
8584855
Add dbt-cloud integration command to dp cli
464321c
Add dbt-cloud integration command to dp cli
ec3f647
Add dbt-cloud integration command to dp cli
becddfe
Add dbt-cloud integration command to dp cli
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 |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| import json | ||
| from typing import Any, Dict | ||
|
|
||
| import click | ||
|
|
||
| from data_pipelines_cli.dbt_cloud_api_client import DbtCloudApiClient | ||
|
|
||
| from ..cli_constants import BUILD_DIR | ||
| from ..cli_utils import echo_info | ||
| from ..config_generation import read_dictionary_from_config_directory | ||
| from ..dbt_utils import _dump_dbt_vars_from_configs_to_string | ||
|
|
||
|
|
||
| def read_dbtcloud_config() -> Dict[str, Any]: | ||
| """ | ||
| Read dbt Cloud configuration. | ||
|
|
||
| :param env: Name of the environment | ||
| :type env: str | ||
| :return: Compiled dictionary | ||
| :rtype: Dict[str, Any] | ||
| """ | ||
| return read_dictionary_from_config_directory(BUILD_DIR.joinpath("dag"), ".", "dbtcloud.yml") | ||
|
|
||
|
|
||
| def read_bigquery_config(env: str) -> Dict[str, Any]: | ||
| """ | ||
| Read dbt Cloud configuration. | ||
|
|
||
| :param env: Name of the environment | ||
| :type env: str | ||
| :return: Compiled dictionary | ||
| :rtype: Dict[str, Any] | ||
| """ | ||
| return read_dictionary_from_config_directory(BUILD_DIR.joinpath("dag"), env, "bigquery.yml") | ||
|
|
||
|
|
||
| @click.command(name="configure-cloud", help="Create dbt Cloud project") | ||
| @click.option( | ||
| "--account_id", | ||
| type=int, | ||
| required=True, | ||
| help="""dbt Cloud Account identifier To obtain your dbt Cloud account ID, sign into dbt Cloud | ||
| in your browser. Take note of the number directly following the accounts path component of the | ||
| URL - this is your account ID""", | ||
| ) | ||
| @click.option( | ||
| "--token", | ||
| type=str, | ||
| required=True, | ||
| help="""API token for your DBT Cloud account. You can retrieve your User API token from your | ||
| User Profile (top right icon) > API Access. You can also use Service Token. Retrieve it from | ||
| Account Settings > Service Tokens > Create Service Token.""", | ||
| ) | ||
| @click.option( | ||
| "--remote_url", | ||
| type=str, | ||
| required=True, | ||
| help="""Git stores remote URL Note: After creating a dbt Cloud repository's SSH key, you will | ||
| need to add the generated key text as a deploy key to the target repository. This gives dbt | ||
| Cloud permissions to read / write in the repository.""", | ||
| ) | ||
| @click.option("--keyfile", type=str, required=True, help="Bigquery keyfile") | ||
| def configure_cloud_command(account_id: int, token: str, remote_url: str, keyfile: str) -> None: | ||
| client = DbtCloudApiClient("https://cloud.getdbt.com/api", account_id, token) | ||
|
|
||
| dbtcloud_config = read_dbtcloud_config() | ||
| base_bq_config = read_bigquery_config("base") | ||
| file = open(keyfile) | ||
| keyfile_data = json.load(file) | ||
| dbtcloud_project_id = client.create_project(dbtcloud_config["project_name"]) | ||
| (repository_id, deploy_key) = client.create_repository(dbtcloud_project_id, remote_url) | ||
| echo_info( | ||
| "You need to add the generated key text as a deploy key to the target repository.\n" | ||
| "This gives dbt Cloud permissions to read / write in the repository\n" | ||
| f"{deploy_key}" | ||
| ) | ||
|
|
||
| environments_projects = {} | ||
| for environment in dbtcloud_config["environments"]: | ||
| bq_config = read_bigquery_config(environment["config_dir"]) | ||
| environments_projects[environment["name"]] = bq_config["project"] | ||
| environment_id = create_environment( | ||
| client, environment, bq_config["dataset"], dbtcloud_project_id | ||
| ) | ||
| if environment["type"] == "deployment": | ||
| dbt_vars = _dump_dbt_vars_from_configs_to_string(environment["config_dir"]).strip() | ||
| client.create_job( | ||
| dbtcloud_project_id, | ||
| environment_id, | ||
| environment["schedule_interval"], | ||
| "Job - " + environment["name"], | ||
| dbt_vars, | ||
| ) | ||
|
|
||
| client.create_environment_variable( | ||
| dbtcloud_project_id, base_bq_config["project"], environments_projects | ||
| ) | ||
|
|
||
| connection_id = create_bq_connection(client, keyfile_data, dbtcloud_project_id) | ||
|
|
||
| client.associate_connection_repository( | ||
| dbtcloud_config["project_name"], dbtcloud_project_id, connection_id, repository_id | ||
| ) | ||
|
|
||
|
|
||
| def create_bq_connection( | ||
| client: DbtCloudApiClient, keyfile_data: Dict[str, str], project_id: int | ||
| ) -> int: | ||
| """ | ||
| Creates a connection to the bigquery warehouse in the dbt Cloud project. | ||
|
|
||
| :param client: API Client | ||
| :param keyfile_data: Data read from Bigquery keyfile | ||
| :param project_id: ID of the project in which the connection is to be created | ||
| :return: ID of the created connection | ||
| """ | ||
| return client.create_bigquery_connection( | ||
| project_id=project_id, | ||
| name="BQ Connection Name", | ||
| is_active=True, | ||
| gcp_project_id='{{ env_var("DBT_GCP_PROJECT") }}', | ||
| timeout_seconds=100, | ||
| private_key_id=keyfile_data["private_key_id"], | ||
| private_key=keyfile_data["private_key"], | ||
| client_email=keyfile_data["client_email"], | ||
| client_id=keyfile_data["client_id"], | ||
| auth_uri=keyfile_data["auth_uri"], | ||
| token_uri=keyfile_data["token_uri"], | ||
| auth_provider_x509_cert_url=keyfile_data["auth_provider_x509_cert_url"], | ||
| client_x509_cert_url=keyfile_data["client_x509_cert_url"], | ||
| ) | ||
p-pekala marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def create_environment( | ||
| client: DbtCloudApiClient, environment: Dict[str, str], dataset: str, project_id: int | ||
| ) -> int: | ||
| """ | ||
| Creates a dbt Cloud environment with the specified configuration | ||
|
|
||
| :param client: API Client | ||
| :param environment: Config of environment to be created | ||
| :param project_id: ID of the project | ||
| :param dataset: Name of target dataset | ||
| :return: ID of created environment | ||
| """ | ||
| if environment["type"] == "deployment": | ||
| credentials_id = client.create_credentials(dataset, project_id) | ||
| else: | ||
| credentials_id = None | ||
| environment_id = client.create_environment( | ||
| project_id, | ||
| environment["type"], | ||
| environment["name"], | ||
| environment["dbt_version"], | ||
| credentials_id, | ||
| ) | ||
| return environment_id | ||
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.
does it resolve the project properly? In bigquery.yml we have project: "{{ env_var('GCP_PROJECT') }}" currently. It should be taken from env durring deployment isn't it?
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.
I added resolving env vars using
dbt showcommand.