-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(source create snowflake): Support format `--account {org}.{accou…
…nt}` (#206) - Upgrade regress dep - Support more user-friendly `--account {org}.{account}` format when creating Snowflake sources ## Test plan Failure: ``` % cargo run -- source create snowflake -n testing-easier-identifier --account ASDF --user redacted --password redacted --database redacted error creating source: Invalid account identifers given. Provide account identifiers either via `--account ${ORG_NAME}.${ACCOUNT_NAME}, or via `--organization ${ORG_NAME} --account ${ACCOUNT_NAME}`. ``` Success: ``` % cargo run -- source create snowflake -n testing-easier-identifier --account ABC123.DEF456 --user redacted --password redacted --database redacted Source created ``` && verifying the expected values were persisted: ``` dpm=> select source_parameters->'organization', source_parameters->'account' from source where name = 'testing-easier-identifier'; ?column? | ?column? -----------+----------- "ABC123" | "DEF456" (1 row) ```
- Loading branch information
1 parent
c5e53a5
commit c7175e8
Showing
4 changed files
with
64 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,21 +1,53 @@ | ||
use anyhow::{bail, Result}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
// Source: https://docs.snowflake.com/en/user-guide/admin-account-identifier#organization-name | ||
// We assume that when ^ refers to "letters" that means [a-zA-Z]. | ||
const ORG_NAME_PATTERN: &str = "[a-zA-Z][a-zA-Z0-9]*"; | ||
const ACCOUNT_NAME_PATTERN: &str = "[a-zA-Z]\\w*"; | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
pub struct OrganizationName(String); | ||
impl std::str::FromStr for OrganizationName { | ||
type Err = anyhow::Error; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
// Source: https://docs.snowflake.com/en/user-guide/admin-account-identifier#organization-name | ||
// We assume that when ^ refers to "letters" that means [a-zA-Z]. | ||
if regress::Regex::new("^[a-zA-Z][a-zA-Z0-9]*$") | ||
.unwrap() | ||
.find(s) | ||
.is_none() | ||
{ | ||
bail!("doesn't match pattern \"^[a-zA-Z][a-zA-Z0-9]*$\""); | ||
let pattern = format!("^{}$", ORG_NAME_PATTERN); | ||
|
||
if regress::Regex::new(&pattern).unwrap().find(s).is_none() { | ||
bail!("doesn't match pattern \"{}\"", pattern); | ||
} | ||
Ok(Self(s.to_string())) | ||
|
||
Ok(Self(s.to_owned())) | ||
} | ||
} | ||
|
||
/// If `account_name` is of the form '{org}.{account}', prefer that. Otherwise, | ||
/// rely on both org and account name having been provided separately. If | ||
/// neither work out, return `Err`. | ||
/// | ||
/// See also: https://docs.snowflake.com/en/user-guide/admin-account-identifier | ||
pub fn resolve_account_identifiers<'a>( | ||
organization_name: Option<&'a OrganizationName>, | ||
account_name: &'a str, | ||
) -> Result<(OrganizationName, &'a str)> { | ||
let combined_pattern = regress::Regex::new(&format!( | ||
"^(?<org_name>{})[.-](?<account_name>{})$", | ||
ORG_NAME_PATTERN, ACCOUNT_NAME_PATTERN | ||
)) | ||
.unwrap(); | ||
|
||
if let Some(result) = combined_pattern.find(account_name) { | ||
// SAFETY: Regex match implies that both groups matched. | ||
let org_name = &account_name[result.named_group("org_name").unwrap()]; | ||
let account_name = &account_name[result.named_group("account_name").unwrap()]; | ||
|
||
return Ok((OrganizationName(org_name.to_owned()), account_name)); | ||
} | ||
|
||
if let Some(org_name) = organization_name { | ||
return Ok((org_name.to_owned(), account_name)); | ||
} | ||
|
||
bail!("Invalid account identifers given. Provide account identifiers either via `--account ${{ORG_NAME}}.${{ACCOUNT_NAME}}`, or via `--organization ${{ORG_NAME}} --account ${{ACCOUNT_NAME}}`.") | ||
} |
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