Enhance Host plugin to provide not only "Managed By" list but also
a list of managed hosts. The new list is generated only when --all
option is passed.

https://fedorahosted.org/freeipa/ticket/993

>From 77a3fbf07ab7e2e60ae442632c6be1cd8f9eee4a Mon Sep 17 00:00:00 2001
From: Martin Kosek <mko...@redhat.com>
Date: Mon, 13 Jun 2011 16:23:09 +0200
Subject: [PATCH] Add a list of managed hosts

Enhance Host plugin to provide not only "Managed By" list but also
a list of managed hosts. The new list is generated only when --all
option is passed.

https://fedorahosted.org/freeipa/ticket/993
---
 ipalib/plugins/host.py |   36 ++++++++++++++++++++++++++++++++++++
 1 files changed, 36 insertions(+), 0 deletions(-)

diff --git a/ipalib/plugins/host.py b/ipalib/plugins/host.py
index a602df4d13757f06a7e7ccce3213e068f752ed35..29f659f9c7aa09267957853b6e801e59495e2c1f 100644
--- a/ipalib/plugins/host.py
+++ b/ipalib/plugins/host.py
@@ -158,6 +158,9 @@ host_output_params = (
     Str('managedby_host',
         label='Managed by',
     ),
+    Str('managing_host',
+        label='Managing',
+    ),
     Str('subject',
         label=_('Subject'),
     ),
@@ -216,6 +219,7 @@ class host(LDAPObject):
         'enrolledby': ['user'],
         'memberof': ['hostgroup', 'netgroup', 'role', 'hbacrule', 'sudorule'],
         'managedby': ['host'],
+        'managing': ['host'],
         'memberofindirect': ['hostgroup', 'netgroup', 'role', 'hbacrule',
         'sudorule'],
     }
@@ -224,6 +228,7 @@ class host(LDAPObject):
         'memberof': ('Member Of', 'in_', 'not_in_'),
         'enrolledby': ('Enrolled by', 'enroll_by_', 'not_enroll_by_'),
         'managedby': ('Managed by', 'man_by_', 'not_man_by_'),
+        'managing': ('Managing', 'man_', 'not_man_'),
     }
 
     label = _('Hosts')
@@ -302,6 +307,23 @@ class host(LDAPObject):
                 pass
         return dn
 
+    def get_managed_hosts(self, dn):
+        host_filter = 'managedBy=%s' % dn
+        host_attrs = ['fqdn']
+        ldap = self.api.Backend.ldap2
+        managed_hosts = []
+
+        try:
+            (hosts, truncated) = ldap.find_entries(base_dn=self.container_dn,
+                                    filter=host_filter, attrs_list=host_attrs)
+
+            for host in hosts:
+                managed_hosts.append(host[0])
+        except errors.NotFound:
+            return []
+
+        return managed_hosts
+
 api.register(host)
 
 
@@ -416,6 +438,10 @@ class host_add(LDAPCreate):
                 reason=_('The host was added but the DNS update failed with: %(exc)s') % dict(exc=exc)
             )
         set_certificate_attrs(entry_attrs)
+
+        if options.get('all', False):
+                entry_attrs['managing'] = self.obj.get_managed_hosts(dn)
+
         return dn
 
 api.register(host_add)
@@ -611,6 +637,10 @@ class host_mod(LDAPUpdate):
         if options.get('random', False):
             entry_attrs['randompassword'] = unicode(getattr(context, 'randompassword'))
         set_certificate_attrs(entry_attrs)
+
+        if options.get('all', False):
+            entry_attrs['managing'] = self.obj.get_managed_hosts(dn)
+
         return dn
 
 api.register(host_mod)
@@ -638,6 +668,9 @@ class host_find(LDAPSearch):
             entry_attrs = entry[1]
             set_certificate_attrs(entry_attrs)
 
+            if options.get('all', False):
+                entry_attrs['managing'] = self.obj.get_managed_hosts(entry[0])
+
 api.register(host_find)
 
 
@@ -664,6 +697,9 @@ class host_show(LDAPRetrieve):
 
         set_certificate_attrs(entry_attrs)
 
+        if options.get('all', False):
+            entry_attrs['managing'] = self.obj.get_managed_hosts(dn)
+
         return dn
 
     def forward(self, *keys, **options):
-- 
1.7.5.2

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to