Repository: brooklyn-server
Updated Branches:
  refs/heads/master 4388cbe58 -> d9cbe117d


Adds /activities/{id}/children/recurse endpoint for fetching all child tasks


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

Branch: refs/heads/master
Commit: a3f15bab85b0e465dbade506952b05b8ae18aa81
Parents: ee3ca1e
Author: Mark McKenna <[email protected]>
Authored: Tue Jun 21 10:52:05 2016 +0100
Committer: Mark McKenna <[email protected]>
Committed: Thu Jun 23 11:39:49 2016 +0100

----------------------------------------------------------------------
 .../apache/brooklyn/rest/api/ActivityApi.java   | 15 ++++++-
 .../rest/resources/ActivityResource.java        | 41 +++++++++++++++-----
 2 files changed, 44 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a3f15bab/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ActivityApi.java
----------------------------------------------------------------------
diff --git 
a/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ActivityApi.java 
b/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ActivityApi.java
index d131606..73beaf3 100644
--- a/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ActivityApi.java
+++ b/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/ActivityApi.java
@@ -20,6 +20,7 @@ package org.apache.brooklyn.rest.api;
 
 import io.swagger.annotations.Api;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.brooklyn.rest.domain.TaskSummary;
 
@@ -44,8 +45,7 @@ public interface ActivityApi {
             @ApiResponse(code = 404, message = "Could not find task")
     })
     public TaskSummary get(
-            @ApiParam(value = "Task ID", required = true) @PathParam("task") 
String taskId
-            );
+            @ApiParam(value = "Task ID", required = true) @PathParam("task") 
String taskId);
 
     @GET
     @Path("/{task}/children")
@@ -57,6 +57,17 @@ public interface ActivityApi {
             @ApiParam(value = "Task ID", required = true) @PathParam("task") 
String taskId);
 
     @GET
+    @Path("/{task}/children/recurse")
+    @ApiOperation(
+            value = "Fetch all child tasks details as Map<String,TaskSummary> 
map key == Task ID",
+            response = Map.class)
+    @ApiResponses(value = {
+            @ApiResponse(code = 404, message = "Could not find task")
+    })
+    public Map<String,TaskSummary> getAllChildrenAsMap(
+            @ApiParam(value = "Task ID", required = true) @PathParam("task") 
String taskId);
+
+    @GET
     @Path("/{task}/stream/{streamId}")
     @ApiOperation(value = "Return the contents of the given stream")
     @ApiResponses(value = {

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a3f15bab/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ActivityResource.java
----------------------------------------------------------------------
diff --git 
a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ActivityResource.java
 
b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ActivityResource.java
index f29827b..3b4bdbe 100644
--- 
a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ActivityResource.java
+++ 
b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/ActivityResource.java
@@ -18,10 +18,9 @@
  */
 package org.apache.brooklyn.rest.resources;
 
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.List;
-
+import com.google.common.collect.Collections2;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
 import org.apache.brooklyn.api.entity.Entity;
 import org.apache.brooklyn.api.mgmt.HasTaskChildren;
 import org.apache.brooklyn.api.mgmt.Task;
@@ -33,8 +32,7 @@ import org.apache.brooklyn.rest.domain.TaskSummary;
 import org.apache.brooklyn.rest.transform.TaskTransformer;
 import org.apache.brooklyn.rest.util.WebResourceUtils;
 
-import com.google.common.collect.Collections2;
-import com.google.common.collect.Lists;
+import java.util.*;
 
 public class ActivityResource extends AbstractBrooklynRestResource implements 
ActivityApi {
 
@@ -50,13 +48,36 @@ public class ActivityResource extends 
AbstractBrooklynRestResource implements Ac
     }
 
     @Override
+    public Map<String, TaskSummary> getAllChildrenAsMap(final String taskId) {
+        final Task<?> parentTask = 
mgmt().getExecutionManager().getTask(taskId);
+        if (parentTask == null) {
+            throw WebResourceUtils.notFound("Cannot find task '%s'", taskId);
+        }
+        checkEntityEntitled(parentTask);
+        return getAllDescendantTasks(parentTask);
+    }
+
+    private LinkedHashMap<String, TaskSummary> getAllDescendantTasks(final 
Task<?> parentTask) {
+        final LinkedHashMap<String, TaskSummary> result = 
Maps.newLinkedHashMap();
+        if (!(parentTask instanceof HasTaskChildren)) {
+            return result;
+        }
+        for (final Task<?> childTask : ((HasTaskChildren) 
parentTask).getChildren()) {
+            result.put(childTask.getId(), 
TaskTransformer.fromTask(ui.getBaseUriBuilder()).apply(childTask));
+            result.putAll(getAllDescendantTasks(childTask));
+        }
+        return result;
+    }
+
+
+    @Override
     public List<TaskSummary> children(String taskId) {
         Task<?> t = mgmt().getExecutionManager().getTask(taskId);
         if (t == null) {
             throw WebResourceUtils.notFound("Cannot find task '%s'", taskId);
         }
         checkEntityEntitled(t);
-        
+
         if (!(t instanceof HasTaskChildren)) {
             return Collections.emptyList();
         }
@@ -72,14 +93,14 @@ public class ActivityResource extends 
AbstractBrooklynRestResource implements Ac
         }
         checkEntityEntitled(t);
         checkStreamEntitled(t, streamId);
-        
+
         WrappedStream stream = BrooklynTaskTags.stream(t, streamId);
         if (stream == null) {
             throw WebResourceUtils.notFound("Cannot find stream '%s' in task 
'%s'", streamId, taskId);
         }
         return stream.streamContents.get();
     }
-    
+
     protected void checkEntityEntitled(Task<?> task) {
         Entity entity = BrooklynTaskTags.getContextEntity(task);
         if (entity != null && 
!Entitlements.isEntitled(mgmt().getEntitlementManager(), 
Entitlements.SEE_ENTITY, entity)) {
@@ -87,7 +108,7 @@ public class ActivityResource extends 
AbstractBrooklynRestResource implements Ac
                     Entitlements.getEntitlementContext().user(), entity);
         }
     }
-    
+
     protected void checkStreamEntitled(Task<?> task, String streamId) {
         Entity entity = BrooklynTaskTags.getContextEntity(task);
         Entitlements.TaskAndItem<String> item = new 
Entitlements.TaskAndItem<String>(task, streamId);

Reply via email to