conductor/client/metadata_client.rs
1// Copyright {{.Year}} Conductor OSS
2// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3
4use tracing::{debug, info};
5
6use crate::error::Result;
7use crate::http::{ApiClient, ApiPath};
8use crate::models::{TaskDef, WorkflowDef};
9
10/// Client for metadata operations (workflow and task definitions).
11#[derive(Clone)]
12pub struct MetadataClient {
13 api: ApiClient,
14}
15
16impl MetadataClient {
17 /// Create a new metadata client.
18 #[must_use]
19 pub fn new(api: ApiClient) -> Self {
20 Self { api }
21 }
22
23 // ==================== Workflow Definitions ====================
24
25 /// Register a new workflow definition.
26 ///
27 /// # Errors
28 ///
29 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
30 pub async fn register_workflow_def(&self, workflow: &WorkflowDef) -> Result<()> {
31 debug!(
32 workflow_name = %workflow.name,
33 version = workflow.version,
34 "Registering workflow definition"
35 );
36
37 let _: serde_json::Value = self.api.post("/metadata/workflow", workflow).await?;
38
39 info!(
40 workflow_name = %workflow.name,
41 version = workflow.version,
42 "Workflow definition registered"
43 );
44
45 Ok(())
46 }
47
48 /// Update an existing workflow definition.
49 ///
50 /// # Errors
51 ///
52 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
53 pub async fn update_workflow_def(&self, workflow: &WorkflowDef) -> Result<()> {
54 debug!(
55 workflow_name = %workflow.name,
56 version = workflow.version,
57 "Updating workflow definition"
58 );
59
60 let _: serde_json::Value = self.api.put("/metadata/workflow", &[workflow]).await?;
61
62 info!(
63 workflow_name = %workflow.name,
64 version = workflow.version,
65 "Workflow definition updated"
66 );
67
68 Ok(())
69 }
70
71 /// Register or update a workflow definition.
72 ///
73 /// # Errors
74 ///
75 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
76 pub async fn register_or_update_workflow_def(
77 &self,
78 workflow: &WorkflowDef,
79 overwrite: bool,
80 ) -> Result<()> {
81 if overwrite {
82 // Use PUT to update existing
83 self.update_workflow_def(workflow).await
84 } else {
85 // Use POST to create new
86 self.register_workflow_def(workflow).await
87 }
88 }
89
90 /// Get a workflow definition by name and version.
91 ///
92 /// # Errors
93 ///
94 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
95 pub async fn get_workflow_def(&self, name: &str, version: Option<i32>) -> Result<WorkflowDef> {
96 let path = if let Some(v) = version {
97 format!("/metadata/workflow/{name}?version={v}")
98 } else {
99 format!("/metadata/workflow/{name}")
100 };
101
102 self.api
103 .get(ApiPath::templated(&path, "/metadata/workflow/{name}"))
104 .await
105 }
106
107 /// Get all versions of a workflow definition.
108 ///
109 /// # Errors
110 ///
111 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
112 pub async fn get_all_workflow_def_versions(&self, name: &str) -> Result<Vec<WorkflowDef>> {
113 let path = format!("/metadata/workflow/{name}/versions");
114 self.api
115 .get(ApiPath::templated(
116 &path,
117 "/metadata/workflow/{name}/versions",
118 ))
119 .await
120 }
121
122 /// Get all workflow definitions.
123 ///
124 /// # Errors
125 ///
126 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
127 pub async fn get_all_workflow_defs(&self) -> Result<Vec<WorkflowDef>> {
128 self.api.get("/metadata/workflow").await
129 }
130
131 /// Get all workflow definitions with latest versions (alias for `get_all_workflow_defs`).
132 ///
133 /// # Errors
134 ///
135 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
136 pub async fn get_all_workflow_defs_latest_versions(&self) -> Result<Vec<WorkflowDef>> {
137 self.get_all_workflow_defs().await
138 }
139
140 /// Delete a workflow definition.
141 ///
142 /// # Errors
143 ///
144 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
145 pub async fn delete_workflow_def(&self, name: &str, version: i32) -> Result<()> {
146 let path = format!("/metadata/workflow/{name}/{version}");
147 self.api
148 .delete_no_content(ApiPath::templated(
149 &path,
150 "/metadata/workflow/{name}/{version}",
151 ))
152 .await
153 }
154
155 // ==================== Task Definitions ====================
156
157 /// Register a new task definition.
158 ///
159 /// # Errors
160 ///
161 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
162 pub async fn register_task_def(&self, task: &TaskDef) -> Result<()> {
163 debug!(task_name = %task.name, "Registering task definition");
164
165 let _: serde_json::Value = self.api.post("/metadata/taskdefs", &[task]).await?;
166
167 info!(task_name = %task.name, "Task definition registered");
168
169 Ok(())
170 }
171
172 /// Register multiple task definitions.
173 ///
174 /// # Errors
175 ///
176 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
177 pub async fn register_task_defs(&self, tasks: &[TaskDef]) -> Result<()> {
178 debug!(count = tasks.len(), "Registering task definitions");
179
180 let _: serde_json::Value = self.api.post("/metadata/taskdefs", tasks).await?;
181
182 info!(count = tasks.len(), "Task definitions registered");
183
184 Ok(())
185 }
186
187 /// Update a task definition.
188 ///
189 /// # Errors
190 ///
191 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
192 pub async fn update_task_def(&self, task: &TaskDef) -> Result<()> {
193 debug!(task_name = %task.name, "Updating task definition");
194
195 let _: serde_json::Value = self.api.put("/metadata/taskdefs", task).await?;
196
197 info!(task_name = %task.name, "Task definition updated");
198
199 Ok(())
200 }
201
202 /// Get a task definition by name.
203 ///
204 /// # Errors
205 ///
206 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
207 pub async fn get_task_def(&self, name: &str) -> Result<TaskDef> {
208 let path = format!("/metadata/taskdefs/{name}");
209 self.api
210 .get(ApiPath::templated(&path, "/metadata/taskdefs/{name}"))
211 .await
212 }
213
214 /// Get all task definitions.
215 ///
216 /// # Errors
217 ///
218 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
219 pub async fn get_all_task_defs(&self) -> Result<Vec<TaskDef>> {
220 self.api.get("/metadata/taskdefs").await
221 }
222
223 /// Delete a task definition.
224 ///
225 /// # Errors
226 ///
227 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
228 pub async fn delete_task_def(&self, name: &str) -> Result<()> {
229 let path = format!("/metadata/taskdefs/{name}");
230 self.api
231 .delete_no_content(ApiPath::templated(&path, "/metadata/taskdefs/{name}"))
232 .await
233 }
234
235 /// Check if a task definition exists.
236 ///
237 /// # Errors
238 ///
239 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
240 pub async fn task_def_exists(&self, name: &str) -> Result<bool> {
241 match self.get_task_def(name).await {
242 Ok(_) => Ok(true),
243 Err(crate::error::ConductorError::Api { .. }) => Ok(false),
244 Err(crate::error::ConductorError::Server { status: 404, .. }) => Ok(false),
245 Err(e) => Err(e),
246 }
247 }
248
249 /// Check if a workflow definition exists.
250 ///
251 /// # Errors
252 ///
253 /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
254 pub async fn workflow_def_exists(&self, name: &str, version: Option<i32>) -> Result<bool> {
255 match self.get_workflow_def(name, version).await {
256 Ok(_) => Ok(true),
257 Err(crate::error::ConductorError::Api { .. }) => Ok(false),
258 Err(crate::error::ConductorError::Server { status: 404, .. }) => Ok(false),
259 Err(e) => Err(e),
260 }
261 }
262}
263
264#[cfg(test)]
265mod tests {
266 use super::*;
267 use crate::models::{RetryLogic, TimeoutPolicy};
268
269 #[test]
270 fn test_task_def_serialization() {
271 let task = TaskDef::new("test_task")
272 .with_description("A test task")
273 .with_retry(3, RetryLogic::ExponentialBackoff, 5)
274 .with_timeout(120, TimeoutPolicy::Retry);
275
276 let json = serde_json::to_string(&task).unwrap();
277 assert!(json.contains("\"name\":\"test_task\""));
278 assert!(json.contains("\"retryCount\":3"));
279 }
280
281 #[test]
282 fn test_workflow_def_serialization() {
283 use crate::models::WorkflowTask;
284
285 let workflow = WorkflowDef::new("test_workflow")
286 .with_description("A test workflow")
287 .with_task(WorkflowTask::simple("task1", "task1_ref"));
288
289 let json = serde_json::to_string(&workflow).unwrap();
290 assert!(json.contains("\"name\":\"test_workflow\""));
291 assert!(json.contains("\"tasks\":["));
292 }
293}