Skip to content

Commit

Permalink
chore: Make bucket name required in storage config (#2336)
Browse files Browse the repository at this point in the history
  • Loading branch information
vrongmeal authored Jan 2, 2024
1 parent 5327193 commit 2aa8ad3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ benchmarks/artifacts/
/deps

.vscode/
.helix/

# Spark jar for iceberg data generation
*.jar
Expand Down
2 changes: 1 addition & 1 deletion crates/glaredb/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl RunCommand for MetastoreArgs {
(Some(bucket), Some(service_account_path), None) => {
let service_account_key = std::fs::read_to_string(service_account_path)?;
StorageConfig::Gcs {
bucket: Some(bucket),
bucket,
service_account_key,
}
}
Expand Down
29 changes: 10 additions & 19 deletions crates/object_store_util/src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ pub enum StorageConfig {
secret_access_key: String,
region: Option<String>,
endpoint: Option<String>,
bucket: Option<String>,
bucket: String,
},
Gcs {
service_account_key: String,
bucket: Option<String>,
bucket: String,
},
Azure {
account_name: String,
access_key: String,
container_name: Option<String>,
container_name: String,
},
Local {
path: PathBuf,
Expand All @@ -49,6 +49,7 @@ impl StorageConfig {
let mut builder = AmazonS3Builder::new()
.with_access_key_id(access_key_id)
.with_secret_access_key(secret_access_key)
.with_bucket_name(bucket)
.with_region(region.clone().unwrap_or_default());

if let Some(endpoint) = endpoint {
Expand All @@ -66,22 +67,15 @@ impl StorageConfig {
}
}

if let Some(bucket) = bucket {
builder = builder.with_bucket_name(bucket);
}

Arc::new(builder.build()?)
}
StorageConfig::Gcs {
service_account_key,
bucket,
} => {
let mut builder =
GoogleCloudStorageBuilder::new().with_service_account_key(service_account_key);

if let Some(bucket) = bucket {
builder = builder.with_bucket_name(bucket);
}
let builder = GoogleCloudStorageBuilder::new()
.with_service_account_key(service_account_key)
.with_bucket_name(bucket);

Arc::new(builder.build()?)
}
Expand All @@ -90,13 +84,10 @@ impl StorageConfig {
access_key,
container_name,
} => {
let mut builder = MicrosoftAzureBuilder::new()
let builder = MicrosoftAzureBuilder::new()
.with_account(account_name)
.with_access_key(access_key);

if let Some(container_name) = container_name {
builder = builder.with_container_name(container_name);
}
.with_access_key(access_key)
.with_container_name(container_name);

Arc::new(builder.build()?)
}
Expand Down
22 changes: 10 additions & 12 deletions crates/sqlexec/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ impl EngineStorageConfig {
let bucket = opts
.get("bucket")
.cloned()
.or(url.host_str().map(|h| h.to_string()));
.or(url.host_str().map(|h| h.to_string()))
.ok_or(ExecError::MissingBucketName)?;

EngineStorageConfig {
location: url.clone(),
Expand Down Expand Up @@ -156,6 +157,8 @@ impl EngineStorageConfig {
}
}

let bucket = bucket.ok_or(ExecError::MissingBucketName)?;

EngineStorageConfig {
location: url.clone(),
conf: StorageConfig::S3 {
Expand Down Expand Up @@ -186,7 +189,8 @@ impl EngineStorageConfig {
let container_name = opts
.get("container_name")
.cloned()
.or(url.host_str().map(|h| h.to_string()));
.or(url.host_str().map(|h| h.to_string()))
.ok_or(ExecError::MissingBucketName)?;

EngineStorageConfig {
location: url.clone(),
Expand Down Expand Up @@ -227,21 +231,15 @@ impl EngineStorageConfig {
.map_err(DatasourceCommonError::from)?,
conf: StorageConfig::Gcs {
service_account_key,
bucket: Some(bucket),
bucket,
},
}
}
// Expected gcs config opts for the session.
(StorageConfig::Gcs { bucket: None, .. }, None) => {
return Err(ExecError::InvalidStorageConfig(
"Missing bucket on session configuration",
))
}
(_, Some(_)) => EngineStorageConfig {
location: Url::parse("memory://").map_err(DatasourceCommonError::from)?,
conf: StorageConfig::Memory,
},
_ => self.clone(),
(_, None) => self.clone(),
})
}

Expand Down Expand Up @@ -567,14 +565,14 @@ mod tests {
secret_access_key,
region: None,
endpoint: None,
bucket: Some("some-bucket".to_string()),
bucket: "some-bucket".to_string(),
}
);

let merged_conf = conf.with_session_config(&SessionStorageConfig {
gcs_bucket: Some("my-other-bucket".to_string()),
})?;
assert_eq!(merged_conf.conf, StorageConfig::Memory,);
assert_eq!(merged_conf.conf, StorageConfig::Memory);
Ok(())
}
}
3 changes: 3 additions & 0 deletions crates/sqlexec/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ pub enum ExecError {
#[error("Invalid URL for remote execution: {0}")]
InvalidRemoteExecUrl(String),

#[error("Missing bucket name: required for storage configuration")]
MissingBucketName,

#[error(transparent)]
DatasourceDebug(#[from] datasources::debug::errors::DebugError),

Expand Down

0 comments on commit 2aa8ad3

Please sign in to comment.