Skip to content
Open
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
9 changes: 9 additions & 0 deletions crates/clickhouse-cloud-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,15 @@ type. Struct-literal callers need `min_auto_granularity: None`.
`ClickStackCASLPermissionSubject` adds `App`; exhaustive matches without a
wildcard arm need the new variant.

Breaking: `organization_roles_get_list` takes `limit: Option<i64>` and
`cursor: Option<&str>` after the organization ID, because the API now pages
the role list. A page holds at most `limit` roles (the server default and
maximum is 250), ordered by role ID; `ApiResponse::next_cursor` is set while
more pages remain and `ApiResponse::limit` echoes the page size. Migrate
`organization_roles_get_list(org)` to `organization_roles_get_list(org, None, None)`;
the request sent is unchanged, but an organization with more than 250 roles
now needs to follow `next_cursor` to see them all.

## 0.5.1

No changes to this crate; released in lockstep with `clickhousectl` 0.5.1.
Expand Down
1,200 changes: 944 additions & 256 deletions crates/clickhouse-cloud-api/clickhouse_cloud_openapi.json

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions crates/clickhouse-cloud-api/src/client/rbac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@ use crate::meta::operations;
use crate::models::*;

impl Client {
/// List all available roles for an organization
/// Get one page of an organization's roles, system and custom.
///
/// Roles are ordered by role ID. The server defaults `limit` to 250, which is
/// also the maximum. Pass the response's `next_cursor` as `cursor` until it is
/// `None`.
pub async fn organization_roles_get_list(
&self,
organization_id: &str,
limit: Option<i64>,
cursor: Option<&str>,
) -> Result<ApiResponse<Vec<RBACRole>>, Error> {
let req = self.request(&operations::ORGANIZATION_ROLES_GET_LIST, &[organization_id])?;
let mut req = self.request(&operations::ORGANIZATION_ROLES_GET_LIST, &[organization_id])?;
if let Some(limit) = limit {
req = req.query(&[("limit", limit)]);
}
if let Some(cursor) = cursor {
req = req.query(&[("cursor", cursor)]);
}
let resp = req.send().await?;
let status = resp.status();
let body_text = resp.text().await?;
Expand Down
1 change: 1 addition & 0 deletions crates/clickhouse-cloud-api/tests/client/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod members;
mod organizations;
mod postgres;
mod query_api_endpoints;
mod rbac;
mod saved_queries;
mod services;
mod udfs;
Expand Down
76 changes: 76 additions & 0 deletions crates/clickhouse-cloud-api/tests/client/rbac.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use wiremock::matchers::{basic_auth, method, path};
use wiremock::{Mock, ResponseTemplate};

use crate::setup;

// ===========================================================================
// Organization roles
// ===========================================================================

#[tokio::test]
async fn list_roles_pagination_encodes_parameters_and_preserves_envelope() {
let (server, client) = setup().await;
let cursor = "opaque +/=&?雪";
Mock::given(method("GET"))
.and(path("/v1/organizations/org-1/roles"))
.and(basic_auth("key", "secret"))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
"status": 200, "requestId": "req-page",
"result": [{"id": "role-1", "name": "Admin", "type": "system"}],
"limit": 25, "nextCursor": cursor
})))
.expect(4)
.mount(&server)
.await;
for (limit, cursor_arg) in [
(None, None),
(Some(25), None),
(None, Some(cursor)),
(Some(25), Some(cursor)),
] {
let response = client
.organization_roles_get_list("org-1", limit, cursor_arg)
.await
.unwrap();
let roles = response.result.unwrap();
assert_eq!(roles.len(), 1);
assert_eq!(roles[0].id.as_deref(), Some("role-1"));
assert_eq!(response.limit, Some(25));
assert_eq!(response.next_cursor.as_deref(), Some(cursor));
}
let requests = server.received_requests().await.unwrap();
let queries: Vec<Vec<(String, String)>> = requests
.iter()
.map(|request| request.url.query_pairs().into_owned().collect())
.collect();
assert_eq!(
queries,
vec![
vec![],
vec![("limit".into(), "25".into())],
vec![("cursor".into(), cursor.into())],
vec![
("limit".into(), "25".into()),
("cursor".into(), cursor.into())
],
]
);
}

#[tokio::test]
async fn list_roles_last_page_has_no_next_cursor() {
let (server, client) = setup().await;
Mock::given(method("GET"))
.and(path("/v1/organizations/org-1/roles"))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
"result": [], "limit": 250, "nextCursor": null
})))
.mount(&server)
.await;
let response = client
.organization_roles_get_list("org-1", None, None)
.await
.unwrap();
assert_eq!(response.result, Some(vec![]));
assert_eq!(response.next_cursor, None);
}
27 changes: 27 additions & 0 deletions crates/clickhouse-cloud-api/tests/common/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,33 @@ pub async fn list_all_api_keys(client: &Client, org_id: &str) -> TestResult<Vec<
}
}

/// Role assertions must see every role, including roles beyond the first page.
/// A small `limit` exercises the cursor walk against the live API.
pub async fn list_all_org_roles(
client: &Client,
org_id: &str,
limit: Option<i64>,
) -> TestResult<Vec<RBACRole>> {
let mut roles = Vec::new();
let mut cursor = None;
let mut seen = std::collections::HashSet::new();
loop {
let response = client
.organization_roles_get_list(org_id, limit, cursor.as_deref())
.await?;
roles.extend(response.result.ok_or("roles list returned no result")?);
match response.next_cursor {
None => return Ok(roles),
Some(next) => {
if !seen.insert(next.clone()) {
return Err("roles list returned a repeated cursor".into());
}
cursor = Some(next);
}
}
}
}

const DEFAULT_CREATE_TIMEOUT_SECS: u64 = 1_800;
const DEFAULT_DELETE_TIMEOUT_SECS: u64 = 900;
const DEFAULT_STEADY_STATE_TIMEOUT_SECS: u64 = 1_800;
Expand Down
14 changes: 4 additions & 10 deletions crates/clickhouse-cloud-api/tests/integration_org_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,7 @@ async fn cloud_org_lifecycle() -> TestResult<()> {
let client = client.clone();
let org_id = ctx.org_id.clone();
async move {
let resp = client.organization_roles_get_list(&org_id).await?;
resp.result.ok_or_else(|| "org roles list returned no result".into())
list_all_org_roles(&client, &org_id, None).await
}
},
)
Expand Down Expand Up @@ -861,10 +860,8 @@ async fn cloud_org_lifecycle() -> TestResult<()> {
let client = client.clone();
let org_id = ctx.org_id.clone();
async move {
let resp = client.organization_roles_get_list(&org_id).await?;
let roles = resp
.result
.ok_or("roles list returned no result")?;
// A one-role page walks the cursor across every page.
let roles = list_all_org_roles(&client, &org_id, Some(1)).await?;
Ok(roles.into_iter().flat_map(|r| r.id).collect::<Vec<_>>())
}
},
Expand Down Expand Up @@ -933,10 +930,7 @@ async fn cloud_org_lifecycle() -> TestResult<()> {
let role_id = role_id_clone;
let pre_ids = pre_ids_clone;
async move {
let resp = client.organization_roles_get_list(&org_id).await?;
let roles = resp
.result
.ok_or("roles list returned no result")?;
let roles = list_all_org_roles(&client, &org_id, None).await?;
if !roles
.iter()
.any(|r| r.id.as_deref() == Some(role_id.as_str()))
Expand Down
2 changes: 1 addition & 1 deletion crates/clickhousectl/src/cloud/organizations/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ impl CloudClient {
) -> crate::cloud::client::Result<Vec<clickhouse_cloud_api::models::RBACRole>> {
let response = self
.api()
.organization_roles_get_list(org_id)
.organization_roles_get_list(org_id, None, None)
.await
.map_err(|error| {
self.convert_error_for_resource(error, ResourceLookup::organization(org_id))
Expand Down
1 change: 1 addition & 0 deletions scripts/classify-cloud-integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@
"crates/clickhouse-cloud-api/tests/client/organizations.rs": NO_SUITES,
"crates/clickhouse-cloud-api/tests/client/postgres.rs": NO_SUITES,
"crates/clickhouse-cloud-api/tests/client/query_api_endpoints.rs": NO_SUITES,
"crates/clickhouse-cloud-api/tests/client/rbac.rs": NO_SUITES,
"crates/clickhouse-cloud-api/tests/client/saved_queries.rs": NO_SUITES,
"crates/clickhouse-cloud-api/tests/client/services.rs": NO_SUITES,
"crates/clickhouse-cloud-api/tests/client/udfs.rs": NO_SUITES,
Expand Down
1 change: 1 addition & 0 deletions scripts/tests/test_classify_cloud_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ def test_current_test_mapping_values(self):
"crates/clickhouse-cloud-api/tests/client/organizations.rs",
"crates/clickhouse-cloud-api/tests/client/postgres.rs",
"crates/clickhouse-cloud-api/tests/client/query_api_endpoints.rs",
"crates/clickhouse-cloud-api/tests/client/rbac.rs",
"crates/clickhouse-cloud-api/tests/client/saved_queries.rs",
"crates/clickhouse-cloud-api/tests/client/services.rs",
"crates/clickhouse-cloud-api/tests/client/udfs.rs",
Expand Down
Loading