Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion crates/admin-cli/src/cfg/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use rpc::admin_cli::OutputFormat;

use crate::cfg::cli_options::SortField;
use crate::errors::CarbideCliError;
use crate::rpc::ApiClient;

// RuntimeContext is context passed to all subcommand
Expand All @@ -36,6 +37,15 @@ pub struct RuntimeConfig {
pub format: OutputFormat,
pub page_size: usize,
pub extended: bool,
pub cloud_unsafe_op_enabled: bool,
pub cloud_unsafe_op: Option<String>,
pub sort_by: SortField,
}

impl RuntimeContext {
pub fn assert_cloud_unsafe_op_message(&self) -> Result<&str, CarbideCliError> {
self.config
.cloud_unsafe_op
.as_deref()
.ok_or(CarbideCliError::CloudUnsafeOp)
}
}
3 changes: 3 additions & 0 deletions crates/admin-cli/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub enum CarbideCliError {
#[error("Generic Error: {0}")]
GenericError(String),

#[error("Operation not allowed due to potential inconsistencies with cloud database.")]
CloudUnsafeOp,

#[error("Cannot specify both {0} and {1}. Please provide only one.")]
ChooseOneError(&'static str, &'static str),

Expand Down
15 changes: 5 additions & 10 deletions crates/admin-cli/src/instance/allocate/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,17 @@
use std::collections::VecDeque;

use super::args::Args;
use crate::cfg::runtime::RuntimeContext;
use crate::errors::{CarbideCliError, CarbideCliResult};
use crate::instance::common::GlobalOptions;
use crate::machine;
use crate::rpc::ApiClient;

pub async fn allocate(
api_client: &ApiClient,
allocate_request: Args,
opts: GlobalOptions<'_>,
ctx: &RuntimeContext,
) -> CarbideCliResult<()> {
if opts.cloud_unsafe_op.is_none() {
return Err(CarbideCliError::GenericError(
"Operation not allowed due to potential inconsistencies with cloud database."
.to_owned(),
));
}
let unsafe_op_msg = ctx.assert_cloud_unsafe_op_message()?;

let number = allocate_request.number.unwrap_or(1);

Expand Down Expand Up @@ -87,7 +82,7 @@ pub async fn allocate(
machine,
&allocate_request,
&format!("{}_{}", allocate_request.prefix_name, i),
opts.cloud_unsafe_op.clone(),
Some(unsafe_op_msg.to_string()),
)
.await?;
requests.push(request);
Expand Down Expand Up @@ -127,7 +122,7 @@ pub async fn allocate(
machine,
&allocate_request,
&format!("{}_{}", allocate_request.prefix_name, i),
opts.cloud_unsafe_op.clone(),
Some(unsafe_op_msg.to_string()),
)
.await
{
Expand Down
13 changes: 1 addition & 12 deletions crates/admin-cli/src/instance/allocate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,13 @@ pub mod cmd;

pub use args::Args;

use super::common::GlobalOptions;
use crate::cfg::run::Run;
use crate::cfg::runtime::RuntimeContext;
use crate::errors::CarbideCliResult;

impl Run for Args {
async fn run(self, ctx: &mut RuntimeContext) -> CarbideCliResult<()> {
let opts = GlobalOptions {
format: ctx.config.format,
page_size: ctx.config.page_size,
sort_by: &ctx.config.sort_by,
cloud_unsafe_op: if ctx.config.cloud_unsafe_op_enabled {
Some("enabled".to_string())
} else {
None
},
};
cmd::allocate(&ctx.api_client, self, opts).await?;
cmd::allocate(&ctx.api_client, self, ctx).await?;
Ok(())
}
}
26 changes: 0 additions & 26 deletions crates/admin-cli/src/instance/common.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/admin-cli/src/instance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/

mod allocate;
pub(crate) mod common;
mod reboot;
mod release;
mod show;
Expand Down
13 changes: 4 additions & 9 deletions crates/admin-cli/src/instance/release/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,16 @@ use ::rpc::forge::InstanceReleaseRequest;
use carbide_uuid::instance::InstanceId;

use super::args::Args;
use crate::cfg::runtime::RuntimeContext;
use crate::errors::{CarbideCliError, CarbideCliResult};
use crate::instance::common::GlobalOptions;
use crate::rpc::ApiClient;

pub async fn release(
api_client: &ApiClient,
release_request: Args,
opts: GlobalOptions<'_>,
ctx: &RuntimeContext,
) -> CarbideCliResult<()> {
if opts.cloud_unsafe_op.is_none() {
return Err(CarbideCliError::GenericError(
"Operation not allowed due to potential inconsistencies with cloud database."
.to_owned(),
));
}
ctx.assert_cloud_unsafe_op_message()?;

let mut instance_ids: Vec<InstanceId> = Vec::new();

Expand Down Expand Up @@ -64,7 +59,7 @@ pub async fn release(
Some(key),
release_request.label_value,
None,
opts.page_size,
ctx.config.page_size,
)
.await?;
if instances.instances.is_empty() {
Expand Down
13 changes: 1 addition & 12 deletions crates/admin-cli/src/instance/release/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,13 @@ pub mod cmd;

pub use args::Args;

use super::common::GlobalOptions;
use crate::cfg::run::Run;
use crate::cfg::runtime::RuntimeContext;
use crate::errors::CarbideCliResult;

impl Run for Args {
async fn run(self, ctx: &mut RuntimeContext) -> CarbideCliResult<()> {
let opts = GlobalOptions {
format: ctx.config.format,
page_size: ctx.config.page_size,
sort_by: &ctx.config.sort_by,
cloud_unsafe_op: if ctx.config.cloud_unsafe_op_enabled {
Some("enabled".to_string())
} else {
None
},
};
cmd::release(&ctx.api_client, self, opts).await?;
cmd::release(&ctx.api_client, self, ctx).await?;
Ok(())
}
}
29 changes: 12 additions & 17 deletions crates/admin-cli/src/instance/show/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use prettytable::{Table, row};

use super::args::Args;
use crate::cfg::cli_options::SortField;
use crate::cfg::runtime::RuntimeContext;
use crate::errors::{CarbideCliError, CarbideCliResult};
use crate::rpc::ApiClient;
use crate::{async_write, async_writeln, invalid_machine_id};
Expand Down Expand Up @@ -484,27 +485,21 @@ async fn show_instance_details(
Ok(())
}

pub async fn handle_show(
args: Args,
output_file: &mut Box<dyn tokio::io::AsyncWrite + Unpin>,
output_format: &OutputFormat,
api_client: &ApiClient,
page_size: usize,
sort_by: &SortField,
) -> CarbideCliResult<()> {
pub async fn handle_show(args: Args, ctx: &mut RuntimeContext) -> CarbideCliResult<()> {
if args.id.is_empty() {
let mut all_instances = api_client
let mut all_instances = ctx
.api_client
.get_all_instances(
args.tenant_org_id,
args.vpc_id,
args.label_key,
args.label_value,
args.instance_type_id,
page_size,
ctx.config.page_size,
)
.await?;

match sort_by {
match ctx.config.sort_by {
SortField::PrimaryId => all_instances.instances.sort_by_key(|instance| instance.id),
SortField::State => all_instances.instances.sort_by(|i1, i2| {
let tenant_status1 = i1
Expand All @@ -524,17 +519,17 @@ pub async fn handle_show(
tenant_status1.cmp(&tenant_status2)
}),
}
match output_format {
match ctx.config.format {
OutputFormat::Json => {
async_writeln!(
output_file,
ctx.output_file,
"{}",
serde_json::to_string_pretty(&all_instances)?
)?;
}
OutputFormat::AsciiTable => {
let table = convert_instances_to_nice_table(all_instances);
async_write!(output_file, "{}", table)?;
async_write!(ctx.output_file, "{}", table)?;
}
OutputFormat::Csv => {
return Err(CarbideCliError::NotImplemented(
Expand All @@ -551,9 +546,9 @@ pub async fn handle_show(
}
show_instance_details(
args.id,
output_file,
output_format,
api_client,
&mut ctx.output_file,
&ctx.config.format,
&ctx.api_client,
args.extrainfo,
)
.await?;
Expand Down
21 changes: 1 addition & 20 deletions crates/admin-cli/src/instance/show/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,13 @@ pub mod cmd;

pub use args::Args;

use super::common::GlobalOptions;
use crate::cfg::run::Run;
use crate::cfg::runtime::RuntimeContext;
use crate::errors::CarbideCliResult;

impl Run for Args {
async fn run(self, ctx: &mut RuntimeContext) -> CarbideCliResult<()> {
let opts = GlobalOptions {
format: ctx.config.format,
page_size: ctx.config.page_size,
sort_by: &ctx.config.sort_by,
cloud_unsafe_op: if ctx.config.cloud_unsafe_op_enabled {
Some("enabled".to_string())
} else {
None
},
};
cmd::handle_show(
self,
&mut ctx.output_file,
&opts.format,
&ctx.api_client,
opts.page_size,
opts.sort_by,
)
.await?;
cmd::handle_show(self, ctx).await?;
Ok(())
}
}
16 changes: 5 additions & 11 deletions crates/admin-cli/src/instance/update_ib_config/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use super::args::Args;
use crate::errors::{CarbideCliError, CarbideCliResult};
use crate::instance::common::GlobalOptions;
use crate::cfg::runtime::RuntimeContext;
use crate::errors::CarbideCliResult;
use crate::rpc::ApiClient;

pub async fn update_ib_config(
api_client: &ApiClient,
update_request: Args,
opts: GlobalOptions<'_>,
ctx: &RuntimeContext,
) -> CarbideCliResult<()> {
if opts.cloud_unsafe_op.is_none() {
return Err(CarbideCliError::GenericError(
"Operation not allowed due to potential inconsistencies with cloud database."
.to_owned(),
));
}
let unsafe_op_msg = ctx.assert_cloud_unsafe_op_message()?;

match api_client
.update_instance_config_with(
Expand All @@ -39,7 +33,7 @@ pub async fn update_ib_config(
config.infiniband = Some(update_request.config);
},
|_metadata| {},
opts.cloud_unsafe_op,
Some(unsafe_op_msg.to_string()),
)
.await
{
Expand Down
13 changes: 1 addition & 12 deletions crates/admin-cli/src/instance/update_ib_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,13 @@ pub mod cmd;

pub use args::Args;

use super::common::GlobalOptions;
use crate::cfg::run::Run;
use crate::cfg::runtime::RuntimeContext;
use crate::errors::CarbideCliResult;

impl Run for Args {
async fn run(self, ctx: &mut RuntimeContext) -> CarbideCliResult<()> {
let opts = GlobalOptions {
format: ctx.config.format,
page_size: ctx.config.page_size,
sort_by: &ctx.config.sort_by,
cloud_unsafe_op: if ctx.config.cloud_unsafe_op_enabled {
Some("enabled".to_string())
} else {
None
},
};
cmd::update_ib_config(&ctx.api_client, self, opts).await?;
cmd::update_ib_config(&ctx.api_client, self, ctx).await?;
Ok(())
}
}
Loading
Loading