]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw: support json format for admin policy API
authorDunrong Huang <riegamaths@gmail.com>
Wed, 3 Feb 2016 07:43:40 +0000 (15:43 +0800)
committerYehuda Sadeh <yehuda@redhat.com>
Thu, 10 Mar 2016 21:35:28 +0000 (13:35 -0800)
Fixes: #14090, #14091
- before this patch:
$ curl -s -X GET http://radosrgw:9090/admin/bucket?format=json&policy&bucket=m8x | xmllint --format -

got XML response with error format

- after this patch:
got correct response with json format

Signed-off-by: Dunrong Huang <riegamaths@gmail.com>
src/rgw/rgw_bucket.cc
src/rgw/rgw_bucket.h

index e77ecfbd174e0336446ad82aec01e155535f53d3..9b2294c15a2a54934c4ef0b08b5785ab23366c2e 100644 (file)
@@ -923,7 +923,19 @@ int RGWBucket::policy_bl_to_stream(bufferlist& bl, ostream& o)
   return 0;
 }
 
-int RGWBucket::get_policy(RGWBucketAdminOpState& op_state, ostream& o)
+static int policy_decode(RGWRados *store, bufferlist& bl, RGWAccessControlPolicy& policy)
+{
+  bufferlist::iterator iter = bl.begin();
+  try {
+    policy.decode(iter);
+  } catch (buffer::error& err) {
+    ldout(store->ctx(), 0) << "ERROR: caught buffer::error, could not decode policy" << dendl;
+    return -EIO;
+  }
+  return 0;
+}
+
+int RGWBucket::get_policy(RGWBucketAdminOpState& op_state, RGWAccessControlPolicy& policy)
 {
   std::string object_name = op_state.get_object_name();
   rgw_bucket bucket = op_state.get_bucket();
@@ -947,7 +959,7 @@ int RGWBucket::get_policy(RGWBucketAdminOpState& op_state, ostream& o)
     if (ret < 0)
       return ret;
 
-    return policy_bl_to_stream(bl, o);
+    return policy_decode(store, bl, policy);
   }
 
   map<string, bufferlist>::iterator aiter = attrs.find(RGW_ATTR_ACL);
@@ -955,12 +967,12 @@ int RGWBucket::get_policy(RGWBucketAdminOpState& op_state, ostream& o)
     return -ENOENT;
   }
 
-  return policy_bl_to_stream(aiter->second, o);
+  return policy_decode(store, aiter->second, policy);
 }
 
 
 int RGWBucketAdminOp::get_policy(RGWRados *store, RGWBucketAdminOpState& op_state,
-                  ostream& os)
+                  RGWAccessControlPolicy& policy)
 {
   RGWBucket bucket;
 
@@ -968,7 +980,7 @@ int RGWBucketAdminOp::get_policy(RGWRados *store, RGWBucketAdminOpState& op_stat
   if (ret < 0)
     return ret;
 
-  ret = bucket.get_policy(op_state, os);
+  ret = bucket.get_policy(op_state, policy);
   if (ret < 0)
     return ret;
 
@@ -981,9 +993,9 @@ int RGWBucketAdminOp::get_policy(RGWRados *store, RGWBucketAdminOpState& op_stat
 int RGWBucketAdminOp::get_policy(RGWRados *store, RGWBucketAdminOpState& op_state,
                   RGWFormatterFlusher& flusher)
 {
-  std::ostringstream policy_stream;
+  RGWAccessControlPolicy policy(store->ctx());
 
-  int ret = get_policy(store, op_state, policy_stream);
+  int ret = get_policy(store, op_state, policy);
   if (ret < 0)
     return ret;
 
@@ -991,7 +1003,9 @@ int RGWBucketAdminOp::get_policy(RGWRados *store, RGWBucketAdminOpState& op_stat
 
   flusher.start(0);
 
-  formatter->dump_string("policy", policy_stream.str());
+  formatter->open_object_section("policy");
+  policy.dump(formatter);
+  formatter->close_section();
 
   flusher.flush();
 
index 9bece1ad0c1653fb5f1483489be20a39b0eba871..924c636021fd50145958e2b19073456fe7393ee3 100644 (file)
@@ -280,7 +280,7 @@ public:
 
   int remove_object(RGWBucketAdminOpState& op_state, std::string *err_msg = NULL);
   int policy_bl_to_stream(bufferlist& bl, ostream& o);
-  int get_policy(RGWBucketAdminOpState& op_state, ostream& o);
+  int get_policy(RGWBucketAdminOpState& op_state, RGWAccessControlPolicy& policy);
 
   void clear_failure() { failure = false; }
 };
@@ -291,7 +291,7 @@ public:
   static int get_policy(RGWRados *store, RGWBucketAdminOpState& op_state,
                   RGWFormatterFlusher& flusher);
   static int get_policy(RGWRados *store, RGWBucketAdminOpState& op_state,
-                  ostream& os);
+                  RGWAccessControlPolicy& policy);
 
 
   static int unlink(RGWRados *store, RGWBucketAdminOpState& op_state);