Skip to content

Commit

Permalink
Add clusters flag to credentials create command
Browse files Browse the repository at this point in the history
  • Loading branch information
Westwooo committed Aug 27, 2024
1 parent 2da8376 commit 3fefe28
Showing 1 changed file with 75 additions and 72 deletions.
147 changes: 75 additions & 72 deletions src/cli/credentials_create.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::cli::util::{
cluster_from_conn_str, find_org_id, find_project_id, get_username_and_password,
cluster_from_conn_str, cluster_identifiers_from, find_org_id, find_project_id,
get_active_cluster, get_username_and_password,
};
use crate::cli::{client_error_to_shell_error, generic_error, no_active_cluster_error};
use crate::cli::{client_error_to_shell_error, generic_error};
use crate::client::cloud_json::CredentialsCreateRequest;
use crate::state::State;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::SyntaxShape;
use nu_protocol::{Category, PipelineData, ShellError, Signature};
use nu_protocol::{Category, PipelineData, ShellError, Signature, SyntaxShape};
use std::ops::Add;
use std::sync::{Arc, Mutex};
use tokio::time::Instant;
Expand Down Expand Up @@ -50,7 +50,12 @@ impl Command for crate::cli::CredentialsCreate {
"registered",
"create credentials with the username/password the active cluster was registered with",
None,
)
) .named(
"clusters",
SyntaxShape::String,
"the clusters which should be contacted",
None,
)
}

fn usage(&self) -> &str {
Expand Down Expand Up @@ -93,75 +98,73 @@ fn credentials_create(
}

let guard = state.lock().unwrap();
let active_cluster = match guard.active_cluster() {
Some(c) => c,
None => {
return Err(no_active_cluster_error(span));
}
};

let org = if let Some(cluster_org) = active_cluster.capella_org() {
guard.get_capella_org(cluster_org)
} else {
guard.active_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 project_id = find_project_id(
ctrl_c.clone(),
guard.active_project().unwrap(),
&client,
deadline,
span,
org_id.clone(),
)?;

let json_cluster = cluster_from_conn_str(
active_cluster.display_name().unwrap_or("".to_string()),
ctrl_c.clone(),
active_cluster.hostnames().clone(),
&client,
deadline,
span,
org_id.clone(),
project_id.clone(),
)?;

if json_cluster.state() != "healthy" {
return Err(generic_error(
"Cluster not healthy",
"Cannot create credentials until cluster is healthy. Check the status of the cluster with 'clusters get'".to_string(),
span
));
}
let cluster_identifiers = cluster_identifiers_from(engine_state, stack, &state, call, true)?;
for identifier in cluster_identifiers {
let cluster = get_active_cluster(identifier.clone(), &guard, span)?;

let (name, password) = if use_registered {
(
active_cluster.username().to_string(),
active_cluster.password().to_string(),
)
} else {
let username_flag = call.get_flag(engine_state, stack, "username")?;
let password_flag = call.get_flag(engine_state, stack, "password")?;
get_username_and_password(username_flag, password_flag)?
};

let payload = CredentialsCreateRequest::new(name.clone(), password.clone(), read, write);

client
.create_credentials(
org_id,
project_id,
json_cluster.id(),
serde_json::to_string(&payload).unwrap(),
let org = if let Some(cluster_org) = cluster.capella_org() {
guard.get_capella_org(cluster_org)
} else {
guard.active_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 project_id = find_project_id(
ctrl_c.clone(),
guard.active_project().unwrap(),
&client,
deadline,
ctrl_c,
)
.map_err(|e| client_error_to_shell_error(e, span))?;
span,
org_id.clone(),
)?;

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

if json_cluster.state() != "healthy" {
return Err(generic_error(
"Cluster not healthy",
"Cannot create credentials until cluster is healthy. Check the status of the cluster with 'clusters get'".to_string(),
span
));
}

let (name, password) = if use_registered {
(
cluster.username().to_string(),
cluster.password().to_string(),
)
} else {
let username_flag = call.get_flag(engine_state, stack, "username")?;
let password_flag = call.get_flag(engine_state, stack, "password")?;
get_username_and_password(username_flag, password_flag)?
};

let payload = CredentialsCreateRequest::new(name.clone(), password.clone(), read, write);

client
.create_credentials(
org_id,
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))?;
}

Ok(PipelineData::empty())
}

0 comments on commit 3fefe28

Please sign in to comment.