Skip to main content

harmos_operations/lifecycle/
cancel.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// Standard Library Imports
17
18// Third Party Imports
19
20// Local Imports
21#[cfg(feature = "runtime")]
22use harmos_sdk::operations::instances::InstanceAccess;
23
24#[cfg(feature = "runtime")]
25use crate::{Error, Operation, Response};
26use harmos_domain::operations::instance::Id;
27
28/* === Constants === */
29
30/* === Macros === */
31
32/* === Type Aliases === */
33
34/* === Structs === */
35
36#[harmos::operation(
37    harmos_path = crate,
38    resources = [InstanceAccess],
39    id = "host.operations.cancel",
40    response = super::InstanceState,
41    policy = super::POLICY,
42    visibility = Shared,
43    grpc(package = "harmos.host.v1", service = "Operations", method = "Cancel"),
44)]
45#[derive(Clone, Default, PartialEq)]
46pub struct Cancel {
47    pub instance: u64,
48}
49
50/* === Enums === */
51
52/* === Traits === */
53
54/* === Implementations === */
55
56impl Cancel {
57    pub fn of(instance: Id) -> Self {
58        Self { instance: instance.value() }
59    }
60}
61
62#[cfg(feature = "runtime")]
63impl Operation for Cancel {
64    type Error = Error;
65
66    async fn run(self, context: harmos_sdk::Context<'_, Self>) -> Result<Response<super::InstanceState>, Error> {
67        let access = context.resource::<harmos_sdk::operations::instances::InstanceAccess>();
68
69        let instance = access.0.owned(Id::new(self.instance)).ok_or(Error::UnknownInstance(self.instance))?;
70        instance.cancel();
71        Ok(super::InstanceState::from(instance.status()).into())
72    }
73}
74
75/* === Public Functions === */
76
77/* === Private Functions === */
78
79/* === Tests === */