2022-02-02 14:35:52 +00:00
|
|
|
use aws_sdk_s3::{Client, Region};
|
|
|
|
use ext::*;
|
2023-05-19 10:52:14 +00:00
|
|
|
use k2v_client::K2vClient;
|
2022-02-02 14:35:52 +00:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
pub mod macros;
|
|
|
|
|
|
|
|
pub mod client;
|
2022-03-11 16:35:08 +00:00
|
|
|
pub mod custom_requester;
|
2022-02-02 14:35:52 +00:00
|
|
|
pub mod ext;
|
|
|
|
pub mod garage;
|
|
|
|
|
2022-03-11 16:35:08 +00:00
|
|
|
use custom_requester::CustomRequester;
|
|
|
|
|
2022-02-02 14:35:52 +00:00
|
|
|
const REGION: Region = Region::from_static("garage-integ-test");
|
|
|
|
|
2023-03-13 14:03:54 +00:00
|
|
|
#[derive(Clone)]
|
2022-02-02 14:35:52 +00:00
|
|
|
pub struct Context {
|
|
|
|
pub garage: &'static garage::Instance,
|
2023-03-13 14:03:54 +00:00
|
|
|
pub key: garage::Key,
|
2022-02-02 14:35:52 +00:00
|
|
|
pub client: Client,
|
2022-03-11 16:35:08 +00:00
|
|
|
pub custom_request: CustomRequester,
|
2022-05-10 11:16:57 +00:00
|
|
|
pub k2v: K2VContext,
|
|
|
|
}
|
|
|
|
|
2023-03-13 14:03:54 +00:00
|
|
|
#[derive(Clone)]
|
2022-05-10 11:16:57 +00:00
|
|
|
pub struct K2VContext {
|
|
|
|
pub request: CustomRequester,
|
2022-02-02 14:35:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Context {
|
|
|
|
fn new() -> Self {
|
|
|
|
let garage = garage::instance();
|
2023-03-13 14:03:54 +00:00
|
|
|
let key = garage.key(None);
|
|
|
|
let client = client::build_client(garage, &key);
|
|
|
|
let custom_request = CustomRequester::new_s3(garage, &key);
|
|
|
|
let k2v_request = CustomRequester::new_k2v(garage, &key);
|
2022-02-02 14:35:52 +00:00
|
|
|
|
2022-03-11 16:35:08 +00:00
|
|
|
Context {
|
|
|
|
garage,
|
|
|
|
client,
|
2023-03-13 14:03:54 +00:00
|
|
|
key,
|
2022-03-11 16:35:08 +00:00
|
|
|
custom_request,
|
2022-05-10 11:16:57 +00:00
|
|
|
k2v: K2VContext {
|
|
|
|
request: k2v_request,
|
|
|
|
},
|
2022-03-11 16:35:08 +00:00
|
|
|
}
|
2022-02-02 14:35:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-04 16:55:54 +00:00
|
|
|
/// Create an unique bucket with a random suffix.
|
|
|
|
///
|
|
|
|
/// Return the created bucket full name.
|
|
|
|
pub fn create_bucket(&self, name: &str) -> String {
|
2022-02-07 16:00:03 +00:00
|
|
|
let bucket_name = name.to_owned();
|
2022-02-04 16:55:54 +00:00
|
|
|
|
2022-02-02 14:35:52 +00:00
|
|
|
self.garage
|
|
|
|
.command()
|
2022-02-04 16:55:54 +00:00
|
|
|
.args(["bucket", "create", &bucket_name])
|
2022-02-02 14:35:52 +00:00
|
|
|
.quiet()
|
|
|
|
.expect_success_status("Could not create bucket");
|
|
|
|
self.garage
|
|
|
|
.command()
|
|
|
|
.args(["bucket", "allow"])
|
|
|
|
.args(["--owner", "--read", "--write"])
|
2022-02-04 16:55:54 +00:00
|
|
|
.arg(&bucket_name)
|
2023-03-13 14:03:54 +00:00
|
|
|
.args(["--key", &self.key.id])
|
2022-02-02 14:35:52 +00:00
|
|
|
.quiet()
|
|
|
|
.expect_success_status("Could not allow key for bucket");
|
2022-02-04 16:55:54 +00:00
|
|
|
|
|
|
|
bucket_name
|
2022-02-02 14:35:52 +00:00
|
|
|
}
|
2023-05-19 10:52:14 +00:00
|
|
|
|
|
|
|
/// Build a K2vClient for a given bucket
|
|
|
|
pub fn k2v_client(&self, bucket: &str) -> K2vClient {
|
|
|
|
let config = k2v_client::K2vClientConfig {
|
|
|
|
region: REGION.to_string(),
|
|
|
|
endpoint: self.garage.k2v_uri().to_string(),
|
|
|
|
aws_access_key_id: self.key.id.clone(),
|
|
|
|
aws_secret_access_key: self.key.secret.clone(),
|
|
|
|
bucket: bucket.to_string(),
|
|
|
|
user_agent: None,
|
|
|
|
};
|
|
|
|
K2vClient::new(config).expect("Could not create K2V client")
|
|
|
|
}
|
2022-02-02 14:35:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn context() -> Context {
|
|
|
|
Context::new()
|
|
|
|
}
|