Compare commits

...

2 commits

Author SHA1 Message Date
Benjamin Renard
6ac1216ed8 LdapClient: improve just-try mode detection
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
2022-05-27 15:29:16 +02:00
Benjamin Renard
5f0527f0c3 config: add get_option() method 2022-05-27 15:15:34 +02:00
2 changed files with 20 additions and 4 deletions

View file

@ -649,6 +649,12 @@ class Config: # pylint: disable=too-many-instance-attributes
log.debug('get(%s, %s): %s (%s)', section, option, value, type(value))
return value
def get_option(self, option, default=None):
""" Get an argument parser option value """
if self.options and hasattr(self.options, option):
return getattr(self.options, option)
return default
def set(self, section, option, value):
""" Set option value """
assert self.config_parser, 'Unconfigured options parser'

View file

@ -369,6 +369,16 @@ class LdapClient:
return default
@property
def _just_try(self):
""" Check if just-try mode is enabled """
return self._get_option(
'just_try', default=(
self._config.get_option('just_try') if self._config
else False
)
)
def configure(self, comment=None, **kwargs):
""" Configure options on registered mylib.Config object """
assert self._config, "mylib.Config object not registered. Must be passed to __init__ as config keyword argument."
@ -697,7 +707,7 @@ class LdapClient:
for attr, values in attrs.items()
)
try:
if self._get_option('just_try', default=False):
if self._just_try:
log.debug('Just-try mode : do not really add object in LDAP')
return True
assert self._conn or self.initialize()
@ -766,7 +776,7 @@ class LdapClient:
log.debug('%s: No change detected on RDN attibute %s', ldap_obj['dn'], rdn_attr)
try:
if self._get_option('just_try', default=False):
if self._just_try:
log.debug('Just-try mode : do not really update object in LDAP')
return True
assert self._conn or self.initialize()
@ -792,7 +802,7 @@ class LdapClient:
:param new_dn_or_rdn: The new LDAP object's DN (or RDN)
"""
try:
if self._get_option('just_try', default=False):
if self._just_try:
log.debug('Just-try mode : do not really move object in LDAP')
return True
assert self._conn or self.initialize()
@ -811,7 +821,7 @@ class LdapClient:
:param ldap_obj: The original LDAP object to delete/drop
"""
try:
if self._get_option('just_try', default=False):
if self._just_try:
log.debug('Just-try mode : do not really drop object in LDAP')
return True
assert self._conn or self.initialize()