-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompute_api.rs
More file actions
118 lines (112 loc) · 3.33 KB
/
compute_api.rs
File metadata and controls
118 lines (112 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use std::path::PathBuf;
use crate::{
client::FirecrestClient,
types::{
GetJobMetadataResponse, GetJobResponse, JobDescriptionModel, JobDescriptionModelEnv,
PostJobSubmissionResponse, PostJobSubmitRequest,
},
};
use eyre::{Result, eyre};
use serde_json::json;
pub async fn post_compute_system_job(
client: &FirecrestClient,
system_name: &str,
name: &str,
script: Option<&str>,
script_path: Option<PathBuf>,
working_dir: Option<PathBuf>,
) -> Result<PostJobSubmissionResponse> {
if script.is_none() && script_path.is_none() {
return Err(eyre!("either script or script_path must be set"));
}
let body = PostJobSubmitRequest {
job: JobDescriptionModel {
name: Some(name.to_string()),
script: script.map(|s| s.to_owned()),
script_path: script_path
.map(|s| s.into_os_string().into_string())
.transpose()
.map_err(|_| eyre!("couldn't convert script path"))?,
working_directory: working_dir
.map(|s| s.into_os_string().into_string())
.transpose()
.map_err(|_| eyre!("couldn't convert working dir path"))?
.unwrap_or("/".to_owned()),
env: Some(JobDescriptionModelEnv::Object(json!({"test":"test"}))),
..Default::default()
},
};
let body_json = serde_json::to_string(&body)?;
let response = client
.post(
format!("compute/{system_name}/jobs").as_str(),
body_json,
None,
None,
)
.await?;
let model: PostJobSubmissionResponse = serde_json::from_str(response.as_str())?;
Ok(model)
}
pub async fn get_compute_system_jobs(
client: &FirecrestClient,
system_name: &str,
all_users: Option<bool>,
) -> Result<GetJobResponse> {
let query = match all_users {
Some(v) => Some(vec![(
"all_users",
match v {
true => "true",
false => "false",
},
)]),
None => None,
};
let response = client
.get(format!("compute/{system_name}/jobs").as_str(), query)
.await?;
let model: GetJobResponse = serde_json::from_str(response.as_str())?;
Ok(model)
}
pub async fn get_compute_system_job(
client: &FirecrestClient,
system_name: &str,
job_id: i64,
) -> Result<GetJobResponse> {
let response = client
.get(
format!("compute/{system_name}/jobs/{job_id}").as_str(),
None,
)
.await?;
let model: GetJobResponse = serde_json::from_str(response.as_str())?;
Ok(model)
}
pub async fn get_compute_system_job_metadata(
client: &FirecrestClient,
system_name: &str,
job_id: i64,
) -> Result<GetJobMetadataResponse> {
let response = client
.get(
format!("compute/{system_name}/jobs/{job_id}/metadata").as_str(),
None,
)
.await?;
let model: GetJobMetadataResponse = serde_json::from_str(response.as_str())?;
Ok(model)
}
pub async fn cancel_compute_system_job(
client: &FirecrestClient,
system_name: &str,
job_id: i64,
) -> Result<()> {
let _ = client
.delete(
format!("compute/{system_name}/jobs/{job_id}").as_str(),
None,
)
.await?;
Ok(())
}