Skip to main content

stackless_core/def/
placement.rs

1//! Hosting placement is independent of the catalog provider for a resource.
2
3use std::collections::BTreeMap;
4
5use super::StackDef;
6use crate::engine::{Step, StepKind};
7
8/// Resolved hosting adapters. Catalog resource providers remain separate.
9#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
10pub struct Placements {
11    pub workloads: BTreeMap<String, String>,
12    pub resources: BTreeMap<String, String>,
13}
14
15/// Stable resource lifetime key shared by planning and the placement journal.
16pub fn placement_key(step: &str) -> Option<String> {
17    let (kind, name) = step.split_once(':')?;
18    if name.is_empty() {
19        return None;
20    }
21    match kind {
22        "integration" => Some(format!("integration:{name}")),
23        "materialize" | "setup" | "prepare" | "start" | "health" | "job" => {
24            Some(format!("service:{name}"))
25        }
26        _ => None,
27    }
28}
29
30impl StackDef {
31    pub fn resolved_placements(&self, default_provider: &str) -> Placements {
32        Placements {
33            workloads: self
34                .services
35                .iter()
36                .map(|(name, workload)| {
37                    (
38                        name.clone(),
39                        workload.on.as_deref().unwrap_or(default_provider).into(),
40                    )
41                })
42                .collect(),
43            resources: self
44                .integrations
45                .iter()
46                .map(|(name, resource)| {
47                    (
48                        name.clone(),
49                        resource.on.as_deref().unwrap_or(default_provider).into(),
50                    )
51                })
52                .collect(),
53        }
54    }
55
56    pub fn placements(&self, default_provider: &str) -> BTreeMap<String, String> {
57        self.services
58            .iter()
59            .map(|(name, workload)| {
60                (
61                    format!("service:{name}"),
62                    workload
63                        .on
64                        .as_deref()
65                        .unwrap_or(default_provider)
66                        .to_owned(),
67                )
68            })
69            .chain(self.integrations.iter().map(|(name, resource)| {
70                (
71                    format!("integration:{name}"),
72                    resource
73                        .on
74                        .as_deref()
75                        .unwrap_or(default_provider)
76                        .to_owned(),
77                )
78            }))
79            .collect()
80    }
81
82    pub fn step_provider<'a>(&'a self, default_provider: &'a str, step: &Step) -> &'a str {
83        if step.kind == StepKind::ProvisionIntegration {
84            self.integrations
85                .get(&step.node)
86                .and_then(|resource| resource.on.as_deref())
87        } else {
88            self.services
89                .get(&step.node)
90                .and_then(|workload| workload.on.as_deref())
91        }
92        .unwrap_or(default_provider)
93    }
94}