From 457f1fe30b66c97e7a8c3bbaaefcba89ab2ea0c1 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Thu, 6 Oct 2016 15:51:07 -0400 Subject: [PATCH] Provide resource to allow clients to expand custom expressions Clients may now format a string as if it were to be an expression for an attribute, and have the server evaluate it using the same engine without passing through the attribute engine. This makes it easier, for example, to do nodeexec n1-n4 ipmitool -H {hardwaremanagement.manager} --- confluent_server/confluent/config/configmanager.py | 8 ++++++++ confluent_server/confluent/core.py | 1 + .../confluent/plugins/configuration/attributes.py | 14 ++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/confluent_server/confluent/config/configmanager.py b/confluent_server/confluent/config/configmanager.py index c5043ab9..63d8aa4a 100644 --- a/confluent_server/confluent/config/configmanager.py +++ b/confluent_server/confluent/config/configmanager.py @@ -761,6 +761,14 @@ class ConfigManager(object): decrypt=self.decrypt) return nodeobj + def expand_attrib_expression(self, nodelist, expression): + if type(nodelist) in (unicode, str): + nodelist = (nodelist,) + for node in nodelist: + cfgobj = self._cfgstore['nodes'][node] + fmt = _ExpressionFormat(cfgobj, node) + yield (node, fmt.format(expression)) + def get_node_attributes(self, nodelist, attributes=(), decrypt=None): if decrypt is None: decrypt = self.decrypt diff --git a/confluent_server/confluent/core.py b/confluent_server/confluent/core.py index 9aa0adaf..608e8206 100644 --- a/confluent_server/confluent/core.py +++ b/confluent_server/confluent/core.py @@ -122,6 +122,7 @@ def _init_core(): 'attributes': { 'all': PluginRoute({'handler': 'attributes'}), 'current': PluginRoute({'handler': 'attributes'}), + 'expression': PluginRoute({'handler': 'attributes'}), }, 'boot': { 'nextdevice': PluginRoute({ diff --git a/confluent_server/confluent/plugins/configuration/attributes.py b/confluent_server/confluent/plugins/configuration/attributes.py index 9ee0f149..10aa3307 100644 --- a/confluent_server/confluent/plugins/configuration/attributes.py +++ b/confluent_server/confluent/plugins/configuration/attributes.py @@ -152,6 +152,20 @@ def update_nodegroup(group, element, configmanager, inputdata): return retrieve_nodegroup(group, element, configmanager, inputdata) +def _expand_expression(nodes, configmanager, inputdata): + expression = inputdata.get_attributes(list(nodes)[0]) + if type(expression) is dict: + expression = expression['expression'] + if type(expression) is dict: + expression = expression['expression'] + for expanded in configmanager.expand_attrib_expression(nodes, expression): + yield msg.KeyValueData({'value': expanded[1]}, expanded[0]) + + +def create(nodes, element, configmanager, inputdata): + if nodes is not None and element[-1] == 'expression': + return _expand_expression(nodes, configmanager, inputdata) + def update_nodes(nodes, element, configmanager, inputdata): updatedict = {} for node in nodes: