1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use nanocl_error::http_client::HttpClientResult;
use nanocl_stubs::generic::GenericFilter;
use nanocl_stubs::resource::{
Resource, ResourcePartial, ResourceSpec, ResourceUpdate,
};
use super::http_client::NanocldClient;
impl NanocldClient {
/// ## Default path for resources
const RESOURCE_PATH: &'static str = "/resources";
/// List existing resources in the system.
///
/// ## Example
///
/// ```no_run,ignore
/// use nanocld_client::NanocldClient;
///
/// let client = NanocldClient::connect_to("http://localhost:8585", None);
/// let res = client.list_resource().await;
/// ```
///
pub async fn list_resource(
&self,
query: Option<&GenericFilter>,
) -> HttpClientResult<Vec<Resource>> {
let query = Self::convert_query(query)?;
let res = self.send_get(Self::RESOURCE_PATH, Some(query)).await?;
Self::res_json(res).await
}
/// Create a new resource from a partial resource in the system.
///
/// ## Example
///
/// ```no_run,ignore
/// use nanocld_client::NanocldClient;
/// use nanocl_stubs::resource::ResourceKind;
///
/// let client = NanocldClient::connect_to("http://localhost:8585", None);
/// let res = client.create_resource(&ResourcePartial {
/// name: "my-resource".into(),
/// kind: String::from("Custom")s,
/// // Your data
/// data: serde_json::json!({}),
/// }).await;
/// ```
pub async fn create_resource(
&self,
data: &ResourcePartial,
) -> HttpClientResult<Resource> {
let res = self
.send_post(Self::RESOURCE_PATH, Some(data), None::<String>)
.await?;
Self::res_json(res).await
}
/// Inspect an existing resource
///
/// ## Example
///
/// ```no_run,ignore
/// use nanocld_client::NanocldClient;
///
/// let client = NanocldClient::connect_to("http://localhost:8585", None);
/// let res = client.inspect_resource("my-resource").await;
/// ```
pub async fn inspect_resource(
&self,
key: &str,
) -> HttpClientResult<Resource> {
let res = self
.send_get(
&format!("{}/{key}/inspect", Self::RESOURCE_PATH),
None::<String>,
)
.await?;
Self::res_json(res).await
}
/// Update the new resource spec and add an history entry
///
/// ## Example
///
/// ```no_run,ignore
/// use nanocld_client::NanocldClient;
///
/// let client = NanocldClient::connect_to("http://localhost:8585", None);
/// let res = client.patch_resource("my-resource", serde_json::json!({})).await;
/// ```
pub async fn put_resource(
&self,
key: &str,
config: &ResourceUpdate,
) -> HttpClientResult<Resource> {
let res = self
.send_put(
&format!("{}/{key}", Self::RESOURCE_PATH),
Some(config),
None::<String>,
)
.await?;
Self::res_json(res).await
}
/// Delete an existing resource
///
/// ## Example
///
/// ```no_run,ignore
/// use nanocld_client::NanocldClient;
///
/// let client = NanocldClient::connect_to("http://localhost:8585", None);
/// let res = client.delete_resource("my-resource").await;
/// ```
pub async fn delete_resource(&self, key: &str) -> HttpClientResult<()> {
self
.send_delete(&format!("{}/{key}", Self::RESOURCE_PATH), None::<String>)
.await?;
Ok(())
}
/// List history of an existing resource
///
/// ## Example
///
/// ```no_run,ignore
/// use nanocld_client::NanocldClient;
///
/// let client = NanocldClient::connect_to("http://localhost:8585", None);
/// let res = client.list_history_resource("my-resource").await;
/// ```
pub async fn list_history_resource(
&self,
key: &str,
) -> HttpClientResult<Vec<ResourceSpec>> {
let res = self
.send_get(
&format!("{}/{key}/histories", Self::RESOURCE_PATH),
None::<String>,
)
.await?;
Self::res_json(res).await
}
/// Revert a resource to a previous version
///
/// ## Example
///
/// ```no_run,ignore
/// use nanocld_client::NanocldClient;
///
/// let client = NanocldClient::connect_to("http://localhost:8585", None);
/// let history = client.list_history_resource("my-resource").await.unwrap().first().unwrap();
/// let res = client.revert_resource("my-resource", history.key).await;
/// ```
pub async fn revert_resource(
&self,
name: &str,
key: &str,
) -> HttpClientResult<Resource> {
let res = self
.send_patch(
&format!("{}/{name}/histories/{key}/revert", Self::RESOURCE_PATH),
None::<String>,
None::<String>,
)
.await?;
Self::res_json(res).await
}
}