Parcourir la source

AMBARI-1561. API should return nagios_alerts as a JSON, not a stringified JSON. (smohanty)

git-svn-id: https://svn.apache.org/repos/asf/incubator/ambari/trunk@1462547 13f79535-47bb-0310-9956-ffa450edef68
Sumit Mohanty il y a 12 ans
Parent
commit
c016a02510

+ 3 - 0
CHANGES.txt

@@ -534,6 +534,9 @@ Trunk (unreleased changes):
 
  BUG FIXES
 
+ AMBARI-1561. API should return nagios_alerts as a JSON, not a stringified 
+ JSON. (smohanty)
+
  AMBARI-1507. Should not install HDPHBaseMaster, HDPNameNode and HDPJobTracker
  ganglia configs on every node. (smohanty)
 

+ 0 - 6
ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractProviderModule.java

@@ -294,12 +294,6 @@ public abstract class AbstractProviderModule implements ProviderModule, Resource
             PropertyHelper.getPropertyId("HostRoles", "host_name"),
             PropertyHelper.getPropertyId("HostRoles", "component_name")));
         
-        providers.add(new HttpProxyPropertyProvider(
-            new URLStreamProvider(1500),
-            PropertyHelper.getPropertyId("HostRoles", "cluster_name"),
-            PropertyHelper.getPropertyId("HostRoles", "host_name"),
-            PropertyHelper.getPropertyId("HostRoles", "component_name")));
-        
         break;
       default :
         break;

+ 11 - 18
ambari-server/src/main/java/org/apache/ambari/server/controller/internal/HttpProxyPropertyProvider.java

@@ -19,15 +19,12 @@ package org.apache.ambari.server.controller.internal;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.Type;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
-import org.apache.ambari.server.api.services.NamedPropertySet;
-import org.apache.ambari.server.api.services.RequestBody;
-import org.apache.ambari.server.api.services.parsers.BodyParseException;
-import org.apache.ambari.server.api.services.parsers.JsonRequestBodyParser;
 import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.PropertyProvider;
 import org.apache.ambari.server.controller.spi.Request;
@@ -39,6 +36,9 @@ import org.apache.commons.io.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+
 /**
  * Property provider that is used to read HTTP data from another server.
  */
@@ -108,32 +108,25 @@ public class HttpProxyPropertyProvider extends BaseProvider implements PropertyP
     return resources;
   }
 
-  private void getHttpResponse(Resource r, String url, String propertyIdToSet) {
+  private void getHttpResponse(Resource r, String url, String propertyIdToSet) throws SystemException {
     
     InputStream in = null;
     try {
       in = streamProvider.readFrom(url);
-      //todo: should not use JsonRequestBodyParser as this is intended only for parsing http bodies.
-      RequestBody body = (new JsonRequestBodyParser().parse(IOUtils.toString(in, "UTF-8")));
-      Set<NamedPropertySet> setNamedProps = body.getNamedPropertySets();
-      Set<Map<String,Object>> setProps = new HashSet<Map<String, Object>>(setNamedProps.size());
-      for (NamedPropertySet ps : setNamedProps) {
-        setProps.add(ps.getProperties());
-      }
-      r.setProperty(propertyIdToSet, setProps);
+      Type mapType = new TypeToken<Map<String, Object>>(){}.getType();
+      Map<String, Object> propertyValueFromJson = new Gson().fromJson(IOUtils.toString(in, "UTF-8"), mapType);
+      r.setProperty(propertyIdToSet, propertyValueFromJson);
     }
     catch (IOException ioe) {
-      //todo: should not eat exception
       LOG.error("Error reading HTTP response from " + url);
-    } catch (BodyParseException e) {
-      LOG.error("Error Parsing Json.", e);
+      throw new SystemException("Unable to get property " + propertyIdToSet + "from URL " + url, ioe);
     } finally {
-      if (null != in) {
+      if (in != null) {
         try {
           in.close();
         }
         catch (IOException ioe) {
-          // 
+          throw new SystemException("Unable to close input stream", ioe);
         }
       }
     }

+ 30 - 13
ambari-server/src/test/java/org/apache/ambari/server/controller/internal/HttpPropertyProviderTest.java

@@ -23,6 +23,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.Collections;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -75,22 +76,37 @@ public class HttpPropertyProviderTest {
      resource.getPropertyValue(PROPERTY_ID_NAGIOS_ALERTS));        
   }
 
+  @SuppressWarnings("rawtypes")
   @Test
   public void testReadWithRequestedJson() throws Exception {
 
-  Set<String> propertyIds = new HashSet<String>();
-  propertyIds.add(PropertyHelper.getPropertyId("HostRoles", "nagios_alerts"));
-  propertyIds.add(PROPERTY_ID_COMPONENT_NAME);
-  Resource resource = doPopulate("NAGIOS_SERVER", propertyIds);
-  Object propertyValue = resource.getPropertyValue(PROPERTY_ID_NAGIOS_ALERTS);
+    Set<String> propertyIds = new HashSet<String>();
+    propertyIds.add(PropertyHelper.getPropertyId("HostRoles", "nagios_alerts"));
+    propertyIds.add(PROPERTY_ID_COMPONENT_NAME);
+    Resource resource = doPopulate("NAGIOS_SERVER", propertyIds);
+    Object propertyValue = resource.getPropertyValue(PROPERTY_ID_NAGIOS_ALERTS);
 
-  Assert.assertNotNull("Expected non-null for 'nagios_alerts'", propertyValue);
-  Assert.assertTrue("Expected Set for parsed JSON", propertyValue instanceof Set);
-
-  Object propertyEntry = ((Set) propertyValue).iterator().next();
-
-  Assert.assertTrue(propertyEntry instanceof Map);
-  Assert.assertEquals("Alert Body", ((Map) propertyEntry).get("nagios_alert"));
+    Assert.assertNotNull("Expected non-null for 'nagios_alerts'", propertyValue);
+    Assert.assertTrue("Expected Map for parsed JSON", propertyValue instanceof Map);
+    
+    Object alertsEntry = ((Map) propertyValue).get("alerts");
+    Object hostcountsEntry = ((Map) propertyValue).get("hostcounts");
+    
+    Assert.assertNotNull("Expected non-null for 'alerts' entry", alertsEntry);
+    Assert.assertNotNull("Expected non-null for 'hostcounts' entry", hostcountsEntry);
+    Assert.assertTrue("Expected List type for 'alerts' entry", alertsEntry instanceof List);
+    Assert.assertTrue("Expected Map type for 'hostcounts' entry", hostcountsEntry instanceof Map);
+    
+    List alertsList = (List) alertsEntry;
+    Map hostcountsMap = (Map) hostcountsEntry;
+    
+    Assert.assertEquals("Expected number of entries in 'alerts' is 1", 1, alertsList.size());
+    Assert.assertTrue("Expected Map type for 'alerts' element", alertsList.get(0) instanceof Map);
+    Assert.assertEquals("Body", ((Map) alertsList.get(0)).get("Alert Body"));
+    
+    Assert.assertEquals("Expected number of entries in 'hostcounts' is 2", 2, hostcountsMap.size());
+    Assert.assertEquals("1", hostcountsMap.get("up_hosts"));
+    Assert.assertEquals("0", hostcountsMap.get("down_hosts"));
   }
 
   @Test
@@ -130,7 +146,8 @@ public class HttpPropertyProviderTest {
 
     @Override
     public InputStream readFrom(String spec) throws IOException {
-        String responseStr = "[{\"nagios_alert\": \"Alert Body\"}]";
+      String responseStr = "{\"alerts\": [{\"Alert Body\": \"Body\"}],"
+          + " \"hostcounts\": {\"up_hosts\":\"1\", \"down_hosts\":\"0\"}}";
         return new ByteArrayInputStream(responseStr.getBytes("UTF-8"));
     }
   }

+ 2 - 0
contrib/addons/src/addOns/nagios/scripts/nagios_alerts.php

@@ -224,6 +224,7 @@ function hdp_mon_generate_response( $response_data )
     if ($services_object["PUPPET"] >= 1) {
       $services_object["PUPPET"] = 1;
     }
+    $services_object = array_map('strval', $services_object);
     return $services_object;
   }
 
@@ -242,6 +243,7 @@ function hdp_mon_generate_response( $response_data )
     }
     $hostcounts_object['up_hosts'] = $up_hosts;
     $hostcounts_object['down_hosts'] = $down_hosts;
+    $hostcounts_object = array_map('strval', $hostcounts_object);
     return $hostcounts_object;
   }