-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add command to list credentials created on a Capella cluster #575
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
use crate::cli::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, NuValueMap, | ||
}; | ||
use crate::state::State; | ||
use nu_protocol::engine::{Call, Command, EngineState, Stack}; | ||
use nu_protocol::{ | ||
Category, IntoInterruptiblePipelineData, PipelineData, Record, ShellError, Signature, | ||
SyntaxShape, Value, | ||
}; | ||
use nu_utils::SharedCow; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
#[derive(Clone)] | ||
pub struct Credentials { | ||
state: Arc<Mutex<State>>, | ||
} | ||
|
||
impl Credentials { | ||
pub fn new(state: Arc<Mutex<State>>) -> Self { | ||
Self { state } | ||
} | ||
} | ||
|
||
impl Command for Credentials { | ||
fn name(&self) -> &str { | ||
"credentials" | ||
} | ||
|
||
fn signature(&self) -> Signature { | ||
Signature::build("credentials") | ||
.named( | ||
"clusters", | ||
SyntaxShape::String, | ||
"the clusters which should be contacted", | ||
None, | ||
) | ||
.category(Category::Custom("couchbase".to_string())) | ||
} | ||
|
||
fn usage(&self) -> &str { | ||
"Lists existing credentials on a Capella cluster" | ||
} | ||
|
||
fn run( | ||
&self, | ||
engine_state: &EngineState, | ||
stack: &mut Stack, | ||
call: &Call, | ||
input: PipelineData, | ||
) -> Result<PipelineData, ShellError> { | ||
credentials(self.state.clone(), engine_state, stack, call, input) | ||
} | ||
} | ||
|
||
fn credentials( | ||
state: Arc<Mutex<State>>, | ||
engine_state: &EngineState, | ||
stack: &mut Stack, | ||
call: &Call, | ||
_input: PipelineData, | ||
) -> Result<PipelineData, ShellError> { | ||
let span = call.head; | ||
let signals = engine_state.signals().clone(); | ||
|
||
let cluster_identifiers = cluster_identifiers_from(engine_state, stack, &state, call, true)?; | ||
let guard = state.lock().unwrap(); | ||
|
||
let mut results: Vec<Value> = vec![]; | ||
for identifier in cluster_identifiers { | ||
let cluster = get_active_cluster(identifier.clone(), &guard, span)?; | ||
|
||
let org = guard.named_or_active_org(cluster.capella_org())?; | ||
|
||
let client = org.client(); | ||
|
||
let org_id = find_org_id(signals.clone(), &client, span)?; | ||
|
||
let project_id = find_project_id( | ||
signals.clone(), | ||
guard.active_project().unwrap(), | ||
&client, | ||
span, | ||
org_id.clone(), | ||
)?; | ||
|
||
let json_cluster = cluster_from_conn_str( | ||
identifier.clone(), | ||
signals.clone(), | ||
cluster.hostnames().clone(), | ||
&client, | ||
span, | ||
org_id.clone(), | ||
project_id.clone(), | ||
)?; | ||
|
||
let credentials = client | ||
.list_credentials(org_id, project_id, json_cluster.id(), signals.clone()) | ||
.map_err(|e| client_error_to_shell_error(e, span))?; | ||
|
||
for creds in credentials.data() { | ||
let mut collected = NuValueMap::default(); | ||
collected.add_string("id", creds.id(), span); | ||
collected.add_string("name", creds.name(), span); | ||
collected.add_string("cluster", identifier.clone(), span); | ||
|
||
let mut access_records = vec![]; | ||
for acc in creds.access() { | ||
let cols = vec![ | ||
"bucket".to_string(), | ||
"scopes".to_string(), | ||
"privileges".to_string(), | ||
]; | ||
let mut vals = vec![]; | ||
|
||
vals.push(Value::String { | ||
val: acc.bucket(), | ||
internal_span: span, | ||
}); | ||
|
||
let mut scope_values = vec![]; | ||
for scope in acc.scopes() { | ||
scope_values.push(Value::String { | ||
val: scope, | ||
internal_span: span, | ||
}) | ||
} | ||
|
||
vals.push(Value::List { | ||
vals: scope_values, | ||
internal_span: span, | ||
}); | ||
|
||
let mut privilege_values = vec![]; | ||
for privilege in acc.privileges() { | ||
privilege_values.push(Value::String { | ||
val: privilege, | ||
internal_span: span, | ||
}) | ||
} | ||
|
||
vals.push(Value::List { | ||
vals: privilege_values, | ||
internal_span: span, | ||
}); | ||
|
||
let access = Record::from_raw_cols_vals(cols, vals, span, span).unwrap(); | ||
access_records.push(Value::Record { | ||
val: SharedCow::new(access), | ||
internal_span: span, | ||
}); | ||
} | ||
|
||
collected.add_vec("access", access_records, span); | ||
results.push(collected.into_value(span)) | ||
} | ||
} | ||
|
||
Ok(results.into_pipeline_data(span, signals)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was changed since the Access struct was changed to support reading the list credentials response. They have been hardcoded to values that preserve the behaviour from before the change, and this issue will allow users to specify scopes/buckets on credential creation: #576