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