From: Casey Bodley Date: Fri, 3 Nov 2023 00:28:03 +0000 (-0400) Subject: rgw: add rgw_owner variant with json encoding X-Git-Tag: v20.0.0~2159^2~153 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=87a74f8603bdeccdea3fdfa7b4e0fd344fba8aae;p=ceph.git rgw: add rgw_owner variant with json encoding existing buckets and objects use `struct rgw_user` for the owner. with the addition of accounts, we need to be able to represent ownership by the account rather than its users add a `rgw_owner` variant that preserves the existing json encoding of `rgw_user` while adding a new representation for account ids Signed-off-by: Casey Bodley --- diff --git a/src/rgw/rgw_basic_types.cc b/src/rgw/rgw_basic_types.cc index 88a510175d41..e6e94f48dc0e 100644 --- a/src/rgw/rgw_basic_types.cc +++ b/src/rgw/rgw_basic_types.cc @@ -7,6 +7,7 @@ #include "cls/user/cls_user_types.h" +#include "rgw_account.h" #include "rgw_basic_types.h" #include "rgw_bucket.h" #include "rgw_xml.h" @@ -189,3 +190,44 @@ void decode_json_obj(rgw_account_id& id, JSONObj* obj) { decode_json_obj(static_cast(id), obj); } + +// rgw_owner variant +rgw_owner parse_owner(const std::string& str) +{ + if (rgw::account::validate_id(str)) { + return rgw_account_id{str}; + } else { + return rgw_user{str}; + } +} + +std::string to_string(const rgw_owner& o) +{ + struct visitor { + std::string operator()(const rgw_account_id& a) { return a; } + std::string operator()(const rgw_user& u) { return u.to_str(); } + }; + return std::visit(visitor{}, o); +} + +std::ostream& operator<<(std::ostream& out, const rgw_owner& o) +{ + struct visitor { + std::ostream& out; + std::ostream& operator()(const rgw_account_id& a) { return out << a; } + std::ostream& operator()(const rgw_user& u) { return out << u; } + }; + return std::visit(visitor{out}, o); +} + +void encode_json_impl(const char *name, const rgw_owner& o, ceph::Formatter *f) +{ + encode_json(name, to_string(o), f); +} + +void decode_json_obj(rgw_owner& o, JSONObj *obj) +{ + std::string str; + decode_json_obj(str, obj); + o = parse_owner(str); +} diff --git a/src/rgw/rgw_user_types.h b/src/rgw/rgw_user_types.h index afd0d930a67f..2329eca3d60e 100644 --- a/src/rgw/rgw_user_types.h +++ b/src/rgw/rgw_user_types.h @@ -19,7 +19,9 @@ #pragma once +#include #include +#include #include #include "common/dout.h" @@ -134,3 +136,19 @@ struct rgw_user { static void generate_test_instances(std::list& o); }; WRITE_CLASS_ENCODER(rgw_user) + + +/// Resources are either owned by accounts, or by users or roles (represented as +/// rgw_user) that don't belong to an account. +/// +/// This variant is present in binary encoding formats, so existing types cannot +/// be changed or removed. New types can only be added to the end. +using rgw_owner = std::variant; + +rgw_owner parse_owner(const std::string& str); +std::string to_string(const rgw_owner& o); + +std::ostream& operator<<(std::ostream& out, const rgw_owner& o); + +void encode_json_impl(const char *name, const rgw_owner& o, ceph::Formatter *f); +void decode_json_obj(rgw_owner& o, JSONObj *obj);