pythonbindings: allow add() to have an array of controls as second parameter
authorMatthieu Patou <mat@matws.net>
Tue, 22 Sep 2009 20:51:25 +0000 (00:51 +0400)
committerJelmer Vernooij <jelmer@samba.org>
Fri, 25 Sep 2009 23:31:38 +0000 (01:31 +0200)
Signed-off-by: Jelmer Vernooij <jelmer@samba.org>
source4/lib/ldb/pyldb.c
source4/lib/ldb/tests/python/api.py

index b4f03dc538640e1ba909df65d30e645ae6b35a27..1ba5109f2fde038464627f8a153bd3ed97d835d6 100644 (file)
@@ -663,21 +663,32 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
        Py_ssize_t dict_pos, msg_pos;
        struct ldb_message_element *msgel;
        struct ldb_message *msg;
        Py_ssize_t dict_pos, msg_pos;
        struct ldb_message_element *msgel;
        struct ldb_message *msg;
+       struct ldb_context *ldb_ctx;
+       struct ldb_request *req;
        PyObject *key, *value;
        PyObject *key, *value;
+       PyObject *py_controls = Py_None;
        TALLOC_CTX *mem_ctx;
        TALLOC_CTX *mem_ctx;
+       struct ldb_control **parsed_controls;
 
 
-       if (!PyArg_ParseTuple(args, "O", &py_msg))
+       if (!PyArg_ParseTuple(args, "O|O", &py_msg, &py_controls ))
                return NULL;
                return NULL;
+       ldb_ctx = PyLdb_AsLdbContext(self);
 
        mem_ctx = talloc_new(NULL);
 
        mem_ctx = talloc_new(NULL);
-
+       if (py_controls == Py_None) {
+               parsed_controls = NULL;
+       } else {
+               const char **controls = PyList_AsStringList(ldb_ctx, py_controls, "controls");
+               parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
+               talloc_free(controls);
+       }
        if (PyDict_Check(py_msg)) {
                PyObject *dn_value = PyDict_GetItemString(py_msg, "dn");
                msg = ldb_msg_new(mem_ctx);
                msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
                msg_pos = dict_pos = 0;
                if (dn_value) {
        if (PyDict_Check(py_msg)) {
                PyObject *dn_value = PyDict_GetItemString(py_msg, "dn");
                msg = ldb_msg_new(mem_ctx);
                msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
                msg_pos = dict_pos = 0;
                if (dn_value) {
-                       if (!PyObject_AsDn(msg, dn_value, PyLdb_AsLdbContext(self), &msg->dn)) {
+                       if (!PyObject_AsDn(msg, dn_value, ldb_ctx, &msg->dn)) {
                                PyErr_SetString(PyExc_TypeError, "unable to import dn object");
                                talloc_free(mem_ctx);
                                return NULL;
                                PyErr_SetString(PyExc_TypeError, "unable to import dn object");
                                talloc_free(mem_ctx);
                                return NULL;
@@ -713,8 +724,52 @@ static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
        } else {
                msg = PyLdbMessage_AsMessage(py_msg);
        }
        } else {
                msg = PyLdbMessage_AsMessage(py_msg);
        }
+        
+       ret = ldb_msg_sanity_check(ldb_ctx, msg);
+        if (ret != LDB_SUCCESS) {
+               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
+               talloc_free(mem_ctx);
+               return NULL;
+        }
+
+        ret = ldb_build_add_req(&req, ldb_ctx, ldb_ctx,
+                                        msg,
+                                        parsed_controls,
+                                        NULL,
+                                        ldb_op_default_callback,
+                                        NULL);
+
+        if (ret != LDB_SUCCESS) {
+               PyErr_SetString(PyExc_TypeError, "failed to build request");
+               talloc_free(mem_ctx);
+               return NULL;
+       }
+
+        /* do request and autostart a transaction */
+       /* Then let's LDB handle the message error in case of pb as they are meaningful */
 
 
-       ret = ldb_add(PyLdb_AsLdbContext(self), msg);
+        ret = ldb_transaction_start(ldb_ctx);
+        if (ret != LDB_SUCCESS) {
+               talloc_free(req);
+               talloc_free(mem_ctx);
+               PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
+        }
+
+        ret = ldb_request(ldb_ctx, req);
+        if (ret == LDB_SUCCESS) {
+                ret = ldb_wait(req->handle, LDB_WAIT_ALL);
+        } 
+
+       if (ret == LDB_SUCCESS) {
+                ret = ldb_transaction_commit(ldb_ctx);
+        } else {
+               ldb_transaction_cancel(ldb_ctx);
+               if (ldb_ctx->err_string == NULL) {
+                       /* no error string was setup by the backend */
+                       ldb_asprintf_errstring(ldb_ctx, "%s (%d)", ldb_strerror(ret), ret);
+               }
+       }
+       talloc_free(req);
        talloc_free(mem_ctx);
        PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
 
        talloc_free(mem_ctx);
        PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self));
 
index 133bd180c17b1275315f182f62e8ccb2380da6b2..956908d7d6946e26f53b400678da4f7a1f5cb1da 100755 (executable)
@@ -136,6 +136,14 @@ class SimpleLdb(unittest.TestCase):
         finally:
             l.delete(ldb.Dn(l, "dc=foo4"))
 
         finally:
             l.delete(ldb.Dn(l, "dc=foo4"))
 
+    def test_add_w_unhandled_ctrl(self):
+        l = ldb.Ldb(filename())
+        m = ldb.Message()
+        m.dn = ldb.Dn(l, "dc=foo4")
+        m["bla"] = "bla"
+        self.assertEquals(len(l.search()), 1)
+        self.assertRaises(ldb.LdbError, lambda: l.add(m,["search_options:1:2"]))
+
     def test_add_dict(self):
         l = ldb.Ldb(filename())
         m = {"dn": ldb.Dn(l, "dc=foo5"),
     def test_add_dict(self):
         l = ldb.Ldb(filename())
         m = {"dn": ldb.Dn(l, "dc=foo5"),