Skip to content

Commit

Permalink
Reformat Capella client collection functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Westwooo committed Sep 19, 2024
1 parent 93034cc commit 3d8ecb9
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 190 deletions.
66 changes: 22 additions & 44 deletions src/cli/collections.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::cli::util::{
cluster_from_conn_str, cluster_identifiers_from, find_org_id, find_project_id,
get_active_cluster, NuValueMap,
cluster_identifiers_from, find_org_project_cluster_ids, get_active_cluster, NuValueMap,
};
use crate::client::ManagementRequest;
use crate::state::{RemoteCapellaOrganization, State};
use crate::state::State;
use log::debug;
use serde_derive::Deserialize;
use std::ops::Add;
Expand All @@ -17,6 +16,7 @@ use crate::cli::error::{
unexpected_status_code_error,
};
use crate::cli::no_active_scope_error;
use crate::client::cloud::CollectionNamespace;
use crate::client::cloud_json::Collection;
use crate::remote_cluster::RemoteClusterType::Provisioned;
use crate::RemoteCluster;
Expand Down Expand Up @@ -103,25 +103,35 @@ fn collections_get(
);

let collections = if active_cluster.cluster_type() == Provisioned {
get_capella_collections(
let client = guard
.named_or_active_org(active_cluster.capella_org())?
.client();

let (org_id, project_id, cluster_id) = find_org_project_cluster_ids(
&client,
ctrl_c.clone(),
span,
identifier.clone(),
guard.named_or_active_org(active_cluster.capella_org())?,
guard.named_or_active_project(active_cluster.project())?,
active_cluster,
bucket.clone(),
scope.clone(),
ctrl_c.clone(),
span,
)
)?;

let namespace = CollectionNamespace::new(org_id, project_id, cluster_id, bucket, scope);

let collections = client
.list_collections(namespace, ctrl_c.clone())
.map_err(|e| client_error_to_shell_error(e, span))?;

collections.items()
} else {
get_server_collections(
active_cluster,
bucket.clone(),
scope.clone(),
ctrl_c.clone(),
span,
)
}?;
)?
};

for collection in collections {
let mut collected = NuValueMap::default();
Expand Down Expand Up @@ -205,38 +215,6 @@ impl Manifest {
}
}

fn get_capella_collections(
identifier: String,
org: &RemoteCapellaOrganization,
project: String,
cluster: &RemoteCluster,
bucket: String,
scope: String,
ctrl_c: Arc<AtomicBool>,
span: Span,
) -> Result<Vec<Collection>, ShellError> {
let client = org.client();

let org_id = find_org_id(ctrl_c.clone(), &client, span)?;
let project_id = find_project_id(ctrl_c.clone(), project, &client, span, org_id.clone())?;

let json_cluster = cluster_from_conn_str(
identifier.clone(),
ctrl_c.clone(),
cluster.hostnames().clone(),
&client,
span,
org_id.clone(),
project_id.clone(),
)?;

let collections = client
.list_collections(org_id, project_id, json_cluster.id(), bucket, scope, ctrl_c)
.map_err(|e| client_error_to_shell_error(e, span))?;

Ok(collections.items())
}

fn get_server_collections(
cluster: &RemoteCluster,
bucket: String,
Expand Down
77 changes: 19 additions & 58 deletions src/cli/collections_create.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! The `collections get` command fetches all of the collection names from the server.

use crate::cli::util::{
cluster_from_conn_str, cluster_identifiers_from, find_org_id, find_project_id,
get_active_cluster,
cluster_identifiers_from, find_org_project_cluster_ids, get_active_cluster,
};
use crate::client::ManagementRequest::CreateCollection;
use crate::state::{RemoteCapellaOrganization, State};
use crate::state::State;
use log::debug;
use std::ops::Add;
use std::sync::atomic::AtomicBool;
Expand All @@ -16,7 +15,7 @@ use crate::cli::collections::{get_bucket_or_active, get_scope_or_active};
use crate::cli::error::{
client_error_to_shell_error, serialize_error, unexpected_status_code_error,
};
use crate::client::cloud_json::Collection;
use crate::client::cloud::CollectionNamespace;
use crate::remote_cluster::RemoteCluster;
use crate::remote_cluster::RemoteClusterType::Provisioned;
use nu_engine::CallExt;
Expand Down Expand Up @@ -109,18 +108,24 @@ fn collections_create(
);

if active_cluster.cluster_type() == Provisioned {
create_capella_collection(
guard.named_or_active_org(active_cluster.capella_org())?,
guard.named_or_active_project(active_cluster.project())?,
active_cluster,
bucket.clone(),
scope.clone(),
collection.clone(),
expiry,
identifier.clone(),
let client = guard
.named_or_active_org(active_cluster.capella_org())?
.client();

let (org_id, project_id, cluster_id) = find_org_project_cluster_ids(
&client,
ctrl_c.clone(),
span,
)
identifier,
guard.named_or_active_project(active_cluster.project())?,
active_cluster,
)?;

let namespace = CollectionNamespace::new(org_id, project_id, cluster_id, bucket, scope);

client
.create_collection(collection.clone(), expiry, namespace, ctrl_c.clone())
.map_err(|e| client_error_to_shell_error(e, span))
} else {
create_server_collection(
active_cluster,
Expand All @@ -137,50 +142,6 @@ fn collections_create(
Ok(PipelineData::empty())
}

#[allow(clippy::too_many_arguments)]
fn create_capella_collection(
org: &RemoteCapellaOrganization,
project: String,
cluster: &RemoteCluster,
bucket: String,
scope: String,
collection: String,
expiry: i64,
identifier: String,
ctrl_c: Arc<AtomicBool>,
span: Span,
) -> Result<(), ShellError> {
let client = org.client();

let org_id = find_org_id(ctrl_c.clone(), &client, span)?;

let project_id = find_project_id(ctrl_c.clone(), project, &client, span, org_id.clone())?;

let json_cluster = cluster_from_conn_str(
identifier.clone(),
ctrl_c.clone(),
cluster.hostnames().clone(),
&client,
span,
org_id.clone(),
project_id.clone(),
)?;

let payload = serde_json::to_string(&Collection::new(collection, expiry)).unwrap();

client
.create_collection(
org_id,
project_id,
json_cluster.id(),
bucket,
scope,
payload,
ctrl_c,
)
.map_err(|e| client_error_to_shell_error(e, span))
}

fn create_server_collection(
cluster: &RemoteCluster,
scope: String,
Expand Down
72 changes: 19 additions & 53 deletions src/cli/collections_drop.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! The `collections drop` commanddrop a collection from the server.

use crate::cli::util::{
cluster_from_conn_str, cluster_identifiers_from, find_org_id, find_project_id,
get_active_cluster,
cluster_identifiers_from, find_org_project_cluster_ids, get_active_cluster,
};
use crate::client::ManagementRequest::DropCollection;
use crate::state::{RemoteCapellaOrganization, State};
use crate::state::State;
use log::debug;
use std::ops::Add;
use std::sync::atomic::AtomicBool;
Expand All @@ -14,6 +13,7 @@ use tokio::time::Instant;

use crate::cli::collections::{get_bucket_or_active, get_scope_or_active};
use crate::cli::error::{client_error_to_shell_error, unexpected_status_code_error};
use crate::client::cloud::CollectionNamespace;
use crate::remote_cluster::RemoteCluster;
use crate::remote_cluster::RemoteClusterType::Provisioned;
use nu_engine::CallExt;
Expand Down Expand Up @@ -97,17 +97,24 @@ fn collections_drop(
);

if active_cluster.cluster_type() == Provisioned {
drop_capella_collection(
guard.named_or_active_org(active_cluster.capella_org())?,
guard.named_or_active_project(active_cluster.project())?,
active_cluster,
bucket.clone(),
scope.clone(),
collection.clone(),
identifier,
let client = guard
.named_or_active_org(active_cluster.capella_org())?
.client();

let (org_id, project_id, cluster_id) = find_org_project_cluster_ids(
&client,
ctrl_c.clone(),
span,
)
identifier,
guard.named_or_active_project(active_cluster.project())?,
active_cluster,
)?;

let namespace = CollectionNamespace::new(org_id, project_id, cluster_id, bucket, scope);

client
.delete_collection(namespace, collection.clone(), ctrl_c.clone())
.map_err(|e| client_error_to_shell_error(e, span))
} else {
drop_server_collection(
active_cluster,
Expand All @@ -123,47 +130,6 @@ fn collections_drop(
Ok(PipelineData::empty())
}

#[allow(clippy::too_many_arguments)]
fn drop_capella_collection(
org: &RemoteCapellaOrganization,
project: String,
cluster: &RemoteCluster,
bucket: String,
scope: String,
collection: String,
identifier: String,
ctrl_c: Arc<AtomicBool>,
span: Span,
) -> Result<(), ShellError> {
let client = org.client();

let org_id = find_org_id(ctrl_c.clone(), &client, span)?;

let project_id = find_project_id(ctrl_c.clone(), project, &client, span, org_id.clone())?;

let json_cluster = cluster_from_conn_str(
identifier.clone(),
ctrl_c.clone(),
cluster.hostnames().clone(),
&client,
span,
org_id.clone(),
project_id.clone(),
)?;

client
.delete_collection(
org_id,
project_id,
json_cluster.id(),
bucket,
scope,
collection,
ctrl_c,
)
.map_err(|e| client_error_to_shell_error(e, span))
}

fn drop_server_collection(
cluster: &RemoteCluster,
bucket: String,
Expand Down
39 changes: 39 additions & 0 deletions src/cli/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,22 @@ pub(crate) fn cluster_from_conn_str(
Err(ShellError::from(ClusterNotFound { identifier, span }))
}

pub(crate) fn find_cluster_id(
identifier: String,
ctrl_c: Arc<AtomicBool>,
hostnames: Vec<String>,
client: &Arc<CapellaClient>,
span: Span,
org_id: String,
project_id: String,
) -> Result<String, ShellError> {
let cluster = cluster_from_conn_str(
identifier, ctrl_c, hostnames, client, span, org_id, project_id,
)?;

Ok(cluster.id())
}

pub(crate) fn find_project_id(
ctrl_c: Arc<AtomicBool>,
name: String,
Expand Down Expand Up @@ -367,6 +383,29 @@ pub(crate) fn find_org_id(
Ok(org_id.to_string())
}

pub(crate) fn find_org_project_cluster_ids(
client: &Arc<CapellaClient>,
ctrl_c: Arc<AtomicBool>,
span: Span,
identifier: String,
project: String,
cluster: &RemoteCluster,
) -> Result<(String, String, String), ShellError> {
let org_id = find_org_id(ctrl_c.clone(), client, span)?;
let project_id = find_project_id(ctrl_c.clone(), project, client, span, org_id.clone())?;
let cluster_id = find_cluster_id(
identifier.clone(),
ctrl_c.clone(),
cluster.hostnames().clone(),
client,
span,
org_id.clone(),
project_id.clone(),
)?;

Ok((org_id, project_id, cluster_id))
}

// duration_to_golang_string creates a golang formatted string to use with timeouts. Unlike Golang
// strings it does not deal with fractional seconds, we do not need that accuracy.
pub fn duration_to_golang_string(duration: Duration) -> String {
Expand Down
Loading

0 comments on commit 3d8ecb9

Please sign in to comment.