You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2012/09/17 23:15:42 UTC

svn commit: r1386845 - in /incubator/mesos/trunk: src/master/slaves_manager.cpp third_party/libprocess/include/stout/numify.hpp

Author: benh
Date: Mon Sep 17 21:15:41 2012
New Revision: 1386845

URL: http://svn.apache.org/viewvc?rev=1386845&view=rev
Log:
Added a numify(const Option<std::string>&) variant
(https://reviews.apache.org/r/7006).

Modified:
    incubator/mesos/trunk/src/master/slaves_manager.cpp
    incubator/mesos/trunk/third_party/libprocess/include/stout/numify.hpp

Modified: incubator/mesos/trunk/src/master/slaves_manager.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/master/slaves_manager.cpp?rev=1386845&r1=1386844&r2=1386845&view=diff
==============================================================================
--- incubator/mesos/trunk/src/master/slaves_manager.cpp (original)
+++ incubator/mesos/trunk/src/master/slaves_manager.cpp Mon Sep 17 21:15:41 2012
@@ -812,29 +812,20 @@ void SlavesManager::updateInactive(
 
 Future<Response> SlavesManager::add(const Request& request)
 {
+  // Make sure we have 'hostname=value'.
   Option<string> hostname = request.query.get("hostname");
-  Option<string> portString = request.query.get("port");
 
-  // Make sure there is at least a 'hostname=' and 'port='.
-  if (hostname.isNone()) {
-    string error = "Slaves manager expecting 'hostname' in query string"
-                   " when trying to add a slave";
-    LOG(WARNING) << error;
-    return BadRequest(error);
-  } else if (portString.isNone()) {
-    string error = "Slaves manager expecting 'port' in query string"
-                   " when trying to add a slave";
-    LOG(WARNING) << error;
-    return BadRequest(error);
-  }
-
-  // Check that 'port' is valid.
-  Try<uint16_t> port = numify<uint16_t>(portString.get());
-  if (port.isError()) {
-    string error = string("Slaves manager failed to parse "
-        "'port = ") + portString.get() + "' when trying to add a slave";
-    LOG(WARNING) << error;
-    return BadRequest(error);
+  if (hostname.isNone() || hostname.get() == "") {
+    return BadRequest("Expecting 'hostname=value' in query.\n");
+  }
+
+  // Make sure we have 'port=value' and that it's a valid number.
+  Result<uint16_t> port = numify<uint16_t>(request.query.get("port"));
+
+  if (port.isNone()) {
+    return BadRequest("Expecting 'port=value' in query.\n");
+  } else if (port.isError()) {
+    return BadRequest("Invalid port: " + port.error() + ".\n");
   }
 
   LOG(INFO) << "Slaves manager received HTTP request to add slave at "
@@ -849,29 +840,20 @@ Future<Response> SlavesManager::add(cons
 
 Future<Response> SlavesManager::remove(const Request& request)
 {
+  // Make sure we have 'hostname=value'.
   Option<string> hostname = request.query.get("hostname");
-  Option<string> portString = request.query.get("port");
 
-  // Make sure there is at least a 'hostname=' and 'port='.
-  if (hostname.isNone()) {
-    string error = "Slaves manager expecting 'hostname' in query string"
-                   " when trying to remove a slave";
-    LOG(WARNING) << error;
-    return BadRequest(error);
-  } else if (portString.isNone()) {
-    string error = "Slaves manager expecting 'port' in query string"
-                   " when trying to remove a slave";
-    LOG(WARNING) << error;
-    return BadRequest(error);
-  }
-
-  // Check that 'port' is valid.
-  Try<uint16_t> port = numify<uint16_t>(portString.get());
-  if (port.isError()) {
-    string error = string("Slaves manager failed to parse "
-        "'port = ") + portString.get() + "' when trying to remove a slave";
-    LOG(WARNING) << error;
-    return BadRequest(error);
+  if (hostname.isNone() || hostname.get() == "") {
+    return BadRequest("Expecting 'hostname=value' in query.\n");
+  }
+
+  // Make sure we have 'port=value' and that it's a valid number.
+  Result<uint16_t> port = numify<uint16_t>(request.query.get("port"));
+
+  if (port.isNone()) {
+    return BadRequest("Expecting 'port=value' in query.\n");
+  } else if (port.isError()) {
+    return BadRequest("Invalid port: " + port.error() + ".\n");
   }
 
   LOG(INFO) << "Slaves manager received HTTP request to remove slave at "
@@ -886,27 +868,20 @@ Future<Response> SlavesManager::remove(c
 
 Future<Response> SlavesManager::activate(const Request& request)
 {
+  // Make sure we have 'hostname=value'.
   Option<string> hostname = request.query.get("hostname");
-  Option<string> portString = request.query.get("port");
 
-  // Make sure there is at least a 'hostname=' and 'port='.
-  if (hostname.isNone()) {
-    LOG(WARNING) << "Slaves manager expecting 'hostname' in query string"
-                 << " when trying to activate a slave";
-    return NotFound();
-  } else if (portString.isNone()) {
-    LOG(WARNING) << "Slaves manager expecting 'port' in query string"
-                 << " when trying to activate a slave";
-    return NotFound();
-  }
-
-  // Check that 'port' is valid.
-  Try<uint16_t> port = numify<uint16_t>(portString.get());
-  if (port.isError()) {
-    string error = string("Slaves manager failed to parse "
-        "'port = ") + portString.get() + "' when trying to activate a slave";
-    LOG(WARNING) << error;
-    return BadRequest(error);
+  if (hostname.isNone() || hostname.get() == "") {
+    return BadRequest("Expecting 'hostname=value' in query.\n");
+  }
+
+  // Make sure we have 'port=value' and that it's a valid number.
+  Result<uint16_t> port = numify<uint16_t>(request.query.get("port"));
+
+  if (port.isNone()) {
+    return BadRequest("Expecting 'port=value' in query.\n");
+  } else if (port.isError()) {
+    return BadRequest("Invalid port: " + port.error() + ".\n");
   }
 
   LOG(INFO) << "Slaves manager received HTTP request to activate slave at "
@@ -921,27 +896,20 @@ Future<Response> SlavesManager::activate
 
 Future<Response> SlavesManager::deactivate(const Request& request)
 {
+  // Make sure we have 'hostname=value'.
   Option<string> hostname = request.query.get("hostname");
-  Option<string> portString = request.query.get("port");
 
-  // Make sure there is at least a 'hostname=' and 'port='.
-  if (hostname.isNone()) {
-    LOG(WARNING) << "Slaves manager expecting 'hostname' in query string"
-                 << " when trying to deactivate a slave";
-    return NotFound();
-  } else if (portString.isNone()) {
-    LOG(WARNING) << "Slaves manager expecting 'port' in query string"
-                 << " when trying to deactivate a slave";
-    return NotFound();
-  }
-
-  // Check that 'port' is valid.
-  Try<uint16_t> port = numify<uint16_t>(portString.get());
-  if (port.isError()) {
-    string error = string("Slaves manager failed to parse "
-        "'port = ") + portString.get() + "' when trying to deactivate a slave";
-    LOG(WARNING) << error;
-    return BadRequest(error);
+  if (hostname.isNone() || hostname.get() == "") {
+    return BadRequest("Expecting 'hostname=value' in query.\n");
+  }
+
+  // Make sure we have 'port=value' and that it's a valid number.
+  Result<uint16_t> port = numify<uint16_t>(request.query.get("port"));
+
+  if (port.isNone()) {
+    return BadRequest("Expecting 'port=value' in query.\n");
+  } else if (port.isError()) {
+    return BadRequest("Invalid port: " + port.error() + ".\n");
   }
 
   LOG(INFO) << "Slaves manager received HTTP request to deactivate slave at "

Modified: incubator/mesos/trunk/third_party/libprocess/include/stout/numify.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/include/stout/numify.hpp?rev=1386845&r1=1386844&r2=1386845&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/include/stout/numify.hpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/include/stout/numify.hpp Mon Sep 17 21:15:41 2012
@@ -8,6 +8,8 @@
 #include <boost/lexical_cast.hpp>
 
 #include "format.hpp"
+#include "option.hpp"
+#include "result.hpp"
 #include "try.hpp"
 
 template <typename T>
@@ -23,7 +25,20 @@ Try<T> numify(const std::string& s)
   }
 }
 
-// TODO(bmahler): Add a numify that takes an Option<string> to simplify
-// http request handling logic.
+
+template <typename T>
+Result<T> numify(const Option<std::string>& s)
+{
+  if (s.isSome()) {
+    Try<T> t = numify<T>(s.get());
+    if (t.isSome()) {
+      return Result<T>::some(t.get());
+    } else if (t.isError()) {
+      return Result<T>::error(t.error());
+    }
+  }
+
+  return Result<T>::none();
+}
 
 #endif // __STOUT_NUMIFY_HPP__