Repository: ambari
Updated Branches:
  refs/heads/branch-2.5 e6ca77224 -> 94fed528c


AMBARI-18374. rolling restart datanode cluster name show null (Wang Yaoxin via 
magyari_sandor)


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

Branch: refs/heads/branch-2.5
Commit: 94fed528c49c8a44f80dff5133255ebb89ac74bb
Parents: e6ca772
Author: Wang Yaoxin <18896724...@139.com>
Authored: Wed Sep 14 15:36:56 2016 +0530
Committer: Sandor Magyari <smagy...@hortonworks.com>
Committed: Mon Sep 19 09:54:08 2016 +0200

----------------------------------------------------------------------
 .../eventcreator/RequestEventCreator.java       | 30 ++++++++++++++++++--
 .../creator/RequestEventCreatorTest.java        | 27 ++++++++++++++++++
 2 files changed, 54 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/94fed528/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/RequestEventCreator.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/RequestEventCreator.java
 
b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/RequestEventCreator.java
index 075e328..f0ea4ec 100644
--- 
a/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/RequestEventCreator.java
+++ 
b/ambari-server/src/main/java/org/apache/ambari/server/audit/request/eventcreator/RequestEventCreator.java
@@ -27,7 +27,8 @@ import org.apache.ambari.server.audit.event.AuditEvent;
 import 
org.apache.ambari.server.audit.event.request.AddRequestRequestAuditEvent;
 import org.apache.ambari.server.controller.internal.RequestOperationLevel;
 import org.apache.ambari.server.controller.spi.Resource;
-
+import java.util.Map;
+import org.apache.ambari.server.controller.internal.RequestResourceProvider;
 import com.google.common.collect.ImmutableSet;
 
 /**
@@ -87,10 +88,33 @@ public class RequestEventCreator implements 
RequestAuditEventCreator {
           .withUrl(request.getURI())
           .withRemoteIp(request.getRemoteAddress())
           
.withCommand(request.getBody().getRequestInfoProperties().get("command"))
-          
.withClusterName(request.getBody().getRequestInfoProperties().get(RequestOperationLevel.OPERATION_CLUSTER_ID))
-          .build();
+          .withClusterName(getClusterName(request, 
RequestOperationLevel.OPERATION_CLUSTER_ID))
+         .build();
       default:
         return null;
     }
   }
+  /**
+   *Returns clusterName from the request based on the propertyName parameter
+   *@param request
+   *@param propertyName
+   *@return
+   */
+  private String getClusterName(Request request ,String propertyName) {
+    Map<String, String> requestInfoProps = 
request.getBody().getRequestInfoProperties();
+    return 
requestInfoProps.containsKey(propertyName)?requestInfoProps.get(propertyName):getProperty(request,
 RequestResourceProvider.REQUEST_CLUSTER_NAME_PROPERTY_ID);
+  }
+
+  /**
+   *Returns property from the request based on the propertyName parameter
+   *@param request
+   *@param propertyName
+   *@return
+   */
+  private String getProperty(Request request, String propertyName) {
+    if (!request.getBody().getPropertySets().isEmpty()) {
+      return 
String.valueOf(request.getBody().getPropertySets().iterator().next().get(propertyName));
+    }
+    return null;
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/94fed528/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/RequestEventCreatorTest.java
----------------------------------------------------------------------
diff --git 
a/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/RequestEventCreatorTest.java
 
b/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/RequestEventCreatorTest.java
index 1074f0e..ddb42a7 100644
--- 
a/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/RequestEventCreatorTest.java
+++ 
b/ambari-server/src/test/java/org/apache/ambari/server/audit/request/creator/RequestEventCreatorTest.java
@@ -19,7 +19,11 @@
 package org.apache.ambari.server.audit.request.creator;
 
 import junit.framework.Assert;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
 
+import org.apache.ambari.server.api.services.NamedPropertySet;
 import org.apache.ambari.server.api.services.Request;
 import org.apache.ambari.server.api.services.Result;
 import org.apache.ambari.server.api.services.ResultStatus;
@@ -27,6 +31,7 @@ import org.apache.ambari.server.audit.event.AuditEvent;
 import 
org.apache.ambari.server.audit.event.request.AddRequestRequestAuditEvent;
 import org.apache.ambari.server.audit.request.eventcreator.RequestEventCreator;
 import org.apache.ambari.server.controller.internal.RequestOperationLevel;
+import org.apache.ambari.server.controller.internal.RequestResourceProvider;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.junit.Test;
 
@@ -50,4 +55,26 @@ public class RequestEventCreatorTest extends 
AuditEventCreatorTestBase{
     Assert.assertEquals(expected, actual);
     Assert.assertTrue(actual.contains(userName));
   }
+  @Test
+  public void postScheduleTest() {
+    RequestEventCreator creator = new RequestEventCreator();
+
+    Request request = 
AuditEventCreatorTestHelper.createRequest(Request.Type.POST, 
Resource.Type.Request, null, null);
+    Result result = AuditEventCreatorTestHelper.createResult(new 
ResultStatus(ResultStatus.STATUS.OK));
+    request.getBody().addRequestInfoProperty("command", "MyCommand");
+
+    Map<String, Object> mapProperties = new HashMap<String, Object>();
+    
mapProperties.put(RequestResourceProvider.REQUEST_CLUSTER_NAME_PROPERTY_ID, 
"mycluster");
+    NamedPropertySet namedPropSet = new NamedPropertySet("", mapProperties);
+    request.getBody().addPropertySet(namedPropSet);
+
+    AuditEvent event = AuditEventCreatorTestHelper.getEvent(creator, request, 
result);
+    String actual = event.getAuditMessage();
+    String expected = "User(" + userName + "), RemoteIp(1.2.3.4), 
Operation(Request from server), RequestType(POST), 
url(http://example.com:8080/api/v1/test), ResultStatus(200 OK), 
Command(MyCommand), Cluster name(mycluster)";
+
+    Assert.assertTrue("Class mismatch", event instanceof 
AddRequestRequestAuditEvent);
+    Assert.assertEquals(expected, actual);
+    Assert.assertTrue(actual.contains(userName));
+  }
+
 }

Reply via email to