Skip to main content

oasis_core_runtime/host/
volume_manager.rs

1use std::collections::BTreeMap;
2
3use async_trait::async_trait;
4
5use crate::protocol::Protocol;
6
7use super::{host_rpc_call, Error};
8
9/// Name of the local RPC endpoint for the volume manager.
10pub const LOCAL_RPC_ENDPOINT_VOLUME_MANAGER: &str = "volume-manager";
11
12/// Name of the VolumeAdd method.
13pub const METHOD_VOLUME_ADD: &str = "VolumeAdd";
14/// Name of the VolumeRemove method.
15pub const METHOD_VOLUME_REMOVE: &str = "VolumeRemove";
16/// Name of the VolumeList method.
17pub const METHOD_VOLUME_LIST: &str = "VolumeList";
18
19/// Volume manager interface.
20#[allow(clippy::double_must_use)]
21#[async_trait]
22pub trait VolumeManager: Send + Sync {
23    /// Request to host to add a volume.
24    ///
25    /// The `PermissionVolumeAdd` permission is required to call this method.
26    async fn volume_add(&self, args: VolumeAddRequest) -> Result<VolumeAddResponse, Error>;
27
28    /// Request to host to remove volumes.
29    ///
30    /// The `PermissionVolumeRemove` permission is required to call this method.
31    async fn volume_remove(&self, args: VolumeRemoveRequest)
32        -> Result<VolumeRemoveResponse, Error>;
33
34    /// Request to host to list volumes.
35    ///
36    /// The `PermissionVolumeAdd` permission is required to call this method.
37    async fn volume_list(&self, args: VolumeListRequest) -> Result<VolumeListResponse, Error>;
38}
39
40#[allow(clippy::double_must_use)]
41#[async_trait]
42impl VolumeManager for Protocol {
43    async fn volume_add(&self, args: VolumeAddRequest) -> Result<VolumeAddResponse, Error> {
44        host_rpc_call(
45            self,
46            LOCAL_RPC_ENDPOINT_VOLUME_MANAGER,
47            METHOD_VOLUME_ADD,
48            args,
49        )
50        .await
51    }
52
53    async fn volume_remove(
54        &self,
55        args: VolumeRemoveRequest,
56    ) -> Result<VolumeRemoveResponse, Error> {
57        host_rpc_call(
58            self,
59            LOCAL_RPC_ENDPOINT_VOLUME_MANAGER,
60            METHOD_VOLUME_REMOVE,
61            args,
62        )
63        .await
64    }
65
66    async fn volume_list(&self, args: VolumeListRequest) -> Result<VolumeListResponse, Error> {
67        host_rpc_call(
68            self,
69            LOCAL_RPC_ENDPOINT_VOLUME_MANAGER,
70            METHOD_VOLUME_LIST,
71            args,
72        )
73        .await
74    }
75}
76
77/// Request to add a volume.
78///
79/// The `PermissionVolumeAdd` permission is required to call this method.
80#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
81pub struct VolumeAddRequest {
82    /// Labels to tag the volume with so it can later be found.
83    pub labels: BTreeMap<String, String>,
84}
85
86/// Response from the VolumeAdd method.
87#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
88pub struct VolumeAddResponse {
89    /// Unique volume identifier.
90    pub id: String,
91}
92
93/// Request to remove volumes.
94///
95/// The `PermissionVolumeRemove` permission is required to call this method.
96#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
97pub struct VolumeRemoveRequest {
98    /// Labels to filter the volumes by.
99    pub labels: BTreeMap<String, String>,
100}
101
102/// Response from the VolumeRemove method.
103#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
104pub struct VolumeRemoveResponse {}
105
106// Request to list volumes.
107//
108// The `PermissionVolumeAdd` permission is required to call this method.
109#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
110pub struct VolumeListRequest {
111    /// Labels to filter the volumes by.
112    pub labels: BTreeMap<String, String>,
113}
114
115/// Response from the VolumeList method.
116#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
117pub struct VolumeListResponse {
118    #[cbor(optional)]
119    pub volumes: Vec<VolumeInfo>,
120}
121
122/// Volume information.
123#[derive(Clone, Debug, Default, cbor::Encode, cbor::Decode)]
124pub struct VolumeInfo {
125    /// Unique volume identifier.
126    pub id: String,
127    /// Labels assigned to this volume.
128    pub labels: BTreeMap<String, String>,
129}