You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by jo...@apache.org on 2015/06/03 17:24:47 UTC

ambari git commit: AMBARI-11656 - Alerts JSON Properties Should Allow For A Mixture of Parameters and Constants (jonathanhurley)

Repository: ambari
Updated Branches:
  refs/heads/trunk 29fc9c297 -> 64e033d08


AMBARI-11656 - Alerts JSON Properties Should Allow For A Mixture of Parameters and Constants (jonathanhurley)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/64e033d0
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/64e033d0
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/64e033d0

Branch: refs/heads/trunk
Commit: 64e033d081d2e715ebcbf97b691b6cacacc74ee0
Parents: 29fc9c2
Author: Jonathan Hurley <jh...@hortonworks.com>
Authored: Wed Jun 3 10:54:43 2015 -0400
Committer: Jonathan Hurley <jh...@hortonworks.com>
Committed: Wed Jun 3 11:24:05 2015 -0400

----------------------------------------------------------------------
 .../python/ambari_agent/alerts/base_alert.py    | 51 ++++++++++++++++----
 .../src/test/python/ambari_agent/TestAlerts.py  | 31 ++++++++++++
 2 files changed, 73 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/64e033d0/ambari-agent/src/main/python/ambari_agent/alerts/base_alert.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/main/python/ambari_agent/alerts/base_alert.py b/ambari-agent/src/main/python/ambari_agent/alerts/base_alert.py
index 08fb8a9..47e15f0 100644
--- a/ambari-agent/src/main/python/ambari_agent/alerts/base_alert.py
+++ b/ambari-agent/src/main/python/ambari_agent/alerts/base_alert.py
@@ -167,26 +167,59 @@ class BaseAlert(object):
   def _get_configuration_value(self, key):
     """
     Gets the value of the specified configuration key from the cache. The key
-    should be of the form {{foo-bar/baz}}. If the key is not a lookup key
+    should be of the form {{foo-bar/baz}}. If the key given is not a lookup key
     and is instead a constant, such as "foo" or "5", then the constant is
     returned.
-    :return:
+
+    If the key contains more than 1 parameter to lookup, then each match is
+    looked up and replaced.
+
+    If the value does not exist in the configs, then return None to indicate
+    that this key could not be found.
+
+    This should turn {{hdfs-site/value}}/whatever/{{hdfs-site/value2}}
+    into
+    value/whatever/value2
+
+    :return:  the resolved value or None if any of the placeholder parameters
+              does not exist in the configs
     """
     if key is None:
       return None
 
-    # parse {{foo-bar/baz}}
-    placeholder_keys = re.findall("{{([\S]+)}}", key)
+    # parse {{foo-bar/baz}}/whatever/{{foobar-site/blah}}
+    # into
+    # ['foo-bar/baz', 'foobar-site/blah']
+    placeholder_keys = re.findall("{{(\S+?)}}", key)
 
     # if none found, then return the original
-    if len(placeholder_keys) == 0:
+    if placeholder_keys is None or len(placeholder_keys) == 0:
       return key
 
-    # this is a lookup key, so transform it into a value from the config cache
-    placeholder_key = placeholder_keys[0]
+    # for every match, get its configuration value and replace it in the key
+    resolved_key = key
+    for placeholder_key in placeholder_keys:
+      value = self.cluster_configuration.get_configuration_value(
+        self.cluster_name, placeholder_key)
+
+      # if any of the placeholder keys is missing from the configuration, then
+      # return None as per the contract of this function
+      if value is None:
+        return None
+
+      # it's possible that a dictionary was request (ie {{hdfs-site}} instead
+      # of {{hdfs-site/foo}} - in which case, we should just return the
+      # dictionary as is
+      if isinstance(value, dict):
+        return value
+
+      # foo-site/bar -> r"{{(foo-site/bar)}}
+      replacement_match_regex = r"{{(%s)}}" % placeholder_key
+
+      # {{foo-bar/baz}}/whatever -> http://server/whatever
+      resolved_key = re.sub(replacement_match_regex, value, resolved_key)
 
-    return self.cluster_configuration.get_configuration_value(
-      self.cluster_name, placeholder_key)
+    return resolved_key
 
     
   def _lookup_uri_property_keys(self, uri_structure):

http://git-wip-us.apache.org/repos/asf/ambari/blob/64e033d0/ambari-agent/src/test/python/ambari_agent/TestAlerts.py
----------------------------------------------------------------------
diff --git a/ambari-agent/src/test/python/ambari_agent/TestAlerts.py b/ambari-agent/src/test/python/ambari_agent/TestAlerts.py
index 02efc6f..151c13b 100644
--- a/ambari-agent/src/test/python/ambari_agent/TestAlerts.py
+++ b/ambari-agent/src/test/python/ambari_agent/TestAlerts.py
@@ -969,6 +969,37 @@ class TestAlerts(TestCase):
     self.assertEquals(5.0, alert.connection_timeout)
 
 
+  def test_get_configuration_values(self):
+    """
+    Tests that we are able to extract parameters correctly from the cached
+    configuration.
+    :return:
+    """
+    configuration = { 'foo-site' :
+      { 'foo-key1' : 'value1',
+        'foo-key2' : 'value2'
+      }
+    }
+
+    collector = AlertCollector()
+    cluster_configuration = self.__get_cluster_configuration()
+    self.__update_cluster_configuration(cluster_configuration, configuration)
+
+    alert = MockAlert()
+    alert.set_helpers(collector, cluster_configuration)
+    alert.set_cluster("c1", "c6401.ambari.apache.org")
+
+    self.assertEquals("constant", alert._get_configuration_value("constant"))
+    self.assertEquals("value1", alert._get_configuration_value("{{foo-site/foo-key1}}"))
+    self.assertEquals("value2", alert._get_configuration_value("{{foo-site/foo-key2}}"))
+
+    # try a mix of parameter and constant
+    self.assertEquals("http://value1/servlet", alert._get_configuration_value("http://{{foo-site/foo-key1}}/servlet"))
+    self.assertEquals("http://value1/servlet/value2", alert._get_configuration_value("http://{{foo-site/foo-key1}}/servlet/{{foo-site/foo-key2}}"))
+
+    # try to request a dictionary object instead of a property
+    self.assertEquals(configuration["foo-site"], alert._get_configuration_value("{{foo-site}}"))
+
   def __get_cluster_configuration(self):
     """
     Gets an instance of the cluster cache where the file read and write