From 3e1850e6dbbd80cdc7a6ed10c764bed9f493d429 Mon Sep 17 00:00:00 2001 From: Andrew Sharp Date: Mon, 11 Jan 2021 16:38:31 -0600 Subject: [PATCH 1/4] list more than 100 kv namespaces --- src/commands/kv/namespace/list.rs | 5 ++-- src/kv/namespace/list.rs | 50 +++++++++++++++++++++++-------- src/kv/namespace/upsert.rs | 1 - 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/commands/kv/namespace/list.rs b/src/commands/kv/namespace/list.rs index 3123ba758..e59b97e98 100644 --- a/src/commands/kv/namespace/list.rs +++ b/src/commands/kv/namespace/list.rs @@ -12,11 +12,10 @@ pub fn run(target: &Target, user: &GlobalUser) -> Result<(), failure::Error> { let client = http::cf_v4_client(user)?; let result = list(&client, target); match result { - Ok(success) => { - let namespaces = success.result; + Ok(namespaces) => { println!("{}", serde_json::to_string(&namespaces)?); } - Err(e) => failure::bail!("{}", kv::format_error(e)), + Err(e) => failure::bail!(e), } Ok(()) } diff --git a/src/kv/namespace/list.rs b/src/kv/namespace/list.rs index b82ffc8a4..c96977c20 100644 --- a/src/kv/namespace/list.rs +++ b/src/kv/namespace/list.rs @@ -4,24 +4,50 @@ use cloudflare::endpoints::workerskv::list_namespaces::ListNamespaces; use cloudflare::endpoints::workerskv::list_namespaces::ListNamespacesParams; use cloudflare::endpoints::workerskv::WorkersKvNamespace; use cloudflare::framework::apiclient::ApiClient; -use cloudflare::framework::response::{ApiFailure, ApiSuccess}; +use cloudflare::framework::response::ApiSuccess; +use serde::Deserialize; + +use crate::commands::kv; use crate::settings::toml::Target; const MAX_NAMESPACES_PER_PAGE: u32 = 100; -const PAGE_NUMBER: u32 = 1; pub fn list( client: &impl ApiClient, target: &Target, -) -> Result>, ApiFailure> { - let params = ListNamespacesParams { - page: Some(PAGE_NUMBER), - per_page: Some(MAX_NAMESPACES_PER_PAGE), - }; - - client.request(&ListNamespaces { - account_identifier: &target.account_id, - params, - }) +) -> Result, failure::Error> { + let mut namespaces: Vec = Vec::new(); + let mut all_namespaces_added = false; + let mut page_number = 1; + while !all_namespaces_added { + let params = ListNamespacesParams { + page: Some(page_number), + per_page: Some(MAX_NAMESPACES_PER_PAGE), + }; + + match client.request(&ListNamespaces { + account_identifier: &target.account_id, + params, + }) { + Ok(response) => { + namespaces.append(&mut response.result.clone()); + page_number += 1; + all_namespaces_added = namespaces.len() >= get_total(&response)?; + } + Err(e) => failure::bail!("{}", kv::format_error(e)), + } + } + Ok(namespaces) +} + +fn get_total(list_response: &ApiSuccess>) -> Result { + let result_info: ListResponseResultInfo = + serde_json::from_value(list_response.result_info.clone().unwrap())?; + Ok(result_info.total_count) +} + +#[derive(Deserialize)] +struct ListResponseResultInfo { + total_count: usize, } diff --git a/src/kv/namespace/upsert.rs b/src/kv/namespace/upsert.rs index 35bba3024..c89f16ca6 100644 --- a/src/kv/namespace/upsert.rs +++ b/src/kv/namespace/upsert.rs @@ -31,7 +31,6 @@ pub fn upsert( log::info!("Namespace {} already exists.", title); let namespace = list(&client, target)? - .result .iter() .find(|ns| ns.title == title) .unwrap() From 12a67bf68421902f6552b4414d1cea416c7fc51a Mon Sep 17 00:00:00 2001 From: Andrew Sharp Date: Tue, 12 Jan 2021 16:40:01 -0600 Subject: [PATCH 2/4] check for namespace not found in list --- src/kv/namespace/upsert.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/kv/namespace/upsert.rs b/src/kv/namespace/upsert.rs index c89f16ca6..16aabf956 100644 --- a/src/kv/namespace/upsert.rs +++ b/src/kv/namespace/upsert.rs @@ -30,12 +30,12 @@ pub fn upsert( } else if api_errors.errors.iter().any(|e| e.code == 10014) { log::info!("Namespace {} already exists.", title); - let namespace = list(&client, target)? + match list(&client, target)? .iter() - .find(|ns| ns.title == title) - .unwrap() - .to_owned(); - Ok(UpsertedNamespace::Reused(namespace)) + .find(|ns| ns.title == title) { + Some(namespace) => Ok(UpsertedNamespace::Reused(namespace.to_owned())), + None => failure::bail!("namespace already exists, but could not be found in the API's listed namespaces"), + } } else { failure::bail!("{}", http::format_error(e, Some(&error_suggestions))) } From 606ea2f76511929ddbce3d1551ca30b936e646bd Mon Sep 17 00:00:00 2001 From: Andrew Sharp Date: Tue, 12 Jan 2021 16:48:00 -0600 Subject: [PATCH 3/4] check for no result_info in list response --- src/kv/namespace/list.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/kv/namespace/list.rs b/src/kv/namespace/list.rs index c96977c20..bc52ed4ec 100644 --- a/src/kv/namespace/list.rs +++ b/src/kv/namespace/list.rs @@ -42,9 +42,14 @@ pub fn list( } fn get_total(list_response: &ApiSuccess>) -> Result { - let result_info: ListResponseResultInfo = - serde_json::from_value(list_response.result_info.clone().unwrap())?; - Ok(result_info.total_count) + match list_response.result_info.clone() { + Some(r) => { + let result_info: ListResponseResultInfo = + serde_json::from_value(r)?; + Ok(result_info.total_count) + }, + None => failure::bail!("KV list response lacks result_info field") + } } #[derive(Deserialize)] From a3655e22b157fcff71ac7b2f2e7d0ec63872723e Mon Sep 17 00:00:00 2001 From: Andrew Sharp Date: Tue, 12 Jan 2021 16:56:14 -0600 Subject: [PATCH 4/4] cargo fmt --- src/kv/namespace/list.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/kv/namespace/list.rs b/src/kv/namespace/list.rs index bc52ed4ec..2658609fb 100644 --- a/src/kv/namespace/list.rs +++ b/src/kv/namespace/list.rs @@ -44,11 +44,10 @@ pub fn list( fn get_total(list_response: &ApiSuccess>) -> Result { match list_response.result_info.clone() { Some(r) => { - let result_info: ListResponseResultInfo = - serde_json::from_value(r)?; + let result_info: ListResponseResultInfo = serde_json::from_value(r)?; Ok(result_info.total_count) - }, - None => failure::bail!("KV list response lacks result_info field") + } + None => failure::bail!("KV list response lacks result_info field"), } }