This is an automated email from the git hooks/post-receive script.
firstyear pushed a commit to branch master
in repository lib389.
commit b66a2d03997c82f558216e151b6622234b431b8a
Author: Ilias Stamatis <stamatis.iliass(a)gmail.com>
Date: Thu Jul 13 23:17:38 2017 +0300
Issue 78 - Add exists() method to DSLdapObject
Description: Add an exists() method to DSLdapObject which returns
a boolean indicating if an entry exists or not. This can be useful in
cases such as when we want to check whether an entry exists in order
to create it if needed, or when checking for the completion of a task
(no task entry means that the task have been completed).
https://pagure.io/lib389/issue/78
Author: Ilias95
Review by: wibrown (thank you so much!)
---
lib389/_mapped_object.py | 8 ++++++++
lib389/tasks.py | 10 ++++++++--
lib389/tests/cli/conf_plugins/memberof_test.py | 4 ++--
lib389/tests/mapped_object_test.py | 23 +++++++++++++++++++++++
4 files changed, 41 insertions(+), 4 deletions(-)
diff --git a/lib389/_mapped_object.py b/lib389/_mapped_object.py
index 6ed141b..c98f1bc 100644
--- a/lib389/_mapped_object.py
+++ b/lib389/_mapped_object.py
@@ -113,6 +113,14 @@ class DSLdapObject(DSLogging):
def raw_entry(self):
return self._instance.getEntry(self._dn)
+ def exists(self):
+ try:
+ self._instance.search_s(self._dn, ldap.SCOPE_BASE, attrsonly=1)
+ except ldap.NO_SUCH_OBJECT:
+ return False
+
+ return True
+
def display(self):
e = self._instance.getEntry(self._dn)
return e.__repr__()
diff --git a/lib389/tasks.py b/lib389/tasks.py
index 19b1f0b..b7e7357 100644
--- a/lib389/tasks.py
+++ b/lib389/tasks.py
@@ -32,16 +32,22 @@ class Task(DSLdapObject):
self._must_attributes = ['cn']
self._create_objectclasses = ['top', 'extensibleObject']
self._protected = False
+ self._exit_code = None
def is_complete(self):
"""Return True if task is complete, else False."""
self._exit_code = self.get_attr_val("nsTaskExitCode")
- return self._exit_code is not None
+ if not self.exists() or self._exit_code is not None:
+ return True
+ return False
def get_exit_code(self):
"""Return task's exit code if task is complete, else
None."""
if self.is_complete():
- return int(self._exit_code)
+ try:
+ return int(self._exit_code)
+ except TypeError:
+ return None
return None
def wait(self):
diff --git a/lib389/tests/cli/conf_plugins/memberof_test.py
b/lib389/tests/cli/conf_plugins/memberof_test.py
index d6dcdba..339138f 100644
--- a/lib389/tests/cli/conf_plugins/memberof_test.py
+++ b/lib389/tests/cli/conf_plugins/memberof_test.py
@@ -19,8 +19,8 @@ def topology(request):
topology = default_topology(request)
plugin = MemberOfPlugin(topology.standalone)
- # no memberof entry exists by default
- plugin.create()
+ if not plugin.exists():
+ plugin.create()
# At the moment memberof plugin needs to be enabled in order to perform
# syntax checking. Additionally, we have to restart the server in order
diff --git a/lib389/tests/mapped_object_test.py b/lib389/tests/mapped_object_test.py
new file mode 100644
index 0000000..89cabde
--- /dev/null
+++ b/lib389/tests/mapped_object_test.py
@@ -0,0 +1,23 @@
+# --- BEGIN COPYRIGHT BLOCK ---
+# Copyright (C) 2017 Red Hat, Inc.
+# All rights reserved.
+#
+# License: GPL (version 3 or any later version).
+# See LICENSE for details.
+# --- END COPYRIGHT BLOCK ---
+#
+
+from lib389.topologies import topology_st
+from lib389._mapped_object import DSLdapObject
+from lib389.idm.group import Group
+from lib389._constants import DEFAULT_SUFFIX
+
+
+def test_exists(topology_st):
+ """
+ Assert that exists method returns True when entry exists, else False.
+ """
+ group = Group(topology_st.standalone, dn="cn=MyTestGroup,ou=Groups," +
DEFAULT_SUFFIX)
+ assert not group.exists()
+ group.create(properties={'cn': 'MyTestGroup', 'ou':
'groups'})
+ assert group.exists()
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.