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

list more than 100 kv namespaces #1717

Merged
merged 4 commits into from
Jan 13, 2021
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
5 changes: 2 additions & 3 deletions src/commands/kv/namespace/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
54 changes: 42 additions & 12 deletions src/kv/namespace/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,54 @@ 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<ApiSuccess<Vec<WorkersKvNamespace>>, 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<Vec<WorkersKvNamespace>, failure::Error> {
let mut namespaces: Vec<WorkersKvNamespace> = 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<Vec<WorkersKvNamespace>>) -> Result<usize, failure::Error> {
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)]
struct ListResponseResultInfo {
total_count: usize,
}
11 changes: 5 additions & 6 deletions src/kv/namespace/upsert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +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)?
.result
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())),
Copy link
Contributor

Choose a reason for hiding this comment

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

😄

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)))
}
Expand Down