summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/Plugins/GroupLogic.py
blob: 1da7c8fec7c3d159bc25154cabe2231720351400 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
""" GroupLogic is a connector plugin that lets you use an XML Genshi
template to dynamically set additional groups for clients. """

import os
import lxml.etree
import Bcfg2.Server.Plugin
from Bcfg2.Server.Plugins.Metadata import MetadataGroup


class GroupLogicConfig(Bcfg2.Server.Plugin.StructFile):
    """ Representation of the GroupLogic groups.xml file """
    create = lxml.etree.Element("GroupLogic",
                                nsmap=dict(py="http://genshi.edgewall.org/"))

    def _match(self, item, metadata, *args):
        if item.tag == 'Group' and not len(item.getchildren()):
            return [item]
        return Bcfg2.Server.Plugin.StructFile._match(self, item, metadata,
                                                     *args)

    def _xml_match(self, item, metadata, *args):
        if item.tag == 'Group' and not len(item.getchildren()):
            return [item]
        return Bcfg2.Server.Plugin.StructFile._xml_match(self, item, metadata,
                                                         *args)


class GroupLogic(Bcfg2.Server.Plugin.Plugin,
                 Bcfg2.Server.Plugin.Connector):
    """ GroupLogic is a connector plugin that lets you use an XML
    Genshi template to dynamically set additional groups for
    clients. """

    def __init__(self, core, datastore):
        Bcfg2.Server.Plugin.Plugin.__init__(self, core, datastore)
        Bcfg2.Server.Plugin.Connector.__init__(self)
        self.config = GroupLogicConfig(os.path.join(self.data, "groups.xml"),
                                       should_monitor=True)

    def get_additional_groups(self, metadata):
        rv = []
        for el in self.config.XMLMatch(metadata).findall("Group"):
            if el.get("category"):
                rv.append(MetadataGroup(el.get("name"),
                                        category=el.get("category")))
            else:
                rv.append(el.get("name"))
        return rv