You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by ma...@apache.org on 2013/12/11 09:32:10 UTC

[18/21] git commit: Add auto scaling policy deployment RESTful service in CLI

Add auto scaling policy deployment RESTful service in CLI


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

Branch: refs/heads/master
Commit: f9b14b72ddd00eaa40f04cd0a3dfaab4ff000b4d
Parents: a7c1a63
Author: Manula Thantriwatte <ma...@apache.org>
Authored: Wed Dec 11 11:51:42 2013 +0530
Committer: Manula Thantriwatte <ma...@apache.org>
Committed: Wed Dec 11 11:51:42 2013 +0530

----------------------------------------------------------------------
 .../stratos/cli/RestCommandLineService.java     |  14 +-
 .../apache/stratos/cli/StratosApplication.java  |   3 +
 .../commands/AutoScalingDeploymentCommand.java  | 141 +++++++++++++++++++
 .../commands/PartitionDeploymentCommand.java    |   3 +-
 .../apache/stratos/cli/utils/CliConstants.java  |   5 +
 5 files changed, 163 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/f9b14b72/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 17ebea6..ebe0e3b 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
@@ -53,6 +53,7 @@ public class RestCommandLineService {
     private final String unsubscribeTenantEndPoint = "/stratos/admin/cartridge/unsubscribe";
     private final String cartridgeDeploymentEndPoint = "/stratos/admin/cartridge/definition";
     private final String partitionDeploymentEndPoint = "/stratos/admin/policy/deployment/partition";
+    private final String autoscalingPolicyDeploymentEndPoint = "/stratos/admin/policy/autoscale";
 
     private static class SingletonHolder {
 		private final static RestCommandLineService INSTANCE = new RestCommandLineService();
@@ -470,7 +471,7 @@ public class RestCommandLineService {
                 System.out.println("Invalid operations. Authorization failed");
             }
             else {
-                System.out.println("You have successfully deployed the cartridge");
+                System.out.println("You have successfully deployed the partition");
             }
 
         } catch (Exception e) {
@@ -480,6 +481,17 @@ public class RestCommandLineService {
 
     public void deployAutoscalingPolicy (String autoScalingPolicy) {
         try {
+            String result = restClientService.doPost(restClientService.getUrl() + autoscalingPolicyDeploymentEndPoint,
+                    autoScalingPolicy, restClientService.getUsername(), restClientService.getPassword());
+
+            System.out.println(result);
+
+            if (Integer.parseInt(result) == CliConstants.RESPONSE_AUTHORIZATION_FAIL) {
+                System.out.println("Invalid operations. Authorization failed");
+            }
+            else {
+                System.out.println("You have successfully deployed the autoscaling policy");
+            }
 
         } catch (Exception e) {
             e.printStackTrace();

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/f9b14b72/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 4787ef4..9387d4a 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
@@ -113,6 +113,9 @@ public class StratosApplication extends CommandLineApplication<StratosCommandCon
 
         command = new PartitionDeploymentCommand();
         commands.put(command.getName(), command);
+
+        command = new AutoScalingDeploymentCommand();
+        commands.put(command.getName(), command);
 		
 		command = new ListCommand();
 		commands.put(command.getName(), command);

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/f9b14b72/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/AutoScalingDeploymentCommand.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/AutoScalingDeploymentCommand.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/AutoScalingDeploymentCommand.java
new file mode 100644
index 0000000..a5b18c7
--- /dev/null
+++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/AutoScalingDeploymentCommand.java
@@ -0,0 +1,141 @@
+/**
+ *  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.*;
+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;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+public class AutoScalingDeploymentCommand implements Command<StratosCommandContext> {
+
+    private static final Logger logger = LoggerFactory.getLogger(AutoScalingDeploymentCommand.class);
+
+    private final Options options;
+
+    public AutoScalingDeploymentCommand(){
+        options = constructOptions();
+    }
+
+    private Options constructOptions() {
+        final Options options = new Options();
+
+        Option resourcePath = new Option(CliConstants.RESOURCE_PATH, CliConstants.RESOURCE_PATH_LONG_OPTION, true,
+                "Autoscaling policy deployment resource path");
+        resourcePath.setArgName("resource path");
+        options.addOption(resourcePath);
+
+        return options;
+    }
+
+    public String getName() {
+        return CliConstants.AUTOSCALING_POLICY_DEPLOYMENT;
+    }
+
+    public String getDescription() {
+        return "Add new autoscaling policy deployment";
+    }
+
+    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) {
+            String resourcePath = null;
+            String autoscalingPolicyDeployment = null;
+
+            final CommandLineParser parser = new GnuParser();
+            CommandLine commandLine;
+
+            try {
+                commandLine = parser.parse(options, args);
+
+                if (logger.isDebugEnabled()) {
+                    logger.debug("Autoscaling policy deployment");
+                }
+
+                if (commandLine.hasOption(CliConstants.RESOURCE_PATH)) {
+                    if (logger.isTraceEnabled()) {
+                        logger.trace("Resource path option is passed");
+                    }
+                    resourcePath = commandLine.getOptionValue(CliConstants.RESOURCE_PATH);
+                    autoscalingPolicyDeployment = readResource(resourcePath);
+                }
+
+                if (resourcePath == null) {
+                    System.out.println("usage: " + getName() + " [-p <resource path>]");
+                    return CliConstants.BAD_ARGS_CODE;
+                }
+
+                System.out.println(autoscalingPolicyDeployment);
+                RestCommandLineService.getInstance().deployAutoscalingPolicy(autoscalingPolicyDeployment);
+                return CliConstants.SUCCESSFUL_CODE;
+
+            } catch (ParseException e) {
+                if (logger.isErrorEnabled()) {
+                    logger.error("Error parsing arguments", e);
+                }
+                System.out.println(e.getMessage());
+                return CliConstants.BAD_ARGS_CODE;
+            } catch (IOException e) {
+                //e.printStackTrace();
+                System.out.println("Invalid resource path");
+                return CliConstants.BAD_ARGS_CODE;
+            }
+        } else {
+            context.getStratosApplication().printUsage(getName());
+            return CliConstants.BAD_ARGS_CODE;
+        }
+    }
+
+    private String readResource(String fileName) throws IOException {
+        BufferedReader br = new BufferedReader(new FileReader(fileName));
+        try {
+            StringBuilder sb = new StringBuilder();
+            String line = br.readLine();
+
+            while (line != null) {
+                sb.append(line);
+                sb.append("\n");
+                line = br.readLine();
+            }
+            return sb.toString();
+        } finally {
+            br.close();
+        }
+    }
+
+    public Options getOptions() {
+        return options;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/f9b14b72/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/PartitionDeploymentCommand.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/PartitionDeploymentCommand.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/PartitionDeploymentCommand.java
index 1ca4074..0abeb09 100644
--- a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/PartitionDeploymentCommand.java
+++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/PartitionDeploymentCommand.java
@@ -34,7 +34,7 @@ import java.io.InputStreamReader;
 
 public class PartitionDeploymentCommand implements Command<StratosCommandContext> {
 
-    private static final Logger logger = LoggerFactory.getLogger(CartridgeDeploymentCommand.class);
+    private static final Logger logger = LoggerFactory.getLogger(PartitionDeploymentCommand.class);
 
     private final Options options;
 
@@ -134,7 +134,6 @@ public class PartitionDeploymentCommand implements Command<StratosCommandContext
         }
     }
 
-    @Override
     public Options getOptions() {
         return options;
     }

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/f9b14b72/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliConstants.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliConstants.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliConstants.java
index 9049f22..e820c87 100644
--- a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliConstants.java
+++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/utils/CliConstants.java
@@ -79,6 +79,11 @@ public class CliConstants {
      */
     public static final String PARTITION_DEPLOYMENT = "partition-deployment";
 
+    /**
+     * Autoscaling policy deployment
+     */
+    public static final String AUTOSCALING_POLICY_DEPLOYMENT = "autoscaling-policy-deployment";
+
 	/**
 	 * Give information of a cartridge.
 	 */