]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr: return get_ceph_option result as typed Py object (not string)
authorSage Weil <sage@redhat.com>
Fri, 23 Aug 2019 17:14:24 +0000 (12:14 -0500)
committerSage Weil <sage@redhat.com>
Tue, 3 Sep 2019 15:32:00 +0000 (10:32 -0500)
We already return module options as the proper Py type; do the same for
native ceph options.

Signed-off-by: Sage Weil <sage@redhat.com>
src/mgr/BaseMgrModule.cc
src/mgr/CMakeLists.txt
src/mgr/PyModule.cc
src/mgr/PyUtil.cc [new file with mode: 0644]
src/mgr/PyUtil.h [new file with mode: 0644]

index 3380250bf0bea9127a287fa4c16f2964c317c8ca..e69d28c3cfda0822048c35210f8836bd27fd8c83 100644 (file)
@@ -26,6 +26,7 @@
 #include "common/errno.h"
 #include "common/version.h"
 
+#include "PyUtil.h"
 #include "BaseMgrModule.h"
 #include "Gil.h"
 
@@ -393,11 +394,13 @@ ceph_option_get(BaseMgrModule *self, PyObject *args)
     return nullptr;
   }
 
-  std::string value;
-  int r = g_conf().get_val(string(what), &value);
-  if (r >= 0) {
+  const Option *opt = g_conf().find_option(string(what));
+  if (opt) {
+    std::string value;
+    int r = g_conf().get_val(string(what), &value);
+    assert(r >= 0);
     dout(10) << "ceph_option_get " << what << " found: " << value << dendl;
-    return PyString_FromString(value.c_str());
+    return get_python_typed_option_value(opt->type, value);
   } else {
     dout(4) << "ceph_option_get " << what << " not found " << dendl;
     Py_RETURN_NONE;
index 3c527a396e3917bb4d99044ddf33ade2759f8f81..6636e0a6f575a9919ae3bb86faaed2e362538b91 100644 (file)
@@ -15,6 +15,7 @@ set(mgr_srcs
   OSDPerfMetricTypes.cc
   OSDPerfMetricCollector.cc
   PyFormatter.cc
+  PyUtil.cc
   PyModule.cc
   PyModuleRegistry.cc
   PyModuleRunner.cc
index 53d9e2b95fa6253e34f7462d619d6d2b063bb9ff..e65b46befff10b86e15db9cd286d5dd6c34f0f65 100644 (file)
@@ -15,6 +15,7 @@
 #include "BaseMgrStandbyModule.h"
 #include "PyOSDMap.h"
 #include "MgrContext.h"
+#include "PyUtil.h"
 
 #include "PyModule.h"
 
@@ -626,29 +627,7 @@ PyObject *PyModule::get_typed_option_value(const std::string& name,
   // are set up exactly once during startup.
   auto p = options.find(name);
   if (p != options.end()) {
-    switch (p->second.type) {
-    case Option::TYPE_INT:
-    case Option::TYPE_UINT:
-    case Option::TYPE_SIZE:
-      return PyInt_FromString((char *)value.c_str(), nullptr, 0);
-    case Option::TYPE_SECS:
-    case Option::TYPE_FLOAT:
-      {
-       PyObject *s = PyString_FromString(value.c_str());
-       PyObject *f = PyFloat_FromString(s, nullptr);
-       Py_DECREF(s);
-       return f;
-      }
-    case Option::TYPE_BOOL:
-      if (value == "1" || value == "true" || value == "True" ||
-         value == "on" || value == "yes") {
-       Py_INCREF(Py_True);
-       return Py_True;
-      } else {
-       Py_INCREF(Py_False);
-       return Py_False;
-      }
-    }
+    return get_python_typed_option_value((Option::type_t)p->second.type, value);
   }
   return PyString_FromString(value.c_str());
 }
diff --git a/src/mgr/PyUtil.cc b/src/mgr/PyUtil.cc
new file mode 100644 (file)
index 0000000..04a3a5d
--- /dev/null
@@ -0,0 +1,40 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "PyUtil.h"
+#include "PythonCompat.h"
+
+PyObject *get_python_typed_option_value(
+  Option::type_t type,
+  const std::string& value)
+{
+  switch (type) {
+  case Option::TYPE_INT:
+  case Option::TYPE_UINT:
+  case Option::TYPE_SIZE:
+    return PyInt_FromString((char *)value.c_str(), nullptr, 0);
+  case Option::TYPE_SECS:
+  case Option::TYPE_FLOAT:
+    {
+      PyObject *s = PyString_FromString(value.c_str());
+      PyObject *f = PyFloat_FromString(s, nullptr);
+      Py_DECREF(s);
+      return f;
+    }
+  case Option::TYPE_BOOL:
+    if (value == "1" || value == "true" || value == "True" ||
+       value == "on" || value == "yes") {
+      Py_INCREF(Py_True);
+      return Py_True;
+    } else {
+      Py_INCREF(Py_False);
+      return Py_False;
+    }
+  case Option::TYPE_STR:
+  case Option::TYPE_ADDR:
+  case Option::TYPE_ADDRVEC:
+  case Option::TYPE_UUID:
+    break;
+  }
+  return PyString_FromString(value.c_str());
+}
diff --git a/src/mgr/PyUtil.h b/src/mgr/PyUtil.h
new file mode 100644 (file)
index 0000000..3d8bc7b
--- /dev/null
@@ -0,0 +1,13 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#pragma once
+
+#include <string>
+
+#include "Python.h"
+#include "common/options.h"
+
+PyObject *get_python_typed_option_value(
+  Option::type_t type,
+  const std::string& value);