You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by im...@apache.org on 2015/05/18 14:58:15 UTC

[2/2] stratos git commit: Display proper response messages in the CLI commands

Display proper response messages in the CLI commands


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

Branch: refs/heads/master
Commit: ee8e72e124218e921a9e978f0b6cce2148cc0de1
Parents: 8cb0c50
Author: Dinithi <di...@wso2.com>
Authored: Mon May 18 17:11:17 2015 +0530
Committer: Imesh Gunaratne <im...@apache.org>
Committed: Mon May 18 18:28:08 2015 +0530

----------------------------------------------------------------------
 .../stratos/autoscaler/util/AutoscalerUtil.java |  5 +-
 .../java/org/apache/stratos/cli/RestClient.java | 48 ++++++++------------
 .../impl/CloudControllerServiceImpl.java        |  4 +-
 3 files changed, 23 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/ee8e72e1/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/util/AutoscalerUtil.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/util/AutoscalerUtil.java b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/util/AutoscalerUtil.java
index b62d7c1..ce71418 100644
--- a/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/util/AutoscalerUtil.java
+++ b/components/org.apache.stratos.autoscaler/src/main/java/org/apache/stratos/autoscaler/util/AutoscalerUtil.java
@@ -660,9 +660,8 @@ public class AutoscalerUtil {
             // network partitions should be added already
             if (null == CloudControllerServiceClient.getInstance().
                     getNetworkPartition(networkPartitionId)) {
-                String msg = String.format("Invalid Application Policy. "
-                                + "Cause -> Network partition not found for network-partition-id : %s",
-                        networkPartitionId);
+                String msg = String.format("Network partition not found : [network-partition-id]  %s in " +
+                                "[application-policy-id] %s", networkPartitionId,applicationPolicy.getId());
                 log.error(msg);
                 throw new InvalidApplicationPolicyException(msg);
             }

http://git-wip-us.apache.org/repos/asf/stratos/blob/ee8e72e1/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java
index 66fe9cb..faf8bd6 100644
--- a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java
+++ b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/RestClient.java
@@ -31,12 +31,15 @@ import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.params.HttpConnectionParams;
 import org.apache.http.params.HttpParams;
 import org.apache.stratos.cli.utils.CliUtils;
+import org.apache.stratos.common.beans.ResponseMessageBean;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
 import java.lang.reflect.Type;
 
+import static org.apache.stratos.cli.utils.CliUtils.getHttpResponseString;
+
 /**
  * This class has three types of methods
  * 1. doPost, doPut, doGet, doDelete
@@ -164,14 +167,8 @@ public class RestClient implements GenericRestClient {
 
     public void deployEntity(String serviceEndpoint, String entityBody, String entityName) {
         try {
-            int responseCode = executePost(serviceEndpoint, entityBody);
-            if (responseCode == 201 || responseCode == 200) {
-                System.out.println(String.format("Successfully added %s", entityName));
-            } else if (responseCode == 500) {
-                System.out.println("Internal server error occurred");
-            } else if (responseCode == 409) {
-                System.out.println(String.format("Specified %s already exists",entityName));
-            }
+            String responseMessage = executePost(serviceEndpoint, entityBody);
+            System.out.println(responseMessage);
         } catch (Exception e) {
             String message = String.format("Error in adding %s", entityName);
             System.out.println(message);
@@ -198,14 +195,8 @@ public class RestClient implements GenericRestClient {
 
     public void updateEntity(String serviceEndpoint, String entityBody, String entityName) {
         try {
-            int responseCode = executePut(serviceEndpoint, entityBody);
-            if (responseCode == 404) {
-                System.out.println(String.format("%s not found", StringUtils.capitalize(entityName)));
-            } else if (responseCode == 500) {
-                System.out.println("Internal server error occurred");
-            } else if (responseCode >= 200 && responseCode < 300) {
-                System.out.println(String.format("Successfully updated %s", entityName));
-            }
+           String responseMessage = executePut(serviceEndpoint, entityBody);
+           System.out.println(responseMessage);
         } catch (Exception e) {
             String message = String.format("Error in updating %s", entityName);
             System.out.println(message);
@@ -275,16 +266,16 @@ public class RestClient implements GenericRestClient {
         }
     }
 
-    private int executePost(String serviceEndpoint, String postBody) throws IOException {
+    private String executePost(String serviceEndpoint, String postBody) throws IOException {
         DefaultHttpClient httpClient = new DefaultHttpClient();
         try {
             HttpResponse response = doPost(httpClient, getBaseURL() + serviceEndpoint, postBody);
+            String result = getHttpResponseString(response);
+
+            GsonBuilder gsonBuilder = new GsonBuilder();
+            Gson gson = gsonBuilder.create();
+            return gson.fromJson(result, ResponseMessageBean.class).getMessage();
 
-            int responseCode = response.getStatusLine().getStatusCode();
-            if (responseCode < 200 || responseCode >= 300) {
-                CliUtils.printError(response);
-            }
-            return responseCode;
         } finally {
             httpClient.getConnectionManager().shutdown();
         }
@@ -327,23 +318,22 @@ public class RestClient implements GenericRestClient {
                 CliUtils.printError(response);
                 return null;
             } else {
-                return CliUtils.getHttpResponseString(response);
+                return getHttpResponseString(response);
             }
         } finally {
             httpClient.getConnectionManager().shutdown();
         }
     }
 
-    private int executePut(String serviceEndpoint, String postBody) throws IOException {
+    private String executePut(String serviceEndpoint, String postBody) throws IOException {
         DefaultHttpClient httpClient = new DefaultHttpClient();
         try {
             HttpResponse response = doPut(httpClient, getBaseURL() + serviceEndpoint, postBody);
+            String result = getHttpResponseString(response);
 
-            int responseCode = response.getStatusLine().getStatusCode();
-            if (responseCode < 200 || responseCode >= 300) {
-                CliUtils.printError(response);
-            }
-            return responseCode;
+            GsonBuilder gsonBuilder = new GsonBuilder();
+            Gson gson = gsonBuilder.create();
+            return gson.fromJson(result, ResponseMessageBean.class).getMessage();
         } finally {
             httpClient.getConnectionManager().shutdown();
         }

http://git-wip-us.apache.org/repos/asf/stratos/blob/ee8e72e1/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/CloudControllerServiceImpl.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/CloudControllerServiceImpl.java b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/CloudControllerServiceImpl.java
index daa4d2b4..f7b44cb 100644
--- a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/CloudControllerServiceImpl.java
+++ b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/services/impl/CloudControllerServiceImpl.java
@@ -1466,7 +1466,7 @@ public class CloudControllerServiceImpl implements CloudControllerService {
             handleNullObject(networkPartitionId, "Network Partition ID is null");
 
             if (cloudControllerContext.getNetworkPartition(networkPartitionId) == null) {
-                String message = "Network partition not exists: [network-partiton-id] " + networkPartitionId;
+                String message = "Network partition not found: [network-partition-id] " + networkPartitionId;
                 log.error(message);
                 throw new NetworkPartitionNotExistsException(message);
             }
@@ -1498,7 +1498,7 @@ public class CloudControllerServiceImpl implements CloudControllerService {
 
             String networkPartitionID = networkPartition.getId();
             if (cloudControllerContext.getNetworkPartition(networkPartitionID) == null) {
-                String message = "Network partition not exists: [network-partition-id] " + networkPartitionID;
+                String message = "Network partition not found: [network-partition-id] " + networkPartitionID;
                 log.error(message);
                 throw new NetworkPartitionNotExistsException(message);
             }