def cb_validate(self, tctx, kp, newval): global maapisock log.debug("==> kp=%s newval=%s" % (kp, newval)) rv = _confd.CONFD_OK threshold = 100 try: maapi.attach(maapisock, maapi_example_ns.ns.hash, tctx) mc = maapi.init_cursor(maapisock, tctx.th, items_keypath_string) val_sum = 0 keys = maapi.get_next(mc) while keys: if maapi.exists( maapisock, tctx.th, "%s{%s}/value" % (items_keypath_string, keys[0])): log.debug("value element exists") val = maapi.get_elem( maapisock, tctx.th, "%s{%s}/value" % (items_keypath_string, keys[0])) log.debug("val=%d", val) val_sum += int(val) keys = maapi.get_next(mc) maapi.destroy_cursor(mc) maapi.detach(maapisock, tctx) if val_sum > threshold: text = "Sum of value elements in %s is %u," \ " which is greater than %u!" % ( items_keypath_string, val_sum, threshold) dp.trans_seterr(tctx, text) log.warn(text) rv = _confd.CONFD_ERR except Exception as e: log.exception(e) rv = _confd.CONFD_ERR log.debug("<== rv=%d" % rv) return rv
def cb_get_next(self, tctx, kp, next): global maapisock log.debug("==> kp=%s next=%d" % (kp, next)) rv = _confd.CONFD_OK try: maapi.attach(maapisock, maapi_example_ns.ns.hash, tctx) if next == -1: # first call next = 0 n = 0 mc = maapi.init_cursor(maapisock, tctx.th, items_keypath_string) keys = maapi.get_next(mc) while keys and n != next: log.debug("n=%d" % n) keys = maapi.get_next(mc) n += 1 if not keys: log.debug("No more item entry, element not found.") dp.data_reply_next_key(tctx, None, -1) else: next += 1 dp.data_reply_next_key(tctx, keys, next) maapi.destroy_cursor(mc) maapi.detach(maapisock, tctx) except Exception as e: log.exception(e) rv = _confd.CONFD_ERR log.debug("<== rv=%d" % rv) return rv
def cb_get_elem(self, tctx, kp): global maapisock log.debug("==> kp=%s" % kp) rv = _confd.CONFD_OK try: maapi.attach(maapisock, maapi_example_ns.ns.hash, tctx) if isinstance( kp[0], _confd.XmlTag ) and kp[0].tag == maapi_example_ns.ns.maapi_example_value: val = maapi.get_elem( maapisock, tctx.th, "%s{%s}/value" % (items_keypath_string, kp[1][0])) dp.data_reply_value(tctx, val) else: dp.data_reply_not_found(tctx) maapi.detach(maapisock, tctx) except Exception as e: log.exception(e) rv = _confd.CONFD_ERR log.debug("<== rv=%d" % rv) return rv
def cb_finish(self, tctx): maapi.detach(maapi_socket, tctx) return confd.CONFD_OK