]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: add rgw_owner variant with json encoding
authorCasey Bodley <cbodley@redhat.com>
Fri, 3 Nov 2023 00:28:03 +0000 (20:28 -0400)
committerCasey Bodley <cbodley@redhat.com>
Fri, 12 Apr 2024 19:34:26 +0000 (15:34 -0400)
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 <cbodley@redhat.com>
(cherry picked from commit 87a74f8603bdeccdea3fdfa7b4e0fd344fba8aae)

src/rgw/rgw_basic_types.cc
src/rgw/rgw_user_types.h

index 88a510175d417db780368d925a62f249d6eb3549..e6e94f48dc0e45c9b0e16482e1bd0e6a9d60f24c 100644 (file)
@@ -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<std::string&>(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);
+}
index afd0d930a67fa254b6002d86fed8c81ad8f22bc1..2329eca3d60e0fce485f39e183262f4ad68000f3 100644 (file)
@@ -19,7 +19,9 @@
 
 #pragma once
 
+#include <iosfwd>
 #include <string>
+#include <variant>
 #include <fmt/format.h>
 
 #include "common/dout.h"
@@ -134,3 +136,19 @@ struct rgw_user {
   static void generate_test_instances(std::list<rgw_user*>& 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_user, rgw_account_id>;
+
+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);