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#[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.cancel",
35    response = super::InstanceState,
36    policy = super::POLICY,
37    visibility = Shared,
38    grpc(package = "harmos.host.v1", service = "Operations", method = "Cancel"),
39)]
40#[derive(Clone, Default, PartialEq)]
41pub struct Cancel {
42    pub instance: u64,
43}
44
45/* === Enums === */
46
47/* === Traits === */
48
49/* === Implementations === */
50
51impl Cancel {
52    pub fn of(instance: Id) -> Self {
53        Self { instance: instance.value() }
54    }
55}
56
57#[cfg(feature = "runtime")]
58impl Operation for Cancel {
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        instance.cancel();
66        Ok(super::InstanceState::from(instance.status()).into())
67    }
68}
69
70/* === Public Functions === */
71
72/* === Private Functions === */
73
74/* === Tests === */