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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use crate::client::apis::configuration::Configuration;
use crate::client::apis::default_api;
use crate::client::commands::get_env_user_name;
use crate::client::commands::output::{print_if_json, print_wrapped_if_json};
use crate::client::commands::{
pagination, print_error, select_workflow_interactively, table_format::display_table_with_count,
};
use crate::models;
use tabled::Tabled;
#[derive(Tabled)]
struct ResourceRequirementsTableRow {
#[tabled(rename = "ID")]
id: i64,
#[tabled(rename = "Name")]
name: String,
#[tabled(rename = "CPUs")]
cpus: i64,
#[tabled(rename = "GPUs")]
gpus: i64,
#[tabled(rename = "Nodes")]
nodes: i64,
#[tabled(rename = "Memory")]
memory: String,
#[tabled(rename = "Runtime")]
runtime: String,
}
#[derive(clap::Subcommand)]
pub enum ResourceRequirementsCommands {
/// Create new resource requirements
Create {
/// Create resource requirements in this workflow.
#[arg()]
workflow_id: Option<i64>,
/// Name of the resource requirements
#[arg(short, long, required = true)]
name: String,
/// Number of CPUs required
#[arg(long, default_value = "1")]
num_cpus: i64,
/// Number of GPUs required
#[arg(long, default_value = "0")]
num_gpus: i64,
/// Number of nodes required
#[arg(long, default_value = "1")]
num_nodes: i64,
/// Amount of memory required (e.g., "20g")
#[arg(short, long, default_value = "1m")]
memory: String,
/// Maximum runtime in ISO 8601 duration format (e.g., "PT1H", "PT30M")
#[arg(short, long, default_value = "PT1M")]
runtime: String,
},
/// List resource requirements
List {
/// List resource requirements for this workflow (optional - will prompt if not provided)
#[arg()]
workflow_id: Option<i64>,
/// Maximum number of resource requirements to return (default: all)
#[arg(short, long)]
limit: Option<i64>,
/// Offset for pagination (0-based)
#[arg(long, default_value = "0")]
offset: i64,
/// Field to sort by
#[arg(long)]
sort_by: Option<String>,
/// Reverse sort order
#[arg(long)]
reverse_sort: bool,
},
/// Get a specific resource requirement by ID
Get {
/// ID of the resource requirement to get
#[arg()]
id: i64,
},
/// Update existing resource requirements
Update {
/// ID of the resource requirement to update
#[arg()]
id: i64,
/// Name of the resource requirements
#[arg(short, long)]
name: Option<String>,
/// Number of CPUs required
#[arg(long)]
num_cpus: Option<i64>,
/// Number of GPUs required
#[arg(long)]
num_gpus: Option<i64>,
/// Number of nodes required
#[arg(long)]
num_nodes: Option<i64>,
/// Amount of memory required (e.g., "20g")
#[arg(long)]
memory: Option<String>,
/// Maximum runtime in ISO 8601 duration format (e.g., "PT1H", "PT30M")
#[arg(long)]
runtime: Option<String>,
},
/// Delete resource requirements
Delete {
/// ID of the resource requirement to remove
#[arg()]
id: i64,
},
}
pub fn handle_resource_requirements_commands(
config: &Configuration,
command: &ResourceRequirementsCommands,
format: &str,
) {
match command {
ResourceRequirementsCommands::Create {
workflow_id,
name,
num_cpus,
num_gpus,
num_nodes,
memory,
runtime,
} => {
let user_name = crate::client::commands::get_env_user_name();
let wf_id = workflow_id.unwrap_or_else(|| {
select_workflow_interactively(config, &user_name).unwrap_or_else(|e| {
eprintln!("Error selecting workflow: {}", e);
std::process::exit(1);
})
});
let mut req = models::ResourceRequirementsModel::new(wf_id, name.to_string());
req.num_cpus = *num_cpus;
req.num_gpus = *num_gpus;
req.num_nodes = *num_nodes;
req.memory = memory.clone();
req.runtime = runtime.clone();
match default_api::create_resource_requirements(config, req) {
Ok(created_req) => {
if print_if_json(format, &created_req, "resource requirements") {
// JSON was printed
} else {
println!("Successfully created resource requirements:");
println!(" ID: {}", created_req.id.unwrap_or(-1));
println!(" Workflow ID: {}", created_req.workflow_id);
println!(" Name: {}", created_req.name);
println!(" Number of CPUs: {}", created_req.num_cpus);
println!(" Number of GPUs: {}", created_req.num_gpus);
println!(" Number of nodes: {}", created_req.num_nodes);
println!(" Memory: {}", created_req.memory);
println!(" Runtime: {}", created_req.runtime);
}
}
Err(e) => {
print_error("creating resource requirements", &e);
std::process::exit(1);
}
}
}
ResourceRequirementsCommands::List {
workflow_id,
limit,
offset,
sort_by,
reverse_sort,
} => {
let user_name = get_env_user_name();
let selected_workflow_id = match workflow_id {
Some(id) => *id,
None => select_workflow_interactively(config, &user_name).unwrap(),
};
// Use pagination utility to get all resource requirements
let mut params = pagination::ResourceRequirementsListParams::new()
.with_offset(*offset)
.with_sort_by(sort_by.clone().unwrap_or_default())
.with_reverse_sort(*reverse_sort);
if let Some(limit_val) = limit {
params = params.with_limit(*limit_val);
}
match pagination::paginate_resource_requirements(
config,
selected_workflow_id as i64,
params,
) {
Ok(requirements) => {
if print_wrapped_if_json(
format,
"resource_requirements",
&requirements,
"resource requirements",
) {
// JSON was printed
} else if requirements.is_empty() {
println!(
"No resource requirements found for workflow ID: {}",
selected_workflow_id
);
} else {
println!(
"Resource requirements for workflow ID {}:",
selected_workflow_id
);
let rows: Vec<ResourceRequirementsTableRow> = requirements
.iter()
.map(|req| ResourceRequirementsTableRow {
id: req.id.unwrap_or(-1),
name: req.name.clone(),
cpus: req.num_cpus,
gpus: req.num_gpus,
nodes: req.num_nodes,
memory: req.memory.clone(),
runtime: req.runtime.clone(),
})
.collect();
display_table_with_count(&rows, "resource requirements");
}
}
Err(e) => {
print_error("listing resource requirements", &e);
std::process::exit(1);
}
}
}
ResourceRequirementsCommands::Get { id } => {
match default_api::get_resource_requirements(config, *id) {
Ok(req) => {
if print_if_json(format, &req, "resource requirements") {
// JSON was printed
} else {
println!("Resource requirements ID {}:", id);
println!(" Workflow ID: {}", req.workflow_id);
println!(" Name: {}", req.name);
println!(" Number of CPUs: {}", req.num_cpus);
println!(" Number of GPUs: {}", req.num_gpus);
println!(" Number of nodes: {}", req.num_nodes);
println!(" Memory: {}", req.memory);
println!(" Runtime: {}", req.runtime);
}
}
Err(e) => {
print_error("getting resource requirements", &e);
std::process::exit(1);
}
}
}
ResourceRequirementsCommands::Update {
id,
name,
num_cpus,
num_gpus,
num_nodes,
memory,
runtime,
} => {
// First get the existing resource requirements
match default_api::get_resource_requirements(config, *id) {
Ok(mut req) => {
// Update fields that were provided
if name.is_some() {
req.name = name.clone().unwrap();
}
if num_cpus.is_some() {
req.num_cpus = num_cpus.unwrap();
}
if num_gpus.is_some() {
req.num_gpus = num_gpus.unwrap();
}
if num_nodes.is_some() {
req.num_nodes = num_nodes.unwrap();
}
if memory.is_some() {
req.memory = memory.clone().unwrap();
}
if runtime.is_some() {
req.runtime = runtime.clone().unwrap();
}
match default_api::update_resource_requirements(config, *id, req) {
Ok(updated_req) => {
if print_if_json(format, &updated_req, "resource requirements") {
// JSON was printed
} else {
println!("Successfully updated resource requirements:");
println!(" ID: {}", updated_req.id.unwrap_or(-1));
println!(" Workflow ID: {}", updated_req.workflow_id);
println!(" Name: {}", updated_req.name);
println!(" Number of CPUs: {}", updated_req.num_cpus);
println!(" Number of GPUs: {}", updated_req.num_gpus);
println!(" Number of nodes: {}", updated_req.num_nodes);
println!(" Memory: {}", updated_req.memory);
println!(" Runtime: {}", updated_req.runtime);
}
}
Err(e) => {
print_error("updating resource requirements", &e);
std::process::exit(1);
}
}
}
Err(e) => {
print_error("getting resource requirements for update", &e);
std::process::exit(1);
}
}
}
ResourceRequirementsCommands::Delete { id } => {
match default_api::delete_resource_requirements(config, *id, None) {
Ok(removed_req) => {
if print_if_json(format, &removed_req, "resource requirements") {
// JSON was printed
} else {
println!("Successfully removed resource requirements:");
println!(" ID: {}", removed_req.id.unwrap_or(-1));
println!(" Workflow ID: {}", removed_req.workflow_id);
println!(" Name: {}", removed_req.name);
}
}
Err(e) => {
print_error("removing resource requirements", &e);
std::process::exit(1);
}
}
}
}
}