1use crate::allocation::{AllocationMetric, AllocationStub};
2use crate::deployment::Deployment;
3use crate::evaluation::Evaluation;
4use crate::option::{QueryOptions, WriteOptions};
5use crate::{ClientError, Nomad};
6
7use reqwest::Method;
8use serde::{Deserialize, Serialize};
9use std::collections::HashMap;
10
11pub const JOB_TYPE_SERVICE: &str = "service";
13pub const JOB_TYPE_BATCH: &str = "batch";
14pub const JOB_TYPE_SYSTEM: &str = "system";
15pub const JOB_TYPE_SYSBATCH: &str = "sysbatch";
16
17pub const JOB_DEFAULT_PRIORITY: i32 = 50;
19
20pub const JOB_DEFAULT_NAMESPACE: &str = "default";
22
23pub const JOB_DEFAULT_REGION: &str = "global";
25
26#[derive(Debug, Serialize, Deserialize)]
28#[serde(rename_all = "PascalCase")]
29pub struct Job {
30 pub region: Option<String>,
31 pub namespace: Option<String>,
32 #[serde(rename = "ID")]
33 pub id: Option<String>,
34 pub name: String,
35 #[serde(rename = "Type")]
36 pub type_: Option<String>,
37 pub priority: Option<i32>,
38 pub all_at_once: Option<bool>,
39 pub datacenters: Option<Vec<String>>,
40 pub node_pool: Option<String>,
41 pub constraints: Option<Vec<Constraint>>,
42 pub affinities: Option<Vec<Affinity>>,
43 pub task_groups: Vec<JobTaskGroup>,
44 pub update: Option<JobUpdateStrategy>,
45 pub multiregion: Option<JobMultiregion>,
46 pub spreads: Option<Vec<JobSpread>>,
47 pub periodic: Option<JobPeriodicConfig>,
48 pub parameterized_job: Option<JobParameterizedConfig>,
49 pub reschedule: Option<ReschedulePolicy>,
50 pub migrate: Option<JobMigrateStrategy>,
51 pub meta: Option<HashMap<String, String>>,
52 #[serde(rename = "UI")]
53 pub ui: Option<JobUIConfig>,
54
55 pub stop: Option<bool>,
58 pub parent_id: Option<String>,
59 pub dispatched: Option<bool>,
60 pub dispatch_idempotency_token: Option<String>,
61 pub payload: Option<Vec<u8>>,
62 pub consul_namespace: Option<String>,
63 pub vault_namespace: Option<String>,
64 pub nomad_token_id: Option<String>,
65 pub status: Option<String>,
66 pub status_description: Option<String>,
67 pub stable: Option<bool>,
68 pub version: Option<u64>,
69 pub submit_time: Option<i64>,
70 pub create_index: Option<u64>,
71 pub modify_index: Option<u64>,
72 pub job_modify_index: Option<u64>,
73 pub version_tag: Option<JobVersionTag>,
74}
75
76impl Job {
77 pub fn new(
78 name: String,
79 region: String,
80 job_type: String,
81 task_groups: Vec<JobTaskGroup>,
82 ) -> Self {
83 Self {
84 name,
85 region: Some(region),
86 type_: Some(job_type.to_string()),
87 task_groups,
88 ..Default::default()
89 }
90 }
91}
92
93impl Default for Job {
94 fn default() -> Self {
95 Self {
96 region: Some(JOB_DEFAULT_REGION.to_string()),
97 namespace: Some(JOB_DEFAULT_NAMESPACE.to_string()),
98 id: None,
99 name: "".to_string(),
100 type_: Some(JOB_TYPE_SERVICE.to_string()),
101 priority: Some(JOB_DEFAULT_PRIORITY),
102 all_at_once: None,
103 datacenters: None,
104 node_pool: None,
105 constraints: None,
106 affinities: None,
107 task_groups: vec![JobTaskGroup::default()],
108 update: None,
109 multiregion: None,
110 spreads: None,
111 periodic: None,
112 parameterized_job: None,
113 reschedule: None,
114 migrate: None,
115 meta: None,
116 ui: None,
117 stop: None,
118 parent_id: None,
119 dispatched: None,
120 dispatch_idempotency_token: None,
121 payload: None,
122 consul_namespace: None,
123 vault_namespace: None,
124 nomad_token_id: None,
125 status: None,
126 status_description: None,
127 stable: None,
128 version: None,
129 submit_time: None,
130 create_index: None,
131 modify_index: None,
132 job_modify_index: None,
133 version_tag: None,
134 }
135 }
136}
137
138#[derive(Debug, Deserialize)]
139#[serde(rename_all = "PascalCase")]
140pub struct JobStub {
141 #[serde(rename = "ID")]
142 pub id: String,
143 #[serde(rename = "ParentID")]
144 pub parent_id: Option<String>,
145 pub name: String,
146 pub namespace: String,
147 pub datacenters: Vec<String>,
148 #[serde(rename = "Type")]
149 pub type_: String,
150 pub priority: i32,
151 pub periodic: bool,
152 pub parameterized_job: bool,
153 pub stop: bool,
154 pub status: String,
155 pub status_description: String,
156 pub job_summary: Option<JobSummary>,
157 pub create_index: u64,
158 pub modify_index: u64,
159 pub job_modify_index: u64,
160 pub submit_time: i64,
161 pub meta: Option<HashMap<String, String>>,
162}
163
164#[derive(Debug, Clone, Deserialize)]
165#[serde(rename_all = "PascalCase")]
166pub struct JobSummary {
167 #[serde(rename = "JobID")]
168 pub job_id: String,
169 pub namespace: String,
170 pub summary: HashMap<String, JobTaskGroupSummary>,
171 pub children: Option<JobSummaryChildren>,
172 pub create_index: u64,
173 pub modify_index: u64,
174}
175
176#[derive(Debug, Clone, Deserialize)]
177#[serde(rename_all = "PascalCase")]
178pub struct JobSummaryChildren {
179 pub pending: i64,
180 pub running: i64,
181 pub dead: i64,
182}
183
184#[derive(Debug, Clone, Deserialize)]
185#[serde(rename_all = "PascalCase")]
186pub struct JobTaskGroupSummary {
187 pub queued: i32,
188 pub complete: i32,
189 pub failed: i32,
190 pub running: i32,
191 pub starting: i32,
192 pub lost: i32,
193 pub unknown: i32,
194}
195
196#[derive(Debug, Clone, Serialize, Deserialize)]
197#[serde(rename_all = "PascalCase")]
198pub struct JobUpdateStrategy {
199 pub stagger: Option<u64>,
200 pub max_parallel: Option<i32>,
201 pub health_check: Option<String>,
202 pub min_healthy_time: Option<u64>,
203 pub healthy_deadline: Option<u64>,
204 pub progress_deadline: Option<u64>,
205 pub canary: Option<i32>,
206 pub auto_revert: Option<bool>,
207 pub auto_promote: Option<bool>,
208}
209
210#[derive(Debug, Clone, Serialize, Deserialize)]
211#[serde(rename_all = "PascalCase")]
212pub struct JobMultiregion {
213 pub strategy: Option<JobMultiregionStrategy>,
214 pub regions: Option<Vec<JobMultiregionRegion>>,
215}
216
217#[derive(Debug, Clone, Serialize, Deserialize)]
218#[serde(rename_all = "PascalCase")]
219pub struct JobMultiregionStrategy {
220 pub max_parallel: Option<i32>,
221 pub on_failure: Option<String>,
222}
223
224#[derive(Debug, Clone, Serialize, Deserialize)]
225#[serde(rename_all = "PascalCase")]
226pub struct JobMultiregionRegion {
227 pub name: String,
228 pub count: Option<i32>,
229 pub datacenters: Option<Vec<String>>,
230 pub node_pool: Option<String>,
231 pub meta: Option<HashMap<String, String>>,
232}
233
234#[derive(Debug, Clone, Serialize, Deserialize)]
235#[serde(rename_all = "PascalCase")]
236pub struct JobPeriodicConfig {
237 pub enabled: Option<bool>,
238 pub spec: Option<String>,
239 pub specs: Option<Vec<String>>,
240 pub spec_type: Option<String>,
241 pub prohibit_overlap: Option<bool>,
242 pub time_zone: Option<String>,
243}
244
245#[derive(Debug, Clone, Serialize, Deserialize)]
246#[serde(rename_all = "PascalCase")]
247pub struct JobParameterizedConfig {
248 pub payload: Option<String>,
249 pub meta_required: Option<Vec<String>>,
250 pub meta_optional: Option<Vec<String>>,
251}
252
253#[derive(Debug, Clone, Serialize, Deserialize)]
254#[serde(rename_all = "PascalCase")]
255pub struct ReschedulePolicy {
256 pub attempts: Option<i32>,
257 pub interval: Option<u64>,
258 pub delay: Option<u64>,
259 pub delay_function: Option<String>,
260 pub max_delay: Option<u64>,
261 pub unlimited: Option<bool>,
262}
263
264#[derive(Debug, Clone, Serialize, Deserialize)]
265#[serde(rename_all = "PascalCase")]
266pub struct JobMigrateStrategy {
267 pub max_parallel: Option<i32>,
268 pub health_check: Option<String>,
269 pub min_healthy_time: Option<u64>,
270 pub healthy_deadline: Option<u64>,
271}
272
273#[derive(Debug, Clone, Serialize, Deserialize)]
274#[serde(rename_all = "PascalCase")]
275pub struct Constraint {
276 pub l_target: Option<String>,
277 pub r_target: Option<String>,
278 pub operand: Option<String>,
279}
280
281#[derive(Debug, Clone, Serialize, Deserialize)]
282#[serde(rename_all = "PascalCase")]
283pub struct Affinity {
284 pub l_target: Option<String>,
285 pub r_target: Option<String>,
286 pub operand: Option<String>,
287 pub weight: Option<i32>,
288}
289
290#[derive(Debug, Clone, Serialize, Deserialize)]
291#[serde(rename_all = "PascalCase")]
292pub struct JobSpread {
293 pub attribute: Option<String>,
294 pub weight: Option<i32>,
295 pub spread_target: Option<Vec<JobSpreadTarget>>,
296}
297
298#[derive(Debug, Clone, Serialize, Deserialize)]
299#[serde(rename_all = "PascalCase")]
300pub struct JobSpreadTarget {
301 pub value: String,
302 pub percent: u8,
303}
304
305#[derive(Debug, Default, Serialize, Deserialize)]
306#[serde(rename_all = "PascalCase")]
307pub struct JobTaskGroup {
308 pub name: String,
309 pub count: Option<i32>,
310 pub constraints: Option<Vec<Constraint>>,
311 pub affinities: Option<Vec<Affinity>>,
312 pub tasks: Vec<Task>,
313 pub spreads: Option<Vec<JobSpread>>,
314 pub volumes: Option<HashMap<String, VolumeRequest>>,
315 pub restart_policy: Option<RestartPolicy>,
316 pub reschedule_policy: Option<ReschedulePolicy>,
317 pub ephemeral_disk: Option<EphemeralDisk>,
318 pub update: Option<JobUpdateStrategy>,
319 pub migrate: Option<JobMigrateStrategy>,
320 pub networks: Option<Vec<NetworkResource>>,
321 pub meta: Option<HashMap<String, String>>,
322 pub services: Option<Vec<Service>>,
323 pub shutdown_delay: Option<u64>,
324 pub stop_after_client_disconnect: Option<u64>,
325 pub max_client_disconnect: Option<u64>,
326 pub scaling: Option<ScalingPolicy>,
327 pub consul_namespace: Option<String>,
328}
329
330impl JobTaskGroup {
331 pub fn new(name: String, tasks: Vec<Task>) -> Self {
332 Self {
333 name,
334 tasks,
335 count: None,
336 constraints: None,
337 affinities: None,
338 spreads: None,
339 volumes: None,
340 restart_policy: None,
341 reschedule_policy: None,
342 ephemeral_disk: None,
343 update: None,
344 migrate: None,
345 networks: None,
346 meta: None,
347 services: None,
348 shutdown_delay: None,
349 stop_after_client_disconnect: None,
350 max_client_disconnect: None,
351 scaling: None,
352 consul_namespace: None,
353 }
354 }
355}
356
357#[derive(Debug, Clone, Serialize, Deserialize)]
358#[serde(rename_all = "PascalCase")]
359pub struct Task {
360 pub name: String,
361 pub driver: String,
362 pub config: Option<HashMap<String, serde_json::Value>>,
363 pub constraints: Option<Vec<Constraint>>,
364 pub affinities: Option<Vec<Affinity>>,
365 pub env: Option<HashMap<String, String>>,
366 pub services: Option<Vec<Service>>,
367 pub resources: Option<TaskResources>,
368 pub meta: Option<HashMap<String, String>>,
369 pub kill_timeout: Option<u64>,
370 pub kill_signal: Option<String>,
371 pub leader: Option<bool>,
372 pub shutdown_delay: Option<u64>,
373 pub user: Option<String>,
374 pub lifecycle: Option<JobTaskLifecycle>,
375 pub templates: Option<Vec<TaskTemplate>>,
376 pub vault: Option<Vault>,
377 pub dispatch_payload: Option<DispatchPayloadConfig>,
378}
379
380impl Task {
381 pub fn new(name: String, driver: String) -> Self {
382 Task {
383 name,
384 driver,
385 config: None,
386 constraints: None,
387 affinities: None,
388 env: None,
389 services: None,
390 resources: None,
391 meta: None,
392 kill_timeout: None,
393 kill_signal: None,
394 leader: None,
395 shutdown_delay: None,
396 user: None,
397 lifecycle: None,
398 templates: None,
399 vault: None,
400 dispatch_payload: None,
401 }
402 }
403}
404
405#[derive(Debug, Clone, Serialize, Deserialize)]
406#[serde(rename_all = "PascalCase")]
407pub struct JobTaskLifecycle {
408 pub hook: String,
409 pub sidecar: bool,
410}
411
412#[derive(Debug, Clone, Serialize, Deserialize)]
413#[serde(rename_all = "PascalCase")]
414pub struct TaskTemplate {
415 pub source_path: Option<String>,
416 pub dest_path: Option<String>,
417 pub embedded_tmpl: Option<String>,
418 pub change_mode: Option<String>,
419 pub change_signal: Option<String>,
420 pub splay: Option<u64>,
421 pub perms: Option<String>,
422 pub left_delim: Option<String>,
423 pub right_delim: Option<String>,
424 pub envvars: Option<bool>,
425 pub vault_grace: Option<u64>,
426 pub wait: Option<TemplateWaitConfig>,
427}
428
429#[derive(Debug, Clone, Serialize, Deserialize)]
430#[serde(rename_all = "PascalCase")]
431pub struct TemplateWaitConfig {
432 pub min: Option<u64>,
433 pub max: Option<u64>,
434}
435
436#[derive(Debug, Clone, Serialize, Deserialize)]
437#[serde(rename_all = "PascalCase")]
438pub struct Vault {
439 pub policies: Option<Vec<String>>,
440 pub namespace: Option<String>,
441 pub env: Option<bool>,
442 pub change_mode: Option<String>,
443 pub change_signal: Option<String>,
444}
445
446#[derive(Debug, Clone, Serialize, Deserialize)]
447#[serde(rename_all = "PascalCase")]
448pub struct DispatchPayloadConfig {
449 pub file: String,
450}
451
452#[derive(Debug, Clone, Serialize, Deserialize)]
453#[serde(rename_all = "PascalCase")]
454pub struct TaskResources {
455 #[serde(rename = "CPU")]
456 pub cpu: Option<i64>,
457 pub cores: Option<i64>,
458 #[serde(rename = "Memory")]
459 pub memory_mb: Option<i64>,
460 #[serde(rename = "MemoryMax")]
461 pub memory_max_mb: Option<i64>,
462 #[serde(rename = "Disk")]
463 pub disk_mb: Option<i64>,
464 pub networks: Option<Vec<NetworkResource>>,
465 pub devices: Option<Vec<RequestedDevice>>,
466}
467
468#[derive(Debug, Clone, Serialize, Deserialize)]
469#[serde(rename_all = "PascalCase")]
470pub struct NetworkResource {
471 pub mode: Option<String>,
472 pub device: Option<String>,
473 #[serde(rename = "CIDR")]
474 pub cidr: Option<String>,
475 #[serde(rename = "IP")]
476 pub ip: Option<String>,
477 pub mbits: Option<i32>,
478 pub dns: Option<DNSConfig>,
479 pub reserved_ports: Option<Vec<Port>>,
480 pub dynamic_ports: Option<Vec<Port>>,
481}
482
483#[derive(Debug, Clone, Serialize, Deserialize)]
484#[serde(rename_all = "PascalCase")]
485pub struct DNSConfig {
486 pub servers: Option<Vec<String>>,
487 pub searches: Option<Vec<String>>,
488 pub options: Option<Vec<String>>,
489}
490
491#[derive(Debug, Clone, Serialize, Deserialize)]
492#[serde(rename_all = "PascalCase")]
493pub struct Port {
494 pub label: String,
495 pub value: Option<i32>,
496 pub to: Option<i32>,
497 pub host_network: Option<String>,
498}
499
500#[derive(Debug, Clone, Serialize, Deserialize)]
501#[serde(rename_all = "PascalCase")]
502pub struct RequestedDevice {
503 pub name: String,
504 pub count: Option<u64>,
505 pub constraints: Option<Vec<Constraint>>,
506 pub affinities: Option<Vec<Affinity>>,
507}
508
509#[derive(Debug, Clone, Serialize, Deserialize)]
510#[serde(rename_all = "PascalCase")]
511pub struct Service {
512 pub name: String,
513 pub tags: Option<Vec<String>>,
514 pub canary_tags: Option<Vec<String>>,
515 pub port_label: Option<String>,
516 pub address_mode: Option<String>,
517 pub checks: Option<Vec<ServiceCheck>>,
518 pub check_restart: Option<CheckRestart>,
519 pub connect: Option<ConsulConnect>,
520 pub meta: Option<HashMap<String, String>>,
521 pub canary_meta: Option<HashMap<String, String>>,
522 pub enable_tag_override: Option<bool>,
523 pub on_update: Option<String>,
524 pub provider: Option<String>,
525}
526
527#[derive(Debug, Clone, Serialize, Deserialize)]
528#[serde(rename_all = "PascalCase")]
529pub struct ServiceCheck {
530 pub name: Option<String>,
531 #[serde(rename = "Type")]
532 pub type_: String,
533 pub command: Option<String>,
534 pub args: Option<Vec<String>>,
535 pub path: Option<String>,
536 pub protocol: Option<String>,
537 pub port_label: Option<String>,
538 pub address_mode: Option<String>,
539 pub interval: Option<u64>,
540 pub timeout: Option<u64>,
541 pub initial_status: Option<String>,
542 pub tls_skip_verify: Option<bool>,
543 pub method: Option<String>,
544 pub header: Option<HashMap<String, Vec<String>>>,
545 pub check_restart: Option<CheckRestart>,
546 pub grpc_service: Option<String>,
547 pub grpc_use_tls: Option<bool>,
548 pub success_before_passing: Option<i32>,
549 pub failures_before_critical: Option<i32>,
550 pub body: Option<String>,
551}
552
553#[derive(Debug, Clone, Serialize, Deserialize)]
554#[serde(rename_all = "PascalCase")]
555pub struct CheckRestart {
556 pub limit: Option<i32>,
557 pub grace: Option<u64>,
558 pub ignore_warnings: Option<bool>,
559}
560
561#[derive(Debug, Clone, Serialize, Deserialize)]
562#[serde(rename_all = "PascalCase")]
563pub struct ConsulConnect {
564 pub native: Option<bool>,
565 pub gateway: Option<ConsulGateway>,
566 pub sidecar_service: Option<ConsulSidecarService>,
567 pub sidecar_task: Option<SidecarTask>,
568}
569
570#[derive(Debug, Clone, Serialize, Deserialize)]
571#[serde(rename_all = "PascalCase")]
572pub struct ConsulGateway {
573 pub proxy: Option<ConsulGatewayProxy>,
574 pub ingress: Option<ConsulIngressGateway>,
575 pub terminating: Option<ConsulTerminatingGateway>,
576 pub mesh: Option<ConsulMeshGateway>,
577}
578
579#[derive(Debug, Clone, Serialize, Deserialize)]
580#[serde(rename_all = "PascalCase")]
581pub struct ConsulGatewayProxy {
582 pub connect_timeout: Option<u64>,
583 pub envoy_gateway_bind_tagged_addresses: Option<bool>,
584 pub envoy_gateway_bind_addresses: Option<HashMap<String, ConsulGatewayBindAddress>>,
585 pub envoy_gateway_no_default_bind: Option<bool>,
586 pub config: Option<HashMap<String, serde_json::Value>>,
587}
588
589#[derive(Debug, Clone, Serialize, Deserialize)]
590#[serde(rename_all = "PascalCase")]
591pub struct ConsulGatewayBindAddress {
592 pub address: String,
593 pub port: i32,
594}
595
596#[derive(Debug, Clone, Serialize, Deserialize)]
597#[serde(rename_all = "PascalCase")]
598pub struct ConsulIngressGateway {
599 pub tls: Option<ConsulGatewayTLSConfig>,
600 pub listeners: Option<Vec<ConsulIngressListener>>,
601}
602
603#[derive(Debug, Clone, Serialize, Deserialize)]
604#[serde(rename_all = "PascalCase")]
605pub struct ConsulGatewayTLSConfig {
606 pub enabled: Option<bool>,
607}
608
609#[derive(Debug, Clone, Serialize, Deserialize)]
610#[serde(rename_all = "PascalCase")]
611pub struct ConsulIngressListener {
612 pub port: i32,
613 pub protocol: String,
614 pub services: Vec<ConsulIngressService>,
615}
616
617#[derive(Debug, Clone, Serialize, Deserialize)]
618#[serde(rename_all = "PascalCase")]
619pub struct ConsulIngressService {
620 pub name: String,
621 pub hosts: Option<Vec<String>>,
622}
623
624#[derive(Debug, Clone, Serialize, Deserialize)]
625#[serde(rename_all = "PascalCase")]
626pub struct ConsulTerminatingGateway {
627 pub services: Vec<ConsulLinkedService>,
628}
629
630#[derive(Debug, Clone, Serialize, Deserialize)]
631#[serde(rename_all = "PascalCase")]
632pub struct ConsulMeshGateway {
633 pub mode: String,
634}
635
636#[derive(Debug, Clone, Serialize, Deserialize)]
637#[serde(rename_all = "PascalCase")]
638pub struct ConsulLinkedService {
639 pub name: String,
640 #[serde(rename = "CAFile")]
641 pub ca_file: Option<String>,
642 pub cert_file: Option<String>,
643 pub key_file: Option<String>,
644 #[serde(rename = "SNI")]
645 pub sni: Option<String>,
646}
647
648#[derive(Debug, Clone, Serialize, Deserialize)]
649#[serde(rename_all = "PascalCase")]
650pub struct ConsulSidecarService {
651 pub port: Option<String>,
652 pub proxy: Option<ConsulProxy>,
653 pub tags: Option<Vec<String>>,
654}
655
656#[derive(Debug, Clone, Serialize, Deserialize)]
657#[serde(rename_all = "PascalCase")]
658pub struct ConsulProxy {
659 pub local_service_address: Option<String>,
660 pub local_service_port: Option<i32>,
661 pub config: Option<HashMap<String, serde_json::Value>>,
662 pub upstreams: Option<Vec<ConsulUpstream>>,
663}
664
665#[derive(Debug, Clone, Serialize, Deserialize)]
666#[serde(rename_all = "PascalCase")]
667pub struct ConsulUpstream {
668 pub destination_name: String,
669 pub local_bind_port: i32,
670 pub datacenter: Option<String>,
671}
672
673#[derive(Debug, Clone, Serialize, Deserialize)]
674#[serde(rename_all = "PascalCase")]
675pub struct SidecarTask {
676 pub name: Option<String>,
677 pub driver: Option<String>,
678 pub user: Option<String>,
679 pub config: Option<HashMap<String, serde_json::Value>>,
680 pub env: Option<HashMap<String, String>>,
681 pub resources: Option<TaskResources>,
682 pub meta: Option<HashMap<String, String>>,
683 pub kill_timeout: Option<u64>,
684 pub kill_signal: Option<String>,
685 pub shutdown_delay: Option<u64>,
686}
687
688#[derive(Debug, Clone, Serialize, Deserialize)]
689#[serde(rename_all = "PascalCase")]
690pub struct RestartPolicy {
691 pub attempts: Option<i32>,
692 pub interval: Option<u64>,
693 pub delay: Option<u64>,
694 pub mode: Option<String>,
695}
696
697#[derive(Debug, Clone, Serialize, Deserialize)]
698#[serde(rename_all = "PascalCase")]
699pub struct EphemeralDisk {
700 pub migrate: Option<bool>,
701 pub size_mb: Option<i32>,
702 pub sticky: Option<bool>,
703}
704
705#[derive(Debug, Clone, Serialize, Deserialize)]
706#[serde(rename_all = "PascalCase")]
707pub struct VolumeRequest {
708 pub name: String,
709 #[serde(rename = "Type")]
710 pub type_: String,
711 pub source: String,
712 pub read_only: Option<bool>,
713 pub mount_options: Option<VolumeMount>,
714}
715
716#[derive(Debug, Clone, Serialize, Deserialize)]
717#[serde(rename_all = "PascalCase")]
718pub struct VolumeMount {
719 pub fs_type: Option<String>,
720 pub mount_flags: Option<Vec<String>>,
721}
722
723#[derive(Debug, Clone, Serialize, Deserialize)]
724#[serde(rename_all = "PascalCase")]
725pub struct ScalingPolicy {
726 pub enabled: Option<bool>,
727 pub min: Option<i64>,
728 pub max: Option<i64>,
729 pub policy: Option<HashMap<String, serde_json::Value>>,
730}
731
732#[derive(Debug, Clone, Serialize, Deserialize)]
733#[serde(rename_all = "PascalCase")]
734pub struct JobUIConfig {
735 pub description: Option<String>,
736 pub links: Option<Vec<JobUILink>>,
737}
738
739#[derive(Debug, Clone, Serialize, Deserialize)]
740#[serde(rename_all = "PascalCase")]
741pub struct JobUILink {
742 pub label: String,
743 #[serde(rename = "URL")]
744 pub url: String,
745}
746
747#[derive(Debug, Clone, Serialize, Deserialize)]
748#[serde(rename_all = "PascalCase")]
749pub struct JobVersionTag {
750 pub name: String,
751 pub description: Option<String>,
752 pub tagged_time: i64,
753}
754
755#[derive(Debug, Clone, Serialize, Deserialize)]
756#[serde(rename_all = "PascalCase")]
757pub struct JobSubmission {
758 pub source: String,
759 pub format: String,
760 pub variable_flags: Option<HashMap<String, String>>,
761 pub variables: Option<String>,
762}
763
764#[derive(Debug, Default)]
765pub struct RegisterOptions {
766 pub enforce_index: bool,
767 pub modify_index: u64,
768 pub policy_override: bool,
769 pub preserve_counts: bool,
770 pub preserve_resources: bool,
771 pub eval_priority: i32,
772 pub submission: Option<JobSubmission>,
773}
774
775#[derive(Debug, Clone)]
776pub struct JobDeregisterRequest {
777 pub job_id: String,
778 pub purge: bool,
779 pub global: bool,
780 pub eval_priority: i32,
781 pub no_shutdown_delay: bool,
782}
783
784impl JobDeregisterRequest {
785 pub fn new(job_id: String) -> Self {
786 JobDeregisterRequest {
787 job_id,
788 purge: false,
789 global: false,
790 eval_priority: 0,
791 no_shutdown_delay: false,
792 }
793 }
794}
795
796#[derive(Debug, Clone, Serialize)]
797#[serde(rename_all = "PascalCase")]
798pub struct JobRegisterRequest<'a> {
799 pub job: &'a Job,
800 pub enforce_index: Option<bool>,
801 pub job_modify_index: Option<u64>,
802 pub policy_override: Option<bool>,
803 pub preserve_counts: Option<bool>,
804 pub preserve_resources: Option<bool>,
805 pub eval_priority: Option<i32>,
806 pub submission: Option<JobSubmission>,
807}
808
809impl<'a> JobRegisterRequest<'a> {
810 pub fn new(job: &'a Job) -> Self {
811 JobRegisterRequest {
812 job,
813 enforce_index: None,
814 job_modify_index: None,
815 policy_override: None,
816 preserve_counts: None,
817 preserve_resources: None,
818 eval_priority: None,
819 submission: None,
820 }
821 }
822}
823
824#[derive(Debug, Clone, Serialize, Deserialize)]
825#[serde(rename_all = "PascalCase")]
826pub struct JobRegisterResponse {
827 #[serde(rename = "EvalID")]
828 pub eval_id: String,
829 pub eval_create_index: u64,
830 pub job_modify_index: u64,
831 pub warnings: Option<String>,
832}
833
834#[derive(Debug, Clone, Serialize, Deserialize)]
835#[serde(rename_all = "PascalCase")]
836pub struct JobDeregisterResponse {
837 #[serde(rename = "EvalID")]
838 pub eval_id: String,
839 pub eval_create_index: u64,
840 pub job_modify_index: u64,
841}
842
843#[derive(Debug, Serialize)]
844#[serde(rename_all = "PascalCase")]
845pub struct JobValidateRequest<'a> {
846 pub job: &'a Job,
847}
848
849impl<'a> JobValidateRequest<'a> {
850 pub fn new(job: &'a Job) -> Self {
851 Self { job }
852 }
853}
854
855#[derive(Debug, Clone, Serialize, Deserialize)]
856#[serde(rename_all = "PascalCase")]
857pub struct JobValidateResponse {
858 pub driver_config_validated: bool,
859 pub validation_errors: Vec<String>,
860 pub error: Option<String>,
861 pub warnings: Option<String>,
862}
863
864#[derive(Debug, Clone, Serialize)]
865#[serde(rename_all = "PascalCase")]
866pub struct JobPlanRequest<'a> {
867 pub job: &'a Job,
868 pub diff: bool,
869 pub policy_override: bool,
870}
871
872#[derive(Debug, Clone, Deserialize)]
873#[serde(rename_all = "PascalCase")]
874pub struct JobPlanResponse {
875 pub job_modify_index: u64,
876 pub created_evals: Vec<Evaluation>,
877 pub diff: Option<JobDiff>,
878 pub annotations: Option<PlanAnnotations>,
879 pub failed_tg_allocs: Option<HashMap<String, AllocationMetric>>,
880 pub next_periodic_launch: Option<String>,
881 pub warnings: Option<String>,
882}
883
884#[derive(Debug, Clone, Serialize, Deserialize)]
885#[serde(rename_all = "PascalCase")]
886pub struct JobDiff {
887 #[serde(rename = "Type")]
888 pub type_: String,
889 #[serde(rename = "ID")]
890 pub id: String,
891 pub fields: Option<Vec<FieldDiff>>,
892 pub objects: Option<Vec<ObjectDiff>>,
893 pub task_groups: Option<Vec<TaskGroupDiff>>,
894}
895
896#[derive(Debug, Clone, Serialize, Deserialize)]
897#[serde(rename_all = "PascalCase")]
898pub struct TaskGroupDiff {
899 #[serde(rename = "Type")]
900 pub type_: String,
901 pub name: String,
902 pub fields: Option<Vec<FieldDiff>>,
903 pub objects: Option<Vec<ObjectDiff>>,
904 pub tasks: Option<Vec<TaskDiff>>,
905 pub updates: Option<HashMap<String, u64>>,
906}
907
908#[derive(Debug, Clone, Serialize, Deserialize)]
909#[serde(rename_all = "PascalCase")]
910pub struct TaskDiff {
911 #[serde(rename = "Type")]
912 pub type_: String,
913 pub name: String,
914 pub fields: Option<Vec<FieldDiff>>,
915 pub objects: Option<Vec<ObjectDiff>>,
916 pub annotations: Option<Vec<String>>,
917}
918
919#[derive(Debug, Clone, Serialize, Deserialize)]
920#[serde(rename_all = "PascalCase")]
921pub struct FieldDiff {
922 #[serde(rename = "Type")]
923 pub type_: String,
924 pub name: String,
925 pub old: String,
926 pub new: String,
927 pub annotations: Option<Vec<String>>,
928}
929
930#[derive(Debug, Clone, Serialize, Deserialize)]
931#[serde(rename_all = "PascalCase")]
932pub struct ObjectDiff {
933 #[serde(rename = "Type")]
934 pub type_: String,
935 pub name: String,
936 pub fields: Option<Vec<FieldDiff>>,
937 pub objects: Option<Vec<ObjectDiff>>,
938}
939
940#[derive(Debug, Clone, Deserialize)]
941#[serde(rename_all = "PascalCase")]
942pub struct PlanAnnotations {
943 pub desired_tg_updates: Option<HashMap<String, DesiredUpdates>>,
944 pub preempted_allocs: Option<Vec<AllocationStub>>,
945}
946
947#[derive(Debug, Clone, Serialize, Deserialize)]
948#[serde(rename_all = "PascalCase")]
949pub struct DesiredUpdates {
950 pub ignore: u64,
951 pub place: u64,
952 pub migrate: u64,
953 pub stop: u64,
954 pub in_place_update: u64,
955 pub destructive_update: u64,
956 pub canary: u64,
957 pub preemptions: u64,
958}
959
960#[derive(Debug, Serialize)]
961#[serde(rename_all = "PascalCase")]
962pub struct JobListDeploymentsRequest {
963 #[serde(rename = "JobID")]
964 job_id: String,
965 all: bool,
966}
967
968impl JobListDeploymentsRequest {
969 pub fn new(job_id: String, all: bool) -> Self {
970 Self { job_id, all }
971 }
972}
973
974#[derive(Debug, Clone, Serialize, Deserialize)]
975#[serde(rename_all = "PascalCase")]
976pub struct JobDispatchRequest {
977 #[serde(rename = "JobID")]
978 pub job_id: String,
979 pub payload: Option<Vec<u8>>,
980 pub meta: Option<HashMap<String, String>>,
981 pub id_prefix_template: Option<String>,
982 pub priority: Option<i32>,
983}
984
985impl JobDispatchRequest {
986 pub fn new(job_id: String) -> Self {
987 JobDispatchRequest {
988 job_id,
989 payload: None,
990 meta: None,
991 id_prefix_template: None,
992 priority: None,
993 }
994 }
995 pub fn with_payload(mut self, payload: Vec<u8>) -> Self {
996 self.payload = Some(payload);
997 self
998 }
999 pub fn with_meta(mut self, meta: HashMap<String, String>) -> Self {
1000 self.meta = Some(meta);
1001 self
1002 }
1003 pub fn with_id_prefix_template(mut self, id_prefix_template: String) -> Self {
1004 self.id_prefix_template = Some(id_prefix_template);
1005 self
1006 }
1007 pub fn with_priority(mut self, priority: i32) -> Self {
1008 self.priority = Some(priority);
1009 self
1010 }
1011}
1012
1013#[derive(Debug, Clone, Serialize, Deserialize)]
1014#[serde(rename_all = "PascalCase")]
1015pub struct JobDispatchResponse {
1016 pub dispatched_job_id: String,
1017 #[serde(rename = "EvalID")]
1018 pub eval_id: String,
1019 pub eval_create_index: u64,
1020 pub job_create_index: u64,
1021}
1022
1023#[derive(Debug, Serialize, Deserialize)]
1024#[serde(rename_all = "PascalCase")]
1025pub struct JobVersionsResponse {
1026 pub versions: Vec<Job>,
1027 pub diffs: Option<Vec<JobDiff>>,
1028}
1029
1030#[derive(Debug, Clone, Serialize, Deserialize)]
1031#[serde(rename_all = "PascalCase")]
1032pub struct JobRevertRequest {
1033 #[serde(rename = "JobID")]
1034 pub job_id: String,
1035 pub job_version: u64,
1036 pub enforce_prior_version: Option<u64>,
1037}
1038
1039#[derive(Debug, Clone, Serialize, Deserialize)]
1040#[serde(rename_all = "PascalCase")]
1041pub struct JobStabilityRequest {
1042 #[serde(rename = "JobID")]
1043 pub job_id: String,
1044 pub job_version: u64,
1045 pub stable: bool,
1046}
1047
1048#[derive(Debug, Clone, Serialize, Deserialize)]
1049#[serde(rename_all = "PascalCase")]
1050pub struct JobStabilityResponse {
1051 pub job_modify_index: u64,
1052}
1053
1054#[derive(Debug, Clone, Serialize, Deserialize)]
1055#[serde(rename_all = "PascalCase")]
1056pub struct JobEvaluationForceRequest {
1057 #[serde(rename = "JobID")]
1058 pub job_id: String,
1059 pub force_reschedule: bool,
1060 pub eval_options: Option<JobEvaluationForce>,
1061}
1062
1063#[derive(Debug, Clone, Serialize, Deserialize)]
1064#[serde(rename_all = "PascalCase")]
1065pub struct JobEvaluationForce {
1066 pub force_reschedule: bool,
1067}
1068
1069impl JobEvaluationForceRequest {
1070 pub fn new(job_id: String, force_reschedule: bool) -> Self {
1071 JobEvaluationForceRequest {
1072 job_id,
1073 force_reschedule,
1074 eval_options: None,
1075 }
1076 }
1077}
1078
1079#[derive(Debug, Clone, Serialize)]
1080#[serde(rename_all = "PascalCase")]
1081pub struct JobAllocationsListRequest {
1082 #[serde(rename = "JobID")]
1083 job_id: String,
1084 all_allocs: bool,
1085}
1086
1087impl JobAllocationsListRequest {
1088 pub fn new(job_id: String, all_allocs: bool) -> Self {
1089 Self { job_id, all_allocs }
1090 }
1091}
1092
1093#[derive(Debug, Clone, Serialize, Deserialize)]
1094#[serde(rename_all = "PascalCase")]
1095pub struct ScalingRequest {
1096 pub count: Option<i64>,
1097 pub target: HashMap<String, String>,
1098 pub error: Option<bool>,
1099 pub message: Option<String>,
1100 pub meta: Option<HashMap<String, serde_json::Value>>,
1101}
1102
1103#[derive(Debug, Clone, Serialize, Deserialize)]
1104#[serde(rename_all = "PascalCase")]
1105pub struct TaskGroupScaleStatus {
1106 pub desired: i32,
1107 pub placed: i32,
1108 pub running: i32,
1109 pub healthy: i32,
1110 pub unhealthy: i32,
1111 pub events: Option<Vec<ScalingEvent>>,
1112}
1113
1114#[derive(Debug, Clone, Serialize, Deserialize)]
1115#[serde(rename_all = "PascalCase")]
1116pub struct ScalingEvent {
1117 pub time: i64,
1118 pub count: Option<i64>,
1119 pub previous_count: i64,
1120 pub error: bool,
1121 pub message: Option<String>,
1122 pub meta: Option<HashMap<String, String>>,
1123 pub eval_id: Option<String>,
1124}
1125
1126#[derive(Debug, Default)]
1127pub struct JobsListRequest {
1128 pub meta: Option<bool>,
1129}
1130
1131pub struct Endpoint<'a> {
1132 client: &'a Nomad,
1133}
1134
1135impl<'a> Endpoint<'a> {
1136 pub fn new(client: &'a Nomad) -> Self {
1139 Self { client }
1140 }
1141
1142 pub async fn deregister(
1152 &self,
1153 job_deregister_request: &JobDeregisterRequest,
1154 opts: Option<WriteOptions>,
1155 ) -> Result<JobDeregisterResponse, ClientError> {
1156 let req = self.client.set_request_write_options(
1157 self.client.build_request(
1158 Method::DELETE,
1159 &format!(
1160 "/v1/job/{}?purge={}&global={}&eval_priority={}&no_shutdown_delay={}",
1161 job_deregister_request.job_id,
1162 job_deregister_request.purge,
1163 job_deregister_request.global,
1164 job_deregister_request.eval_priority,
1165 job_deregister_request.no_shutdown_delay
1166 ),
1167 ),
1168 &opts.unwrap_or_default(),
1169 );
1170
1171 self.client
1172 .send_with_response::<JobDeregisterResponse>(req)
1173 .await
1174 }
1175
1176 pub async fn dispatch(
1187 &self,
1188 job_dispatch_request: &JobDispatchRequest,
1189 opts: Option<WriteOptions>,
1190 ) -> Result<JobDispatchResponse, ClientError> {
1191 let req = self
1192 .client
1193 .set_request_write_options(
1194 self.client.build_request(
1195 Method::POST,
1196 &format!("/v1/job/{}/dispatch", job_dispatch_request.job_id),
1197 ),
1198 &opts.unwrap_or_default(),
1199 )
1200 .json(job_dispatch_request);
1201
1202 self.client
1203 .send_with_response::<JobDispatchResponse>(req)
1204 .await
1205 }
1206
1207 pub async fn force_evaluation(
1218 &self,
1219 job_evaluation_force_request: &JobEvaluationForceRequest,
1220 opts: Option<WriteOptions>,
1221 ) -> Result<JobRegisterResponse, ClientError> {
1222 let req = self
1223 .client
1224 .set_request_write_options(
1225 self.client.build_request(
1226 Method::POST,
1227 &format!("/v1/job/{}/evaluate", job_evaluation_force_request.job_id),
1228 ),
1229 &opts.unwrap_or_default(),
1230 )
1231 .json(job_evaluation_force_request);
1232
1233 self.client
1234 .send_with_response::<JobRegisterResponse>(req)
1235 .await
1236 }
1237
1238 pub async fn force_periodic(
1248 &self,
1249 job_id: &str,
1250 opts: Option<WriteOptions>,
1251 ) -> Result<JobRegisterResponse, ClientError> {
1252 let req = self.client.set_request_write_options(
1253 self.client
1254 .build_request(Method::POST, &format!("/v1/job/{}/periodic/force", job_id)),
1255 &opts.unwrap_or_default(),
1256 );
1257
1258 self.client
1259 .send_with_response::<JobRegisterResponse>(req)
1260 .await
1261 }
1262
1263 pub async fn get(&self, job_id: &str, opts: Option<QueryOptions>) -> Result<Job, ClientError> {
1273 let req = self.client.set_request_query_options(
1274 self.client
1275 .build_request(Method::GET, &format!("/v1/job/{}", job_id)),
1276 &opts.unwrap_or_default(),
1277 );
1278 self.client.send_with_response::<Job>(req).await
1279 }
1280
1281 pub async fn get_latest_deployment(
1290 &self,
1291 job_id: &str,
1292 opts: Option<QueryOptions>,
1293 ) -> Result<Option<Deployment>, ClientError> {
1294 let req = self.client.set_request_query_options(
1295 self.client
1296 .build_request(Method::GET, &format!("/v1/job/{}/deployment", job_id)),
1297 &opts.unwrap_or_default(),
1298 );
1299
1300 self.client
1301 .send_with_response::<Option<Deployment>>(req)
1302 .await
1303 }
1304
1305 pub async fn get_summary(
1315 &self,
1316 job_id: &str,
1317 opts: Option<QueryOptions>,
1318 ) -> Result<JobSummary, ClientError> {
1319 let req = self.client.set_request_query_options(
1320 self.client
1321 .build_request(Method::GET, &format!("/v1/job/{}/summary", job_id)),
1322 &opts.unwrap_or_default(),
1323 );
1324
1325 self.client.send_with_response::<JobSummary>(req).await
1326 }
1327
1328 pub async fn list(
1338 &self,
1339 jobs_list_request: Option<&JobsListRequest>,
1340 opts: Option<QueryOptions>,
1341 ) -> Result<Vec<JobStub>, ClientError> {
1342 let meta = match jobs_list_request {
1345 Some(req) => req.meta.unwrap_or(false),
1346 None => false,
1347 };
1348
1349 let req = self.client.set_request_query_options(
1350 self.client
1351 .build_request(Method::GET, &format!("/v1/jobs?meta={}", meta)),
1352 &opts.unwrap_or_default(),
1353 );
1354 let mut jobs = self.client.send_with_response::<Vec<JobStub>>(req).await?;
1355 jobs.sort_by(|a, b| a.id.cmp(&b.id));
1356 Ok(jobs)
1357 }
1358
1359 pub async fn list_allocations(
1371 &self,
1372 job_allocations_list_request: &JobAllocationsListRequest,
1373 opts: Option<QueryOptions>,
1374 ) -> Result<Vec<AllocationStub>, ClientError> {
1375 let req = self.client.set_request_query_options(
1376 self.client.build_request(
1377 Method::GET,
1378 &format!(
1379 "/v1/job/{}/allocations?all={}",
1380 job_allocations_list_request.job_id, job_allocations_list_request.all_allocs
1381 ),
1382 ),
1383 &opts.unwrap_or_default(),
1384 );
1385
1386 let mut allocs = self
1387 .client
1388 .send_with_response::<Vec<AllocationStub>>(req)
1389 .await?;
1390
1391 allocs.sort_by(|a, b| b.create_index.cmp(&a.create_index));
1392 Ok(allocs)
1393 }
1394
1395 pub async fn list_deployments(
1406 &self,
1407 job_list_deployments_request: &JobListDeploymentsRequest,
1408 opts: Option<QueryOptions>,
1409 ) -> Result<Vec<Deployment>, ClientError> {
1410 let req = self.client.set_request_query_options(
1411 self.client.build_request(
1412 Method::GET,
1413 &format!(
1414 "/v1/job/{}/deployments?all={}",
1415 job_list_deployments_request.job_id, job_list_deployments_request.all
1416 ),
1417 ),
1418 &opts.unwrap_or_default(),
1419 );
1420
1421 let mut deployments = self
1422 .client
1423 .send_with_response::<Vec<Deployment>>(req)
1424 .await?;
1425 deployments.sort_by(|a, b| b.create_index.cmp(&a.create_index));
1426 Ok(deployments)
1427 }
1428
1429 pub async fn list_evaluations(
1439 &self,
1440 job_id: &str,
1441 opts: Option<QueryOptions>,
1442 ) -> Result<Vec<Evaluation>, ClientError> {
1443 let req = self.client.set_request_query_options(
1444 self.client
1445 .build_request(Method::GET, &format!("/v1/job/{}/evaluations", job_id)),
1446 &opts.unwrap_or_default(),
1447 );
1448
1449 let mut evals = self
1450 .client
1451 .send_with_response::<Vec<Evaluation>>(req)
1452 .await?;
1453 evals.sort_by(|a, b| b.create_index.cmp(&a.create_index));
1454 Ok(evals)
1455 }
1456
1457 pub async fn plan(
1467 &self,
1468 job_plan_request: &JobPlanRequest<'_>,
1469 opts: Option<WriteOptions>,
1470 ) -> Result<JobPlanResponse, ClientError> {
1471 match job_plan_request.job.id {
1472 Some(_) => {}
1473 None => {
1474 return Err(ClientError::InvalidInputError(
1475 "Job ID must be set".to_string(),
1476 ));
1477 }
1478 }
1479
1480 let req = self
1481 .client
1482 .set_request_write_options(
1483 self.client.build_request(
1484 Method::POST,
1485 &format!("/v1/job/{}/plan", job_plan_request.job.id.as_ref().unwrap()),
1486 ),
1487 &opts.unwrap_or_default(),
1488 )
1489 .json(&job_plan_request);
1490
1491 self.client.send_with_response::<JobPlanResponse>(req).await
1492 }
1493
1494 pub async fn regsiter(
1505 &self,
1506 job_register_request: &JobRegisterRequest<'_>,
1507 opts: Option<WriteOptions>,
1508 ) -> Result<JobRegisterResponse, ClientError> {
1509 let req = self
1510 .client
1511 .set_request_write_options(
1512 self.client.build_request(Method::POST, "/v1/jobs"),
1513 &opts.unwrap_or_default(),
1514 )
1515 .json(&job_register_request);
1516
1517 self.client
1518 .send_with_response::<JobRegisterResponse>(req)
1519 .await
1520 }
1521
1522 pub async fn validate(
1532 &self,
1533 job_validate_request: &JobValidateRequest<'_>,
1534 opts: Option<WriteOptions>,
1535 ) -> Result<JobValidateResponse, ClientError> {
1536 let req = self
1537 .client
1538 .set_request_write_options(
1539 self.client.build_request(Method::POST, "/v1/validate/job"),
1540 &opts.unwrap_or_default(),
1541 )
1542 .json(&job_validate_request);
1543
1544 self.client
1545 .send_with_response::<JobValidateResponse>(req)
1546 .await
1547 }
1548}