More on entry transformation

When beginning a new LDAP directory you don’t have to remain enslaved to the past. Transform and conquer!

Standing up a new LDAP directory whose structure is going to be a lot simpler than my original environment and initial attribute set much less extensive.

This is a script I wrote to transform “People” entries from their original form to something much simpler. The possibilities here are endless.

#!/usr/bin/perl
# Transform Source LDAP entries into Target entries,
# for bulk loading file produced by ldif-export utility on OpenDJ.
use strict;
use Net::LDAP;
use Net::LDAP::Entry;
use Net::LDAP::LDIF;
use List::Compare;

my $HOME = $ENV{'HOME'};

my $inldif = "$HOME/data/source.ldif";
my $outldif = "$HOME/data/target.ldif";

my @attrs = qw(objectclass uid cn sn givenname displayname title mail
 telephonenumber facsimiletelephonenumber mobile o ou departmentnumber
 c street l st postalcode manager);

get_infile();
write_ldif();

sub get_infile {
# FUTURE EXPANSION -- Maybe an sftp routine?
}

sub write_ldif {

  my $ldif = Net::LDAP::LDIF->new($inldif, 'r') or die $!;
  my $ldifw = Net::LDAP::LDIF->new($outldif, 'w') or die $!;

  while (not $ldif->eof() ) {
     my $entry = $ldif->read_entry();
     if ($ldif->error() ) {
        print "tError! ",$ldif->error(),"n"; 
     }
     else {
	my $dn = $entry->dn;
	if($dn =~ /uid=.+ou=people.+dc=com/gi) {

	   my($rdn,$superior) = split(",", $dn);
	   $superior = "ou=people,dc=example,dc=com";
	   my $newdn = $rdn . "," . $superior;
	   $entry->dn($newdn);

	   my $givenname = $entry->get_value('givenname');
	   my $sn = $entry->get_value('sn');
	   my $fullname = $givenname . " " . $sn;
	   $entry->replace('displayname' =>$fullname);

	   print $newdn, "n";
		  		  
	   my @eattrs = $entry->attributes;
	   my @eattrs = map { lc } @eattrs;

	   my $lc = List::Compare->new(@eattrs, @attrs);
	   my @eonly = $lc->get_unique;

	   foreach my $attr(@eonly) {
	 	$entry->delete($attr);
	   }

	   $ldifw->write($entry);

	}
     }
  }
  $ldifw->done;		
  $ldif->done;
}
__END__;