canic-core 0.111.0

Canic — a canister orchestration and management toolkit for the Internet Computer
Documentation
//! Module: ops::ic::mgmt
//!
//! Responsibility: expose observable IC management canister calls.
//! Does not own: canister lifecycle policy, placement decisions, or endpoint DTOs.
//! Boundary: records metrics and delegates management call mechanics to infra.

mod cycles;
mod lifecycle;
mod signing;
mod snapshots;
mod status_settings;
mod types;

use crate::{
    InternalError,
    domain::metrics::{
        PlatformCallMetricMode, PlatformCallMetricOutcome, PlatformCallMetricReason,
        PlatformCallMetricSurface,
    },
    dto::canister::{
        CanisterSettings as CanisterSettingsDto, CanisterStatusResponse,
        EnvironmentVariable as EnvironmentVariableDto, MemoryMetrics, QueryStats,
    },
    infra::ic::{IcInfraError, mgmt::MgmtInfra},
    ops::{OpsError, prelude::*, runtime::metrics::platform_call::PlatformCallMetrics},
};
use std::future::Future;
use types::{
    canister_status_from_infra, ecdsa_public_key_args_to_infra, ecdsa_public_key_from_infra,
    sign_with_ecdsa_args_to_infra, sign_with_ecdsa_from_infra, update_settings_to_infra,
};

#[expect(
    unused_imports,
    reason = "preserves public management ops value paths while domain owns the values"
)]
pub use types::{CanisterSettings, CanisterStatusType, LogVisibility};
pub use types::{
    CanisterSettingsSnapshot, CanisterStatus, CanisterStatusObservation, EcdsaKeyId,
    EcdsaPublicKeyArgs, EcdsaPublicKeyResult, EnvironmentVariable, MemoryMetricsSnapshot,
    QueryStatsSnapshot, SignWithEcdsaArgs, SignWithEcdsaResult, UpdateSettingsArgs,
};

///
/// MgmtOps
///
/// Operations-layer facade for IC management canister calls.
///

pub struct MgmtOps;

// Execute one management-canister call and record low-cardinality outcomes.
async fn management_call<T>(
    fut: impl Future<Output = Result<T, IcInfraError>>,
) -> Result<T, InternalError> {
    management_call_infra(fut)
        .await
        .map_err(|err| OpsError::from(err).into())
}

async fn management_call_infra<T>(
    fut: impl Future<Output = Result<T, IcInfraError>>,
) -> Result<T, IcInfraError> {
    record_management_call(
        PlatformCallMetricOutcome::Started,
        PlatformCallMetricReason::Ok,
    );

    match fut.await {
        Ok(value) => {
            record_management_call(
                PlatformCallMetricOutcome::Completed,
                PlatformCallMetricReason::Ok,
            );
            Ok(value)
        }
        Err(err) => {
            record_management_call(
                PlatformCallMetricOutcome::Failed,
                PlatformCallMetricReason::Infra,
            );
            Err(err)
        }
    }
}

// Record management-call metrics with no target or method labels.
fn record_management_call(
    platform_outcome: PlatformCallMetricOutcome,
    platform_reason: PlatformCallMetricReason,
) {
    PlatformCallMetrics::record(
        PlatformCallMetricSurface::Management,
        PlatformCallMetricMode::Update,
        platform_outcome,
        platform_reason,
    );
}