1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
4 #ifndef CEPH_RGW_BASIC_TYPES_H
5 #define CEPH_RGW_BASIC_TYPES_H
9 #include "include/types.h"
12 class cls_user_bucket
;
19 explicit rgw_user(const std::string
& s
) {
22 rgw_user(const std::string
& tenant
, const std::string
& id
)
26 rgw_user(std::string
&& tenant
, std::string
&& id
)
27 : tenant(std::move(tenant
)),
31 void encode(bufferlist
& bl
) const {
32 ENCODE_START(1, 1, bl
);
37 void decode(bufferlist::const_iterator
& bl
) {
44 void to_str(std::string
& str
) const {
45 if (!tenant
.empty()) {
46 str
= tenant
+ '$' + id
;
61 string
to_str() const {
67 void from_str(const std::string
& str
) {
68 size_t pos
= str
.find('$');
69 if (pos
!= std::string::npos
) {
70 tenant
= str
.substr(0, pos
);
71 id
= str
.substr(pos
+ 1);
78 rgw_user
& operator=(const string
& str
) {
83 int compare(const rgw_user
& u
) const {
84 int r
= tenant
.compare(u
.tenant
);
88 return id
.compare(u
.id
);
90 int compare(const string
& str
) const {
95 bool operator!=(const rgw_user
& rhs
) const {
96 return (compare(rhs
) != 0);
98 bool operator==(const rgw_user
& rhs
) const {
99 return (compare(rhs
) == 0);
101 bool operator<(const rgw_user
& rhs
) const {
102 if (tenant
< rhs
.tenant
) {
104 } else if (tenant
> rhs
.tenant
) {
107 return (id
< rhs
.id
);
109 void dump(Formatter
*f
) const;
110 static void generate_test_instances(list
<rgw_user
*>& o
);
112 WRITE_CLASS_ENCODER(rgw_user
)
118 rgw_pool() = default;
119 rgw_pool(const rgw_pool
& _p
) : name(_p
.name
), ns(_p
.ns
) {}
120 rgw_pool(rgw_pool
&&) = default;
121 rgw_pool(const string
& _s
) {
124 rgw_pool(const string
& _name
, const string
& _ns
) : name(_name
), ns(_ns
) {}
126 string
to_str() const;
127 void from_str(const string
& s
);
129 void init(const string
& _s
) {
137 int compare(const rgw_pool
& p
) const {
138 int r
= name
.compare(p
.name
);
142 return ns
.compare(p
.ns
);
145 void encode(bufferlist
& bl
) const {
146 ENCODE_START(10, 10, bl
);
152 void decode_from_bucket(bufferlist::const_iterator
& bl
);
154 void decode(bufferlist::const_iterator
& bl
) {
155 DECODE_START_LEGACY_COMPAT_LEN(10, 3, 3, bl
);
162 * note that rgw_pool can be used where rgw_bucket was used before
163 * therefore we inherit rgw_bucket's old versions. However, we only
164 * need the first field from rgw_bucket. unless we add more fields
165 * in which case we'll need to look at struct_v, and check the actual
166 * version. Anything older than 10 needs to be treated as old rgw_bucket
176 rgw_pool
& operator=(const rgw_pool
&) = default;
178 bool operator==(const rgw_pool
& p
) const {
179 return (compare(p
) == 0);
181 bool operator!=(const rgw_pool
& p
) const {
182 return !(*this == p
);
184 bool operator<(const rgw_pool
& p
) const {
185 int r
= name
.compare(p
.name
);
187 return (ns
.compare(p
.ns
) < 0);
192 WRITE_CLASS_ENCODER(rgw_pool
)
194 inline ostream
& operator<<(ostream
& out
, const rgw_pool
& p
) {
199 struct rgw_data_placement_target
{
201 rgw_pool data_extra_pool
;
204 rgw_data_placement_target() = default;
205 rgw_data_placement_target(const rgw_data_placement_target
&) = default;
206 rgw_data_placement_target(rgw_data_placement_target
&&) = default;
208 rgw_data_placement_target(const rgw_pool
& data_pool
,
209 const rgw_pool
& data_extra_pool
,
210 const rgw_pool
& index_pool
)
211 : data_pool(data_pool
),
212 data_extra_pool(data_extra_pool
),
213 index_pool(index_pool
) {
216 rgw_data_placement_target
&
217 operator=(const rgw_data_placement_target
&) = default;
219 const rgw_pool
& get_data_extra_pool() const {
220 if (data_extra_pool
.empty()) {
223 return data_extra_pool
;
226 int compare(const rgw_data_placement_target
& t
) {
227 int c
= data_pool
.compare(t
.data_pool
);
231 c
= data_extra_pool
.compare(t
.data_extra_pool
);
235 return index_pool
.compare(t
.index_pool
);
238 void dump(Formatter
*f
) const;
239 void decode_json(JSONObj
*obj
);
242 struct rgw_bucket_key
{
245 std::string bucket_id
;
247 rgw_bucket_key(const std::string
& _tenant
,
248 const std::string
& _name
,
249 const std::string
& _bucket_id
) : tenant(_tenant
),
251 bucket_id(_bucket_id
) {}
252 rgw_bucket_key(const std::string
& _tenant
,
253 const std::string
& _name
) : tenant(_tenant
),
261 std::string bucket_id
;
262 rgw_data_placement_target explicit_placement
;
265 // cppcheck-suppress noExplicitConstructor
266 explicit rgw_bucket(const rgw_user
& u
, const cls_user_bucket
& b
);
268 rgw_bucket(const rgw_bucket_key
& bk
) : tenant(bk
.tenant
),
270 bucket_id(bk
.bucket_id
) {}
271 rgw_bucket(const rgw_bucket
&) = default;
272 rgw_bucket(rgw_bucket
&&) = default;
274 bool match(const rgw_bucket
& b
) const {
275 return (tenant
== b
.tenant
&&
277 (bucket_id
== b
.bucket_id
||
279 b
.bucket_id
.empty()));
282 void convert(cls_user_bucket
*b
) const;
284 void encode(bufferlist
& bl
) const {
285 ENCODE_START(10, 10, bl
);
288 encode(bucket_id
, bl
);
290 bool encode_explicit
= !explicit_placement
.data_pool
.empty();
291 encode(encode_explicit
, bl
);
292 if (encode_explicit
) {
293 encode(explicit_placement
.data_pool
, bl
);
294 encode(explicit_placement
.data_extra_pool
, bl
);
295 encode(explicit_placement
.index_pool
, bl
);
299 void decode(bufferlist::const_iterator
& bl
) {
300 DECODE_START_LEGACY_COMPAT_LEN(10, 3, 3, bl
);
303 decode(explicit_placement
.data_pool
.name
, bl
);
311 snprintf(buf
, sizeof(buf
), "%" PRIu64
, id
);
314 decode(bucket_id
, bl
);
319 decode(explicit_placement
.index_pool
.name
, bl
);
321 explicit_placement
.index_pool
= explicit_placement
.data_pool
;
324 decode(explicit_placement
.data_extra_pool
.name
, bl
);
330 if (struct_v
>= 10) {
331 bool decode_explicit
= !explicit_placement
.data_pool
.empty();
332 decode(decode_explicit
, bl
);
333 if (decode_explicit
) {
334 decode(explicit_placement
.data_pool
, bl
);
335 decode(explicit_placement
.data_extra_pool
, bl
);
336 decode(explicit_placement
.index_pool
, bl
);
342 void update_bucket_id(const string
& new_bucket_id
) {
343 bucket_id
= new_bucket_id
;
346 // format a key for the bucket/instance. pass delim=0 to skip a field
347 std::string
get_key(char tenant_delim
= '/',
349 size_t reserve
= 0) const;
351 const rgw_pool
& get_data_extra_pool() const {
352 return explicit_placement
.get_data_extra_pool();
355 void dump(Formatter
*f
) const;
356 void decode_json(JSONObj
*obj
);
357 static void generate_test_instances(list
<rgw_bucket
*>& o
);
359 rgw_bucket
& operator=(const rgw_bucket
&) = default;
361 bool operator<(const rgw_bucket
& b
) const {
364 } else if (name
> b
.name
) {
368 if (bucket_id
< b
.bucket_id
) {
370 } else if (bucket_id
> b
.bucket_id
) {
374 return (tenant
< b
.tenant
);
377 bool operator==(const rgw_bucket
& b
) const {
378 return (tenant
== b
.tenant
) && (name
== b
.name
) && \
379 (bucket_id
== b
.bucket_id
);
381 bool operator!=(const rgw_bucket
& b
) const {
382 return (tenant
!= b
.tenant
) || (name
!= b
.name
) ||
383 (bucket_id
!= b
.bucket_id
);
386 WRITE_CLASS_ENCODER(rgw_bucket
)
388 inline ostream
& operator<<(ostream
& out
, const rgw_bucket
&b
) {
389 out
<< b
.tenant
<< ":" << b
.name
<< "[" << b
.bucket_id
<< "])";
393 struct rgw_bucket_shard
{
397 rgw_bucket_shard() : shard_id(-1) {}
398 rgw_bucket_shard(const rgw_bucket
& _b
, int _sid
) : bucket(_b
), shard_id(_sid
) {}
400 std::string
get_key(char tenant_delim
= '/', char id_delim
= ':',
401 char shard_delim
= ':') const;
403 bool operator<(const rgw_bucket_shard
& b
) const {
404 if (bucket
< b
.bucket
) {
407 if (b
.bucket
< bucket
) {
410 return shard_id
< b
.shard_id
;
413 bool operator==(const rgw_bucket_shard
& b
) const {
414 return (bucket
== b
.bucket
&&
415 shard_id
== b
.shard_id
);
419 inline ostream
& operator<<(ostream
& out
, const rgw_bucket_shard
& bs
) {
420 if (bs
.shard_id
<= 0) {
421 return out
<< bs
.bucket
;
424 return out
<< bs
.bucket
<< ":" << bs
.shard_id
;
432 rgw_zone_id(const string
& _id
) : id(_id
) {}
433 rgw_zone_id(string
&& _id
) : id(std::move(_id
)) {}
435 void encode(bufferlist
& bl
) const {
436 /* backward compatiblity, not using ENCODE_{START,END} macros */
437 ceph::encode(id
, bl
);
440 void decode(bufferlist::const_iterator
& bl
) {
441 /* backward compatiblity, not using DECODE_{START,END} macros */
442 ceph::decode(id
, bl
);
449 bool operator==(const string
& _id
) const {
452 bool operator==(const rgw_zone_id
& zid
) const {
453 return (id
== zid
.id
);
455 bool operator!=(const rgw_zone_id
& zid
) const {
456 return (id
!= zid
.id
);
458 bool operator<(const rgw_zone_id
& zid
) const {
459 return (id
< zid
.id
);
461 bool operator>(const rgw_zone_id
& zid
) const {
462 return (id
> zid
.id
);
469 WRITE_CLASS_ENCODER(rgw_zone_id
)
471 static inline ostream
& operator<<(ostream
& os
, const rgw_zone_id
& zid
) {
476 void encode_json_impl(const char *name
, const rgw_zone_id
& zid
, Formatter
*f
);
477 void decode_json_obj(rgw_zone_id
& zid
, JSONObj
*obj
);
480 // Represents an identity. This is more wide-ranging than a
481 // 'User'. Its purposes is to be matched against by an
482 // IdentityApplier. The internal representation will doubtless change as
483 // more types are added. We may want to expose the type enum and make
484 // the member public so people can switch/case on it.
489 enum types
{ User
, Role
, Tenant
, Wildcard
, OidcProvider
};
494 explicit Principal(types t
)
497 Principal(types t
, std::string
&& n
, std::string i
)
498 : t(t
), u(std::move(n
), std::move(i
)) {}
500 Principal(string
&& idp_url
)
501 : t(OidcProvider
), idp_url(std::move(idp_url
)) {}
505 static Principal
wildcard() {
506 return Principal(Wildcard
);
509 static Principal
user(std::string
&& t
, std::string
&& u
) {
510 return Principal(User
, std::move(t
), std::move(u
));
513 static Principal
role(std::string
&& t
, std::string
&& u
) {
514 return Principal(Role
, std::move(t
), std::move(u
));
517 static Principal
tenant(std::string
&& t
) {
518 return Principal(Tenant
, std::move(t
), {});
521 static Principal
oidc_provider(string
&& idp_url
) {
522 return Principal(std::move(idp_url
));
525 bool is_wildcard() const {
526 return t
== Wildcard
;
529 bool is_user() const {
533 bool is_role() const {
537 bool is_tenant() const {
541 bool is_oidc_provider() const {
542 return t
== OidcProvider
;
545 const std::string
& get_tenant() const {
549 const std::string
& get_id() const {
553 const string
& get_idp_url() const {
557 bool operator ==(const Principal
& o
) const {
558 return (t
== o
.t
) && (u
== o
.u
);
561 bool operator <(const Principal
& o
) const {
562 return (t
< o
.t
) || ((t
== o
.t
) && (u
< o
.u
));
566 std::ostream
& operator <<(std::ostream
& m
, const Principal
& p
);
572 void decode_json_obj(rgw_user
& val
, JSONObj
*obj
);
573 void encode_json(const char *name
, const rgw_user
& val
, Formatter
*f
);
574 void encode_xml(const char *name
, const rgw_user
& val
, Formatter
*f
);
576 inline ostream
& operator<<(ostream
& out
, const rgw_user
&u
) {