Skip to content

Commit

Permalink
Make deadline part of Capella client
Browse files Browse the repository at this point in the history
Reformat Capella client collection functions

Refactor scope commands for Capella clusters

Refactor buckets commands for Capella clusters
  • Loading branch information
Westwooo committed Sep 19, 2024
1 parent 177c57b commit 8fe1015
Show file tree
Hide file tree
Showing 27 changed files with 381 additions and 840 deletions.
9 changes: 1 addition & 8 deletions src/cli/allow_ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape, Value};
use std::ops::Add;
use std::sync::{Arc, Mutex};
use tokio::time::Instant;

#[derive(Clone)]
pub struct AllowIP {
Expand Down Expand Up @@ -105,17 +103,14 @@ fn allow_ip(
let cluster = get_active_cluster(identifier.clone(), &guard, span)?;

let org = guard.named_or_active_org(cluster.capella_org())?;

let client = org.client();
let deadline = Instant::now().add(org.timeout());

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

let project_id = find_project_id(
ctrl_c.clone(),
guard.named_or_active_project(cluster.project())?,
&client,
deadline,
span,
org_id.clone(),
)?;
Expand All @@ -125,7 +120,6 @@ fn allow_ip(
ctrl_c.clone(),
cluster.hostnames().clone(),
&client,
deadline,
span,
org_id.clone(),
project_id.clone(),
Expand All @@ -137,7 +131,6 @@ fn allow_ip(
project_id,
json_cluster.id(),
ip_address.clone(),
deadline,
ctrl_c.clone(),
)
.map_err(|e| client_error_to_shell_error(e, span))?;
Expand Down
26 changes: 6 additions & 20 deletions src/cli/buckets.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::cli::buckets_builder::{BucketSettings, JSONBucketSettings};
use crate::cli::buckets_get::bucket_to_nu_value;
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;
use crate::state::{RemoteCapellaOrganization, State};
Expand Down Expand Up @@ -170,31 +169,18 @@ pub fn get_capella_buckets(
span: Span,
) -> Result<Vec<BucketSettings>, ShellError> {
let client = org.client();
let deadline = Instant::now().add(org.timeout());

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

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

let buckets = client
.list_buckets(org_id, project_id, json_cluster.id(), deadline, ctrl_c)
.list_buckets(org_id, project_id, cluster_id, ctrl_c)
.map_err(|e| client_error_to_shell_error(e, span))?;
let mut buckets_settings: Vec<BucketSettings> = vec![];
for bucket in buckets.items() {
Expand Down
16 changes: 2 additions & 14 deletions src/cli/buckets_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,25 +224,14 @@ pub fn create_capella_bucket(
span: Span,
) -> Result<(), ShellError> {
let client = org.client();
let deadline = Instant::now().add(org.timeout());

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

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

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,
deadline,
span,
org_id.clone(),
project_id.clone(),
Expand All @@ -262,7 +251,6 @@ pub fn create_capella_bucket(
project_id,
json_cluster.id(),
serde_json::to_string(&json).unwrap(),
deadline,
ctrl_c,
)
.map_err(|e| client_error_to_shell_error(e, span))
Expand Down
77 changes: 18 additions & 59 deletions src/cli/buckets_drop.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
//! The `buckets get` command fetches buckets from the server.
use crate::cli::error::client_error_to_shell_error;
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::{ClientError, ManagementRequest};
use crate::remote_cluster::RemoteCluster;
use crate::remote_cluster::RemoteClusterType::Provisioned;
use crate::state::{RemoteCapellaOrganization, State};
use crate::state::State;
use log::debug;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
Expand Down Expand Up @@ -78,22 +77,27 @@ fn buckets_drop(
debug!("Running buckets drop for bucket {:?}", &name);

for identifier in cluster_identifiers {
let cluster = get_active_cluster(identifier.clone(), &guard, span)?;
let active_cluster = get_active_cluster(identifier.clone(), &guard, span)?;

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

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

client
.delete_bucket(org_id, project_id, cluster_id, name.clone(), ctrl_c.clone())
.map_err(|e| client_error_to_shell_error(e, span))
} else {
drop_server_bucket(cluster, name.clone(), ctrl_c.clone(), span)
drop_server_bucket(active_cluster, name.clone(), ctrl_c.clone(), span)
}?;
}

Expand Down Expand Up @@ -125,48 +129,3 @@ fn drop_server_bucket(
}
Ok(())
}

fn drop_capella_bucket(
org: &RemoteCapellaOrganization,
project: String,
cluster: &RemoteCluster,
bucket: String,
identifier: String,
ctrl_c: Arc<AtomicBool>,
span: Span,
) -> Result<(), ShellError> {
let client = org.client();
let deadline = Instant::now().add(org.timeout());

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

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

client
.delete_bucket(
org_id,
project_id,
json_cluster.id(),
bucket,
deadline,
ctrl_c.clone(),
)
.map_err(|e| client_error_to_shell_error(e, span))
}
49 changes: 14 additions & 35 deletions src/cli/buckets_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use crate::state::{RemoteCapellaOrganization, State};

use crate::cli::buckets_builder::{BucketSettings, JSONBucketSettings};
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::{HttpResponse, ManagementRequest};
use log::debug;
Expand Down Expand Up @@ -88,23 +87,23 @@ fn buckets_get(
let mut results: Vec<Value> = vec![];
for identifier in cluster_identifiers {
let guard = state.lock().unwrap();
let cluster = get_active_cluster(identifier.clone(), &guard, span)?;
let active_cluster = get_active_cluster(identifier.clone(), &guard, span)?;

let content = if cluster.cluster_type() == Provisioned {
let org = guard.named_or_active_org(cluster.capella_org())?;
let content = if active_cluster.cluster_type() == Provisioned {
let org = guard.named_or_active_org(active_cluster.capella_org())?;

get_capella_bucket(
org,
guard.named_or_active_project(cluster.project())?,
cluster,
guard.named_or_active_project(active_cluster.project())?,
active_cluster,
bucket.clone(),
identifier.clone(),
ctrl_c.clone(),
span,
)?
)
} else {
get_server_bucket(cluster, bucket.clone(), ctrl_c.clone(), span)?
};
get_server_bucket(active_cluster, bucket.clone(), ctrl_c.clone(), span)
}?;

results.push(bucket_to_nu_value(content, identifier, false, span));
}
Expand Down Expand Up @@ -158,38 +157,18 @@ pub fn get_capella_bucket(
span: Span,
) -> Result<BucketSettings, ShellError> {
let client = org.client();
let deadline = Instant::now().add(org.timeout());

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

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

let bucket = client
.get_bucket(
org_id,
project_id,
json_cluster.id(),
bucket_name,
deadline,
ctrl_c,
)
.get_bucket(org_id, project_id, cluster_id, bucket_name, ctrl_c)
.map_err(|e| client_error_to_shell_error(e, span))?;

BucketSettings::try_from(&bucket).map_err(|e| {
Expand Down
Loading

0 comments on commit 8fe1015

Please sign in to comment.