Skip to main content

greengrass_ipc/model/
lifecycle.rs

1//! Component lifecycle and update-management shapes.
2
3use serde::{Deserialize, Serialize};
4
5/// The lifecycle state of a component on the core device (`aws.greengrass#LifecycleState`).
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7pub enum LifecycleState {
8    /// The component is running.
9    #[serde(rename = "RUNNING")]
10    Running,
11    /// The component encountered an error.
12    #[serde(rename = "ERRORED")]
13    Errored,
14    /// The component is new.
15    #[serde(rename = "NEW")]
16    New,
17    /// The component finished running.
18    #[serde(rename = "FINISHED")]
19    Finished,
20    /// The component is installed.
21    #[serde(rename = "INSTALLED")]
22    Installed,
23    /// The component is broken.
24    #[serde(rename = "BROKEN")]
25    Broken,
26    /// The component is starting.
27    #[serde(rename = "STARTING")]
28    Starting,
29    /// The component is stopping.
30    #[serde(rename = "STOPPING")]
31    Stopping,
32}
33
34/// Request for `UpdateState`.
35#[derive(Debug, Clone, Serialize)]
36pub struct UpdateStateRequest {
37    /// The state to report.
38    pub state: LifecycleState,
39}
40
41/// Response for `UpdateState` (empty).
42#[derive(Debug, Clone, Default, Deserialize)]
43pub struct UpdateStateResponse {}
44
45/// Request for `SubscribeToComponentUpdates` (empty).
46#[derive(Debug, Clone, Default, Serialize)]
47pub struct SubscribeToComponentUpdatesRequest {}
48
49/// Response for `SubscribeToComponentUpdates` (empty subscription ack).
50#[derive(Debug, Clone, Default, Deserialize)]
51pub struct SubscribeToComponentUpdatesResponse {}
52
53/// An event indicating the nucleus wants to update a component (`aws.greengrass#PreComponentUpdateEvent`).
54#[derive(Debug, Clone, Deserialize)]
55pub struct PreComponentUpdateEvent {
56    /// The ID of the deployment that will update the component.
57    #[serde(rename = "deploymentId")]
58    pub deployment_id: String,
59    /// Whether the nucleus needs to restart to apply the update.
60    #[serde(rename = "isGgcRestarting", default)]
61    pub is_ggc_restarting: bool,
62}
63
64/// An event indicating the nucleus updated a component (`aws.greengrass#PostComponentUpdateEvent`).
65#[derive(Debug, Clone, Deserialize)]
66pub struct PostComponentUpdateEvent {
67    /// The ID of the deployment that updated the component.
68    #[serde(rename = "deploymentId")]
69    pub deployment_id: String,
70}
71
72/// A streamed component-update event (`aws.greengrass#ComponentUpdatePolicyEvents`, a tagged union).
73#[derive(Debug, Clone, Deserialize)]
74pub struct ComponentUpdatePolicyEvents {
75    /// Set when the nucleus wants to update the component.
76    #[serde(rename = "preUpdateEvent", default)]
77    pub pre_update_event: Option<PreComponentUpdateEvent>,
78    /// Set when the nucleus finished updating the component.
79    #[serde(rename = "postUpdateEvent", default)]
80    pub post_update_event: Option<PostComponentUpdateEvent>,
81}
82
83/// Request for `DeferComponentUpdate`.
84#[derive(Debug, Clone, Serialize)]
85pub struct DeferComponentUpdateRequest {
86    /// The ID of the deployment to defer.
87    #[serde(rename = "deploymentId")]
88    pub deployment_id: String,
89    /// Optional component name for which to defer updates (defaults to this component).
90    #[serde(rename = "message", skip_serializing_if = "Option::is_none")]
91    pub message: Option<String>,
92    /// Milliseconds to defer the update. `0` (or `None`) acknowledges/allows the update.
93    #[serde(rename = "recheckAfterMs", skip_serializing_if = "Option::is_none")]
94    pub recheck_after_ms: Option<i64>,
95}
96
97/// Response for `DeferComponentUpdate` (empty).
98#[derive(Debug, Clone, Default, Deserialize)]
99pub struct DeferComponentUpdateResponse {}
100
101/// Request for `PauseComponent`.
102#[derive(Debug, Clone, Serialize)]
103pub struct PauseComponentRequest {
104    /// The name of the component to pause.
105    #[serde(rename = "componentName")]
106    pub component_name: String,
107}
108
109/// Response for `PauseComponent` (empty).
110#[derive(Debug, Clone, Default, Deserialize)]
111pub struct PauseComponentResponse {}
112
113/// Request for `ResumeComponent`.
114#[derive(Debug, Clone, Serialize)]
115pub struct ResumeComponentRequest {
116    /// The name of the component to resume.
117    #[serde(rename = "componentName")]
118    pub component_name: String,
119}
120
121/// Response for `ResumeComponent` (empty).
122#[derive(Debug, Clone, Default, Deserialize)]
123pub struct ResumeComponentResponse {}
124
125/// Request for `RestartComponent`.
126#[derive(Debug, Clone, Serialize)]
127pub struct RestartComponentRequest {
128    /// The name of the component to restart.
129    #[serde(rename = "componentName")]
130    pub component_name: String,
131}
132
133/// Response for `RestartComponent`.
134#[derive(Debug, Clone, Deserialize)]
135pub struct RestartComponentResponse {
136    /// The status of the restart request (`SUCCEEDED` / `FAILED`).
137    #[serde(rename = "restartStatus", default)]
138    pub restart_status: Option<String>,
139    /// An optional message about the restart.
140    #[serde(rename = "message", default)]
141    pub message: Option<String>,
142}
143
144#[cfg(test)]
145mod tests {
146    use super::*;
147
148    #[test]
149    fn update_state_serializes_enum_as_screaming_case() {
150        let json = serde_json::to_value(UpdateStateRequest {
151            state: LifecycleState::Running,
152        })
153        .unwrap();
154        assert_eq!(json, serde_json::json!({ "state": "RUNNING" }));
155    }
156
157    #[test]
158    fn defer_omits_none_fields() {
159        let json = serde_json::to_value(DeferComponentUpdateRequest {
160            deployment_id: "d".into(),
161            message: None,
162            recheck_after_ms: Some(0),
163        })
164        .unwrap();
165        assert_eq!(
166            json,
167            serde_json::json!({ "deploymentId": "d", "recheckAfterMs": 0 })
168        );
169    }
170
171    #[test]
172    fn pre_update_event_deserializes() {
173        let ev: ComponentUpdatePolicyEvents = serde_json::from_value(serde_json::json!({
174            "preUpdateEvent": { "deploymentId": "abc", "isGgcRestarting": true }
175        }))
176        .unwrap();
177        let pre = ev.pre_update_event.unwrap();
178        assert_eq!(pre.deployment_id, "abc");
179        assert!(pre.is_ggc_restarting);
180    }
181}