ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject ambari git commit: AMBARI-15870. Add dryrun method on request object (ajit)
Date Thu, 14 Apr 2016 20:53:10 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk 6bd2de218 -> f2b3c2a76


AMBARI-15870. Add dryrun method on request object (ajit)


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

Branch: refs/heads/trunk
Commit: f2b3c2a76f24b9509e21eb8c7f435a5694c268cc
Parents: 6bd2de2
Author: Ajit Kumar <ajit@apache.org>
Authored: Thu Apr 14 12:57:24 2016 -0700
Committer: Ajit Kumar <ajit@apache.org>
Committed: Thu Apr 14 13:52:46 2016 -0700

----------------------------------------------------------------------
 .../VersionDefinitionResourceDefinition.java    |  3 +-
 .../server/controller/internal/RequestImpl.java | 40 +++++++++++---------
 .../VersionDefinitionResourceProvider.java      | 14 +------
 .../ambari/server/controller/spi/Request.java   | 22 +++++++----
 .../controller/internal/RequestImplTest.java    | 15 +++++++-
 .../internal/RequestResourceProviderTest.java   |  2 +
 .../VersionDefinitionResourceProviderTest.java  |  2 +-
 7 files changed, 57 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/f2b3c2a7/ambari-server/src/main/java/org/apache/ambari/server/api/resources/VersionDefinitionResourceDefinition.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/resources/VersionDefinitionResourceDefinition.java
b/ambari-server/src/main/java/org/apache/ambari/server/api/resources/VersionDefinitionResourceDefinition.java
index 12b91e3..30afa69 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/resources/VersionDefinitionResourceDefinition.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/resources/VersionDefinitionResourceDefinition.java
@@ -22,6 +22,7 @@ import java.util.Collections;
 import java.util.Set;
 
 import org.apache.ambari.server.controller.internal.VersionDefinitionResourceProvider;
+import org.apache.ambari.server.controller.spi.Request;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.apache.ambari.server.controller.spi.Resource.Type;
 
@@ -51,7 +52,7 @@ public class VersionDefinitionResourceDefinition extends BaseResourceDefinition
 
   @Override
   public Collection<String> getCreateDirectives() {
-    return Collections.singleton(VersionDefinitionResourceProvider.DIRECTIVE_DRY_RUN);
+    return Collections.singleton(Request.DIRECTIVE_DRY_RUN);
   }
 
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/f2b3c2a7/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestImpl.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestImpl.java
index f27f621..36ad4c3 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestImpl.java
@@ -18,13 +18,12 @@
 
 package org.apache.ambari.server.controller.internal;
 
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
 import org.apache.ambari.server.controller.spi.PageRequest;
 import org.apache.ambari.server.controller.spi.Request;
 import org.apache.ambari.server.controller.spi.ResourceProvider;
@@ -52,25 +51,29 @@ public class RequestImpl implements Request {
    * Request Info properties.  These are properties that are specific to the request
    * but not to any resource.
    */
-  private Map<String, String> requestInfoProperties;
+  private final Map<String, String> requestInfoProperties;
 
   /**
    * Map of property to temporal info.
    */
-  private Map<String, TemporalInfo> m_mapTemporalInfo = new HashMap<String, TemporalInfo>();
+  private final Map<String, TemporalInfo> m_mapTemporalInfo;
 
   /**
    * An optional page request which a concrete {@link ResourceProvider} can use
    * to return a slice of results.
    */
-  private PageRequest m_pageRequest = null;
+  private final PageRequest m_pageRequest;
 
   /**
    * An optional sort request which a concrete {@link ResourceProvider} can use
    * to return sorted results.
    */
-  private SortRequest m_sortRequest = null;
+  private final SortRequest m_sortRequest;
 
+  /**
+   * Is it a dry run request?
+   */
+  private final boolean dryRun;
 
   // ----- Constructors ------------------------------------------------------
 
@@ -100,22 +103,22 @@ public class RequestImpl implements Request {
   public RequestImpl(Set<String> propertyIds, Set<Map<String, Object>>
properties,
                      Map<String, String> requestInfoProperties, Map<String, TemporalInfo>
mapTemporalInfo,
                      SortRequest sortRequest, PageRequest pageRequest) {
+
     this.propertyIds = propertyIds == null ?
-        Collections.unmodifiableSet(new HashSet<String>()) :
-        Collections.unmodifiableSet(propertyIds);
+        ImmutableSet.<String>of() : ImmutableSet.copyOf(propertyIds);
 
     this.properties = properties == null ?
-        Collections.unmodifiableSet(new HashSet<Map<String, Object>>()) :
-        Collections.unmodifiableSet(properties);
+        ImmutableSet.<Map<String,Object>>of() : ImmutableSet.copyOf(properties);
 
     this.requestInfoProperties = requestInfoProperties == null ?
-        Collections.unmodifiableMap(new HashMap<String, String>()) :
-        Collections.unmodifiableMap(requestInfoProperties);
+        ImmutableMap.<String, String>of() : ImmutableMap.copyOf(requestInfoProperties);
 
-    setTemporalInfo(mapTemporalInfo);
 
+    m_mapTemporalInfo = mapTemporalInfo;
     m_sortRequest = sortRequest;
     m_pageRequest = pageRequest;
+
+    this.dryRun = this.requestInfoProperties.containsKey(DIRECTIVE_DRY_RUN) && Boolean.parseBoolean(this.requestInfoProperties.get(DIRECTIVE_DRY_RUN));
   }
 
   // ----- Request -----------------------------------------------------------
@@ -140,10 +143,6 @@ public class RequestImpl implements Request {
     return m_mapTemporalInfo == null ? null : m_mapTemporalInfo.get(id);
   }
 
-  private void setTemporalInfo(Map<String, TemporalInfo> mapTemporalInfo) {
-    m_mapTemporalInfo = mapTemporalInfo;
-  }
-
   @Override
   public PageRequest getPageRequest() {
     return m_pageRequest;
@@ -155,6 +154,11 @@ public class RequestImpl implements Request {
   }
 
   @Override
+  public boolean isDryRunRequest() {
+    return dryRun;
+  }
+
+  @Override
   public boolean equals(Object o) {
     if (this == o) {
       return true;

http://git-wip-us.apache.org/repos/asf/ambari/blob/f2b3c2a7/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/VersionDefinitionResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/VersionDefinitionResourceProvider.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/VersionDefinitionResourceProvider.java
index f8f8290..97eda0a 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/VersionDefinitionResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/VersionDefinitionResourceProvider.java
@@ -99,8 +99,6 @@ public class VersionDefinitionResourceProvider extends AbstractAuthorizedResourc
   protected static final String VERSION_DEF_STACK_SERVICES           = "VersionDefinition/stack_services";
   protected static final String SHOW_AVAILABLE                       = "VersionDefinition/show_available";
 
-  public static final String DIRECTIVE_DRY_RUN                       = "dry_run";
-
   public static final String SUBRESOURCE_OPERATING_SYSTEMS_PROPERTY_ID  = new OperatingSystemResourceDefinition().getPluralName();
 
   @Inject
@@ -193,14 +191,7 @@ public class VersionDefinitionResourceProvider extends AbstractAuthorizedResourc
           VERSION_DEF_DEFINITION_URL));
     }
 
-    Map<String, String> requestInfo = request.getRequestInfoProperties();
-
-    final boolean dryRun;
-    if (requestInfo.containsKey(DIRECTIVE_DRY_RUN)) {
-      dryRun = Boolean.parseBoolean(requestInfo.get(DIRECTIVE_DRY_RUN));
-    } else {
-      dryRun = false;
-    }
+    final boolean dryRun = request.isDryRunRequest();
 
     XmlHolder xmlHolder = createResources(new Command<XmlHolder>() {
       @Override
@@ -357,7 +348,6 @@ public class VersionDefinitionResourceProvider extends AbstractAuthorizedResourc
 
   /**
    * In the case of a patch, check if there is a parent repo.
-   * @param entity the entity to check
    */
   private void checkForParent(XmlHolder holder) throws AmbariException {
     RepositoryVersionEntity entity = holder.entity;
@@ -486,8 +476,6 @@ public class VersionDefinitionResourceProvider extends AbstractAuthorizedResourc
   /**
    * Transforms a XML version defintion to an entity
    *
-   * @param definitionUrl the String URL for loading
-   * @return constructed entity
    * @throws AmbariException if some properties are missing or json has incorrect structure
    */
   protected void toRepositoryVersionEntity(XmlHolder holder) throws AmbariException {

http://git-wip-us.apache.org/repos/asf/ambari/blob/f2b3c2a7/ambari-server/src/main/java/org/apache/ambari/server/controller/spi/Request.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/spi/Request.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/spi/Request.java
index 50eed9a..f8f2e96 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/spi/Request.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/spi/Request.java
@@ -29,7 +29,9 @@ public interface Request {
   /**
    * Constant for request info property which contains the raw request body.
    */
-  public static final String REQUEST_INFO_BODY_PROPERTY = "RAW_REQUEST_BODY";
+  String REQUEST_INFO_BODY_PROPERTY = "RAW_REQUEST_BODY";
+
+  String DIRECTIVE_DRY_RUN = "dry_run";
 
   /**
    * Get the set of property ids being requested.  Used for requests to get
@@ -38,7 +40,7 @@ public interface Request {
    *
    * @return the set of property ids being requested
    */
-  public Set<String> getPropertyIds();
+  Set<String> getPropertyIds();
 
   /**
    * Get the property values of the request.  Used
@@ -49,13 +51,13 @@ public interface Request {
    *
    * @return the set of properties being requested
    */
-  public Set<Map<String, Object>> getProperties();
+  Set<Map<String, Object>> getProperties();
 
   /**
    * Get any request info properties of the request.  These are optional properties
    * that are specific to the request but not related to any resource.
    */
-  public Map<String, String> getRequestInfoProperties();
+  Map<String, String> getRequestInfoProperties();
 
   /**
    * Get the {@link TemporalInfo temporal information} for the given property
@@ -64,7 +66,7 @@ public interface Request {
    * @param id the property id
    * @return the temporal information for the given property id; null if noe exists
    */
-  public TemporalInfo getTemporalInfo(String id);
+  TemporalInfo getTemporalInfo(String id);
 
   /**
    * Obtain the pagination request information. This structure can be used for
@@ -73,7 +75,7 @@ public interface Request {
    *
    * @return the page request information.
    */
-  public PageRequest getPageRequest();
+  PageRequest getPageRequest();
 
   /**
    * Obtain information to order the results by. This structure can be used for
@@ -82,5 +84,11 @@ public interface Request {
    *
    * @return the sort request information.
    */
-  public SortRequest getSortRequest();
+  SortRequest getSortRequest();
+
+  /**
+   * Is this a dry run request.
+   * @return true - if request is dry run request, false otherwise.
+   */
+  boolean isDryRunRequest();
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/f2b3c2a7/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestImplTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestImplTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestImplTest.java
index 93fe36f..e1ebc19 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestImplTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestImplTest.java
@@ -18,7 +18,9 @@
 
 package org.apache.ambari.server.controller.internal;
 
+import java.util.Collections;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 
 import junit.framework.Assert;
@@ -33,7 +35,7 @@ import org.junit.Test;
  */
 public class RequestImplTest {
 
-  private static final Set<String> propertyIds = new HashSet<String>();
+  private static final Set<String> propertyIds = new HashSet<>();
 
   static {
     propertyIds.add(PropertyHelper.getPropertyId("c1", "p1"));
@@ -258,4 +260,15 @@ public class RequestImplTest {
     Assert.assertTrue(validPropertyIds.contains("StackServiceComponents/is_master"));
     Assert.assertTrue(validPropertyIds.contains("StackServiceComponents/is_client"));
   }
+
+  @Test
+  public void testDryRunRequest() {
+    Request dryRunRequest = PropertyHelper.getCreateRequest(Collections.<Map<String,Object>>emptySet(),
Collections.singletonMap(Request.DIRECTIVE_DRY_RUN, "true"));
+    Request nonDryRunReqest1 = PropertyHelper.getCreateRequest(Collections.<Map<String,Object>>emptySet(),
Collections.singletonMap(Request.DIRECTIVE_DRY_RUN, "false"));
+    Request nonDryRunReqest2 = PropertyHelper.getCreateRequest(Collections.<Map<String,Object>>emptySet(),
Collections.<String, String>emptyMap());
+
+    Assert.assertTrue(dryRunRequest.isDryRunRequest());
+    Assert.assertFalse(nonDryRunReqest1.isDryRunRequest());
+    Assert.assertFalse(nonDryRunReqest2.isDryRunRequest());
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/f2b3c2a7/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java
index ea4afde..42e75a7 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestResourceProviderTest.java
@@ -1085,12 +1085,14 @@ public class RequestResourceProviderTest {
     // Both action and command are specified
     requestInfoProperties.put(RequestResourceProvider.COMMAND_ID, "HDFS_SERVICE_CHECK");
     requestInfoProperties.put(RequestResourceProvider.ACTION_ID, "a1");
+    request = PropertyHelper.getCreateRequest(propertySet, requestInfoProperties);
     try {
       provider.createResources(request);
     } catch (UnsupportedOperationException ex) {
       Assert.assertTrue(ex.getMessage().contains("Both command and action cannot be specified"));
     }
     requestInfoProperties.remove(RequestResourceProvider.ACTION_ID);
+    request = PropertyHelper.getCreateRequest(propertySet, requestInfoProperties);
 
     provider.createResources(request);
     Assert.assertTrue(actionRequest.hasCaptured());

http://git-wip-us.apache.org/repos/asf/ambari/blob/f2b3c2a7/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/VersionDefinitionResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/VersionDefinitionResourceProviderTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/VersionDefinitionResourceProviderTest.java
index 633815e..3637cab 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/VersionDefinitionResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/VersionDefinitionResourceProviderTest.java
@@ -328,7 +328,7 @@ public class VersionDefinitionResourceProviderTest {
         file.toURI().toURL().toString());
     propertySet.add(properties);
 
-    Map<String, String> info = Collections.singletonMap(VersionDefinitionResourceProvider.DIRECTIVE_DRY_RUN,
"true");
+    Map<String, String> info = Collections.singletonMap(Request.DIRECTIVE_DRY_RUN,
"true");
 
     final Request createRequest = PropertyHelper.getCreateRequest(propertySet, info);
     RequestStatus status = versionProvider.createResources(createRequest);


Mime
View raw message