feat: Implement Google Cloud Storage-related CLI arguments

pull/24376/head
Carol (Nichols || Goulding) 2021-03-03 15:14:33 -05:00
parent ef13c1023e
commit 02d981451d
3 changed files with 78 additions and 20 deletions

View File

@ -239,10 +239,13 @@ impl ObjectStoreApi for GoogleCloudStorage {
impl GoogleCloudStorage {
/// Configure a connection to Google Cloud Storage.
pub fn new(service_account_path: impl AsRef<std::ffi::OsStr>, bucket_name: impl Into<String>) -> Self {
// The cloud storage crate currently only supports authentication via environment
// variables. Set the environment variable explicitly so that we can optionally accept
// command line arguments instead.
pub fn new(
service_account_path: impl AsRef<std::ffi::OsStr>,
bucket_name: impl Into<String>,
) -> Self {
// The cloud storage crate currently only supports authentication via
// environment variables. Set the environment variable explicitly so
// that we can optionally accept command line arguments instead.
env::set_var("SERVICE_ACCOUNT", service_account_path);
Self {
bucket_name: bucket_name.into(),
@ -277,10 +280,7 @@ mod test {
() => {{
dotenv::dotenv().ok();
let required_vars = [
"INFLUXDB_IOX_BUCKET",
"GOOGLE_SERVICE_ACCOUNT",
];
let required_vars = ["INFLUXDB_IOX_BUCKET", "GOOGLE_SERVICE_ACCOUNT"];
let unset_vars: Vec<_> = required_vars
.iter()
.filter_map(|&name| match env::var(name) {
@ -309,7 +309,8 @@ mod test {
GoogleCloudConfig {
bucket: env::var("INFLUXDB_IOX_BUCKET")
.expect("already checked INFLUXDB_IOX_BUCKET"),
service_account: env::var("GOOGLE_SERVICE_ACCOUNT").expect("already checked GOOGLE_SERVICE_ACCOUNT"),
service_account: env::var("GOOGLE_SERVICE_ACCOUNT")
.expect("already checked GOOGLE_SERVICE_ACCOUNT"),
}
}
}};

View File

@ -107,7 +107,7 @@ Possible values (case insensitive):
* file: Stores objects in the local filesystem. Must also set `--data-dir`.
* s3: Amazon S3. Must also set `--bucket`, `--aws-access-key-id`, `--aws-secret-access-key`, and
possibly `--aws-region`.
* google: Google Cloud Storage. Must also set `--bucket` and SERVICE_ACCOUNT.
* google: Google Cloud Storage. Must also set `--bucket` and `--google-service-account`.
* azure: Microsoft Azure blob storage. Must also set `--bucket`, AZURE_STORAGE_ACCOUNT,
and AZURE_STORAGE_MASTER_KEY.
"#,
@ -117,8 +117,8 @@ Possible values (case insensitive):
/// Name of the bucket to use for the object store. Must also set
/// `--object-store` to a cloud object storage to have any effect.
///
/// If using Google Cloud Storage for the object store, this item, as well
/// as SERVICE_ACCOUNT must be set.
/// If using Google Cloud Storage for the object store, this item as well
/// as `--google-service-account` must be set.
///
/// If using S3 for the object store, must set this item as well
/// as `--aws-access-key-id` and `--aws-secret-access-key`. Can also set
@ -161,6 +161,13 @@ Possible values (case insensitive):
)]
pub aws_region: String,
/// When using Google Cloud Storage as the object store, set this to the
/// path to the JSON file that contains the Google credentials.
///
/// Must also set `--object-store=google` and `--bucket`.
#[structopt(long = "--google-service-account", env = "GOOGLE_SERVICE_ACCOUNT")]
pub google_service_account: Option<String>,
/// If set, Jaeger traces are emitted to this host
/// using the OpenTelemetry tracer.
///

View File

@ -178,15 +178,31 @@ impl TryFrom<&Config> for ObjectStore {
Ok(Self::new_in_memory(object_store::memory::InMemory::new()))
}
Some(ObjStoreOpt::Google) => match config.bucket.as_ref() {
Some(bucket) => Ok(Self::new_google_cloud_storage(GoogleCloudStorage::new(
bucket,
))),
None => InvalidCloudObjectStoreConfiguration {
object_store: ObjStoreOpt::Google,
Some(ObjStoreOpt::Google) => {
match (
config.bucket.as_ref(),
config.google_service_account.as_ref(),
) {
(Some(bucket), Some(service_account)) => Ok(Self::new_google_cloud_storage(
GoogleCloudStorage::new(service_account, bucket),
)),
(bucket, service_account) => {
let mut missing_args = vec![];
if bucket.is_none() {
missing_args.push("bucket");
}
if service_account.is_none() {
missing_args.push("google-service-account");
}
MissingObjectStoreConfig {
object_store: ObjStoreOpt::Google,
missing: missing_args.join(", "),
}
.fail()
}
}
.fail(),
},
}
Some(ObjStoreOpt::S3) => {
match (
@ -308,4 +324,38 @@ mod tests {
bucket, aws-access-key-id, aws-secret-access-key"
);
}
#[test]
fn valid_google_config() {
let config = Config::from_iter_safe(&[
"server",
"--object-store",
"google",
"--bucket",
"mybucket",
"--google-service-account",
"~/Not/A/Real/path.json",
])
.unwrap();
let object_store = ObjectStore::try_from(&config).unwrap();
assert!(matches!(
object_store,
ObjectStore(ObjectStoreIntegration::GoogleCloudStorage(_))
));
}
#[test]
fn google_config_missing_params() {
let config = Config::from_iter_safe(&["server", "--object-store", "google"]).unwrap();
let err = ObjectStore::try_from(&config).unwrap_err().to_string();
assert_eq!(
err,
"Specifed Google for the object store, required configuration missing for \
bucket, google-service-account"
);
}
}