Skip to content
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(source create snowflake): Support format --account {org}.{account} #206

Merged
merged 3 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dialoguer = "0.10.4"
directories = "5.0.1"
inquire = "0.6.2"
prost = "0.11.9"
regress = "0.6.0"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was motivated by 0.6.0 wrongly rejecting a valid pattern:

^(?<org_name>[a-zA-Z][a-zA-Z0-9]*)[.-](?<account_name>[a-zA-Z_]+)$
thread 'main' panicked at src/command/snowflake.rs:34:6:
called `Result::unwrap()` on an `Err` value: Error { text: "Invalid token at named capture group identifier" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

regress = "0.7.1"
reqwest = { version = "0.11.18", default-features = false, features = ["json", "rustls-tls-native-roots"] }
rust-embed = { version = "6.6.1", features = ["include-exclude"] }
semver = { version = "1.0.18", features = ["serde"] }
Expand Down
50 changes: 41 additions & 9 deletions src/command/snowflake.rs
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()];
Comment on lines +41 to +43
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going forward, I'd like to accompany any non-trivial .unwrap() calls with // SAFETY comments, explaining why they won't panic. The hope is that this practice will promote quality and make it easier to audit the codebase for any preexisting, unjustified unwrapping.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL: Safety comments.


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}}`.")
}
32 changes: 20 additions & 12 deletions src/command/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ pub enum CreateSource {
name: String,

#[arg(long, value_name = "NAME")]
organization: snowflake::OrganizationName,
organization: Option<snowflake::OrganizationName>,

/// An account identifier string like `${ORG_NAME}.${ACCOUNT_NAME}`.
/// Alternatively, you can provide the components separately via
/// `--organization ${ORG_NAME} --account ${ACCOUNT_NAME}`.
ajmasci marked this conversation as resolved.
Show resolved Hide resolved
#[arg(long, value_name = "NAME")]
account: String,

Expand Down Expand Up @@ -109,17 +112,22 @@ pub async fn create(cs: &CreateSource) -> Result<()> {
user,
password,
staging_database,
} => CreateSourceInput {
name,
source_parameters: CreateSourceParameters::Snowflake {
organization: organization.to_owned(),
account,
database,
user,
authentication_method: SnowflakeAuthenticationMethod::Password { password },
staging_database,
},
},
} => {
let (organization, account) =
snowflake::resolve_account_identifiers(organization.as_ref(), account)?;

CreateSourceInput {
name,
source_parameters: CreateSourceParameters::Snowflake {
organization,
account,
database,
user,
authentication_method: SnowflakeAuthenticationMethod::Password { password },
staging_database,
},
}
}
};

let token = session::get_token()?;
Expand Down
Loading