Add CLI command to list cartridge group

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

Branch: refs/heads/master
Commit: 40f42a56695746743a42140b987737b1144a4ea0
Parents: ad71a49
Author: Gayan Gunarathne <[email protected]>
Authored: Tue Jan 27 18:06:11 2015 +0530
Committer: Gayan Gunarathne <[email protected]>
Committed: Tue Jan 27 22:09:45 2015 +0530

----------------------------------------------------------------------
 .../stratos/cli/RestCommandLineService.java     | 39 ++++++++++++
 .../apache/stratos/cli/StratosApplication.java  |  3 +
 .../commands/ListCartridgeGroupsCommand.java    | 66 ++++++++++++++++++++
 3 files changed, 108 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/40f42a56/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java
 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java
index ed7173b..d8d6bee 100644
--- 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java
+++ 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestCommandLineService.java
@@ -94,6 +94,7 @@ public class RestCommandLineService {
     private static final String ENDPOINT_LIST_AUTOSCALING_POLICIES = 
API_CONTEXT + "/autoscalingPolicies";
     private static final String ENDPOINT_LIST_DEPLOYMENT_POLICIES = 
API_CONTEXT + "/deploymentPolicies";
     private static final String ENDPOINT_LIST_CARTRIDGES = API_CONTEXT + 
"/cartridges";
+       private static final String ENDPOINT_LIST_CARTRIDGE_GROUPS = 
API_CONTEXT + "/cartridgeGroups";
     private static final String ENDPOINT_LIST_TENANTS = API_CONTEXT + 
"/tenants";
     private static final String ENDPOINT_LIST_USERS = API_CONTEXT + "/users";
     private static final String ENDPOINT_LIST_KUBERNETES_CLUSTERS = 
API_CONTEXT + "/kubernetesCluster";
@@ -292,6 +293,44 @@ public class RestCommandLineService {
         }
     }
 
+       /**
+        * List cartridge groups
+        *
+        * @throws CommandException
+        */
+       public void listCartridgeGroups() throws CommandException {
+               try {
+                       Type listType = new 
TypeToken<ArrayList<CartridgeBean>>() {
+                       }.getType();
+                       List<GroupBean> cartridgeGroupList = (List<GroupBean>) 
restClient.listEntity(ENDPOINT_LIST_CARTRIDGE_GROUPS,
+                                                                               
                        listType, "cartridgeGroups");
+
+                       if ((cartridgeGroupList == null) || 
(cartridgeGroupList.size() == 0)) {
+                               System.out.println("No cartridges found");
+                               return;
+                       }
+
+                       RowMapper<GroupBean> cartridgeGroupMapper = new 
RowMapper<GroupBean>() {
+                               public String[] getData(GroupBean 
cartridgeGroup) {
+                                       String[] data = new String[6];
+                                       data[0] = cartridgeGroup.getName();
+                                       data[1] = 
String.valueOf(cartridgeGroup.getCartridges().size());
+                                       data[2] = 
String.valueOf(cartridgeGroup.getGroups().size());
+                                       data[3] = 
String.valueOf(cartridgeGroup.isGroupScalingEnabled());;
+                                       return data;
+                               }
+                       };
+
+                       GroupBean[] cartridgeGroups = new 
GroupBean[cartridgeGroupList.size()];
+                       cartridgeGroups = 
cartridgeGroupList.toArray(cartridgeGroups);
+
+                       System.out.println("Cartridge Groups found:");
+                       CliUtils.printTable(cartridgeGroups, 
cartridgeGroupMapper, "Name", "No. of Cartridges", "No of Groups", "Dependency 
scaling");
+               } catch (Exception e) {
+                       String message = "Error in listing cartridge groups";
+                       printError(message, e);
+               }
+       }
     /**
      * Describe a cartridge
      * @param cartridgeType

http://git-wip-us.apache.org/repos/asf/stratos/blob/40f42a56/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/StratosApplication.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/StratosApplication.java
 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/StratosApplication.java
index b6703d2..5f56669 100644
--- 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/StratosApplication.java
+++ 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/StratosApplication.java
@@ -177,6 +177,9 @@ public class StratosApplication extends 
CommandLineApplication<StratosCommandCon
         command = new DescribeCartridgeGroupCommand();
         commands.put(command.getName(), command);
 
+               command = new ListCartridgeGroupsCommand();
+               commands.put(command.getName(), command);
+
         command = new RemoveCartridgeGroupCommand();
         commands.put(command.getName(), command);
 

http://git-wip-us.apache.org/repos/asf/stratos/blob/40f42a56/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/ListCartridgeGroupsCommand.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/ListCartridgeGroupsCommand.java
 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/ListCartridgeGroupsCommand.java
new file mode 100644
index 0000000..6a2a540
--- /dev/null
+++ 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/ListCartridgeGroupsCommand.java
@@ -0,0 +1,66 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+
+ *  http://www.apache.org/licenses/LICENSE-2.0
+
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.stratos.cli.commands;
+
+import org.apache.commons.cli.Options;
+import org.apache.stratos.cli.Command;
+import org.apache.stratos.cli.RestCommandLineService;
+import org.apache.stratos.cli.StratosCommandContext;
+import org.apache.stratos.cli.exception.CommandException;
+import org.apache.stratos.cli.utils.CliConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ListCartridgeGroupsCommand implements 
Command<StratosCommandContext> {
+
+       private static final Logger logger = 
LoggerFactory.getLogger(ListCartridgeGroupsCommand.class);
+
+       public ListCartridgeGroupsCommand() {
+       }
+
+       public String getName() {
+               return "list-cartridge-groups";
+       }
+
+       public String getDescription() {
+               return "List available cartridge groups";
+       }
+
+       public String getArgumentSyntax() {
+               return null;
+       }
+
+       public int execute(StratosCommandContext context, String[] args) throws 
CommandException {
+               if (logger.isDebugEnabled()) {
+                       logger.debug("Executing {} command...", getName());
+               }
+               if (args == null || args.length == 0) {
+            RestCommandLineService.getInstance().listCartridgeGroups();
+                       return CliConstants.COMMAND_SUCCESSFULL;
+               } else {
+                       context.getStratosApplication().printUsage(getName());
+                       return CliConstants.COMMAND_FAILED;
+               }
+       }
+
+       public Options getOptions() {
+               return null;
+       }
+
+}

Reply via email to