use crate::cli::clap::{parsed_options, with_source_options};
use ic_query::report::CollectionSourceRequest;
mod node;
mod provider;
use crate::presentation::cloud_engine::cloud_engine_list_report_text;
use crate::presentation::cloud_engine::cloud_engine_operator_report_text;
use crate::presentation::cloud_engine::cloud_engine_prices_report_text;
use crate::{
cli::common::{
COLLECTION_MODE_LIVE, CurrentUnixSecsError, OutputFormat, SourceOutputOptions,
collection_help, current_unix_secs, write_text_or_json,
},
progress::StderrQueryProgress,
storage::{CacheRootError, cache_root},
};
use clap::{ArgMatches, Command as ClapCommand};
use ic_query::cloud_engine::CloudEngineHostError;
use ic_query::cloud_engine::DEFAULT_CLOUD_ENGINE_SOURCE_ENDPOINT;
use ic_query::cloud_engine::build_cloud_engine_list_report_with_sources_and_progress;
use ic_query::cloud_engine::build_cloud_engine_operator_report;
use ic_query::cloud_engine::build_cloud_engine_prices_report;
use ic_query::ic::IcHostError;
use ic_query::subnet_catalog::DEFAULT_STALE_AFTER_SECONDS;
use ic_query::subnet_catalog::DEFAULT_SUBNET_CATALOG_SOURCE_ENDPOINT;
use ic_query::subnet_catalog::SubnetCatalogCacheRequest;
use ic_query::subnet_catalog::SubnetCatalogListRequest;
use std::io;
use thiserror::Error as ThisError;
const SOURCE_ENDPOINT_HELP: &str = "IC API endpoint used for native CloudEngine queries";
const INFO_HELP_AFTER: &str = "\
Examples:
icq cloud-engine info 2nl67-oqoc5-cmocj-otlhq-kr2kr-53hov-drrds-7ihcs-fhomv-2eyvu-6qe
icq cloud-engine info 2nl67-oqoc5-cmocj-otlhq-kr2kr-53hov-drrds-7ihcs-fhomv-2eyvu-6qe --json
This command makes one control-plane query to resolve the Subnet and four
public operator queries when an operator is registered. The responses are not
certified and the sequential calls are not an exact point-in-time snapshot.";
const LIST_COLLECTION_MODE: &str = "Cache-backed Registry inventory; refreshes missing or recoverably invalid catalog content, then makes one live operator-binding query per returned Subnet.";
const LIST_HELP_AFTER: &str = "\
Examples:
icq cloud-engine list
icq cloud-engine list --json
The Registry supplies the complete CloudEngine Subnet inventory. The command
then makes one exact public control-plane operator-binding query per returned
Subnet, up to 100. Registry and control-plane provenance remain separate, and
one failed binding lookup is reported on its row without discarding the
Registry snapshot.";
const PRICES_HELP_AFTER: &str = "\
Examples:
icq cloud-engine prices
icq cloud-engine prices --json
This command makes exactly two control-plane queries: one for the network fee
and one for at most 1,000 public marketplace rows. The responses are not
certified or presented as an exact point-in-time snapshot.";
#[derive(Debug, ThisError)]
pub enum CloudEngineCommandError {
#[error(transparent)]
Host(#[from] CloudEngineHostError),
#[error(transparent)]
Dashboard(#[from] IcHostError),
#[error(transparent)]
CacheRoot(#[from] CacheRootError),
#[error(transparent)]
Clock(#[from] CurrentUnixSecsError),
#[error(transparent)]
Io(#[from] io::Error),
#[error(transparent)]
Json(#[from] serde_json::Error),
}
pub async fn run_matches(
matches: &ArgMatches,
network: &str,
) -> Result<(), CloudEngineCommandError> {
match matches.subcommand() {
Some(("info", matches)) => run_info(matches, network).await,
Some(("list", matches)) => run_list(matches, network).await,
Some(("node", matches)) => node::run_matches(matches, network).await,
Some(("prices", matches)) => run_prices(matches, network).await,
Some(("provider", matches)) => provider::run_matches(matches, network).await,
_ => unreachable!("clap requires a known cloud-engine subcommand"),
}
}
async fn run_list(matches: &ArgMatches, network: &str) -> Result<(), CloudEngineCommandError> {
let options: OperatorListOptions = parsed_options(matches);
let now_unix_secs = current_unix_secs()?;
let registry_source_endpoint = options.registry_source_endpoint;
let cache = SubnetCatalogCacheRequest::new(cache_root()?, network);
let catalog_request = SubnetCatalogListRequest::new(
cache,
registry_source_endpoint,
now_unix_secs,
DEFAULT_STALE_AFTER_SECONDS,
);
let (format, control_plane_request) = source_request_at(options.source, network, now_unix_secs);
let report = build_cloud_engine_list_report_with_sources_and_progress(
&catalog_request,
&control_plane_request,
&ic_query::nns::LiveNnsSource::default(),
&ic_query::cloud_engine::LiveCloudEngineSource::default(),
&mut StderrQueryProgress::new(),
)
.await?;
write_text_or_json(format, &report, cloud_engine_list_report_text)
}
async fn run_info(matches: &ArgMatches, network: &str) -> Result<(), CloudEngineCommandError> {
let options: OperatorInfoOptions = parsed_options(matches);
let (format, request) = source_request_at(options.source, network, current_unix_secs()?);
let report = build_cloud_engine_operator_report(&request, &options.subnet_id).await?;
write_text_or_json(format, &report, cloud_engine_operator_report_text)
}
async fn run_prices(matches: &ArgMatches, network: &str) -> Result<(), CloudEngineCommandError> {
let (format, request) = source_request_at(
SourceOutputOptions::from_matches(matches),
network,
current_unix_secs()?,
);
let report = build_cloud_engine_prices_report(&request).await?;
write_text_or_json(format, &report, cloud_engine_prices_report_text)
}
fn source_request_at(
source: SourceOutputOptions,
network: &str,
fetched_at_unix_secs: u64,
) -> (OutputFormat, CollectionSourceRequest) {
(
source.format(),
CollectionSourceRequest::from_unix_secs(
network,
source.source_endpoint,
fetched_at_unix_secs,
"ic-query",
),
)
}
pub fn command() -> ClapCommand {
ClapCommand::new("cloud-engine")
.about("Inspect public CloudEngine metadata")
.subcommand(info_command())
.subcommand(list_command())
.subcommand(node::command())
.subcommand(prices_command())
.subcommand(provider::command())
.after_help(
"Examples:\n icq cloud-engine list\n icq cloud-engine info <subnet-id>\n icq cloud-engine node list\n icq cloud-engine prices\n icq cloud-engine provider list",
)
}
#[derive(clap::Args)]
#[group(skip)]
struct OperatorListOptions {
#[arg(id = REGISTRY_SOURCE_ENDPOINT_ARG, long = REGISTRY_SOURCE_ENDPOINT_ARG, value_name = "url", default_value = DEFAULT_SUBNET_CATALOG_SOURCE_ENDPOINT, help = "IC API endpoint used to refresh the Registry Subnet Catalog")]
registry_source_endpoint: String,
#[command(flatten)]
source: SourceOutputOptions,
}
fn list_command() -> ClapCommand {
with_source_options::<OperatorListOptions>(
ClapCommand::new("list")
.about("List Registry CloudEngine Subnets and public operator bindings"),
DEFAULT_CLOUD_ENGINE_SOURCE_ENDPOINT,
SOURCE_ENDPOINT_HELP,
)
.after_help(collection_help(LIST_COLLECTION_MODE, LIST_HELP_AFTER))
}
#[derive(clap::Args)]
#[group(skip)]
struct OperatorInfoOptions {
#[command(flatten)]
source: SourceOutputOptions,
#[arg(
id = "subnet-id",
value_name = "subnet-id",
help = "CloudEngine Subnet principal to resolve"
)]
subnet_id: String,
}
fn info_command() -> ClapCommand {
with_source_options::<OperatorInfoOptions>(
ClapCommand::new("info")
.about("Show the operator binding and public settings for one CloudEngine Subnet")
.after_help(collection_help(COLLECTION_MODE_LIVE, INFO_HELP_AFTER)),
DEFAULT_CLOUD_ENGINE_SOURCE_ENDPOINT,
SOURCE_ENDPOINT_HELP,
)
}
fn prices_command() -> ClapCommand {
with_source_options::<SourceOutputOptions>(
ClapCommand::new("prices")
.about("Show the public CloudEngine network fee and marketplace prices"),
DEFAULT_CLOUD_ENGINE_SOURCE_ENDPOINT,
SOURCE_ENDPOINT_HELP,
)
.after_help(collection_help(COLLECTION_MODE_LIVE, PRICES_HELP_AFTER))
}
const REGISTRY_SOURCE_ENDPOINT_ARG: &str = "registry-source-endpoint";
#[cfg(test)]
mod tests;