2009-05-12 11:45:30 -05:00
|
|
|
# Authors:
|
|
|
|
# Rob Crittenden <rcritten@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2009 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License as
|
|
|
|
# published by the Free Software Foundation; version 2 only
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
"""
|
|
|
|
Groups of hosts.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from ipalib import api
|
2009-06-16 07:38:27 -05:00
|
|
|
from ipalib.plugins.basegroup import *
|
2009-05-12 11:45:30 -05:00
|
|
|
|
|
|
|
_container_dn = api.env.container_hostgroup
|
2009-05-22 05:12:27 -05:00
|
|
|
_default_attributes = ['cn', 'description', 'member', 'memberof']
|
|
|
|
_default_class = 'ipahostgroup'
|
2009-05-12 11:45:30 -05:00
|
|
|
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
class hostgroup(basegroup):
|
2009-05-12 11:45:30 -05:00
|
|
|
"""
|
|
|
|
Hostgroup object.
|
|
|
|
"""
|
|
|
|
container = _container_dn
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(hostgroup)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
|
|
|
|
2009-06-16 09:51:44 -05:00
|
|
|
class hostgroup_add(basegroup_add):
|
2009-05-12 11:45:30 -05:00
|
|
|
"""
|
|
|
|
Create a new hostgroup.
|
|
|
|
"""
|
2009-06-16 09:51:44 -05:00
|
|
|
base_classes = basegroup_add.base_classes + (_default_class, )
|
2009-05-12 11:45:30 -05:00
|
|
|
|
|
|
|
def execute(self, cn, **kw):
|
2009-06-16 09:51:44 -05:00
|
|
|
return super(hostgroup_add, self).execute(cn, **kw)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
2009-06-16 09:51:44 -05:00
|
|
|
api.register(hostgroup_add)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
|
|
|
|
2009-06-16 09:51:44 -05:00
|
|
|
class hostgroup_del(basegroup_del):
|
2009-05-12 11:45:30 -05:00
|
|
|
"""
|
|
|
|
Delete an existing hostgroup.
|
|
|
|
"""
|
|
|
|
container = _container_dn
|
|
|
|
|
|
|
|
def execute(self, cn, **kw):
|
2009-06-16 09:51:44 -05:00
|
|
|
return super(hostgroup_del, self).execute(cn, **kw)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
2009-06-16 09:51:44 -05:00
|
|
|
api.register(hostgroup_del)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
class hostgroup_mod(basegroup_mod):
|
2009-05-12 11:45:30 -05:00
|
|
|
"""
|
|
|
|
Edit an existing hostgroup.
|
|
|
|
"""
|
|
|
|
container = _container_dn
|
|
|
|
|
|
|
|
def execute(self, cn, **kw):
|
2009-06-16 07:38:27 -05:00
|
|
|
return super(hostgroup_mod, self).execute(cn, **kw)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(hostgroup_mod)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
class hostgroup_find(basegroup_find):
|
2009-05-12 11:45:30 -05:00
|
|
|
"""
|
|
|
|
Search the groups.
|
|
|
|
"""
|
|
|
|
container = _container_dn
|
|
|
|
|
2009-06-24 08:18:34 -05:00
|
|
|
def execute(self, term, **kw):
|
|
|
|
return super(hostgroup_find, self).execute(term, **kw)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(hostgroup_find)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
class hostgroup_show(basegroup_show):
|
2009-05-12 11:45:30 -05:00
|
|
|
"""
|
|
|
|
Examine an existing hostgroup.
|
|
|
|
"""
|
|
|
|
container = _container_dn
|
|
|
|
|
|
|
|
def execute(self, cn, **kw):
|
2009-06-16 07:38:27 -05:00
|
|
|
return super(hostgroup_show, self).execute(cn, **kw)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(hostgroup_show)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
class hostgroup_add_member(basegroup_add_member):
|
2009-05-12 11:45:30 -05:00
|
|
|
"""
|
|
|
|
Add members to hostgroup.
|
|
|
|
"""
|
|
|
|
container = _container_dn
|
|
|
|
|
|
|
|
takes_options = (
|
|
|
|
List('groups?',
|
|
|
|
cli_name='groups',
|
|
|
|
doc='comma-separated list of user groups to add'
|
|
|
|
),
|
|
|
|
List('hosts?',
|
|
|
|
cli_name='hosts',
|
|
|
|
doc='comma-separated list of hosts to add'
|
|
|
|
),
|
|
|
|
List('hostgroups?',
|
|
|
|
cli_name='hostgroups',
|
|
|
|
doc='comma-separated list of hostgroups to add'
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def execute(self, cn, **kw):
|
|
|
|
"""
|
|
|
|
Execute the group-add-member operation.
|
|
|
|
|
|
|
|
Returns a tuple containing the number of members added
|
|
|
|
and the updated entry.
|
|
|
|
|
|
|
|
:param cn: The group name to add new members to.
|
|
|
|
:param kw: groups is a comma-separated list of groups to add
|
|
|
|
:parem kw: users is a comma-separated list of users to add
|
|
|
|
"""
|
|
|
|
assert self.container
|
|
|
|
ldap = self.api.Backend.ldap2
|
2009-06-24 08:18:34 -05:00
|
|
|
(dn, entry_attrs) = ldap.find_entry_by_attr(
|
|
|
|
'cn', cn, self.filter_class, [''], self.container
|
|
|
|
)
|
2009-05-12 11:45:30 -05:00
|
|
|
to_add = []
|
2009-07-02 08:17:50 -05:00
|
|
|
add_failed = {}
|
2009-05-12 11:45:30 -05:00
|
|
|
completed = 0
|
|
|
|
|
|
|
|
members = kw.get('groups', [])
|
|
|
|
(to_add, add_failed) = find_members(
|
2009-05-22 05:12:27 -05:00
|
|
|
ldap, add_failed, members, 'cn', 'ipausergroup',
|
2009-05-12 11:45:30 -05:00
|
|
|
self.api.env.container_group
|
|
|
|
)
|
|
|
|
(completed, add_failed) = add_members(
|
|
|
|
ldap, completed, to_add, add_failed, dn, 'member'
|
|
|
|
)
|
|
|
|
|
|
|
|
members = kw.get('hosts', [])
|
|
|
|
(to_add, add_failed) = find_members(
|
2009-05-22 05:12:27 -05:00
|
|
|
ldap, add_failed, members, 'cn', 'ipahost',
|
2009-05-12 11:45:30 -05:00
|
|
|
self.api.env.container_host
|
|
|
|
)
|
|
|
|
(completed, add_failed) = add_members(
|
|
|
|
ldap, completed, to_add, add_failed, dn, 'member'
|
|
|
|
)
|
|
|
|
|
|
|
|
members = kw.get('hostgroups', [])
|
|
|
|
(to_add, add_failed) = find_members(
|
2009-05-22 05:12:27 -05:00
|
|
|
ldap, add_failed, members, 'cn', 'ipahostgroup',
|
2009-05-12 11:45:30 -05:00
|
|
|
self.api.env.container_hostgroup
|
|
|
|
)
|
|
|
|
(completed, add_failed) = add_members(
|
|
|
|
ldap, completed, to_add, add_failed, dn, 'member'
|
|
|
|
)
|
|
|
|
|
2009-07-02 08:17:50 -05:00
|
|
|
return (
|
|
|
|
completed, add_failed, ldap.get_entry(dn, self.default_attributes)
|
|
|
|
)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
2009-06-16 07:38:27 -05:00
|
|
|
api.register(hostgroup_add_member)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
|
|
|
|
2009-07-09 12:02:44 -05:00
|
|
|
class hostgroup_remove_member(basegroup_remove_member):
|
2009-05-12 11:45:30 -05:00
|
|
|
"""
|
|
|
|
Remove members from hostgroup.
|
|
|
|
"""
|
|
|
|
container = _container_dn
|
|
|
|
|
|
|
|
takes_options = (
|
|
|
|
List('groups?',
|
|
|
|
cli_name='groups',
|
|
|
|
doc='comma-separated list of user groups to add'
|
|
|
|
),
|
|
|
|
List('hosts?',
|
|
|
|
cli_name='hosts',
|
|
|
|
doc='comma-separated list of hosts to add'
|
|
|
|
),
|
|
|
|
List('hostgroups?',
|
|
|
|
cli_name='hostgroups',
|
|
|
|
doc='comma-separated list of hostgroups to add'
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
def execute(self, cn, **kw):
|
|
|
|
"""
|
2009-07-09 12:02:44 -05:00
|
|
|
Execute the group-remove-member operation.
|
2009-05-12 11:45:30 -05:00
|
|
|
|
|
|
|
Returns a tuple containing the number of members removed
|
|
|
|
and the updated entry.
|
|
|
|
|
|
|
|
:param cn: The group name to remove new members from.
|
|
|
|
:parem kw: users is a comma-separated list of users to remove
|
|
|
|
"""
|
|
|
|
assert self.container
|
|
|
|
ldap = self.api.Backend.ldap2
|
2009-06-24 08:18:34 -05:00
|
|
|
(dn, entry_attrs) = ldap.find_entry_by_attr(
|
|
|
|
'cn', cn, self.filter_class, [''], self.container
|
|
|
|
)
|
2009-05-12 11:45:30 -05:00
|
|
|
to_remove = []
|
2009-07-02 08:17:50 -05:00
|
|
|
rem_failed = {}
|
2009-05-12 11:45:30 -05:00
|
|
|
completed = 0
|
|
|
|
|
|
|
|
members = kw.get('groups', [])
|
2009-07-02 08:17:50 -05:00
|
|
|
(to_remove, rem_failed) = find_members(
|
|
|
|
ldap, rem_failed, members, 'cn', 'ipausergroup',
|
2009-05-12 11:45:30 -05:00
|
|
|
self.api.env.container_group
|
|
|
|
)
|
2009-07-02 08:17:50 -05:00
|
|
|
(completed, rem_failed) = del_members(
|
|
|
|
ldap, completed, to_remove, rem_failed, dn, 'member'
|
2009-05-12 11:45:30 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
members = kw.get('hosts', [])
|
2009-07-02 08:17:50 -05:00
|
|
|
(to_remove, rem_failed) = find_members(
|
|
|
|
ldap, rem_failed, members, 'cn', 'ipahost',
|
2009-05-12 11:45:30 -05:00
|
|
|
self.api.env.container_host
|
|
|
|
)
|
2009-07-02 08:17:50 -05:00
|
|
|
(completed, rem_failed) = del_members(
|
|
|
|
ldap, completed, to_remove, rem_failed, dn, 'member'
|
2009-05-12 11:45:30 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
members = kw.get('hostgroups', [])
|
2009-07-02 08:17:50 -05:00
|
|
|
(to_remove, rem_failed) = find_members(
|
|
|
|
ldap, rem_failed, members, 'cn', 'ipahostgroup',
|
2009-05-12 11:45:30 -05:00
|
|
|
self.api.env.container_hostgroup
|
|
|
|
)
|
2009-07-02 08:17:50 -05:00
|
|
|
(completed, rem_failed) = del_members(
|
|
|
|
ldap, completed, to_remove, rem_failed, dn, 'member'
|
2009-05-12 11:45:30 -05:00
|
|
|
)
|
|
|
|
|
2009-07-02 08:17:50 -05:00
|
|
|
return (
|
|
|
|
completed, rem_failed, ldap.get_entry(dn, _default_attributes)
|
|
|
|
)
|
2009-05-12 11:45:30 -05:00
|
|
|
|
2009-07-09 12:02:44 -05:00
|
|
|
api.register(hostgroup_remove_member)
|
2009-05-12 11:45:30 -05:00
|
|
|
|