-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Snowflake Profile mapping when using AWS default region (#1406)
When using a Cosmos Snowflake Profile mapping using a Snowflake account set in the AWS default region, Cosmos would fail if the default region was specified in the Airflow connection. The dbt docs state: > For AWS accounts in the US West default region, you can use abc123 (without any other segments). For some AWS accounts you will have to append the region and/or cloud platform. For example, abc123.eu-west-1 or abc123.eu-west-2.aws. https://docs.getdbt.com/docs/core/connect-data-platform/snowflake-setup#account Although it seems that defining the default region would be optional, a Cosmos user reported facing 404 and seeing a dbt error message when attempting to use `SnowflakeUserPasswordProfileMapping` with an Airflow Snowflake connection that defined the region `us-west-2`. ![snowflake-404](https://github.com/user-attachments/assets/c1884fff-1cad-4c57-b2f3-11a4f44b085b) We solved the issue by removing the region `us-west-2` from the connection. Since this restriction only applies to AWS and this Snowflake region only exists to AWS, this change seems safe: ![Screenshot 2024-12-18 at 18 45 31](https://github.com/user-attachments/assets/ff2f8a0b-578b-4a62-9fc3-258a43148775)
- Loading branch information
Showing
6 changed files
with
66 additions
and
72 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,28 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
from cosmos.profiles.base import BaseProfileMapping | ||
|
||
DEFAULT_AWS_REGION = "us-west-2" | ||
|
||
|
||
class SnowflakeBaseProfileMapping(BaseProfileMapping): | ||
|
||
@property | ||
def profile(self) -> dict[str, Any | None]: | ||
"""Gets profile.""" | ||
profile_vars = { | ||
**self.mapped_params, | ||
**self.profile_args, | ||
} | ||
return profile_vars | ||
|
||
def transform_account(self, account: str) -> str: | ||
"""Transform the account to the format <account>.<region> if it's not already.""" | ||
region = self.conn.extra_dejson.get("region") | ||
# | ||
if region and region != DEFAULT_AWS_REGION and region not in account: | ||
account = f"{account}.{region}" | ||
|
||
return str(account) |
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
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
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,19 @@ | ||
from unittest.mock import patch | ||
|
||
from cosmos.profiles.snowflake.base import SnowflakeBaseProfileMapping | ||
|
||
|
||
@patch("cosmos.profiles.snowflake.base.SnowflakeBaseProfileMapping.conn.extra_dejson", {"region": "us-west-2"}) | ||
@patch("cosmos.profiles.snowflake.base.SnowflakeBaseProfileMapping.conn") | ||
def test_default_region(mock_conn): | ||
profile_mapping = SnowflakeBaseProfileMapping(conn_id="fake-conn") | ||
response = profile_mapping.transform_account("myaccount") | ||
assert response == "myaccount" | ||
|
||
|
||
@patch("cosmos.profiles.snowflake.base.SnowflakeBaseProfileMapping.conn.extra_dejson", {"region": "us-east-1"}) | ||
@patch("cosmos.profiles.snowflake.base.SnowflakeBaseProfileMapping.conn") | ||
def test_non_default_region(mock_conn): | ||
profile_mapping = SnowflakeBaseProfileMapping(conn_id="fake-conn") | ||
response = profile_mapping.transform_account("myaccount") | ||
assert response == "myaccount.us-east-1" |