Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

allow creation of preview namespace when namespace exists in toml #1414

Merged
merged 2 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/commands/kv/namespace/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ pub fn run(
user: &GlobalUser,
binding: &str,
) -> Result<(), failure::Error> {
let target = manifest.get_target(env, is_preview)?;
kv::validate_target(&target)?;
let account_id = manifest.get_account_id(env)?;
let worker_name = manifest.worker_name(env);
validate_binding(binding)?;

let mut title = format!("{}-{}", target.name, binding);
let mut title = format!("{}-{}", worker_name, binding);
if is_preview {
title.push_str("_preview");
}
let msg = format!("Creating namespace with title \"{}\"", title);
message::working(&msg);

let client = http::cf_v4_client(user)?;
let result = create(&client, &target, &title);
let result = create(&client, &account_id, &title);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should validate the presence of account id in this command; since manifest allows it to be missing or empty, we will get back the cryptic api gateway error.


match result {
Ok(success) => {
Expand Down
6 changes: 2 additions & 4 deletions src/kv/namespace/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ use cloudflare::endpoints::workerskv::WorkersKvNamespace;
use cloudflare::framework::apiclient::ApiClient;
use cloudflare::framework::response::{ApiFailure, ApiSuccess};

use crate::settings::toml::Target;

pub fn create(
client: &impl ApiClient,
target: &Target,
account_id: &str,
title: &str,
) -> Result<ApiSuccess<WorkersKvNamespace>, ApiFailure> {
client.request(&CreateNamespace {
account_identifier: &target.account_id,
account_identifier: account_id,
params: CreateNamespaceParams {
title: title.to_string(),
},
Expand Down
2 changes: 1 addition & 1 deletion src/kv/namespace/upsert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn upsert(
title: String,
) -> Result<UpsertedNamespace, failure::Error> {
let client = http::cf_v4_client(user)?;
let response = create(&client, target, &title);
let response = create(&client, &target.account_id, &title);

match response {
Ok(success) => Ok(UpsertedNamespace::Created(success.result)),
Expand Down
19 changes: 19 additions & 0 deletions src/settings/toml/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,25 @@ impl Manifest {
}
}

pub fn get_account_id(&self, environment_name: Option<&str>) -> Result<String, failure::Error> {
let environment = self.get_environment(environment_name)?;
let mut result = self.account_id.to_string();
if let Some(environment) = environment {
if let Some(account_id) = &environment.account_id {
result = account_id.to_string();
}
}
if result.is_empty() {
let mut msg = "Your wrangler.toml is missing an account_id field".to_string();
if let Some(environment_name) = environment_name {
msg.push_str(&format!(" in [env.{}]", environment_name));
}
failure::bail!("{}", &msg)
} else {
Ok(result)
}
}

pub fn get_target(
&self,
environment_name: Option<&str>,
Expand Down