Enable interpolation/backreferences in regexp-class-mappings
Signed-off-by: martin f. krafft <madduck@madduck.net>
diff --git a/doc/source/operations.rst b/doc/source/operations.rst
index a157403..4801803 100644
--- a/doc/source/operations.rst
+++ b/doc/source/operations.rst
@@ -97,6 +97,12 @@
define all nodes (and if only to allow them to be enumerated for the
inventory).
+The mapped classes can also contain backreferences when regular expressions
+are used, although they need to be escaped, e.g.::
+
+ class_mappings:
+ - /\.(\S+)$/ tld-\\1
+
Parameter interpolation
------------------------
Parameters may reference each other, including deep references, e.g.::
diff --git a/doc/source/todo.rst b/doc/source/todo.rst
index 446dd59..3899ec0 100644
--- a/doc/source/todo.rst
+++ b/doc/source/todo.rst
@@ -29,16 +29,6 @@
people understand what's going on, where data are being changed/merged, and to
help solve problems.
-Mapping interpolation
----------------------
-Given class mappings, it should be possible to use references in the classes,
-e.g.:
-
- /\.(\S+)$/ → in-domain-\1
-
-such that a host like ``example.org`` would get the class ``in-domain-org``
-assigned to it.
-
Data from CMS for interpolation
-------------------------------
Depending on the CMS in question, it would be nice if |reclass| had access to
diff --git a/reclass/storage/__init__.py b/reclass/storage/__init__.py
index 96f3624..b164d2a 100644
--- a/reclass/storage/__init__.py
+++ b/reclass/storage/__init__.py
@@ -67,11 +67,14 @@
key, klasses = self._shlex_split(mapping)
if key[0] == ('/'):
matched = self._match_regexp(key[1:-1], nodename)
+ if matched:
+ for klass in klasses:
+ c.append_if_new(matched.expand(klass))
+
else:
- matched = self._match_glob(key, nodename)
- if matched:
- for klass in klasses:
- c.append_if_new(klass)
+ if self._match_glob(key, nodename):
+ for klass in klasses:
+ c.append_if_new(klass)
return Entity(classes=c,
name='class mappings for node {0}'.format(nodename))