예제 #1
0
/**
 *******************************************************************************************************
 * Closes already opened connection to the database.
 *
 * @param self                  AerospikeClient object
 * @param args                  The args is a tuple object containing an argument
 *                              list passed from Python to a C function
 * @param kwds                  Dictionary of keywords
 *
 * Returns None.
 * In case of error,appropriate exceptions will be raised.
 *******************************************************************************************************
 */
PyObject * AerospikeClient_Close(AerospikeClient * self, PyObject * args, PyObject * kwds)
{
	as_error err;
	char *alias_to_search = NULL;

	// Initialize error
	as_error_init(&err);

	if (!self || !self->as) {
		as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object");
		goto CLEANUP;
	}

	alias_to_search = return_search_string(self->as);
	PyObject *py_persistent_item = NULL;

	py_persistent_item = PyDict_GetItemString(py_global_hosts, alias_to_search); 
	if (py_persistent_item) {
		close_aerospike_object(self->as, &err, alias_to_search, py_persistent_item);
	} else {
		aerospike_close(self->as, &err);

		for (unsigned int i = 0; i < self->as->config.hosts_size; i++) {
			free((void *) self->as->config.hosts[i].addr);
		}

		Py_BEGIN_ALLOW_THREADS
		aerospike_destroy(self->as);
		Py_END_ALLOW_THREADS
	}
	self->is_conn_16 = false;
	self->as = NULL;
	PyMem_Free(alias_to_search);
	alias_to_search = NULL;

	Py_INCREF(Py_None);

CLEANUP:
	if ( err.code != AEROSPIKE_OK ) {
		PyObject * py_err = NULL;
		error_to_pyobject(&err, &py_err);
		PyObject *exception_type = raise_exception(&err);
		PyErr_SetObject(exception_type, py_err);
		Py_DECREF(py_err);
		return NULL;
	}
	return Py_None;
}
예제 #2
0
/**
 *******************************************************************************************************
 * Changes the password of a particular user in the Aerospike DB.
 *
 * @param self                  AerospikeClient object
 * @param args                  The args is a tuple object containing an argument
 *                              list passed from Python to a C function
 * @param kwds                  Dictionary of keywords
 *
 * Returns an integer status. 0(Zero) is success value.
 * In case of error,appropriate exceptions will be raised.
 *******************************************************************************************************
 */
PyObject * AerospikeClient_Admin_Change_Password( AerospikeClient *self, PyObject *args, PyObject *kwds )
{
	// Initialize error
	as_error err;
	as_error_init(&err);

	// Python Function Arguments
	PyObject * py_policy = NULL;
	PyObject * py_user = NULL;
	PyObject * py_password = NULL;

	as_policy_admin admin_policy;
	as_policy_admin *admin_policy_p = NULL;

	// Python Function Keyword Arguments
	static char * kwlist[] = {"user", "password", "policy", NULL};

	// Python Function Argument Parsing
	if (PyArg_ParseTupleAndKeywords(args, kwds, "OO|O:admin_change_password", kwlist,
				&py_user, &py_password, &py_policy) == false) {
		return NULL;
	}

	if (!self || !self->as) {
		as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object");
		goto CLEANUP;
	}

	if (!self->is_conn_16) {
		as_error_update(&err, AEROSPIKE_ERR_CLUSTER, "No connection to aerospike cluster");
		goto CLEANUP;
	}

	// Aerospike Operation Arguments
	char *user = NULL, *password = NULL;

	// Convert python object to policy_admin
	pyobject_to_policy_admin(&err, py_policy, &admin_policy, &admin_policy_p,
			&self->as->config.policies.admin);
	if (err.code != AEROSPIKE_OK) {
		goto CLEANUP;
	}

	// Convert python objects into username and password strings
	if (!PyString_Check(py_user)) {
		as_error_update(&err, AEROSPIKE_ERR_PARAM, "Username should be a string");
		goto CLEANUP;
	}

	user = PyString_AsString(py_user);

	if (!PyString_Check(py_password)) {
		as_error_update(&err, AEROSPIKE_ERR_PARAM, "Password should be a string");
		goto CLEANUP;
	}

	password = PyString_AsString(py_password);

	// Invoke operation
	Py_BEGIN_ALLOW_THREADS
	aerospike_change_password( self->as, &err, admin_policy_p, user, password );
	Py_END_ALLOW_THREADS

	char *alias_to_search = NULL;
	alias_to_search = return_search_string(self->as);
	PyObject *py_persistent_item = NULL;

	py_persistent_item = PyDict_GetItemString(py_global_hosts, alias_to_search); 
	if (py_persistent_item) {
		PyDict_DelItemString(py_global_hosts, alias_to_search);
		AerospikeGlobalHosts_Del(py_persistent_item);
	}
	PyMem_Free(alias_to_search);
	alias_to_search = NULL;

	if (err.code != AEROSPIKE_OK) {
		as_error_update(&err, err.code, NULL);
		goto CLEANUP;
	}

CLEANUP:

	if (err.code != AEROSPIKE_OK) {
		PyObject * py_err = NULL;
		error_to_pyobject(&err, &py_err);
		PyObject *exception_type = raise_exception(&err);
		PyErr_SetObject(exception_type, py_err);
		Py_DECREF(py_err);
		return NULL;
	}

	return PyLong_FromLong(0);
}