This is an automated email from the ASF dual-hosted git repository.

bmahler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit a8a0573e1e53237b9eb13f1e288b358d707b9e89
Author: Benjamin Mahler <[email protected]>
AuthorDate: Tue Jun 18 15:04:26 2019 -0400

    Updated master's /roles endpoint to use jsonify.
    
    Review: https://reviews.apache.org/r/70877
---
 src/common/http.cpp             | 30 +++++++------
 src/common/http.hpp             |  6 ++-
 src/master/readonly_handler.cpp | 97 ++++++++++++++---------------------------
 3 files changed, 54 insertions(+), 79 deletions(-)

diff --git a/src/common/http.cpp b/src/common/http.cpp
index dcf3cb0..6962959 100644
--- a/src/common/http.cpp
+++ b/src/common/http.cpp
@@ -493,22 +493,9 @@ JSON::Object model(const FileInfo& fileInfo)
   return file;
 }
 
-
-JSON::Object model(const quota::QuotaInfo& quotaInfo)
-{
-  JSON::Object object;
-
-  object.values["guarantee"] = model(quotaInfo.guarantee());
-  object.values["role"] = quotaInfo.role();
-  if (quotaInfo.has_principal()) {
-    object.values["principal"] = quotaInfo.principal();
-  }
-
-  return object;
-}
-
 }  // namespace internal {
 
+
 void json(JSON::ObjectWriter* writer, const Attributes& attributes)
 {
   foreach (const Attribute& attribute, attributes) {
@@ -868,6 +855,21 @@ static void json(JSON::StringWriter* writer, const 
Value::Text& text)
 }
 
 
+namespace quota {
+
+void json(JSON::ObjectWriter* writer, const QuotaInfo& quotaInfo)
+{
+  writer->field("role", quotaInfo.role());
+  writer->field("guarantee", quotaInfo.guarantee());
+
+  if (quotaInfo.has_principal()) {
+    writer->field("principal", quotaInfo.principal());
+  }
+}
+
+} // namespace quota {
+
+
 Future<Owned<ObjectApprovers>> ObjectApprovers::create(
     const Option<Authorizer*>& authorizer,
     const Option<Principal>& principal,
diff --git a/src/common/http.hpp b/src/common/http.hpp
index a0f9b67..b8c71ec 100644
--- a/src/common/http.hpp
+++ b/src/common/http.hpp
@@ -189,7 +189,6 @@ JSON::Object model(const ExecutorInfo& executorInfo);
 JSON::Array model(const Labels& labels);
 JSON::Object model(const Task& task);
 JSON::Object model(const FileInfo& fileInfo);
-JSON::Object model(const quota::QuotaInfo& quotaInfo);
 
 void json(JSON::ObjectWriter* writer, const Task& task);
 
@@ -217,6 +216,11 @@ void json(JSON::ObjectWriter* writer, const Task& task);
 void json(JSON::ObjectWriter* writer, const TaskStatus& status);
 
 
+namespace quota {
+void json(JSON::ObjectWriter* writer, const QuotaInfo& quotaInfo);
+} // namespace quota {
+
+
 // Implementation of the `ObjectApprover` interface authorizing all objects.
 class AcceptingObjectApprover : public ObjectApprover
 {
diff --git a/src/master/readonly_handler.cpp b/src/master/readonly_handler.cpp
index 66d6160..d62f139 100644
--- a/src/master/readonly_handler.cpp
+++ b/src/master/readonly_handler.cpp
@@ -694,82 +694,51 @@ process::http::Response 
Master::ReadOnlyHandler::frameworks(
 }
 
 
-// Returns a JSON object modeled after a role.
-JSON::Object model(
-    const string& name,
-    Option<double> weight,
-    Option<Quota> quota,
-    Option<Role*> _role)
-{
-  JSON::Object object;
-  object.values["name"] = name;
-
-  if (weight.isSome()) {
-    object.values["weight"] = weight.get();
-  } else {
-    object.values["weight"] = 1.0; // Default weight.
-  }
-
-  if (quota.isSome()) {
-    object.values["quota"] = model(quota->info);
-  }
-
-  if (_role.isNone()) {
-    object.values["resources"] = model(Resources());
-    object.values["frameworks"] = JSON::Array();
-  } else {
-    Role* role = _role.get();
-
-    object.values["resources"] = model(role->allocatedResources());
-
-    {
-      JSON::Array array;
-
-      foreachkey (const FrameworkID& frameworkId, role->frameworks) {
-        array.values.push_back(frameworkId.value());
-      }
-
-      object.values["frameworks"] = std::move(array);
-    }
-  }
-
-  return object;
-}
-
-
 process::http::Response Master::ReadOnlyHandler::roles(
     const hashmap<std::string, std::string>& query,
     const process::Owned<ObjectApprovers>& approvers) const
 {
-  JSON::Object object;
+  const Master* master = this->master;
   const vector<string> filteredRoles = master->filterRoles(approvers);
 
-  {
-    JSON::Array array;
+  auto roles = [&](JSON::ObjectWriter* writer) {
+    writer->field(
+        "roles",
+        [&](JSON::ArrayWriter* writer) {
+          foreach (const string& name, filteredRoles) {
+            writer->element([&](JSON::ObjectWriter* writer) {
+              writer->field("name", name);
 
-    foreach (const string& name, filteredRoles) {
-      Option<double> weight = None();
-      if (master->weights.contains(name)) {
-        weight = master->weights.at(name);
-      }
+              // Default weight is 1.0.
+              writer->field("weight", 
master->weights.get(name).getOrElse(1.0));
 
-      Option<Quota> quota = None();
-      if (master->quotas.contains(name)) {
-        quota = master->quotas.at(name);
-      }
+              if (master->quotas.contains(name)) {
+                writer->field("quota", master->quotas.at(name).info);
+              }
 
-      Option<Role*> role = None();
-      if (master->roles.contains(name)) {
-        role = master->roles.at(name);
-      }
+              Option<Role*> role = master->roles.get(name);
 
-      array.values.push_back(model(name, weight, quota, role));
-    }
+              if (role.isNone()) {
+                writer->field("resources", Resources());
+              } else {
+                writer->field("resources", (*role)->allocatedResources());
+              }
 
-    object.values["roles"] = std::move(array);
-  }
+              if (role.isNone()) {
+                writer->field("frameworks", [](JSON::ArrayWriter*) {});
+              } else {
+                writer->field("frameworks", [&](JSON::ArrayWriter* writer) {
+                  foreachkey (const FrameworkID& id, (*role)->frameworks) {
+                    writer->element(id.value());
+                  }
+                });
+              }
+            });
+          }
+        });
+  };
 
-  return OK(object, query.get("jsonp"));
+  return OK(jsonify(roles), query.get("jsonp"));
 }
 
 

Reply via email to