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 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #392 from cloudflare/alewis/feat-create-namespace
feat create namespace
- Loading branch information
Showing
10 changed files
with
380 additions
and
2 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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,27 @@ | ||
# ποΈ `kv` | ||
|
||
## Overview | ||
|
||
The `kv` subcommand allows you to store application data in the Cloudflare network to be accessed from Workers. KV operations are scoped to your account, so in order to use any of these commands, you need to: | ||
|
||
* have a Wrangler project set up with your `account_id` configured in the `wrangler.toml` | ||
* call commands from within a Wrangler project directory. | ||
|
||
<!-- TODO: add gif of `wrangler generate` through `wrangler kv create` --> | ||
|
||
## Commands | ||
|
||
### β¨ `create <namespace-title>` | ||
|
||
Creates a new namespace. | ||
|
||
#### Usage | ||
|
||
``` sh | ||
$ wrangler kv create "new kv namespace" | ||
π Creating namespace with title "new kv namespace" π | ||
β¨ Success: WorkersKVNamespace { | ||
id: "f7b02e7fc70443149ac906dd81ec1791", | ||
title: "new kv namespace", | ||
} | ||
``` |
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,25 @@ | ||
use cloudflare::apiclient::APIClient; | ||
|
||
use cloudflare::workerskv::create_namespace::CreateNamespace; | ||
use cloudflare::workerskv::create_namespace::CreateNamespaceParams; | ||
|
||
use crate::terminal::message; | ||
|
||
pub fn create_namespace(title: &str) -> Result<(), failure::Error> { | ||
let client = super::api_client()?; | ||
let account_id = super::account_id()?; | ||
|
||
let msg = format!("Creating namespace with title \"{}\"", title); | ||
message::working(&msg); | ||
|
||
let response = client.request(&CreateNamespace { | ||
account_identifier: &account_id, | ||
params: CreateNamespaceParams { | ||
title: title.to_string(), | ||
}, | ||
}); | ||
|
||
super::print_response(response); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use cloudflare::auth::Credentials; | ||
use cloudflare::response::APIFailure; | ||
use cloudflare::response::APIResponse; | ||
use cloudflare::response::APIResult; | ||
use cloudflare::HTTPAPIClient; | ||
|
||
use crate::settings; | ||
use crate::terminal::message; | ||
|
||
mod create_namespace; | ||
|
||
pub use create_namespace::create_namespace; | ||
|
||
fn api_client() -> Result<HTTPAPIClient, failure::Error> { | ||
let user = settings::global_user::GlobalUser::new()?; | ||
|
||
Ok(HTTPAPIClient::new(Credentials::from(user))) | ||
} | ||
|
||
fn account_id() -> Result<String, failure::Error> { | ||
let project = settings::project::Project::new()?; | ||
// we need to be certain that account id is present to make kv calls | ||
if project.account_id.is_empty() { | ||
panic!("Your wrangler.toml is missing the account_id field which is required to create KV namespaces!"); | ||
} | ||
Ok(project.account_id) | ||
} | ||
|
||
fn print_response<T: APIResult>(response: APIResponse<T>) { | ||
match response { | ||
Ok(success) => message::success(&format!("Success: {:#?}", success.result)), | ||
Err(e) => match e { | ||
APIFailure::Error(_status, errors) => { | ||
for error in errors { | ||
message::warn(&format!("Error {}: {}", error.code, error.message,)); | ||
|
||
let suggestion = help(error.code); | ||
if !suggestion.is_empty() { | ||
message::help(suggestion); | ||
} | ||
} | ||
} | ||
APIFailure::Invalid(reqwest_err) => message::warn(&format!("Error: {}", reqwest_err)), | ||
}, | ||
} | ||
} | ||
|
||
fn help(error_code: u16) -> &'static str { | ||
// https://api.cloudflare.com/#workers-kv-namespace-errors | ||
match error_code { | ||
// namespace errors | ||
10010 | 10011 | 10012 | 10013 | 10014 | 10018 => { | ||
"Run `wrangler kv list` to see your existing namespaces with IDs" | ||
} | ||
10009 => "Run `wrangler kv list <namespaceID>` to see your existing keys", // key errors | ||
// TODO: link to more info | ||
// limit errors | ||
10022 | 10024 | 10030 => "See documentation", | ||
// TODO: link to tool for this? | ||
// legacy namespace errors | ||
10021 | 10035 | 10038 => "Consider moving this namespace", | ||
// cloudflare account errors | ||
10017 | 10026 => "Check your account settings in the Cloudflare dashboard", | ||
_ => "", | ||
} | ||
} |
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 |
---|---|---|
|
@@ -21,6 +21,7 @@ mod terminal; | |
use crate::settings::project::ProjectType; | ||
use exitfailure::ExitFailure; | ||
use terminal::emoji; | ||
use terminal::message; | ||
|
||
fn main() -> Result<(), ExitFailure> { | ||
env_logger::init(); | ||
|
@@ -45,6 +46,19 @@ fn run() -> Result<(), failure::Error> { | |
.author("ashley g williams <[email protected]>") | ||
.setting(AppSettings::ArgRequiredElseHelp) | ||
.setting(AppSettings::DeriveDisplayOrder) | ||
.subcommand( | ||
SubCommand::with_name("kv") | ||
.about(&*format!( | ||
"{} Interact with your Workers KV Store", | ||
emoji::KV | ||
)) | ||
.subcommand( | ||
SubCommand::with_name("create") | ||
.arg( | ||
Arg::with_name("title") | ||
) | ||
) | ||
) | ||
.subcommand( | ||
SubCommand::with_name("generate") | ||
.about(&*format!( | ||
|
@@ -233,6 +247,15 @@ fn run() -> Result<(), failure::Error> { | |
.expect("The subdomain name you are requesting must be provided."); | ||
|
||
commands::subdomain(name, &user, &project)?; | ||
} else if let Some(kv_matches) = matches.subcommand_matches("kv") { | ||
match kv_matches.subcommand() { | ||
("create", Some(create_matches)) => { | ||
let title = create_matches.value_of("title").unwrap(); | ||
commands::kv::create_namespace(title)?; | ||
} | ||
("", None) => message::warn("kv expects a subcommand"), | ||
_ => unreachable!(), | ||
} | ||
} | ||
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