summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/Lint/AWSTags.py
blob: a6af63dd618b89c4ec01b49add3d37efbda8ad5f (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
import re
import sys
import Bcfg2.Server.Lint


class AWSTags(Bcfg2.Server.Lint.ServerPlugin):
    """ ``bcfg2-lint`` plugin to check all given :ref:`AWSTags
    <server-plugins-connectors-awstags>` patterns for validity. """

    def Run(self):
        cfg = self.core.plugins['AWSTags'].config
        for entry in cfg.xdata.xpath('//Tag'):
            self.check(entry, "name")
            if entry.get("value"):
                self.check(entry, "value")

    @classmethod
    def Errors(cls):
        return {"pattern-fails-to-initialize": "error"}

    def check(self, entry, attr):
        """ Check a single attribute (``name`` or ``value``) of a
        single entry for validity. """
        try:
            re.compile(entry.get(attr))
        except re.error:
            self.LintError("pattern-fails-to-initialize",
                           "'%s' regex could not be compiled: %s\n    %s" %
                           (attr, sys.exc_info()[1], entry.get("name")))