use crate::core::error::Result;
use crate::core::resources::ResourceSpec;
use crate::infra::types::{InstanceStatus, ProvisionedInstance};
use async_trait::async_trait;
use blueprint_std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct BlueprintDeploymentResult {
pub instance: ProvisionedInstance,
pub blueprint_id: String,
pub port_mappings: HashMap<u16, u16>,
pub metadata: HashMap<String, String>,
}
impl BlueprintDeploymentResult {
pub fn qos_metrics_port(&self) -> Option<u16> {
self.port_mappings.get(&9615).copied()
}
pub fn rpc_port(&self) -> Option<u16> {
self.port_mappings.get(&9944).copied()
}
pub fn qos_grpc_endpoint(&self) -> Option<String> {
match (self.qos_metrics_port(), &self.instance.public_ip) {
(Some(port), Some(ip)) => Some(format!("http://{ip}:{port}")),
_ => None,
}
}
}
#[async_trait]
pub trait CloudProviderAdapter: Send + Sync {
async fn provision_instance(
&self,
instance_type: &str,
region: &str,
require_tee: bool,
) -> Result<ProvisionedInstance>;
async fn terminate_instance(&self, instance_id: &str) -> Result<()>;
async fn get_instance_status(&self, instance_id: &str) -> Result<InstanceStatus>;
async fn get_instance_details(&self, _instance_id: &str) -> Result<ProvisionedInstance> {
Err(crate::core::error::Error::Other(
"get_instance_details not implemented for this provider".into(),
))
}
async fn deploy_blueprint_with_target(
&self,
target: &crate::core::deployment_target::DeploymentTarget,
blueprint_image: &str,
resource_spec: &ResourceSpec,
env_vars: HashMap<String, String>,
) -> Result<BlueprintDeploymentResult>;
async fn deploy_blueprint(
&self,
_instance: &ProvisionedInstance,
blueprint_image: &str,
resource_spec: &ResourceSpec,
env_vars: HashMap<String, String>,
) -> Result<BlueprintDeploymentResult> {
use crate::core::deployment_target::{ContainerRuntime, DeploymentTarget};
let target = DeploymentTarget::VirtualMachine {
runtime: ContainerRuntime::Docker,
};
self.deploy_blueprint_with_target(&target, blueprint_image, resource_spec, env_vars)
.await
}
async fn health_check_blueprint(&self, deployment: &BlueprintDeploymentResult) -> Result<bool>;
async fn cleanup_blueprint(&self, deployment: &BlueprintDeploymentResult) -> Result<()>;
}