This removes the last use of dn.Editable* from IPA code.
For cases where an EditableDN was used, lists/generators of RDNs tend to
work better IMO.
When this is merged, Editable* can be removed (which I don't want to do
right now since there's a patch on review that would conflict).
--
Petr Viktorin
From 16fee8dd83d71789e97e85a88993d499cca34d9c Mon Sep 17 00:00:00 2001
From: Petr Viktorin <[email protected]>
Date: Wed, 1 Apr 2015 16:37:51 +0200
Subject: [PATCH] rename_managed: Remove use of EditableDN
This was the last use of EditableDN in IPA; the class can now be removed.
---
ipaserver/install/plugins/rename_managed.py | 51 ++++++++++++++++++-----------
1 file changed, 31 insertions(+), 20 deletions(-)
diff --git a/ipaserver/install/plugins/rename_managed.py b/ipaserver/install/plugins/rename_managed.py
index adb814c1799ebbdb57118acf1ba6a52550f2f818..c5e701a588bf34b0f86de197859e9c55e8db5dd0 100644
--- a/ipaserver/install/plugins/rename_managed.py
+++ b/ipaserver/install/plugins/rename_managed.py
@@ -21,7 +21,7 @@
from ipaserver.install.plugins.baseupdate import PreUpdate, PostUpdate
from ipalib import api, errors
from ipapython import ipautil
-from ipapython.dn import DN, EditableDN
+from ipapython.dn import DN
def entry_to_update(entry):
"""
@@ -41,7 +41,21 @@ def entry_to_update(entry):
return update
+
class GenerateUpdateMixin(object):
+ def _dn_replace(self, dn, old, new):
+ """Replace all occurences of "old" AVAs in a DN by "new"
+
+ If a replacement is made, return the resulting DN.
+ Otherwise, log, an raise ValueError.
+ """
+ new_dn = DN(new if ava == old else ava for ava in dn)
+ if new_dn == dn:
+ self.error("unable to replace '%s' with '%s' in '%s'",
+ old, new, dn)
+ raise ValueError('no replacement made')
+ return new_dn
+
def generate_update(self, deletes=False):
"""
We need to separate the deletes that need to happen from the
@@ -82,14 +96,13 @@ def generate_update(self, deletes=False):
pass
else:
# Compute the new dn by replacing the old container with the new container
- new_dn = EditableDN(entry.dn)
- if new_dn.replace(old_template_container, new_template_container) != 1:
- self.error("unable to replace '%s' with '%s' in '%s'",
- old_template_container, new_template_container, entry.dn)
+ try:
+ new_dn = self._dn_replace(entry.dn,
+ old=old_template_container,
+ new=new_template_container)
+ except ValueError:
continue
- new_dn = DN(new_dn)
-
# The old attributes become defaults for the new entry
new_update = {'dn': new_dn,
'default': entry_to_update(entry)}
@@ -103,23 +116,21 @@ def generate_update(self, deletes=False):
else:
# Update the template dn by replacing the old containter with the new container
- old_dn = entry['managedtemplate'][0]
- new_dn = EditableDN(old_dn)
- if new_dn.replace(old_template_container, new_template_container) != 1:
- self.error("unable to replace '%s' with '%s' in '%s'",
- old_template_container, new_template_container, old_dn)
+ try:
+ new_dn = self._dn_replace(entry['managedtemplate'][0],
+ old=old_template_container,
+ new=new_template_container)
+ except ValueError:
continue
- new_dn = DN(new_dn)
entry['managedtemplate'] = new_dn
- # Edit the dn, then convert it back to an immutable DN
- old_dn = entry.dn
- new_dn = EditableDN(old_dn)
- if new_dn.replace(old_definition_container, new_definition_container) != 1:
- self.error("unable to replace '%s' with '%s' in '%s'",
- old_definition_container, new_definition_container, old_dn)
+ # Update the entry dn similarly
+ try:
+ new_dn = self._dn_replace(entry.dn,
+ old=old_definition_container,
+ new=new_definition_container)
+ except ValueError:
continue
- new_dn = DN(new_dn)
# The old attributes become defaults for the new entry
new_update = {'dn': new_dn,
--
2.1.0
--
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code