Skip to content

Commit

Permalink
Support configuration of api endpoint for Capella organization
Browse files Browse the repository at this point in the history
  • Loading branch information
Westwooo committed Sep 24, 2024
1 parent 2a0b7b7 commit 36d0b2f
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/cli/cbenv_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ pub fn update_config_file(guard: &mut MutexGuard<State>, span: Span) -> Result<(
c.access_key(),
Some(c.timeout()),
c.default_project(),
None,
))
}

Expand Down
10 changes: 6 additions & 4 deletions src/client/cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ use std::time::{SystemTime, UNIX_EPOCH};
use tokio::runtime::Runtime;
use tokio::{select, time::Instant};

const CLOUD_URL: &str = "https://cloudapi.cloud.couchbase.com";
pub const CLOUD_URL: &str = "https://cloudapi.cloud.couchbase.com";
pub const CAPELLA_SRV_SUFFIX: &str = "cloud.couchbase.com";

pub struct CapellaClient {
secret_key: String,
access_key: String,
api_endpoint: String,
}

impl CapellaClient {
pub fn new(secret_key: String, access_key: String) -> Self {
pub fn new(secret_key: String, access_key: String, api_endpoint: String) -> Self {
Self {
secret_key,
access_key,
api_endpoint,
}
}

Expand All @@ -50,7 +52,7 @@ impl CapellaClient {
let timeout = deadline.sub(now);
let ctrl_c_fut = CtrlcFuture::new(ctrl_c);

let uri = format!("{}{}", CLOUD_URL, path);
let uri = format!("{}{}", self.api_endpoint, path);

let client = Client::new();
let mut res_builder = match verb {
Expand Down Expand Up @@ -170,7 +172,7 @@ impl CapellaClient {
Ok(HttpResponse::new(
content,
status,
Endpoint::new(CLOUD_URL.to_string(), 443),
Endpoint::new(self.api_endpoint.to_string(), 443),
))
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/cloud_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub use crate::client::cloud::CapellaClient;
pub use crate::client::cloud::CAPELLA_SRV_SUFFIX;
pub use crate::client::cloud::CLOUD_URL;
pub use crate::client::error::ClientError;
pub use crate::client::http_client::{
AnalyticsQueryRequest, Endpoint, HTTPClient, ManagementRequest, QueryRequest,
Expand Down
8 changes: 8 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ pub struct CapellaOrganizationConfig {
management_timeout: Option<Duration>,
#[serde(rename(deserialize = "default-project", serialize = "default-project"))]
default_project: Option<String>,

#[serde(rename(deserialize = "api-endpoint", serialize = "api-endpoint"))]
api_endpoint: Option<String>,
}

impl CapellaOrganizationConfig {
Expand All @@ -253,6 +256,7 @@ impl CapellaOrganizationConfig {
access_key: String,
management_timeout: Option<Duration>,
default_project: Option<String>,
api_endpoint: Option<String>,
) -> Self {
Self {
identifier,
Expand All @@ -262,6 +266,7 @@ impl CapellaOrganizationConfig {
},
management_timeout,
default_project,
api_endpoint,
}
}
pub fn identifier(&self) -> String {
Expand All @@ -279,6 +284,9 @@ impl CapellaOrganizationConfig {
pub fn default_project(&self) -> Option<String> {
self.default_project.as_ref().cloned()
}
pub fn api_endpoint(&self) -> Option<String> {
self.api_endpoint.clone()
}

pub fn credentials_mut(&mut self) -> &mut OrganizationCredentials {
&mut self.credentials
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use nu_protocol::{
Span, Value,
};

use crate::client::RustTlsConfig;
use crate::client::{RustTlsConfig, CLOUD_URL};
use std::collections::HashMap;
use std::convert::TryFrom;
use std::error::Error;
Expand Down Expand Up @@ -658,12 +658,14 @@ fn make_state(
None => DEFAULT_MANAGEMENT_TIMEOUT,
};
let name = c.identifier();
let api_endpoint = c.api_endpoint().unwrap_or(CLOUD_URL.to_string());

let plane = RemoteCapellaOrganization::new(
c.secret_key(),
c.access_key(),
management_timeout,
c.default_project(),
api_endpoint,
);

if active_capella_org.is_none() {
Expand Down
4 changes: 4 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ pub struct RemoteCapellaOrganization {
client: Mutex<Option<Arc<CapellaClient>>>,
timeout: Duration,
default_project: Option<String>,
api_endpoint: String,
}

impl RemoteCapellaOrganization {
Expand All @@ -343,13 +344,15 @@ impl RemoteCapellaOrganization {
access_key: String,
timeout: Duration,
default_project: Option<String>,
api_endpoint: String,
) -> Self {
Self {
secret_key,
access_key,
client: Mutex::new(None),
timeout,
default_project,
api_endpoint,
}
}

Expand All @@ -367,6 +370,7 @@ impl RemoteCapellaOrganization {
*c = Some(Arc::new(CapellaClient::new(
self.secret_key.clone(),
self.access_key.clone(),
self.api_endpoint.clone(),
)));
}
c.as_ref().unwrap().clone()
Expand Down

0 comments on commit 36d0b2f

Please sign in to comment.