client_python.cc 15.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

// Enable this if you use s# variants with PyArg_ParseTuple(), see
// http://docs.python.org/py3k/c-api/arg.html#strings-and-buffers
//#define PY_SSIZE_T_CLEAN

// Python.h needs to be placed at the head of the program file, see:
// http://docs.python.org/py3k/extending/extending.html#a-simple-example
#include <Python.h>

#include <util/python/pycppwrapper_util.h>

#include <datasrc/client.h>
26
#include <datasrc/factory.h>
27
#include <datasrc/database.h>
Jelte Jansen's avatar
Jelte Jansen committed
28
#include <datasrc/data_source.h>
29
30
#include <datasrc/sqlite3_accessor.h>
#include <datasrc/iterator.h>
31
#include <datasrc/client_list.h>
32
33
34
35
36
37
38
39
40
41

#include <dns/python/name_python.h>
#include <dns/python/rrset_python.h>
#include <dns/python/pydnspp_common.h>

#include "datasrc.h"
#include "client_python.h"
#include "finder_python.h"
#include "iterator_python.h"
#include "updater_python.h"
42
#include "journal_reader_python.h"
43
#include "client_inc.cc"
44
45
46

using namespace std;
using namespace isc::util::python;
Jelte Jansen's avatar
Jelte Jansen committed
47
using namespace isc::dns::python;
48
49
50
51
using namespace isc::datasrc;
using namespace isc::datasrc::python;

namespace {
52
53
54
// The s_* Class simply covers one instantiation of the object
class s_DataSourceClient : public PyObject {
public:
55
56
    s_DataSourceClient() :
        cppobj(NULL),
57
58
        client(NULL),
        keeper(NULL)
59
    {};
60
    DataSourceClientContainer* cppobj;
61
    DataSourceClient* client;
62
63
64
65
    // We can't rely on the constructor or destructor being
    // called, so this is a pointer to shared pointer, so we
    // can call the new and delete explicitly.
    boost::shared_ptr<ClientList::FindResult::LifeKeeper>* keeper;
66
67
};

68
PyObject*
69
DataSourceClient_findZone(PyObject* po_self, PyObject* args) {
70
71
    s_DataSourceClient* const self = static_cast<s_DataSourceClient*>(po_self);
    PyObject *name;
Jelte Jansen's avatar
Jelte Jansen committed
72
    if (PyArg_ParseTuple(args, "O!", &name_type, &name)) {
Jelte Jansen's avatar
Jelte Jansen committed
73
74
        try {
            DataSourceClient::FindResult find_result(
75
                self->client->findZone(PyName_ToName(name)));
Jelte Jansen's avatar
Jelte Jansen committed
76
77
78

            result::Result r = find_result.code;
            ZoneFinderPtr zfp = find_result.zone_finder;
79
            // Use N instead of O so refcount isn't increased twice
80
            return (Py_BuildValue("IN", r, createZoneFinderObject(zfp, po_self)));
Jelte Jansen's avatar
Jelte Jansen committed
81
82
83
        } catch (const std::exception& exc) {
            PyErr_SetString(getDataSourceException("Error"), exc.what());
            return (NULL);
84
        } catch (...) {
Jelte Jansen's avatar
Jelte Jansen committed
85
86
            PyErr_SetString(getDataSourceException("Error"),
                            "Unexpected exception");
87
            return (NULL);
Jelte Jansen's avatar
Jelte Jansen committed
88
        }
89
90
91
92
93
94
    } else {
        return (NULL);
    }
}

PyObject*
95
DataSourceClient_getIterator(PyObject* po_self, PyObject* args) {
96
    s_DataSourceClient* const self = static_cast<s_DataSourceClient*>(po_self);
97
    PyObject* name_obj;
98
    PyObject* separate_rrs_obj = NULL;
99
    if (PyArg_ParseTuple(args, "O!|O", &name_type, &name_obj,
100
                         &separate_rrs_obj)) {
Jelte Jansen's avatar
Jelte Jansen committed
101
        try {
102
103
            bool separate_rrs = false;
            if (separate_rrs_obj != NULL) {
104
105
                // store result in local var so we can explicitely check for
                // -1 error return value
106
107
108
109
                int separate_rrs_true = PyObject_IsTrue(separate_rrs_obj);
                if (separate_rrs_true == 1) {
                    separate_rrs = true;
                } else if (separate_rrs_true == -1) {
110
                    PyErr_SetString(getDataSourceException("Error"),
111
                                    "Error getting value of separate_rrs");
112
113
                    return (NULL);
                }
114
            }
Jelte Jansen's avatar
Jelte Jansen committed
115
            return (createZoneIteratorObject(
116
                self->client->getIterator(PyName_ToName(name_obj),
117
                                                        separate_rrs),
118
                po_self));
Jelte Jansen's avatar
Jelte Jansen committed
119
        } catch (const isc::NotImplemented& ne) {
Jelte Jansen's avatar
Jelte Jansen committed
120
121
            PyErr_SetString(getDataSourceException("NotImplemented"),
                            ne.what());
Jelte Jansen's avatar
Jelte Jansen committed
122
            return (NULL);
Jelte Jansen's avatar
Jelte Jansen committed
123
124
        } catch (const DataSourceError& dse) {
            PyErr_SetString(getDataSourceException("Error"), dse.what());
Jelte Jansen's avatar
Jelte Jansen committed
125
            return (NULL);
Jelte Jansen's avatar
Jelte Jansen committed
126
127
128
        } catch (const std::exception& exc) {
            PyErr_SetString(getDataSourceException("Error"), exc.what());
            return (NULL);
129
        } catch (...) {
Jelte Jansen's avatar
Jelte Jansen committed
130
131
            PyErr_SetString(getDataSourceException("Error"),
                            "Unexpected exception");
132
            return (NULL);
Jelte Jansen's avatar
Jelte Jansen committed
133
        }
134
135
136
137
138
139
    } else {
        return (NULL);
    }
}

PyObject*
140
DataSourceClient_getUpdater(PyObject* po_self, PyObject* args) {
141
142
    s_DataSourceClient* const self = static_cast<s_DataSourceClient*>(po_self);
    PyObject *name_obj;
143
144
145
146
147
148
149
    PyObject *replace_obj = NULL;
    PyObject *journaling_obj = Py_False;
    if (PyArg_ParseTuple(args, "O!O|O", &name_type, &name_obj,
                         &replace_obj, &journaling_obj) &&
        PyBool_Check(replace_obj) && PyBool_Check(journaling_obj)) {
        const bool replace = (replace_obj != Py_False);
        const bool journaling = (journaling_obj == Py_True);
Jelte Jansen's avatar
Jelte Jansen committed
150
        try {
151
            ZoneUpdaterPtr updater =
152
                self->client->getUpdater(PyName_ToName(name_obj),
153
                                                       replace, journaling);
154
155
156
            if (!updater) {
                return (Py_None);
            }
157
            return (createZoneUpdaterObject(updater, po_self));
Jelte Jansen's avatar
Jelte Jansen committed
158
        } catch (const isc::NotImplemented& ne) {
Jelte Jansen's avatar
Jelte Jansen committed
159
160
            PyErr_SetString(getDataSourceException("NotImplemented"),
                            ne.what());
Jelte Jansen's avatar
Jelte Jansen committed
161
            return (NULL);
Jelte Jansen's avatar
Jelte Jansen committed
162
163
        } catch (const DataSourceError& dse) {
            PyErr_SetString(getDataSourceException("Error"), dse.what());
Jelte Jansen's avatar
Jelte Jansen committed
164
            return (NULL);
Jelte Jansen's avatar
Jelte Jansen committed
165
166
167
        } catch (const std::exception& exc) {
            PyErr_SetString(getDataSourceException("Error"), exc.what());
            return (NULL);
168
        } catch (...) {
Jelte Jansen's avatar
Jelte Jansen committed
169
170
            PyErr_SetString(getDataSourceException("Error"),
                            "Unexpected exception");
171
            return (NULL);
Jelte Jansen's avatar
Jelte Jansen committed
172
        }
173
    } else {
174
175
176
177
178
179
180
181
182
        // PyBool_Check doesn't set the error, so we have to set it ourselves.
        if (replace_obj != NULL && !PyBool_Check(replace_obj)) {
            PyErr_SetString(PyExc_TypeError, "'replace' for "
                            "DataSourceClient.get_updater must be boolean");
        }
        if (!PyBool_Check(journaling_obj)) {
            PyErr_SetString(PyExc_TypeError, "'journaling' for "
                            "DataSourceClient.get_updater must be boolean");
        }
183
184
185
186
        return (NULL);
    }
}

187
188
189
190
191
192
193
194
PyObject*
DataSourceClient_getJournalReader(PyObject* po_self, PyObject* args) {
    s_DataSourceClient* const self = static_cast<s_DataSourceClient*>(po_self);
    PyObject *name_obj;
    unsigned long begin_obj, end_obj;

    if (PyArg_ParseTuple(args, "O!kk", &name_type, &name_obj,
                         &begin_obj, &end_obj)) {
195
196
        try {
            pair<ZoneJournalReader::Result, ZoneJournalReaderPtr> result =
197
                self->client->getJournalReader(
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
                    PyName_ToName(name_obj), static_cast<uint32_t>(begin_obj),
                    static_cast<uint32_t>(end_obj));
            PyObject* po_reader;
            if (result.first == ZoneJournalReader::SUCCESS) {
                po_reader = createZoneJournalReaderObject(result.second,
                                                          po_self);
            } else {
                po_reader = Py_None;
                Py_INCREF(po_reader); // this will soon be released
            }
            PyObjectContainer container(po_reader);
            return (Py_BuildValue("(iO)", result.first, container.get()));
        } catch (const isc::NotImplemented& ex) {
            PyErr_SetString(getDataSourceException("NotImplemented"),
                            ex.what());
        } catch (const DataSourceError& ex) {
            PyErr_SetString(getDataSourceException("Error"), ex.what());
        } catch (const std::exception& ex) {
216
            PyErr_SetString(PyExc_SystemError, ex.what());
217
        } catch (...) {
218
            PyErr_SetString(PyExc_SystemError, "Unexpected exception");
219
220
221
222
223
        }
    }
    return (NULL);
}

224
225
226
227
228
229
230
// This list contains the actual set of functions we have in
// python. Each entry has
// 1. Python method name
// 2. Our static function here
// 3. Argument type
// 4. Documentation
PyMethodDef DataSourceClient_methods[] = {
231
232
    { "find_zone", DataSourceClient_findZone, METH_VARARGS,
      DataSourceClient_findZone_doc },
Jelte Jansen's avatar
Jelte Jansen committed
233
    { "get_iterator",
234
      DataSourceClient_getIterator, METH_VARARGS,
235
      DataSourceClient_getIterator_doc },
236
    { "get_updater", DataSourceClient_getUpdater,
Jelte Jansen's avatar
Jelte Jansen committed
237
      METH_VARARGS, DataSourceClient_getUpdater_doc },
238
    { "get_journal_reader", DataSourceClient_getJournalReader,
239
      METH_VARARGS, DataSourceClient_getJournalReader_doc },
240
241
242
243
    { NULL, NULL, 0, NULL }
};

int
244
245
DataSourceClient_init(PyObject* po_self, PyObject* args, PyObject*) {
    s_DataSourceClient* self = static_cast<s_DataSourceClient*>(po_self);
246
247
    char* ds_type_str;
    char* ds_config_str;
248
    try {
249
250
        // Turn the given argument into config Element; then simply call
        // factory class to do its magic
251
252
253

        // for now, ds_config must be JSON string
        if (PyArg_ParseTuple(args, "ss", &ds_type_str, &ds_config_str)) {
254
255
256
257
            isc::data::ConstElementPtr ds_config =
                isc::data::Element::fromJSON(ds_config_str);
            self->cppobj = new DataSourceClientContainer(ds_type_str,
                                                         ds_config);
258
            self->client = &self->cppobj->getInstance();
259
            self->keeper = NULL;
260
261
262
263
            return (0);
        } else {
            return (-1);
        }
264
265
266
267
    } catch (const isc::data::JSONError& je) {
        const string ex_what = "JSON parse error in data source configuration "
                               "data for type " +
                               string(ds_type_str) + ":" + je.what();
268
        PyErr_SetString(getDataSourceException("Error"), ex_what.c_str());
269
270
271
272
273
274
        return (-1);
    } catch (const DataSourceError& dse) {
        const string ex_what = "Failed to create DataSourceClient of type " +
                               string(ds_type_str) + ":" + dse.what();
        PyErr_SetString(getDataSourceException("Error"), ex_what.c_str());
        return (-1);
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    } catch (const exception& ex) {
        const string ex_what = "Failed to construct DataSourceClient object: " +
            string(ex.what());
        PyErr_SetString(getDataSourceException("Error"), ex_what.c_str());
        return (-1);
    } catch (...) {
        PyErr_SetString(PyExc_RuntimeError,
            "Unexpected exception in constructing DataSourceClient");
        return (-1);
    }
    PyErr_SetString(PyExc_TypeError,
                    "Invalid arguments to DataSourceClient constructor");

    return (-1);
}

void
292
293
DataSourceClient_destroy(PyObject* po_self) {
    s_DataSourceClient* const self = static_cast<s_DataSourceClient*>(po_self);
294
    delete self->cppobj;
295
    delete self->keeper;
296
    self->cppobj = NULL;
297
    self->client = NULL;
298
    self->keeper = NULL;
299
300
301
302
303
304
305
306
307
308
309
310
311
312
    Py_TYPE(self)->tp_free(self);
}

} // end anonymous namespace

namespace isc {
namespace datasrc {
namespace python {
// This defines the complete type for reflection in python and
// parsing of PyObject* to s_DataSourceClient
// Most of the functions are not actually implemented and NULL here.
PyTypeObject datasourceclient_type = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "datasrc.DataSourceClient",
Jelte Jansen's avatar
Jelte Jansen committed
313
    sizeof(s_DataSourceClient),         // tp_basicsize
314
    0,                                  // tp_itemsize
315
    DataSourceClient_destroy,           // tp_dealloc
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
    NULL,                               // tp_print
    NULL,                               // tp_getattr
    NULL,                               // tp_setattr
    NULL,                               // tp_reserved
    NULL,                               // tp_repr
    NULL,                               // tp_as_number
    NULL,                               // tp_as_sequence
    NULL,                               // tp_as_mapping
    NULL,                               // tp_hash
    NULL,                               // tp_call
    NULL,                               // tp_str
    NULL,                               // tp_getattro
    NULL,                               // tp_setattro
    NULL,                               // tp_as_buffer
    Py_TPFLAGS_DEFAULT,                 // tp_flags
331
    DataSourceClient_doc,
332
333
334
335
336
337
    NULL,                               // tp_traverse
    NULL,                               // tp_clear
    NULL,                               // tp_richcompare
    0,                                  // tp_weaklistoffset
    NULL,                               // tp_iter
    NULL,                               // tp_iternext
Jelte Jansen's avatar
Jelte Jansen committed
338
    DataSourceClient_methods,           // tp_methods
339
340
341
342
343
344
345
    NULL,                               // tp_members
    NULL,                               // tp_getset
    NULL,                               // tp_base
    NULL,                               // tp_dict
    NULL,                               // tp_descr_get
    NULL,                               // tp_descr_set
    0,                                  // tp_dictoffset
346
    DataSourceClient_init,              // tp_init
347
348
349
350
351
352
353
354
355
356
357
358
359
    NULL,                               // tp_alloc
    PyType_GenericNew,                  // tp_new
    NULL,                               // tp_free
    NULL,                               // tp_is_gc
    NULL,                               // tp_bases
    NULL,                               // tp_mro
    NULL,                               // tp_cache
    NULL,                               // tp_subclasses
    NULL,                               // tp_weaklist
    NULL,                               // tp_del
    0                                   // tp_version_tag
};

360
PyObject*
361
362
363
364
wrapDataSourceClient(DataSourceClient* client,
                     const boost::shared_ptr<ClientList::FindResult::
                     LifeKeeper>& life_keeper)
{
365
366
367
    s_DataSourceClient *result =
        static_cast<s_DataSourceClient*>(PyObject_New(s_DataSourceClient,
                                                      &datasourceclient_type));
368
369
    CPPPyObjectContainer<s_DataSourceClient, DataSourceClientContainer>
        container(result);
370
    result->cppobj = NULL;
371
372
    result->keeper =
        new boost::shared_ptr<ClientList::FindResult::LifeKeeper>(life_keeper);
373
    result->client = client;
374
    return (container.release());
375
376
}

377
378
379
380
381
382
DataSourceClient&
PyDataSourceClient_ToDataSourceClient(PyObject* client_obj) {
    if (client_obj == NULL) {
        isc_throw(PyCPPWrapperException,
                  "obj argument NULL in Name PyObject conversion");
    }
383
384
    const s_DataSourceClient* client =
        static_cast<const s_DataSourceClient*>(client_obj);
385
386
387
    return (*client->client);
}

388
389
390
} // namespace python
} // namespace datasrc
} // namespace isc