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

Commit

Permalink
create preview namespace with preexisting toml
Browse files Browse the repository at this point in the history
  • Loading branch information
EverlastingBugstopper committed Jun 26, 2020
1 parent 5e83bee commit cfa2984
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
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);

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
10 changes: 10 additions & 0 deletions src/settings/toml/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,16 @@ impl Manifest {
}
}

pub fn get_account_id(&self, environment_name: Option<&str>) -> Result<String, failure::Error> {
let environment = self.get_environment(environment_name)?;
if let Some(environment) = environment {
if let Some(account_id) = &environment.account_id {
return Ok(account_id.to_string());
}
}
Ok(self.account_id.to_string())
}

pub fn get_target(
&self,
environment_name: Option<&str>,
Expand Down
46 changes: 46 additions & 0 deletions tests/namespace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use assert_cmd::prelude::*;
use std::env;
use std::process::Command;
use wrangler::fixtures::{EnvConfig, Fixture, KvConfig, WranglerToml, TEST_ENV_NAME};

#[test]
fn can_create_preview_namespace() {
let fixture = Fixture::new();
let mut wrangler_toml = WranglerToml::javascript("test-preview-javascript");
wrangler_toml.kv_namespaces = Some(vec![KvConfig {
binding: Some("BINDING"),
id: Some("1234"),
}]);
fixture.create_wrangler_toml(wrangler_toml);
preview_namespace_creation_succeeds(&fixture, None);
}

#[test]
fn can_create_env_preview_namespace() {
let script_name = "test-env-preview-javascript";
let fixture = Fixture::new();
let mut env_config = EnvConfig::default();
env_config.kv_namespaces = Some(vec![KvConfig {
binding: Some("BINDING"),
id: Some("1234"),
}]);
let wrangler_toml = WranglerToml::with_env(script_name, env_config);
fixture.create_wrangler_toml(wrangler_toml);
preview_namespace_creation_succeeds(&fixture, Some(TEST_ENV_NAME));
}

fn preview_namespace_creation_succeeds(fixture: &Fixture, env: Option<&str>) {
env::remove_var("CF_ACCOUNT_ID");
env::remove_var("CF_ZONE_ID");
let mut create_namespace = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
create_namespace.current_dir(fixture.get_path());
create_namespace
.arg("kv:namespace")
.arg("create")
.arg("BINDING")
.arg("--preview");
if let Some(env) = env {
create_namespace.arg("--env").arg(env);
}
create_namespace.assert().success();
}

0 comments on commit cfa2984

Please sign in to comment.