This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: r2 bindings and subcommands (#2146)
note: the api-backends for these features are implemented, but generally available in production yet. commands: add r2 subcommands to create/delete/list buckets I opted for sub-commands like wrangler r2 bucket create, rather than how kv does wrangler kv:namespace create. bindings: add support for binding a bucket to a worker this implementation is essentially copy-pasted from the kv binding code, and functions very similary. Co-authored-by: Taylor Lee <tlee@cloudflare.com>
- Loading branch information
Showing
20 changed files
with
324 additions
and
4 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
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use super::Cli; | ||
use crate::commands; | ||
use crate::settings::{global_user::GlobalUser, toml::Manifest}; | ||
|
||
use anyhow::Result; | ||
use structopt::StructOpt; | ||
|
||
#[derive(Debug, Clone, StructOpt)] | ||
#[structopt(rename_all = "lower")] | ||
pub enum R2 { | ||
/// Interact with your Workers R2 Buckets | ||
Bucket(Bucket), | ||
} | ||
|
||
#[derive(Debug, Clone, StructOpt)] | ||
#[structopt(rename_all = "lower")] | ||
pub enum Bucket { | ||
/// List existing buckets | ||
List, | ||
/// Create a new bucket | ||
Create { | ||
/// The name for your new bucket | ||
#[structopt(index = 1)] | ||
name: String, | ||
}, | ||
/// Delete an existing bucket | ||
Delete { | ||
/// The name of the bucket to delete | ||
/// Note: bucket must be empty | ||
#[structopt(index = 1)] | ||
name: String, | ||
}, | ||
} | ||
|
||
pub fn r2_bucket(r2: R2, cli_params: &Cli) -> Result<()> { | ||
let user = GlobalUser::new()?; | ||
let manifest = Manifest::new(&cli_params.config)?; | ||
let env = cli_params.environment.as_deref(); | ||
|
||
match r2 { | ||
R2::Bucket(Bucket::List) => commands::r2::list(&manifest, env, &user), | ||
R2::Bucket(Bucket::Create { name }) => commands::r2::create(&manifest, env, &user, &name), | ||
R2::Bucket(Bucket::Delete { name }) => commands::r2::delete(&manifest, env, &user, &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
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
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use anyhow::Result; | ||
|
||
use crate::http; | ||
use crate::settings::global_user::GlobalUser; | ||
use crate::settings::toml::Manifest; | ||
use crate::terminal::message::{Message, StdOut}; | ||
|
||
use cloudflare::endpoints::r2::{CreateBucket, DeleteBucket, ListBuckets}; | ||
use cloudflare::framework::apiclient::ApiClient; | ||
|
||
pub fn list(manifest: &Manifest, env: Option<&str>, user: &GlobalUser) -> Result<()> { | ||
let account_id = manifest.get_account_id(env)?; | ||
let client = http::cf_v4_client(user)?; | ||
let result = client.request(&ListBuckets { | ||
account_identifier: &account_id, | ||
}); | ||
|
||
match result { | ||
Ok(response) => { | ||
let buckets: Vec<String> = response | ||
.result | ||
.buckets | ||
.into_iter() | ||
.map(|b| b.name) | ||
.collect(); | ||
println!("{:?}", buckets); | ||
} | ||
Err(e) => println!("{}", e), | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn create(manifest: &Manifest, env: Option<&str>, user: &GlobalUser, name: &str) -> Result<()> { | ||
let account_id = manifest.get_account_id(env)?; | ||
let msg = format!("Creating bucket \"{}\"", name); | ||
StdOut::working(&msg); | ||
|
||
let client = http::cf_v4_client(user)?; | ||
let result = client.request(&CreateBucket { | ||
account_identifier: &account_id, | ||
bucket_name: name, | ||
}); | ||
|
||
match result { | ||
Ok(_) => { | ||
StdOut::success("Success!"); | ||
} | ||
Err(e) => print!("{}", e), | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn delete(manifest: &Manifest, env: Option<&str>, user: &GlobalUser, name: &str) -> Result<()> { | ||
let account_id = manifest.get_account_id(env)?; | ||
let msg = format!("Deleting bucket \"{}\"", name); | ||
StdOut::working(&msg); | ||
|
||
let client = http::cf_v4_client(user)?; | ||
let result = client.request(&DeleteBucket { | ||
account_identifier: &account_id, | ||
bucket_name: name, | ||
}); | ||
|
||
match result { | ||
Ok(_) => { | ||
StdOut::success("Success!"); | ||
} | ||
Err(e) => print!("{}", e), | ||
} | ||
|
||
Ok(()) | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use std::fmt; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::settings::binding::Binding; | ||
|
||
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] | ||
pub struct ConfigR2Bucket { | ||
pub binding: String, | ||
pub bucket_name: Option<String>, | ||
pub preview_bucket_name: Option<String>, | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq)] | ||
pub struct R2Bucket { | ||
pub binding: String, | ||
pub bucket_name: String, | ||
} | ||
|
||
impl fmt::Display for R2Bucket { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!( | ||
f, | ||
"binding: {}, bucket_name: {}", | ||
self.binding, self.bucket_name | ||
) | ||
} | ||
} | ||
|
||
impl R2Bucket { | ||
pub fn binding(&self) -> Binding { | ||
Binding::new_r2_bucket(self.binding.clone(), self.bucket_name.clone()) | ||
} | ||
} |
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
Oops, something went wrong.