forked from reductstore/reduct-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.h
More file actions
298 lines (248 loc) · 9.68 KB
/
Copy pathclient.h
File metadata and controls
298 lines (248 loc) · 9.68 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2022-2024 ReductSoftware UG
#ifndef REDUCT_CPP_CLIENT_H
#define REDUCT_CPP_CLIENT_H
#include <chrono>
#include <memory>
#include <optional>
#include <ostream>
#include <string>
#include <vector>
#include "diagnostics.h"
#include "reduct/bucket.h"
#include "reduct/error.h"
#include "reduct/http_options.h"
#include "reduct/result.h"
namespace reduct {
/**
* Interface for Reduct Storage HTTP Client
*/
class IClient {
public:
using Time = std::chrono::time_point<std::chrono::system_clock>;
/**
* Reduct Storage Information
*/
struct ServerInfo {
std::string version; // version of storage
size_t bucket_count; // number of buckets
size_t usage; // disk usage in bytes
std::chrono::seconds uptime; // server uptime
Time oldest_record;
Time latest_record;
/**
* @brief License information
*/
struct License {
std::string licensee; // Licensee usually is the company name
std::string invoice; // Invoice number
Time expiry_date; // Expiry date of the license in ISO 8601 format (UTC)
std::string plan; // Plan name
uint64_t device_number; // Number of devices (0 for unlimited)
uint64_t disk_quota; // Disk quota in TB (0 for unlimited)
std::string fingerprint; // License fingerprint
auto operator<=>(const License&) const = default;
};
std::optional<License> license; // License information. If empty, then it is BUSL-1.1
struct Defaults {
IBucket::Settings bucket; // default settings for a new bucket
auto operator<=>(const IClient::ServerInfo::Defaults&) const = default;
};
Defaults defaults;
auto operator<=>(const IClient::ServerInfo&) const = default;
};
/**
* @brief Get information about the server
* @return
*/
virtual Result<ServerInfo> GetInfo() const noexcept = 0;
/**
* @brief Get list of buckets with stats
* @return the list or an error
*/
virtual Result<std::vector<IBucket::BucketInfo>> GetBucketList() const noexcept = 0;
/**
* Get an existing bucket
* @param name name of bucket
* @return pointer to bucket or an error
*/
virtual UPtrResult<IBucket> GetBucket(std::string_view name) const noexcept = 0;
/**
* Create a new bucket
* @param name unique name for bucket
* @param settings optional settings
* @return pointer to created bucket
*/
virtual UPtrResult<IBucket> CreateBucket(std::string_view name, IBucket::Settings settings = {}) const noexcept = 0;
/**
* Get an existing bucket or create a new bucket
* @param name unique name for bucket
* @param settings optional settings
* @return pointer to created bucket
*/
virtual UPtrResult<IBucket> GetOrCreateBucket(std::string_view name,
IBucket::Settings settings = {}) const noexcept = 0;
/**
* API Token for authentication
*/
struct Token {
std::string name; // name of token
Time created_at; // creation time
bool is_provisioned; // true if token is provisioned, you can't remove it or change its permissions
std::optional<Time> expires_at; // absolute expiry timestamp (UTC)
std::optional<uint64_t> ttl; // inactivity TTL in seconds
std::optional<Time> last_access; // last access timestamp
std::vector<std::string> ip_allowlist; // allowed source IP addresses
bool is_expired = false; // token cannot be used anymore
auto operator<=>(const IClient::Token&) const = default;
};
/**
* Token permissions
*/
struct Permissions {
bool full_access = false; // full access to manage tokens and buckets
std::vector<std::string> read; // list of buckets with read access
std::vector<std::string> write; // list of buckets with write access
auto operator<=>(const IClient::Permissions&) const = default;
};
/**
* Token with permissions
*/
struct FullTokenInfo {
std::string name; // name of token
Time created_at; // creation time
bool is_provisioned; // true if token is provisioned, you can't remove it or change its permissions
std::optional<Time> expires_at; // absolute expiry timestamp (UTC)
std::optional<uint64_t> ttl; // inactivity TTL in seconds
std::optional<Time> last_access; // last access timestamp
std::vector<std::string> ip_allowlist; // allowed source IP addresses
bool is_expired = false; // token cannot be used anymore
Permissions permissions;
auto operator<=>(const IClient::FullTokenInfo&) const = default;
};
struct TokenCreateRequest {
Permissions permissions;
std::optional<Time> expires_at;
std::optional<uint64_t> ttl;
std::vector<std::string> ip_allowlist;
auto operator<=>(const IClient::TokenCreateRequest&) const = default;
};
/**
* @brief Get list of tokens
* @return the list or an error
*/
[[nodiscard]] virtual Result<std::vector<Token>> GetTokenList() const noexcept = 0;
/**
* @brief Get information about token
* @param name name of token
* @return token info or an error
*/
[[nodiscard]] virtual Result<FullTokenInfo> GetToken(std::string_view name) const noexcept = 0;
/**
* @brief Create a new token
* @param name name of token
* @param permissions permissions for token
* @return token value or an error
*/
[[nodiscard]] virtual Result<std::string> CreateToken(std::string_view name,
TokenCreateRequest request) const noexcept = 0;
[[deprecated("Use CreateToken(name, TokenCreateRequest) to set ttl/expires_at/ip_allowlist")]]
[[nodiscard]] virtual Result<std::string> CreateToken(std::string_view name,
Permissions permissions) const noexcept = 0;
/**
* @brief Update token permissions
* @param name name of token
* @param permissions permissions for token
* @return an error
*/
virtual Error RemoveToken(std::string_view name) const noexcept = 0;
[[nodiscard]] virtual Result<std::string> RotateToken(std::string_view name) const noexcept = 0;
/**
* @brief Returns information about the currently authenticated token.
*
* Makes a GET request to the '/me' endpoint using the client object stored as a member variable.
*
* @return A Result object containing a FullTokenInfo object or an Error object.
*/
[[nodiscard]] virtual Result<FullTokenInfo> Me() const noexcept = 0;
/**
* Replication information
*/
enum class ReplicationMode { kEnabled, kPaused, kDisabled };
struct ReplicationInfo {
std::string name; // Replication name
ReplicationMode mode = ReplicationMode::kEnabled; // Replication mode
bool is_active; // Remote instance is available and replication is active
bool is_provisioned; // Replication settings
uint64_t pending_records; // Number of records pending replication
auto operator<=>(const ReplicationInfo&) const = default;
};
/**
* Replication settings
*/
struct ReplicationSettings {
std::string src_bucket; // Source bucket
std::string dst_bucket; // Destination bucket
std::string dst_host; // Destination host URL (e.g. https://reductstore.com)
std::optional<std::string> dst_token; // Destination access token
std::vector<std::string>
entries; // Entries to replicate. If empty, all entries are replicated. Wildcards are supported.
std::optional<std::string> when; // Replication condition
ReplicationMode mode = ReplicationMode::kEnabled; // Replication mode
auto operator<=>(const ReplicationSettings&) const = default;
};
/**
* Replication full info with settings and diagnostics
*/
struct FullReplicationInfo {
ReplicationInfo info; // Replication info
ReplicationSettings settings; // Replication settings
Diagnostics diagnostics; // Diagnostics
bool operator==(const FullReplicationInfo&) const = default;
};
/**
* @brief Get list of replications
* @return the list or an error
*/
[[nodiscard]] virtual Result<std::vector<ReplicationInfo>> GetReplicationList() const noexcept = 0;
/**
* @brief Get replication info with settings and diagnostics
* @param name name of replication
* @return the info or an error
*/
[[nodiscard]] virtual Result<FullReplicationInfo> GetReplication(std::string_view name) const noexcept = 0;
/**
* @brief Create a new replication
* @param name name of replication
* @param settings replication settings
* @return error
*/
[[nodiscard]] virtual Error CreateReplication(std::string_view name, ReplicationSettings settings) const noexcept = 0;
/**
* @brief Update replication settings
* @param name name of replication
* @param settings replication settings
* @return error
*/
[[nodiscard]] virtual Error UpdateReplication(std::string_view name, ReplicationSettings settings) const noexcept = 0;
/**
* @brief Update replication mode without changing settings
* @param name name of replication
* @param mode replication mode
* @return error
*/
[[nodiscard]] virtual Error SetReplicationMode(std::string_view name, ReplicationMode mode) const noexcept = 0;
/**
* @brief Remove replication
* @param name name of replication
* @return error
*/
[[nodiscard]] virtual Error RemoveReplication(std::string_view name) const noexcept = 0;
/**
* @brief Build a client
* @param url URL of React Storage
* @return
*/
static std::unique_ptr<IClient> Build(std::string_view url, HttpOptions options = {}) noexcept;
};
} // namespace reduct
#endif // REDUCT_CPP_CLIENT_H