zombienet_provider/native/
provider.rs1use std::{
2 collections::HashMap,
3 path::{Path, PathBuf},
4 sync::{Arc, Weak},
5};
6
7use async_trait::async_trait;
8use support::fs::FileSystem;
9use tokio::sync::RwLock;
10
11use super::namespace::NativeNamespace;
12use crate::{
13 shared::helpers::extract_namespace_info, types::ProviderCapabilities, DynNamespace, Provider,
14 ProviderError, ProviderNamespace,
15};
16
17pub const PROVIDER_NAME: &str = "native";
18
19pub struct NativeProvider<FS>
20where
21 FS: FileSystem + Send + Sync + Clone,
22{
23 weak: Weak<NativeProvider<FS>>,
24 capabilities: ProviderCapabilities,
25 tmp_dir: PathBuf,
26 filesystem: FS,
27 pub(super) namespaces: RwLock<HashMap<String, Arc<NativeNamespace<FS>>>>,
28}
29
30impl<FS> NativeProvider<FS>
31where
32 FS: FileSystem + Send + Sync + Clone,
33{
34 pub fn new(filesystem: FS) -> Arc<Self> {
35 Arc::new_cyclic(|weak| NativeProvider {
36 weak: weak.clone(),
37 capabilities: ProviderCapabilities {
38 has_resources: false,
39 requires_image: false,
40 prefix_with_full_path: true,
41 use_default_ports_in_cmd: false,
42 has_secrets: false,
43 fileserver_fetches_db_snapshot_url: false,
44 },
45 tmp_dir: std::env::temp_dir(),
50 filesystem,
51 namespaces: RwLock::new(HashMap::new()),
52 })
53 }
54
55 pub fn tmp_dir(mut self, tmp_dir: impl Into<PathBuf>) -> Self {
56 self.tmp_dir = tmp_dir.into();
57 self
58 }
59}
60
61#[async_trait]
62impl<FS> Provider for NativeProvider<FS>
63where
64 FS: FileSystem + Send + Sync + Clone + 'static,
65{
66 fn name(&self) -> &str {
67 PROVIDER_NAME
68 }
69
70 fn capabilities(&self) -> &ProviderCapabilities {
71 &self.capabilities
72 }
73
74 async fn namespaces(&self) -> HashMap<String, DynNamespace> {
75 self.namespaces
76 .read()
77 .await
78 .iter()
79 .map(|(name, namespace)| (name.clone(), namespace.clone() as DynNamespace))
80 .collect()
81 }
82
83 async fn create_namespace(&self) -> Result<DynNamespace, ProviderError> {
84 let namespace = NativeNamespace::new(
85 &self.weak,
86 &self.tmp_dir,
87 &self.capabilities,
88 &self.filesystem,
89 None,
90 )
91 .await?;
92
93 self.namespaces
94 .write()
95 .await
96 .insert(namespace.name().to_string(), namespace.clone());
97
98 Ok(namespace)
99 }
100
101 async fn create_namespace_with_base_dir(
102 &self,
103 base_dir: &Path,
104 ) -> Result<DynNamespace, ProviderError> {
105 let namespace = NativeNamespace::new(
106 &self.weak,
107 &self.tmp_dir,
108 &self.capabilities,
109 &self.filesystem,
110 Some(base_dir),
111 )
112 .await?;
113
114 self.namespaces
115 .write()
116 .await
117 .insert(namespace.name().to_string(), namespace.clone());
118
119 Ok(namespace)
120 }
121
122 async fn create_namespace_from_json(
123 &self,
124 json_value: &serde_json::Value,
125 ) -> Result<DynNamespace, ProviderError> {
126 let (base_dir, name) = extract_namespace_info(json_value)?;
127
128 let namespace = NativeNamespace::attach_to_live(
129 &self.weak,
130 &self.capabilities,
131 &self.filesystem,
132 &base_dir,
133 &name,
134 )
135 .await?;
136
137 self.namespaces
138 .write()
139 .await
140 .insert(namespace.name().to_string(), namespace.clone());
141
142 Ok(namespace)
143 }
144}