From 93034cc093b0dfc1af21db1560a6791e095ea4fc Mon Sep 17 00:00:00 2001 From: Jack Westwood Date: Tue, 17 Sep 2024 13:48:56 +0100 Subject: [PATCH] Make deadline part of Capella client --- src/cli/allow_ip.rs | 9 +-- src/cli/buckets.rs | 15 +---- src/cli/buckets_create.rs | 14 +---- src/cli/buckets_drop.rs | 14 +---- src/cli/buckets_get.rs | 22 +------ src/cli/buckets_sample.rs | 14 +---- src/cli/buckets_update.rs | 14 +---- src/cli/clusters.rs | 16 +---- src/cli/clusters_create.rs | 16 +---- src/cli/clusters_drop.rs | 26 ++------ src/cli/clusters_get.rs | 24 ++----- src/cli/collections.rs | 23 +------ src/cli/collections_create.rs | 14 +---- src/cli/collections_drop.rs | 14 +---- src/cli/credentials_create.rs | 8 +-- src/cli/organizations.rs | 5 +- src/cli/projects.rs | 7 +-- src/cli/projects_create.rs | 7 +-- src/cli/projects_drop.rs | 16 +---- src/cli/scopes.rs | 22 +------ src/cli/scopes_create.rs | 23 +------ src/cli/scopes_drop.rs | 14 +---- src/cli/util.rs | 10 +-- src/client/cloud.rs | 114 +++++++++++++--------------------- src/client/cloud_json.rs | 2 +- src/state.rs | 1 + 26 files changed, 101 insertions(+), 363 deletions(-) diff --git a/src/cli/allow_ip.rs b/src/cli/allow_ip.rs index dfe7fda8..d1be94f6 100644 --- a/src/cli/allow_ip.rs +++ b/src/cli/allow_ip.rs @@ -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 { @@ -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(), )?; @@ -125,7 +120,6 @@ fn allow_ip( ctrl_c.clone(), cluster.hostnames().clone(), &client, - deadline, span, org_id.clone(), project_id.clone(), @@ -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))?; diff --git a/src/cli/buckets.rs b/src/cli/buckets.rs index be4890af..d3bbeeaa 100644 --- a/src/cli/buckets.rs +++ b/src/cli/buckets.rs @@ -170,31 +170,22 @@ pub fn get_capella_buckets( 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(), )?; let buckets = client - .list_buckets(org_id, project_id, json_cluster.id(), deadline, ctrl_c) + .list_buckets(org_id, project_id, json_cluster.id(), ctrl_c) .map_err(|e| client_error_to_shell_error(e, span))?; let mut buckets_settings: Vec = vec![]; for bucket in buckets.items() { diff --git a/src/cli/buckets_create.rs b/src/cli/buckets_create.rs index a0f3f495..63ff30e8 100644 --- a/src/cli/buckets_create.rs +++ b/src/cli/buckets_create.rs @@ -224,25 +224,16 @@ 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 org_id = find_org_id(ctrl_c.clone(), &client, span)?; - let project_id = find_project_id( - ctrl_c.clone(), - project, - &client, - deadline, - span, - org_id.clone(), - )?; + 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(), @@ -262,7 +253,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)) diff --git a/src/cli/buckets_drop.rs b/src/cli/buckets_drop.rs index 6340d7de..155c9a8f 100644 --- a/src/cli/buckets_drop.rs +++ b/src/cli/buckets_drop.rs @@ -136,24 +136,15 @@ fn drop_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(), @@ -165,7 +156,6 @@ fn drop_capella_bucket( project_id, json_cluster.id(), bucket, - deadline, ctrl_c.clone(), ) .map_err(|e| client_error_to_shell_error(e, span)) diff --git a/src/cli/buckets_get.rs b/src/cli/buckets_get.rs index 80f63c6c..4b61abb1 100644 --- a/src/cli/buckets_get.rs +++ b/src/cli/buckets_get.rs @@ -158,38 +158,22 @@ pub fn get_capella_bucket( span: Span, ) -> Result { 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(), )?; let bucket = client - .get_bucket( - org_id, - project_id, - json_cluster.id(), - bucket_name, - deadline, - ctrl_c, - ) + .get_bucket(org_id, project_id, json_cluster.id(), bucket_name, ctrl_c) .map_err(|e| client_error_to_shell_error(e, span))?; BucketSettings::try_from(&bucket).map_err(|e| { diff --git a/src/cli/buckets_sample.rs b/src/cli/buckets_sample.rs index ea569233..ba02b40b 100644 --- a/src/cli/buckets_sample.rs +++ b/src/cli/buckets_sample.rs @@ -132,25 +132,16 @@ fn load_capella_sample( 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 org_id = find_org_id(ctrl_c.clone(), &client, span)?; - let project_id = find_project_id( - ctrl_c.clone(), - project, - &client, - deadline, - span, - org_id.clone(), - )?; + 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(), @@ -162,7 +153,6 @@ fn load_capella_sample( project_id, json_cluster.id(), sample, - deadline, ctrl_c.clone(), ) .map_err(|e| client_error_to_shell_error(e, span)) diff --git a/src/cli/buckets_update.rs b/src/cli/buckets_update.rs index 7a9f8746..c247cc36 100644 --- a/src/cli/buckets_update.rs +++ b/src/cli/buckets_update.rs @@ -208,25 +208,16 @@ fn update_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 org_id = find_org_id(ctrl_c.clone(), &client, span)?; - let project_id = find_project_id( - ctrl_c.clone(), - project, - &client, - deadline, - span, - org_id.clone(), - )?; + 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(), @@ -241,7 +232,6 @@ fn update_capella_bucket( json_cluster.id(), settings.name().into(), serde_json::to_string(&json).unwrap(), - deadline, ctrl_c, ) .map_err(|e| client_error_to_shell_error(e, span)) diff --git a/src/cli/clusters.rs b/src/cli/clusters.rs index 3dc4844b..b93f07e6 100644 --- a/src/cli/clusters.rs +++ b/src/cli/clusters.rs @@ -7,9 +7,7 @@ use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::{ Category, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Value, }; -use std::ops::Add; use std::sync::{Arc, Mutex}; -use tokio::time::Instant; #[derive(Clone)] pub struct Clusters { @@ -77,20 +75,12 @@ fn clusters( guard.named_or_active_project(call.get_flag(engine_state, stack, "project")?)?; let client = control.client(); - let deadline = Instant::now().add(control.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 clusters = client - .list_clusters(org_id, project_id, deadline, ctrl_c) + .list_clusters(org_id, project_id, ctrl_c) .map_err(|e| client_error_to_shell_error(e, span))?; let mut results = vec![]; diff --git a/src/cli/clusters_create.rs b/src/cli/clusters_create.rs index 17885dcc..219b51ad 100644 --- a/src/cli/clusters_create.rs +++ b/src/cli/clusters_create.rs @@ -2,9 +2,7 @@ use crate::client::cloud_json::{ClusterCreateRequest, Provider}; use crate::state::State; use log::{debug, info}; use std::convert::TryFrom; -use std::ops::Add; use std::sync::{Arc, Mutex}; -use tokio::time::Instant; use crate::cli::error::{client_error_to_shell_error, serialize_error}; use crate::cli::generic_error; @@ -144,25 +142,17 @@ fn clusters_create( let guard = state.lock().unwrap(); let control = guard.named_or_active_org(capella)?; let client = control.client(); - let deadline = Instant::now().add(control.timeout()); let project = guard.named_or_active_project(call.get_flag(engine_state, stack, "project")?)?; - 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 payload = serde_json::to_string(&definition).map_err(|e| serialize_error(e.to_string(), span))?; client - .create_cluster(org_id, project_id, payload, deadline, ctrl_c) + .create_cluster(org_id, project_id, payload, ctrl_c) .map_err(|e| client_error_to_shell_error(e, span))?; Ok(PipelineData::empty()) diff --git a/src/cli/clusters_drop.rs b/src/cli/clusters_drop.rs index f4425289..156060d1 100644 --- a/src/cli/clusters_drop.rs +++ b/src/cli/clusters_drop.rs @@ -1,8 +1,6 @@ use crate::state::State; use log::debug; -use std::ops::Add; use std::sync::{Arc, Mutex}; -use tokio::time::Instant; use crate::cli::error::client_error_to_shell_error; use crate::cli::util::{find_org_id, find_project_id}; @@ -82,30 +80,16 @@ fn clusters_drop( guard.named_or_active_project(call.get_flag(engine_state, stack, "project")?)?; let client = control.client(); - let deadline = Instant::now().add(control.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 cluster = client - .get_cluster( - name, - org_id.clone(), - project_id.clone(), - deadline, - ctrl_c.clone(), - ) + .get_cluster(name, org_id.clone(), project_id.clone(), ctrl_c.clone()) .map_err(|e| client_error_to_shell_error(e, span))?; client - .delete_cluster(org_id, project_id, cluster.id(), deadline, ctrl_c) + .delete_cluster(org_id, project_id, cluster.id(), ctrl_c) .map_err(|e| client_error_to_shell_error(e, span))?; let identifier = { diff --git a/src/cli/clusters_get.rs b/src/cli/clusters_get.rs index 25f8eeec..5bba9f65 100644 --- a/src/cli/clusters_get.rs +++ b/src/cli/clusters_get.rs @@ -1,8 +1,6 @@ use crate::state::State; use log::debug; -use std::ops::Add; use std::sync::{Arc, Mutex}; -use tokio::time::Instant; use crate::cli::error::client_error_to_shell_error; use crate::cli::util::{convert_json_value_to_nu_value, find_org_id, find_project_id, NuValueMap}; @@ -82,26 +80,12 @@ fn clusters_get( guard.named_or_active_project(call.get_flag(engine_state, stack, "project")?)?; let client = control.client(); - let deadline = Instant::now().add(control.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 cluster = client - .get_cluster( - name, - org_id.clone(), - project_id.clone(), - deadline, - ctrl_c.clone(), - ) + .get_cluster(name, org_id.clone(), project_id.clone(), ctrl_c.clone()) .map_err(|e| client_error_to_shell_error(e, span))?; let mut collected = NuValueMap::default(); diff --git a/src/cli/collections.rs b/src/cli/collections.rs index 959270ad..9ce08c86 100644 --- a/src/cli/collections.rs +++ b/src/cli/collections.rs @@ -216,39 +216,22 @@ fn get_capella_collections( 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(), )?; let collections = client - .list_collections( - org_id, - project_id, - json_cluster.id(), - bucket, - scope, - deadline, - ctrl_c, - ) + .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()) diff --git a/src/cli/collections_create.rs b/src/cli/collections_create.rs index 01ccee72..434a5851 100644 --- a/src/cli/collections_create.rs +++ b/src/cli/collections_create.rs @@ -151,25 +151,16 @@ fn create_capella_collection( 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 org_id = find_org_id(ctrl_c.clone(), &client, span)?; - let project_id = find_project_id( - ctrl_c.clone(), - project, - &client, - deadline, - span, - org_id.clone(), - )?; + 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(), @@ -185,7 +176,6 @@ fn create_capella_collection( bucket, scope, payload, - deadline, ctrl_c, ) .map_err(|e| client_error_to_shell_error(e, span)) diff --git a/src/cli/collections_drop.rs b/src/cli/collections_drop.rs index c57555f5..24da277c 100644 --- a/src/cli/collections_drop.rs +++ b/src/cli/collections_drop.rs @@ -136,25 +136,16 @@ fn drop_capella_collection( 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 org_id = find_org_id(ctrl_c.clone(), &client, span)?; - let project_id = find_project_id( - ctrl_c.clone(), - project, - &client, - deadline, - span, - org_id.clone(), - )?; + 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(), @@ -168,7 +159,6 @@ fn drop_capella_collection( bucket, scope, collection, - deadline, ctrl_c, ) .map_err(|e| client_error_to_shell_error(e, span)) diff --git a/src/cli/credentials_create.rs b/src/cli/credentials_create.rs index c23d2821..1e582264 100644 --- a/src/cli/credentials_create.rs +++ b/src/cli/credentials_create.rs @@ -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}; -use std::ops::Add; use std::sync::{Arc, Mutex}; -use tokio::time::Instant; #[derive(Clone)] pub struct CredentialsCreate { @@ -106,15 +104,13 @@ fn credentials_create( 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.active_project().unwrap(), &client, - deadline, span, org_id.clone(), )?; @@ -124,7 +120,6 @@ fn credentials_create( ctrl_c.clone(), cluster.hostnames().clone(), &client, - deadline, span, org_id.clone(), project_id.clone(), @@ -157,7 +152,6 @@ fn credentials_create( project_id, json_cluster.id(), serde_json::to_string(&payload).unwrap(), - deadline, ctrl_c.clone(), ) .map_err(|e| client_error_to_shell_error(e, span))?; diff --git a/src/cli/organizations.rs b/src/cli/organizations.rs index 19c4106d..77745ef4 100644 --- a/src/cli/organizations.rs +++ b/src/cli/organizations.rs @@ -3,8 +3,6 @@ use crate::state::State; use std::sync::{Arc, Mutex}; use log::debug; -use std::ops::Add; -use tokio::time::Instant; use crate::cli::error::client_error_to_shell_error; use nu_protocol::ast::Call; @@ -66,9 +64,8 @@ fn organizations( let mut collected = NuValueMap::default(); collected.add_string("identifier", identifier, span); - let deadline = Instant::now().add(org.timeout()); let orgs = client - .list_organizations(deadline, ctrl_c.clone()) + .list_organizations(ctrl_c.clone()) .map_err(|e| client_error_to_shell_error(e, span))?; for org in orgs.items() { diff --git a/src/cli/projects.rs b/src/cli/projects.rs index 95bbc385..517f1079 100644 --- a/src/cli/projects.rs +++ b/src/cli/projects.rs @@ -3,9 +3,7 @@ use crate::cli::util::NuValueMap; use crate::state::State; use log::debug; use nu_engine::CallExt; -use std::ops::Add; use std::sync::{Arc, Mutex}; -use tokio::time::Instant; use crate::cli::error::client_error_to_shell_error; use nu_protocol::ast::Call; @@ -65,12 +63,11 @@ fn projects( let guard = &mut state.lock().unwrap(); let control = guard.active_capella_org()?; let client = control.client(); - let deadline = Instant::now().add(control.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 projects = client - .list_projects(org_id, deadline, ctrl_c) + .list_projects(org_id, ctrl_c) .map_err(|e| client_error_to_shell_error(e, span))?; let mut results = vec![]; diff --git a/src/cli/projects_create.rs b/src/cli/projects_create.rs index 44162f49..03b87c67 100644 --- a/src/cli/projects_create.rs +++ b/src/cli/projects_create.rs @@ -1,9 +1,7 @@ use crate::cli::util::find_org_id; use crate::state::State; use log::debug; -use std::ops::Add; use std::sync::{Arc, Mutex}; -use tokio::time::Instant; use crate::cli::error::client_error_to_shell_error; use nu_engine::CallExt; @@ -64,11 +62,10 @@ fn projects_create( let guard = &mut state.lock().unwrap(); let control = guard.active_capella_org()?; let client = control.client(); - let deadline = Instant::now().add(control.timeout()); - let org_id = find_org_id(ctrl_c.clone(), &client, deadline, span)?; + let org_id = find_org_id(ctrl_c.clone(), &client, span)?; client - .create_project(org_id, name, deadline, ctrl_c) + .create_project(org_id, name, ctrl_c) .map_err(|e| client_error_to_shell_error(e, span))?; Ok(PipelineData::empty()) diff --git a/src/cli/projects_drop.rs b/src/cli/projects_drop.rs index e07a573b..4d88167d 100644 --- a/src/cli/projects_drop.rs +++ b/src/cli/projects_drop.rs @@ -1,9 +1,7 @@ use crate::cli::util::{find_org_id, find_project_id}; use crate::state::State; use log::debug; -use std::ops::Add; use std::sync::{Arc, Mutex}; -use tokio::time::Instant; use crate::cli::error::client_error_to_shell_error; use nu_engine::CallExt; @@ -65,20 +63,12 @@ fn projects_drop( let guard = &mut state.lock().unwrap(); let control = guard.active_capella_org()?; let client = control.client(); - let deadline = Instant::now().add(control.timeout()); - let org_id = find_org_id(ctrl_c.clone(), &client, deadline, span)?; - let project_id = find_project_id( - ctrl_c.clone(), - name, - &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(), name, &client, span, org_id.clone())?; client - .delete_project(org_id, project_id, deadline, ctrl_c) + .delete_project(org_id, project_id, ctrl_c) .map_err(|e| client_error_to_shell_error(e, span))?; Ok(PipelineData::empty()) diff --git a/src/cli/scopes.rs b/src/cli/scopes.rs index ffc06b5c..87b06888 100644 --- a/src/cli/scopes.rs +++ b/src/cli/scopes.rs @@ -130,38 +130,22 @@ fn get_capella_scopes( 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(), )?; let scopes = client - .list_scopes( - org_id, - project_id, - json_cluster.id(), - bucket, - deadline, - ctrl_c, - ) + .list_scopes(org_id, project_id, json_cluster.id(), bucket, ctrl_c) .map_err(|e| client_error_to_shell_error(e, span))?; Ok(scopes.scopes().iter().map(|s| s.name().clone()).collect()) diff --git a/src/cli/scopes_create.rs b/src/cli/scopes_create.rs index a153452b..a3ae957f 100644 --- a/src/cli/scopes_create.rs +++ b/src/cli/scopes_create.rs @@ -131,40 +131,23 @@ fn create_capella_scope( 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 org_id = find_org_id(ctrl_c.clone(), &client, span)?; - let project_id = find_project_id( - ctrl_c.clone(), - project, - &client, - deadline, - span, - org_id.clone(), - )?; + 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(), )?; client - .create_scope( - org_id, - project_id, - json_cluster.id(), - bucket, - name, - deadline, - ctrl_c, - ) + .create_scope(org_id, project_id, json_cluster.id(), bucket, name, ctrl_c) .map_err(|e| client_error_to_shell_error(e, span)) } diff --git a/src/cli/scopes_drop.rs b/src/cli/scopes_drop.rs index e03328e9..c8f36ae4 100644 --- a/src/cli/scopes_drop.rs +++ b/src/cli/scopes_drop.rs @@ -161,24 +161,15 @@ fn drop_capella_scope( 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(), @@ -191,7 +182,6 @@ fn drop_capella_scope( json_cluster.id(), bucket, scope_name, - deadline, ctrl_c.clone(), ) .map_err(|e| client_error_to_shell_error(e, span)) diff --git a/src/cli/util.rs b/src/cli/util.rs index f41dc810..1e7d6ce3 100644 --- a/src/cli/util.rs +++ b/src/cli/util.rs @@ -20,7 +20,6 @@ use regex::Regex; use std::sync::atomic::AtomicBool; use std::sync::{Arc, Mutex, MutexGuard}; use std::time::Duration; -use tokio::time::Instant; pub fn is_http_status(response: &HttpResponse, status: u16, span: Span) -> Result<(), ShellError> { if response.status() != status { @@ -312,13 +311,12 @@ pub(crate) fn cluster_from_conn_str( ctrl_c: Arc, hostnames: Vec, client: &Arc, - deadline: Instant, span: Span, org_id: String, project_id: String, ) -> Result { let response = client - .list_clusters(org_id, project_id, deadline, ctrl_c) + .list_clusters(org_id, project_id, ctrl_c) .map_err(|e| client_error_to_shell_error(e, span))?; for c in response.items() { @@ -336,12 +334,11 @@ pub(crate) fn find_project_id( ctrl_c: Arc, name: String, client: &Arc, - deadline: Instant, span: Span, org_id: String, ) -> Result { let projects = client - .list_projects(org_id, deadline, ctrl_c) + .list_projects(org_id, ctrl_c) .map_err(|e| client_error_to_shell_error(e, span))?; for p in projects.items() { @@ -356,11 +353,10 @@ pub(crate) fn find_project_id( pub(crate) fn find_org_id( ctrl_c: Arc, client: &Arc, - deadline: Instant, span: Span, ) -> Result { let orgs = client - .list_organizations(deadline, ctrl_c.clone()) + .list_organizations(ctrl_c.clone()) .map_err(|e| client_error_to_shell_error(e, span))?; let org_id = match orgs.items().first() { diff --git a/src/client/cloud.rs b/src/client/cloud.rs index 06e2280a..2ff9f735 100644 --- a/src/client/cloud.rs +++ b/src/client/cloud.rs @@ -12,10 +12,10 @@ use hmac::{Hmac, Mac}; use log::debug; use reqwest::Client; use sha2::Sha256; -use std::ops::Sub; +use std::ops::{Add, Sub}; use std::sync::atomic::AtomicBool; use std::sync::Arc; -use std::time::{SystemTime, UNIX_EPOCH}; +use std::time::{Duration, SystemTime, UNIX_EPOCH}; use tokio::runtime::Runtime; use tokio::{select, time::Instant}; @@ -25,29 +25,34 @@ pub const CAPELLA_SRV_SUFFIX: &str = "cloud.couchbase.com"; pub struct CapellaClient { secret_key: String, access_key: String, + timeout: Duration, } impl CapellaClient { - pub fn new(secret_key: String, access_key: String) -> Self { + pub fn new(secret_key: String, access_key: String, timeout: Duration) -> Self { Self { secret_key, access_key, + timeout, } } + fn deadline(&self) -> Instant { + Instant::now().add(self.timeout) + } + fn http_do( &self, verb: HttpVerb, path: &str, payload: Option>, - deadline: Instant, ctrl_c: Arc, ) -> Result<(String, u16), ClientError> { let now = Instant::now(); - if now >= deadline { + if now >= self.deadline() { return Err(ClientError::Timeout { key: None }); } - let timeout = deadline.sub(now); + let timeout = self.deadline().sub(now); let ctrl_c_fut = CtrlcFuture::new(ctrl_c); let uri = format!("{}{}", CLOUD_URL, path); @@ -109,62 +114,49 @@ impl CapellaClient { }) } - fn http_get( - &self, - path: &str, - deadline: Instant, - ctrl_c: Arc, - ) -> Result<(String, u16), ClientError> { - self.http_do(HttpVerb::Get, path, None, deadline, ctrl_c) + fn http_get(&self, path: &str, ctrl_c: Arc) -> Result<(String, u16), ClientError> { + self.http_do(HttpVerb::Get, path, None, ctrl_c) } fn http_delete( &self, path: &str, payload: Option>, - deadline: Instant, ctrl_c: Arc, ) -> Result<(String, u16), ClientError> { - self.http_do(HttpVerb::Delete, path, payload, deadline, ctrl_c) + self.http_do(HttpVerb::Delete, path, payload, ctrl_c) } fn http_post( &self, path: &str, payload: Option>, - deadline: Instant, ctrl_c: Arc, ) -> Result<(String, u16), ClientError> { - self.http_do(HttpVerb::Post, path, payload, deadline, ctrl_c) + self.http_do(HttpVerb::Post, path, payload, ctrl_c) } fn http_put( &self, path: &str, payload: Option>, - deadline: Instant, ctrl_c: Arc, ) -> Result<(String, u16), ClientError> { - self.http_do(HttpVerb::Put, path, payload, deadline, ctrl_c) + self.http_do(HttpVerb::Put, path, payload, ctrl_c) } pub fn capella_request( &self, request: CapellaRequest, - deadline: Instant, ctrl_c: Arc, ) -> Result { let (content, status) = match request.verb() { - HttpVerb::Get => self.http_get(request.path().as_str(), deadline, ctrl_c)?, - HttpVerb::Post => { - self.http_post(request.path().as_str(), request.payload(), deadline, ctrl_c)? - } + HttpVerb::Get => self.http_get(request.path().as_str(), ctrl_c)?, + HttpVerb::Post => self.http_post(request.path().as_str(), request.payload(), ctrl_c)?, HttpVerb::Delete => { - self.http_delete(request.path().as_str(), request.payload(), deadline, ctrl_c)? - } - HttpVerb::Put => { - self.http_put(request.path().as_str(), request.payload(), deadline, ctrl_c)? + self.http_delete(request.path().as_str(), request.payload(), ctrl_c)? } + HttpVerb::Put => self.http_put(request.path().as_str(), request.payload(), ctrl_c)?, }; // This endpoint is pretty undenyably a hack, but doesn't really matter for now. Ok(HttpResponse::new( @@ -176,11 +168,10 @@ impl CapellaClient { pub fn list_organizations( &self, - deadline: Instant, ctrl_c: Arc, ) -> Result { let request = CapellaRequest::OrganizationList {}; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 200 { return Err(ClientError::RequestFailed { @@ -196,11 +187,10 @@ impl CapellaClient { pub fn list_projects( &self, org_id: String, - deadline: Instant, ctrl_c: Arc, ) -> Result { let request = CapellaRequest::ProjectList { org_id }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 200 { return Err(ClientError::RequestFailed { @@ -217,14 +207,13 @@ impl CapellaClient { &self, org_id: String, name: String, - deadline: Instant, ctrl_c: Arc, ) -> Result<(), ClientError> { let request = CapellaRequest::ProjectCreate { org_id, payload: format!("{{\"name\": \"{}\"}}", name), }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 201 { return Err(ClientError::RequestFailed { @@ -240,11 +229,10 @@ impl CapellaClient { &self, org_id: String, project_id: String, - deadline: Instant, ctrl_c: Arc, ) -> Result<(), ClientError> { let request = CapellaRequest::ProjectDelete { org_id, project_id }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 204 { return Err(ClientError::RequestFailed { @@ -261,11 +249,10 @@ impl CapellaClient { cluster_name: String, org_id: String, project_id: String, - deadline: Instant, ctrl_c: Arc, ) -> Result { let request = CapellaRequest::ClusterList { org_id, project_id }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 200 { return Err(ClientError::RequestFailed { @@ -289,11 +276,10 @@ impl CapellaClient { &self, org_id: String, project_id: String, - deadline: Instant, ctrl_c: Arc, ) -> Result { let request = CapellaRequest::ClusterList { org_id, project_id }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 200 { return Err(ClientError::RequestFailed { @@ -311,7 +297,6 @@ impl CapellaClient { org_id: String, project_id: String, payload: String, - deadline: Instant, ctrl_c: Arc, ) -> Result<(), ClientError> { let request = CapellaRequest::ClusterCreate { @@ -319,7 +304,7 @@ impl CapellaClient { project_id, payload, }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 202 { return Err(ClientError::RequestFailed { @@ -335,7 +320,6 @@ impl CapellaClient { org_id: String, project_id: String, cluster_id: String, - deadline: Instant, ctrl_c: Arc, ) -> Result<(), ClientError> { let request = CapellaRequest::ClusterDelete { @@ -343,7 +327,7 @@ impl CapellaClient { project_id, cluster_id, }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 202 { return Err(ClientError::RequestFailed { @@ -360,7 +344,6 @@ impl CapellaClient { project_id: String, cluster_id: String, payload: String, - deadline: Instant, ctrl_c: Arc, ) -> Result<(), ClientError> { let request = CapellaRequest::CredentialsCreate { @@ -369,7 +352,7 @@ impl CapellaClient { cluster_id, payload, }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 201 { return Err(ClientError::RequestFailed { @@ -386,7 +369,6 @@ impl CapellaClient { project_id: String, cluster_id: String, bucket: String, - deadline: Instant, ctrl_c: Arc, ) -> Result { let request = CapellaRequest::BucketGet { @@ -395,7 +377,7 @@ impl CapellaClient { cluster_id, bucket_id: BASE64_STANDARD.encode(bucket), }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 200 { return Err(ClientError::RequestFailed { @@ -413,7 +395,6 @@ impl CapellaClient { org_id: String, project_id: String, cluster_id: String, - deadline: Instant, ctrl_c: Arc, ) -> Result { let request = CapellaRequest::BucketList { @@ -421,7 +402,7 @@ impl CapellaClient { project_id, cluster_id, }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 200 { return Err(ClientError::RequestFailed { @@ -440,7 +421,6 @@ impl CapellaClient { project_id: String, cluster_id: String, payload: String, - deadline: Instant, ctrl_c: Arc, ) -> Result<(), ClientError> { let request = CapellaRequest::BucketCreate { @@ -449,7 +429,7 @@ impl CapellaClient { cluster_id, payload, }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 201 { return Err(ClientError::RequestFailed { @@ -466,7 +446,6 @@ impl CapellaClient { project_id: String, cluster_id: String, bucket: String, - deadline: Instant, ctrl_c: Arc, ) -> Result<(), ClientError> { let request = CapellaRequest::BucketDelete { @@ -475,7 +454,7 @@ impl CapellaClient { cluster_id, bucket_id: BASE64_STANDARD.encode(bucket), }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 204 { return Err(ClientError::RequestFailed { @@ -493,7 +472,6 @@ impl CapellaClient { cluster_id: String, bucket: String, payload: String, - deadline: Instant, ctrl_c: Arc, ) -> Result<(), ClientError> { let request = CapellaRequest::BucketUpdate { @@ -503,7 +481,7 @@ impl CapellaClient { bucket_id: BASE64_STANDARD.encode(bucket), payload, }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 204 { return Err(ClientError::RequestFailed { @@ -520,7 +498,6 @@ impl CapellaClient { project_id: String, cluster_id: String, sample: String, - deadline: Instant, ctrl_c: Arc, ) -> Result<(), ClientError> { let request = CapellaRequest::BucketLoadSample { @@ -529,7 +506,7 @@ impl CapellaClient { cluster_id, payload: format!("{{\"name\": \"{}\"}}", sample.clone()), }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; // TODO - need to add handling for sample already loaded once AV-82577 is complete match response.status() { @@ -548,7 +525,6 @@ impl CapellaClient { project_id: String, cluster_id: String, address: String, - deadline: Instant, ctrl_c: Arc, ) -> Result<(), ClientError> { let request = CapellaRequest::AllowIPAddress { @@ -557,7 +533,7 @@ impl CapellaClient { cluster_id, payload: format!("{{\"cidr\": \"{}\"}}", address.clone()), }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; match response.status() { 201 => Ok(()), @@ -574,7 +550,6 @@ impl CapellaClient { project_id: String, cluster_id: String, bucket: String, - deadline: Instant, ctrl_c: Arc, ) -> Result { let request = CapellaRequest::ScopeList { @@ -583,7 +558,7 @@ impl CapellaClient { cluster_id, bucket_id: BASE64_STANDARD.encode(bucket), }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 200 { return Err(ClientError::RequestFailed { @@ -605,7 +580,6 @@ impl CapellaClient { bucket: String, scope: String, payload: String, - deadline: Instant, ctrl_c: Arc, ) -> Result<(), ClientError> { let request = CapellaRequest::CollectionCreate { @@ -616,7 +590,7 @@ impl CapellaClient { scope, payload, }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 201 { return Err(ClientError::RequestFailed { @@ -637,7 +611,6 @@ impl CapellaClient { bucket: String, scope: String, collection: String, - deadline: Instant, ctrl_c: Arc, ) -> Result<(), ClientError> { let request = CapellaRequest::CollectionDelete { @@ -648,7 +621,7 @@ impl CapellaClient { scope, collection, }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 204 { return Err(ClientError::RequestFailed { @@ -667,7 +640,6 @@ impl CapellaClient { cluster_id: String, bucket: String, scope: String, - deadline: Instant, ctrl_c: Arc, ) -> Result { let request = CapellaRequest::CollectionList { @@ -677,7 +649,7 @@ impl CapellaClient { bucket_id: BASE64_STANDARD.encode(bucket), scope, }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 200 { return Err(ClientError::RequestFailed { @@ -697,7 +669,6 @@ impl CapellaClient { cluster_id: String, bucket: String, scope: String, - deadline: Instant, ctrl_c: Arc, ) -> Result<(), ClientError> { let request = CapellaRequest::ScopeCreate { @@ -707,7 +678,7 @@ impl CapellaClient { bucket_id: BASE64_STANDARD.encode(bucket), payload: format!("{{\"name\": \"{}\"}}", scope), }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 201 { return Err(ClientError::RequestFailed { @@ -726,7 +697,6 @@ impl CapellaClient { cluster_id: String, bucket: String, scope: String, - deadline: Instant, ctrl_c: Arc, ) -> Result<(), ClientError> { let request = CapellaRequest::ScopeDelete { @@ -736,7 +706,7 @@ impl CapellaClient { bucket_id: BASE64_STANDARD.encode(bucket), scope, }; - let response = self.capella_request(request, deadline, ctrl_c)?; + let response = self.capella_request(request, ctrl_c)?; if response.status() != 204 { return Err(ClientError::RequestFailed { diff --git a/src/client/cloud_json.rs b/src/client/cloud_json.rs index f78ec407..a373e1e1 100644 --- a/src/client/cloud_json.rs +++ b/src/client/cloud_json.rs @@ -103,7 +103,7 @@ impl Cluster { let mut total = 0; for sg in &self.service_groups { - total = sg.num_of_nodes.unwrap() + total; + total += sg.num_of_nodes.unwrap(); } total } diff --git a/src/state.rs b/src/state.rs index 8947a6a4..54013dc7 100644 --- a/src/state.rs +++ b/src/state.rs @@ -367,6 +367,7 @@ impl RemoteCapellaOrganization { *c = Some(Arc::new(CapellaClient::new( self.secret_key.clone(), self.access_key.clone(), + self.timeout, ))); } c.as_ref().unwrap().clone()