Skip to main content

harmos_operations/lifecycle/
status.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10/* === Modules === */
11
12/* === Re-Exports === */
13
14/* === Imports === */
15
16#[cfg(feature = "runtime")]
17use harmos_sdk::operations::instances::InstanceAccess;
18
19#[cfg(feature = "runtime")]
20use crate::{Error, Operation, Response};
21use harmos_domain::operations::instance::Id;
22
23/* === Constants === */
24
25/* === Macros === */
26
27/* === Type Aliases === */
28
29/* === Structs === */
30
31#[harmos::operation(
32    harmos_path = crate,
33    resources = [InstanceAccess],
34    id = "host.operations.status",
35    response = super::InstanceState,
36    policy = super::POLICY,
37    visibility = Shared,
38    grpc(package = "harmos.host.v1", service = "Operations", method = "Status"),
39)]
40#[derive(Clone, Default, PartialEq)]
41pub struct Status {
42    pub instance: u64,
43}
44
45/* === Enums === */
46
47/* === Traits === */
48
49/* === Implementations === */
50
51impl Status {
52    pub fn of(instance: Id) -> Self {
53        Self { instance: instance.value() }
54    }
55}
56
57#[cfg(feature = "runtime")]
58impl Operation for Status {
59    type Error = Error;
60
61    async fn run(self, context: harmos_sdk::Context<'_, Self>) -> Result<Response<super::InstanceState>, Error> {
62        let access = context.resource::<harmos_sdk::operations::instances::InstanceAccess>();
63
64        let instance = access.0.owned(Id::new(self.instance)).ok_or(Error::UnknownInstance(self.instance))?;
65        Ok(super::InstanceState::from(instance.status()).into())
66    }
67}
68
69#[harmos::message(harmos_path = crate)]
70#[derive(Clone, Default, PartialEq)]
71pub struct InstanceState {
72    pub phase: String,
73    pub outcome: String,
74    pub cancellation_requested: bool,
75    pub attempt: u32,
76}
77impl From<harmos_domain::operations::instance::Status> for InstanceState {
78    fn from(status: harmos_domain::operations::instance::Status) -> Self {
79        Self {
80            phase: status.phase.as_str().to_owned(),
81            outcome: status.outcome.map(harmos_domain::operations::instance::Outcome::as_str).unwrap_or_default().to_owned(),
82            cancellation_requested: status.cancellation_requested,
83            attempt: status.attempt,
84        }
85    }
86}
87
88impl TryFrom<&InstanceState> for harmos_domain::operations::instance::Status {
89    type Error = harmos_domain::operations::instance::ParseError;
90
91    fn try_from(state: &InstanceState) -> Result<Self, Self::Error> {
92        let phase = state.phase.parse::<harmos_domain::operations::instance::Phase>()?;
93        let outcome = match state.outcome.as_str() {
94            "" => None,
95            outcome => Some(outcome.parse::<harmos_domain::operations::instance::Outcome>()?),
96        };
97        Ok(Self {
98            phase,
99            outcome,
100            cancellation_requested: state.cancellation_requested,
101            attempt: state.attempt,
102        })
103    }
104}
105
106/* === Public Functions === */
107
108/* === Private Functions === */
109
110/* === Tests === */