summaryrefslogtreecommitdiff
path: root/makima/src/daemon/api/directive.rs
blob: fbd27fe261498589a43a610748e5bafd0d04f7e5 (plain) (blame)
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
//! Directive API methods.

use serde::Serialize;
use uuid::Uuid;

use super::client::{ApiClient, ApiError};
use super::supervisor::JsonValue;

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateStepRequest {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub task_plan: Option<String>,
    pub depends_on: Vec<Uuid>,
    pub order_index: i32,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateGoalRequest {
    pub goal: String,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateStepDepsRequest {
    pub depends_on: Vec<Uuid>,
}

impl ApiClient {
    /// List all directives.
    pub async fn list_directives(&self) -> Result<JsonValue, ApiError> {
        self.get("/api/v1/directives").await
    }

    /// Get a directive with its steps.
    pub async fn get_directive(&self, directive_id: Uuid) -> Result<JsonValue, ApiError> {
        self.get(&format!("/api/v1/directives/{}", directive_id)).await
    }

    /// Add a step to a directive.
    pub async fn directive_add_step(
        &self,
        directive_id: Uuid,
        req: CreateStepRequest,
    ) -> Result<JsonValue, ApiError> {
        self.post(&format!("/api/v1/directives/{}/steps", directive_id), &req).await
    }

    /// Remove a step from a directive.
    pub async fn directive_remove_step(
        &self,
        directive_id: Uuid,
        step_id: Uuid,
    ) -> Result<(), ApiError> {
        self.delete(&format!("/api/v1/directives/{}/steps/{}", directive_id, step_id)).await
    }

    /// Set dependencies for a step.
    pub async fn directive_set_deps(
        &self,
        directive_id: Uuid,
        step_id: Uuid,
        depends_on: Vec<Uuid>,
    ) -> Result<JsonValue, ApiError> {
        let req = UpdateStepDepsRequest { depends_on };
        self.put(&format!("/api/v1/directives/{}/steps/{}", directive_id, step_id), &req).await
    }

    /// Start a directive.
    pub async fn directive_start(&self, directive_id: Uuid) -> Result<JsonValue, ApiError> {
        self.post_empty(&format!("/api/v1/directives/{}/start", directive_id)).await
    }

    /// Pause a directive.
    pub async fn directive_pause(&self, directive_id: Uuid) -> Result<JsonValue, ApiError> {
        self.post_empty(&format!("/api/v1/directives/{}/pause", directive_id)).await
    }

    /// Advance the directive DAG.
    pub async fn directive_advance(&self, directive_id: Uuid) -> Result<JsonValue, ApiError> {
        self.post_empty(&format!("/api/v1/directives/{}/advance", directive_id)).await
    }

    /// Mark a step as completed.
    pub async fn directive_complete_step(
        &self,
        directive_id: Uuid,
        step_id: Uuid,
    ) -> Result<JsonValue, ApiError> {
        self.post_empty(&format!("/api/v1/directives/{}/steps/{}/complete", directive_id, step_id)).await
    }

    /// Mark a step as failed.
    pub async fn directive_fail_step(
        &self,
        directive_id: Uuid,
        step_id: Uuid,
    ) -> Result<JsonValue, ApiError> {
        self.post_empty(&format!("/api/v1/directives/{}/steps/{}/fail", directive_id, step_id)).await
    }

    /// Mark a step as skipped.
    pub async fn directive_skip_step(
        &self,
        directive_id: Uuid,
        step_id: Uuid,
    ) -> Result<JsonValue, ApiError> {
        self.post_empty(&format!("/api/v1/directives/{}/steps/{}/skip", directive_id, step_id)).await
    }

    /// Update the directive's goal.
    pub async fn directive_update_goal(
        &self,
        directive_id: Uuid,
        goal: &str,
    ) -> Result<JsonValue, ApiError> {
        let req = UpdateGoalRequest { goal: goal.to_string() };
        self.put(&format!("/api/v1/directives/{}/goal", directive_id), &req).await
    }
}