ic-query-cli 0.104.0

Command-line wrapper for read-only Internet Computer metadata queries
Documentation
//! Module: cloud_engine
//!
//! Responsibility: parse and dispatch public CloudEngine metadata reports.
//! Does not own: native transport, report construction, or text rendering.
//! Boundary: exposes mainnet-only CloudEngine inventory and bounded public reports at the CLI root.

use crate::cli::clap::parsed_options;
use ic_query::cloud_engine::LiveCloudEngineSource;
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, Subcommand};
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_with_source;
use ic_query::cloud_engine::build_cloud_engine_prices_report_with_source;
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.";

///
/// CloudEngineCommandError
///
/// Errors surfaced while parsing or running a CloudEngine command.
///

#[derive(Debug, ThisError)]
pub enum CloudEngineCommandError {
    /// Native CloudEngine collection or evidence validation failed.
    #[error(transparent)]
    Host(#[from] CloudEngineHostError),
    /// Official Dashboard collection or evidence validation failed.
    #[error(transparent)]
    Dashboard(#[from] IcHostError),
    /// The CLI cache root could not be resolved.
    #[error(transparent)]
    CacheRoot(#[from] CacheRootError),
    /// The process clock could not supply a Unix collection timestamp.
    #[error(transparent)]
    Clock(#[from] CurrentUnixSecsError),
    /// Writing the selected report output failed.
    #[error(transparent)]
    Io(#[from] io::Error),
    /// JSON serialization failed.
    #[error(transparent)]
    Json(#[from] serde_json::Error),
}

pub async fn run_matches(
    matches: &ArgMatches,
    network: &str,
) -> Result<(), CloudEngineCommandError> {
    match parsed_options::<CloudEngineCommand>(matches) {
        CloudEngineCommand::Info(options) => run_info(options, network).await,
        CloudEngineCommand::List(options) => run_list(options, network).await,
        CloudEngineCommand::Node(command) => node::run(command, network).await,
        CloudEngineCommand::Prices(options) => run_prices(options, network).await,
        CloudEngineCommand::Provider(command) => provider::run(command, network).await,
    }
}

async fn run_list(
    options: OperatorListOptions,
    network: &str,
) -> Result<(), CloudEngineCommandError> {
    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(),
        &LiveCloudEngineSource::default(),
        &mut StderrQueryProgress::new(),
    )
    .await?;
    write_text_or_json(format, &report, cloud_engine_list_report_text)
}

async fn run_info(
    options: OperatorInfoOptions,
    network: &str,
) -> Result<(), CloudEngineCommandError> {
    let (format, request) = source_request_at(options.source, network, current_unix_secs()?);
    let report = build_cloud_engine_operator_report_with_source(
        &request,
        &options.subnet_id,
        &LiveCloudEngineSource::default(),
    )
    .await?;
    write_text_or_json(format, &report, cloud_engine_operator_report_text)
}

async fn run_prices(
    options: SourceOutputOptions,
    network: &str,
) -> Result<(), CloudEngineCommandError> {
    let (format, request) = source_request_at(options, network, current_unix_secs()?);
    let report =
        build_cloud_engine_prices_report_with_source(&request, &LiveCloudEngineSource::default())
            .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 {
    CloudEngineCommand::augment_subcommands(ClapCommand::new("cloud-engine"))
}

///
/// CloudEngineCommand
///
/// Typed public CloudEngine operations.
///

#[derive(Subcommand)]
#[command(about = "Inspect public CloudEngine metadata", long_about = None, 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")]
enum CloudEngineCommand {
    #[command(about = "Show the operator binding and public settings for one CloudEngine Subnet", long_about = None, after_help = collection_help(COLLECTION_MODE_LIVE, INFO_HELP_AFTER), mut_arg("source-endpoint", |arg| arg.default_value(DEFAULT_CLOUD_ENGINE_SOURCE_ENDPOINT).help(SOURCE_ENDPOINT_HELP)))]
    Info(OperatorInfoOptions),
    #[command(about = "List Registry CloudEngine Subnets and public operator bindings", long_about = None, after_help = collection_help(LIST_COLLECTION_MODE, LIST_HELP_AFTER), mut_arg("source-endpoint", |arg| arg.default_value(DEFAULT_CLOUD_ENGINE_SOURCE_ENDPOINT).help(SOURCE_ENDPOINT_HELP)))]
    List(OperatorListOptions),
    #[command(subcommand)]
    Node(node::NodeCommand),
    #[command(about = "Show the public CloudEngine network fee and marketplace prices", long_about = None, after_help = collection_help(COLLECTION_MODE_LIVE, PRICES_HELP_AFTER), mut_arg("source-endpoint", |arg| arg.default_value(DEFAULT_CLOUD_ENGINE_SOURCE_ENDPOINT).help(SOURCE_ENDPOINT_HELP)))]
    Prices(SourceOutputOptions),
    #[command(subcommand)]
    Provider(provider::ProviderCommand),
}

///
/// OperatorListOptions
///
/// Typed arguments for the list_command operation.
///

#[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,
}

///
/// OperatorInfoOptions
///
/// Typed arguments for the info_command operation.
///

#[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,
}

const REGISTRY_SOURCE_ENDPOINT_ARG: &str = "registry-source-endpoint";

#[cfg(test)]
mod tests;