]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: put metadata, other cleanups
authorYehuda Sadeh <yehuda@inktank.com>
Tue, 5 Mar 2013 22:08:21 +0000 (14:08 -0800)
committerYehuda Sadeh <yehuda@inktank.com>
Wed, 8 May 2013 17:55:50 +0000 (10:55 -0700)
Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
src/cls/version/cls_version_types.cc
src/cls/version/cls_version_types.h
src/rgw/rgw_admin.cc
src/rgw/rgw_metadata.cc
src/rgw/rgw_metadata.h
src/rgw/rgw_user.cc

index 2ed69733653d61fdfb3eeac015e2e76308704ada..4ec678675d88138eb58e15be76e8448031b2697b 100644 (file)
@@ -1,6 +1,7 @@
 
 #include "cls/version/cls_version_types.h"
 #include "common/Formatter.h"
+#include "common/ceph_json.h"
 
 
 void obj_version::dump(Formatter *f) const
@@ -9,3 +10,9 @@ void obj_version::dump(Formatter *f) const
   f->dump_string("tag", tag);
 }
 
+void obj_version::decode_json(JSONObj *obj)
+{
+  JSONDecoder::decode_json("ver", ver, obj);
+  JSONDecoder::decode_json("tag", tag, obj);
+}
+
index 82f37c0008115d1c8d49353d573be3e4b4b47497..4b77e5dd26d3c2c2c07dbd1b3cec0dabc9cf6b5f 100644 (file)
@@ -4,6 +4,8 @@
 #include "include/encoding.h"
 #include "include/types.h"
 
+class JSONObj;
+
 
 struct obj_version {
   uint64_t ver;
@@ -39,6 +41,7 @@ struct obj_version {
   }
 
   void dump(Formatter *f) const;
+  void decode_json(JSONObj *obj);
 };
 WRITE_CLASS_ENCODER(obj_version)
 
index e85e0eed74fff0703e26ff35a50c99d45c362c4a..3a669c0aee1c2e714165d6ae0c38a05c68f36f25 100644 (file)
@@ -85,6 +85,8 @@ void _usage()
   cerr << "  gc list                    dump expired garbage collection objects\n";
   cerr << "  gc process                 manually process garbage\n";
   cerr << "  metadata get               get metadata info\n";
+  cerr << "  metadata put               put metadata info\n";
+  cerr << "  metadata list              list metadata info\n";
   cerr << "options:\n";
   cerr << "   --uid=<id>                user id\n";
   cerr << "   --subuser=<name>          subuser name\n";
@@ -187,6 +189,7 @@ enum {
   OPT_CAPS_ADD,
   OPT_CAPS_RM,
   OPT_METADATA_GET,
+  OPT_METADATA_PUT,
   OPT_METADATA_LIST,
 };
 
@@ -341,6 +344,8 @@ static int get_cmd(const char *cmd, const char *prev_cmd, bool *need_more)
   } else if (strcmp(prev_cmd, "metadata") == 0) {
     if (strcmp(cmd, "get") == 0)
       return OPT_METADATA_GET;
+    if (strcmp(cmd, "put") == 0)
+      return OPT_METADATA_PUT;
     if (strcmp(cmd, "list") == 0)
       return OPT_METADATA_LIST;
   }
@@ -657,18 +662,35 @@ int main(int argc, char **argv)
   }
   else {
     const char *prev_cmd = NULL;
-    for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ++i) {
+    std::vector<const char*>::iterator i ;
+    for (i = args.begin(); i != args.end(); ++i) {
       opt_cmd = get_cmd(*i, prev_cmd, &need_more);
       if (opt_cmd < 0) {
        cerr << "unrecognized arg " << *i << std::endl;
        return usage();
       }
-      if (!need_more)
+      if (!need_more) {
+       ++i;
        break;
+      }
       prev_cmd = *i;
     }
+
     if (opt_cmd == OPT_NO_CMD)
       return usage();
+
+    /* some commands may have an optional extra param */
+    if (i != args.end()) {
+      switch (opt_cmd) {
+        case OPT_METADATA_GET:
+        case OPT_METADATA_PUT:
+        case OPT_METADATA_LIST:
+          metadata_key = *i;
+          break;
+        default:
+          break;
+      }
+    }
   }
 
   // default to pretty json
@@ -1462,6 +1484,20 @@ next:
     formatter->flush(cout);
   }
 
+  if (opt_cmd == OPT_METADATA_PUT) {
+    bufferlist bl;
+    int ret = read_input(infile, bl);
+    if (ret < 0) {
+      cerr << "ERROR: failed to read input: " << cpp_strerror(-ret) << std::endl;
+      return ret;
+    }
+    ret = store->meta_mgr->put(metadata_key, bl);
+    if (ret < 0) {
+      cerr << "ERROR: can't put key: " << cpp_strerror(-ret) << std::endl;
+      return -ret;
+    }
+  }
+
   if (opt_cmd == OPT_METADATA_LIST) {
     void *handle;
     int max = 1000;
index 841779f3ceb2853e820bfbbc9fc0591c8b561b46..8dfa1c74d282edf712aae7e60a828ec5979b77f7 100644 (file)
@@ -1,8 +1,14 @@
 
 
 #include "rgw_metadata.h"
+#include "common/ceph_json.h"
+#include "cls/version/cls_version_types.h"
 
 
+obj_version& RGWMetadataObject::get_version()
+{
+  return objv;
+}
 
 RGWMetadataManager::~RGWMetadataManager()
 {
@@ -63,10 +69,25 @@ int RGWMetadataManager::get(string& metadata_key, Formatter *f)
     return ret;
   }
 
-  return handler->get(store, metadata_key, entry, f);
+  RGWMetadataObject *obj;
+
+  ret = handler->get(store, entry, &obj);
+  if (ret < 0) {
+    return ret;
+  }
+
+  f->open_object_section("metadata_info");
+  encode_json("key", metadata_key, f);
+  encode_json("ver", obj->get_version(), f);
+  encode_json("data", *obj, f);
+  f->close_section();
+
+  delete obj;
+
+  return 0;
 }
 
-int RGWMetadataManager::update(string& metadata_key, bufferlist& bl)
+int RGWMetadataManager::put(string& metadata_key, bufferlist& bl)
 {
   RGWMetadataHandler *handler;
   string entry;
@@ -75,7 +96,23 @@ int RGWMetadataManager::update(string& metadata_key, bufferlist& bl)
   if (ret < 0)
     return ret;
 
-  return handler->update(store, entry, bl);
+  JSONParser parser;
+  if (!parser.parse(bl.c_str(), bl.length())) {
+    return -EINVAL;
+  }
+
+  string meadata_key;
+  obj_version objv;
+
+  JSONDecoder::decode_json("key", metadata_key, &parser);
+  JSONDecoder::decode_json("ver", objv, &parser);
+
+  JSONObj *jo = parser.find_obj("data");
+  if (!jo) {
+    return -EINVAL;
+  }
+
+  return handler->put(store, entry, objv, jo);
 }
 
 struct list_keys_handle {
index 704c894f55208e0d2dbb1e122236f24b02b038a5..3f92ea0c352ef29355c928ba919ff2f5bb85fd41 100644 (file)
@@ -5,24 +5,39 @@
 
 #include "include/types.h"
 #include "rgw_common.h"
+#include "cls/version/cls_version_types.h"
+
 
 class RGWRados;
+class JSONObj;
+
+struct obj_version;
 
 
+class RGWMetadataObject {
+protected:
+  obj_version objv;
+  
+public:
+  virtual ~RGWMetadataObject() {}
+  obj_version& get_version();
+
+  virtual void dump(Formatter *f) const = 0;
+};
+
 class RGWMetadataHandler {
 public:
   virtual ~RGWMetadataHandler() {}
   virtual string get_type() = 0;
 
-  virtual int get(RGWRados *store, string& key, string& entry, Formatter *f) = 0;
-  virtual int update(RGWRados *store, string& entry, bufferlist& bl) = 0;
+  virtual int get(RGWRados *store, string& entry, RGWMetadataObject **obj) = 0;
+  virtual int put(RGWRados *store, string& entry, obj_version& objv, JSONObj *obj) = 0;
 
   virtual int list_keys_init(RGWRados *store, void **phandle) = 0;
   virtual int list_keys_next(void *handle, int max, list<string>& keys, bool *truncated) = 0;
   virtual void list_keys_complete(void *handle) = 0;
 };
 
-
 class RGWMetadataManager {
   map<string, RGWMetadataHandler *> handlers;
   RGWRados *store;
@@ -38,7 +53,7 @@ public:
   int register_handler(RGWMetadataHandler *handler);
 
   int get(string& metadata_key, Formatter *f);
-  int update(string& metadata_key, bufferlist& bl);
+  int put(string& metadata_key, bufferlist& bl);
 
   int list_keys_init(string& section, void **phandle);
   int list_keys_next(void *handle, int max, list<string>& keys, bool *truncated);
index 1a45ebbb8171976aa1d826d38dc3f24ee5ce8a15..109237b7b3a5cc706d0a192e692ff0156b200039 100644 (file)
@@ -2219,10 +2219,23 @@ int RGWUserAdminOp_Caps::remove(RGWRados *store, RGWUserAdminOpState& op_state,
   return 0;
 }
 
+class RGWUserMetadataObject : public RGWMetadataObject {
+  RGWUserInfo info;
+public:
+  RGWUserMetadataObject(RGWUserInfo& i, obj_version& v) : info(i) {
+    objv = v;
+  }
+
+  void dump(Formatter *f) const {
+    info.dump(f);
+  }
+};
+
 class RGWUserMetadataHandler : public RGWMetadataHandler {
 public:
   string get_type() { return "user"; }
-  int get(RGWRados *store, string& key, string& entry, Formatter *f) {
+
+  int get(RGWRados *store, string& entry, RGWMetadataObject **obj) {
     RGWUserInfo info;
 
     obj_version objv;
@@ -2231,16 +2244,29 @@ public:
     if (ret < 0)
       return ret;
 
-    f->open_object_section("metadata_info");
-    encode_json("key", key, f);
-    encode_json("ver", objv, f);
-    encode_json("data", info, f);
-    f->close_section();
-    f->flush(cout);
+    RGWUserMetadataObject *mdo = new RGWUserMetadataObject(info, objv);
+
+    *obj = mdo;
+
     return 0;
   }
 
-  int update(RGWRados *store, string& metadata_key, bufferlist& bl) {
+  int put(RGWRados *store, string& entry, obj_version& objv, JSONObj *obj) {
+    RGWUserInfo info;
+
+    decode_json_obj(info, obj);
+
+    RGWUserInfo old_info;
+    obj_version old_objv;
+    int ret = rgw_read_uid_info(store, entry, old_info, &old_objv);
+    if (ret < 0)
+      return ret;
+
+
+    ret = rgw_store_user_info(store, info, &old_info, false);
+    if (ret < 0)
+      return ret;
+
     return 0;
   }