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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
use std::convert::{TryFrom, TryInto};
use std::fmt::Display;
use fluvio_future::net::DomainConnector;
use tracing::{debug, instrument};
use dataplane::core::Encoder;
use dataplane::core::Decoder;
use fluvio_sc_schema::objects::{Metadata, AllCreatableSpec};
use fluvio_sc_schema::AdminRequest;
use fluvio_socket::SocketError;
use fluvio_socket::MultiplexerSocket;
use crate::sockets::{ClientConfig, VersionedSerialSocket, SerialFrame};
use crate::{FluvioError, FluvioConfig};
use crate::metadata::objects::{ListResponse, ListSpec, DeleteSpec, CreateRequest};
use crate::config::ConfigFile;
use crate::sync::MetadataStores;
pub struct FluvioAdmin {
socket: VersionedSerialSocket,
#[allow(dead_code)]
metadata: MetadataStores,
}
impl FluvioAdmin {
pub(crate) fn new(socket: VersionedSerialSocket, metadata: MetadataStores) -> Self {
Self { socket, metadata }
}
#[instrument]
pub async fn connect() -> Result<Self, FluvioError> {
let config_file = ConfigFile::load_default_or_new()?;
let cluster_config = config_file.config().current_cluster()?;
Self::connect_with_config(cluster_config).await
}
#[instrument(skip(config))]
pub async fn connect_with_config(config: &FluvioConfig) -> Result<Self, FluvioError> {
use fluvio_sc_schema::objects::WatchRequest;
use fluvio_protocol::api::Request;
let connector = DomainConnector::try_from(config.tls.clone())?;
let config = ClientConfig::new(&config.endpoint, connector, config.use_spu_local_address);
let inner_client = config.connect().await?;
debug!(addr = %inner_client.config().addr(), "connected to cluster");
let (socket, config, versions) = inner_client.split();
if let Some(watch_version) = versions.lookup_version(WatchRequest::API_KEY) {
let socket = MultiplexerSocket::shared(socket);
let metadata = MetadataStores::start(socket.clone(), watch_version).await?;
let versioned_socket = VersionedSerialSocket::new(socket, config, versions);
Ok(Self {
socket: versioned_socket,
metadata,
})
} else {
Err(FluvioError::Other("WatchApi versio not found".to_string()))
}
}
#[instrument(skip(self, request))]
async fn send_receive<R>(&self, request: R) -> Result<R::Response, SocketError>
where
R: AdminRequest + Send + Sync,
{
self.socket.send_receive(request).await
}
#[instrument(skip(self, name, dry_run, spec))]
pub async fn create<S>(&self, name: String, dry_run: bool, spec: S) -> Result<(), FluvioError>
where
S: Into<AllCreatableSpec>,
{
let create_request = CreateRequest {
name,
dry_run,
spec: spec.into(),
};
self.send_receive(create_request).await?.as_result()?;
Ok(())
}
#[instrument(skip(self, key))]
pub async fn delete<S, K>(&self, key: K) -> Result<(), FluvioError>
where
S: DeleteSpec,
K: Into<S::DeleteKey>,
{
let delete_request = S::into_request(key);
self.send_receive(delete_request).await?.as_result()?;
Ok(())
}
#[instrument(skip(self, filters))]
pub async fn list<S, F>(&self, filters: F) -> Result<Vec<Metadata<S>>, FluvioError>
where
S: ListSpec + Encoder + Decoder,
S::Status: Encoder + Decoder,
F: Into<Vec<S::Filter>>,
ListResponse: TryInto<Vec<Metadata<S>>>,
<ListResponse as TryInto<Vec<Metadata<S>>>>::Error: Display,
{
use std::io::Error;
use std::io::ErrorKind;
let list_request = S::into_list_request(filters.into());
let response = self.send_receive(list_request).await?;
response
.try_into()
.map_err(|err| Error::new(ErrorKind::Other, format!("can't convert: {}", err)).into())
}
}
#[cfg(feature = "unstable")]
mod unstable {
use super::*;
use futures_util::Stream;
use crate::sync::AlwaysNewContext;
use crate::metadata::topic::TopicSpec;
use crate::metadata::partition::PartitionSpec;
use crate::metadata::spu::SpuSpec;
use crate::metadata::store::MetadataChanges;
impl FluvioAdmin {
pub fn watch_topics(
&self,
) -> impl Stream<Item = MetadataChanges<TopicSpec, AlwaysNewContext>> {
self.metadata.topics().watch()
}
pub fn watch_partitions(
&self,
) -> impl Stream<Item = MetadataChanges<PartitionSpec, AlwaysNewContext>> {
self.metadata.partitions().watch()
}
pub fn watch_spus(&self) -> impl Stream<Item = MetadataChanges<SpuSpec, AlwaysNewContext>> {
self.metadata.spus().watch()
}
}
}