Repository: stratos
Updated Branches:
  refs/heads/master 536f9bd3c -> 0704df1ff


Fixing domain mappings CLI commands


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

Branch: refs/heads/master
Commit: 0704df1ffde9c9131a012cd7b52237de3f4e2b34
Parents: 536f9bd
Author: Imesh Gunaratne <[email protected]>
Authored: Fri Jan 16 10:22:54 2015 +0530
Committer: Imesh Gunaratne <[email protected]>
Committed: Fri Jan 16 10:22:54 2015 +0530

----------------------------------------------------------------------
 .../stratos/cli/RestCommandLineService.java     |  52 ++++++++-
 .../apache/stratos/cli/StratosApplication.java  |   9 ++
 .../cli/commands/AddDomainMappingCommand.java   |  87 --------------
 .../cli/commands/AddDomainMappingsCommand.java  | 114 +++++++++++++++++++
 .../cli/commands/ListDomainMappingsCommand.java |  67 +++++++++++
 .../commands/RemoveDomainMappingsCommand.java   |  76 +++++++++++++
 6 files changed, 314 insertions(+), 91 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/0704df1f/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 baac666..4b28419 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
@@ -36,15 +36,16 @@ import org.apache.stratos.cli.utils.CliUtils;
 import org.apache.stratos.cli.utils.RowMapper;
 import org.apache.stratos.common.beans.TenantInfoBean;
 import org.apache.stratos.common.beans.UserInfoBean;
+import org.apache.stratos.common.beans.application.ApplicationBean;
 import org.apache.stratos.common.beans.application.GroupBean;
-import org.apache.stratos.common.beans.policy.autoscale.AutoscalePolicyBean;
-import org.apache.stratos.common.beans.policy.deployment.DeploymentPolicyBean;
+import 
org.apache.stratos.common.beans.application.domain.mapping.DomainMappingBean;
 import org.apache.stratos.common.beans.cartridge.CartridgeBean;
 import org.apache.stratos.common.beans.cartridge.IaasProviderBean;
 import org.apache.stratos.common.beans.kubernetes.KubernetesClusterBean;
 import org.apache.stratos.common.beans.kubernetes.KubernetesHostBean;
+import org.apache.stratos.common.beans.policy.autoscale.AutoscalePolicyBean;
+import org.apache.stratos.common.beans.policy.deployment.DeploymentPolicyBean;
 import org.apache.stratos.common.beans.topology.ClusterBean;
-import org.apache.stratos.common.beans.application.ApplicationBean;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -77,6 +78,7 @@ public class RestCommandLineService {
     private static final String ENDPOINT_DEPLOY_SERVICE_GROUP = API_CONTEXT + 
"/groups";
     private static final String ENDPOINT_DEPLOY_APPLICATION = API_CONTEXT + 
"/applications/{applicationId}/deploy";
 
+
     private static final String ENDPOINT_UNDEPLOY_KUBERNETES_CLUSTER= 
API_CONTEXT + "/kubernetesCluster/{id}";
     private static final String ENDPOINT_UNDEPLOY_KUBERNETES_HOST = 
API_CONTEXT + "/kubernetesCluster/{kubernetesClusterId}/hosts/{id}";
     private static final String ENDPOINT_UNDEPLOY_SERVICE_GROUP = API_CONTEXT 
+ "/groups/{id}";
@@ -93,6 +95,8 @@ public class RestCommandLineService {
     private static final String ENDPOINT_LIST_SERVICE_GROUP = API_CONTEXT + 
"/groups/{groupDefinitionName}";
     private static final String ENDPOINT_LIST_APPLICATION = API_CONTEXT + 
"/applications";
 
+    private static final String ENDPOINT_DOMAIN_MAPPINGS = API_CONTEXT + 
"/applications/{applicationId}/domainMappings";
+
     private static final String ENDPOINT_GET_APPLICATION = API_CONTEXT + 
"/applications/{appId}";
     private static final String ENDPOINT_GET_AUTOSCALING_POLICY = API_CONTEXT 
+ "/autoscalingPolicies/{id}";
     private static final String ENDPOINT_GET_DEPLOYMENT_POLICY = API_CONTEXT + 
"/applications/{applicationId}/deploymentPolicy";
@@ -112,7 +116,6 @@ public class RestCommandLineService {
     private static final String ENDPOINT_UPDATE_DEPLOYMENT_POLICY = 
API_CONTEXT + "/deploymentPolicies";
     private static final String ENDPOINT_UPDATE_AUTOSCALING_POLICY = 
API_CONTEXT + "/autoscalePolicies";
 
-
     private static class SingletonHolder {
         private final static RestCommandLineService INSTANCE = new 
RestCommandLineService();
     }
@@ -848,6 +851,47 @@ public class RestCommandLineService {
         }
     }
 
+    public void addDomainMappings(String resourceFileContent) {
+        restClient.deployEntity(ENDPOINT_DOMAIN_MAPPINGS, resourceFileContent, 
"domain mappings");
+    }
+
+    public void listDomainMappings(String applicationId) {
+        try {
+            Type listType = new TypeToken<ArrayList<DomainMappingBean>>() {
+            }.getType();
+            List<DomainMappingBean> list = (List<DomainMappingBean>) 
restClient.listEntity(
+                    ENDPOINT_DOMAIN_MAPPINGS.replace("{applicationId}", 
applicationId),
+                    listType, "domain mappings");
+            if ((list != null) && (list.size() > 0)) {
+                RowMapper<DomainMappingBean> rowMapper = new 
RowMapper<DomainMappingBean>() {
+                    public String[] getData(DomainMappingBean 
domainMappingBean) {
+                        String[] data = new String[3];
+                        data[0] = domainMappingBean.getCartridgeAlias();
+                        data[1] = domainMappingBean.getDomainName();
+                        data[2] = domainMappingBean.getContextPath();
+                        return data;
+                    }
+                };
+
+                DomainMappingBean[] array = new DomainMappingBean[list.size()];
+                array = list.toArray(array);
+                System.out.println("Domain mappings found in application: 
[application-id] " + applicationId);
+                CliUtils.printTable(array, rowMapper, "Cartridge Alias", 
"Domain Name", "Context Path");
+            } else {
+                System.out.println("No domain mappings found in application: 
[application-id] " + applicationId);
+                return;
+            }
+        } catch (Exception e) {
+            String message = "Could not list domain mappings in application: 
[application-id] " + applicationId;
+            printError(message, e);
+        }
+    }
+
+    public void removeDomainMappings(String applicationId) {
+        String endpoint = ENDPOINT_DOMAIN_MAPPINGS.replace("{applicationId}", 
applicationId);
+        restClient.undeployEntity(endpoint, "domain mappings", applicationId);
+    }
+
     public void undeployKubernetesHost(String clusterId, String hostId) {
         
restClient.undeployEntity(ENDPOINT_UNDEPLOY_KUBERNETES_HOST.replace("{kubernetesClusterId}",
 clusterId), "kubernetes host", hostId);
     }

http://git-wip-us.apache.org/repos/asf/stratos/blob/0704df1f/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 7ddf1bc..c170369 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
@@ -183,6 +183,15 @@ public class StratosApplication extends 
CommandLineApplication<StratosCommandCon
         command = new DescribeApplicationCommand();
         commands.put(command.getName(), command);
 
+               command = new AddDomainMappingsCommand();
+               commands.put(command.getName(), command);
+
+               command = new ListDomainMappingsCommand();
+               commands.put(command.getName(), command);
+
+               command = new RemoveDomainMappingsCommand();
+               commands.put(command.getName(), command);
+
         if (logger.isDebugEnabled()) {
                        logger.debug("Created {} commands for the application. 
{}", commands.size(), commands.keySet());
                }

http://git-wip-us.apache.org/repos/asf/stratos/blob/0704df1f/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/AddDomainMappingCommand.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/AddDomainMappingCommand.java
 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/AddDomainMappingCommand.java
deleted file mode 100644
index 773e64f..0000000
--- 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/AddDomainMappingCommand.java
+++ /dev/null
@@ -1,87 +0,0 @@
-/**
- *  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.commons.lang3.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.apache.stratos.cli.Command;
-import org.apache.stratos.cli.CommandLineService;
-import org.apache.stratos.cli.StratosCommandContext;
-import org.apache.stratos.cli.exception.CommandException;
-import org.apache.stratos.cli.utils.CliConstants;
-
-public class AddDomainMappingCommand implements Command<StratosCommandContext> 
{
-
-       private static final Logger logger = 
LoggerFactory.getLogger(AddDomainMappingCommand.class);
-
-       public AddDomainMappingCommand() {
-       }
-
-       @Override
-       public String getName() {
-               return CliConstants.ADD_DOMAIN_MAPPING_ACTION;
-       }
-
-       @Override
-       public String getDescription() {
-               return "Map domain for the subscribed cartridge";
-       }
-
-       @Override
-       public String getArgumentSyntax() {
-               return "[Cartridge alias] [Domain]";
-       }
-
-       @Override
-       public int execute(StratosCommandContext context, String[] args) throws 
CommandException {
-               if (logger.isDebugEnabled()) {
-                       logger.debug("Executing {} command...", getName());
-               }
-               if (args != null && args.length == 2) {
-                       String alias = args[0];
-                       String domain = args[1];
-                       if (logger.isDebugEnabled()) {
-                               logger.debug("Adding domain mapping {} for 
alias {}", domain, alias);
-                       }
-
-                       String domainToDisplay = null;
-
-                       domainToDisplay = 
CommandLineService.getInstance().addDomainMapping(domain, alias);
-
-                       if (StringUtils.isBlank(domainToDisplay)) {
-                               System.out.println("Error adding domain 
mapping.");
-                               return CliConstants.COMMAND_FAILED;
-                       } else {
-                               System.out.format("Your own domain is added. 
Please CNAME it to systems domain %s.%n", domainToDisplay);
-                               return CliConstants.COMMAND_SUCCESSFULL;
-                       }
-               } else {
-                       context.getStratosApplication().printUsage(getName());
-                       return CliConstants.COMMAND_FAILED;
-               }
-       }
-
-       @Override
-       public Options getOptions() {
-               return null;
-       }
-
-}

http://git-wip-us.apache.org/repos/asf/stratos/blob/0704df1f/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/AddDomainMappingsCommand.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/AddDomainMappingsCommand.java
 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/AddDomainMappingsCommand.java
new file mode 100644
index 0000000..e9036ba
--- /dev/null
+++ 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/AddDomainMappingsCommand.java
@@ -0,0 +1,114 @@
+/**
+ *  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.apache.stratos.cli.utils.CliUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+
+/**
+ * Add domain mappings command.
+ */
+public class AddDomainMappingsCommand implements 
Command<StratosCommandContext> {
+
+    private static final Logger logger = 
LoggerFactory.getLogger(AddDomainMappingsCommand.class);
+
+    private Options options;
+
+    public AddDomainMappingsCommand() {
+        options = new Options();
+        Option option = new Option(CliConstants.RESOURCE_PATH, 
CliConstants.RESOURCE_PATH_LONG_OPTION, true,
+                "Domain mappings resource path");
+        option.setArgName("resource path");
+        options.addOption(option);
+    }
+
+    @Override
+    public String getName() {
+        return "add-domain-mappings";
+    }
+
+    @Override
+    public String getDescription() {
+        return "Add domain mappings";
+    }
+
+    @Override
+    public String getArgumentSyntax() {
+        return null;
+    }
+
+    @Override
+    public Options getOptions() {
+        return options;
+    }
+
+    @Override
+    public int execute(StratosCommandContext context, String[] args) throws 
CommandException {
+        if (logger.isDebugEnabled()) {
+            logger.debug("Executing command: ", getName());
+        }
+
+        if ((args == null) || (args.length <= 0)) {
+            context.getStratosApplication().printUsage(getName());
+            return CliConstants.COMMAND_FAILED;
+        }
+
+        try {
+            CommandLineParser parser = new GnuParser();
+            CommandLine commandLine = parser.parse(options, args);
+            if (commandLine.hasOption(CliConstants.RESOURCE_PATH)) {
+                String resourcePath = 
commandLine.getOptionValue(CliConstants.RESOURCE_PATH);
+                if (resourcePath == null) {
+                    System.out.println("usage: " + getName() + " [-" + 
CliConstants.RESOURCE_PATH + " "
+                            + CliConstants.RESOURCE_PATH_LONG_OPTION + "]");
+                    return CliConstants.COMMAND_FAILED;
+                }
+                String resourceFileContent = 
CliUtils.readResource(resourcePath);
+                
RestCommandLineService.getInstance().addDomainMappings(resourceFileContent);
+                return CliConstants.COMMAND_SUCCESSFULL;
+            } else {
+                System.out.println("usage: " + getName() + " [-" + 
CliConstants.RESOURCE_PATH + " "
+                        + CliConstants.RESOURCE_PATH_LONG_OPTION + "]");
+                return CliConstants.COMMAND_FAILED;
+            }
+        } catch (ParseException e) {
+            logger.error("Error parsing arguments", e);
+            System.out.println(e.getMessage());
+            return CliConstants.COMMAND_FAILED;
+        } catch (IOException e) {
+            System.out.println("Invalid resource path");
+            return CliConstants.COMMAND_FAILED;
+        } catch (Exception e) {
+            String message = "Unknown error occurred: " + e.getMessage();
+            System.out.println(message);
+            logger.error(message, e);
+            return CliConstants.COMMAND_FAILED;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/stratos/blob/0704df1f/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/ListDomainMappingsCommand.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/ListDomainMappingsCommand.java
 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/ListDomainMappingsCommand.java
new file mode 100644
index 0000000..ca66caf
--- /dev/null
+++ 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/ListDomainMappingsCommand.java
@@ -0,0 +1,67 @@
+/**
+ *  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 ListDomainMappingsCommand implements 
Command<StratosCommandContext> {
+
+       private static final Logger logger = 
LoggerFactory.getLogger(ListDomainMappingsCommand.class);
+
+       public ListDomainMappingsCommand() {
+       }
+
+       public String getName() {
+               return "list-domain-mappings";
+       }
+
+       public String getDescription() {
+               return "List domain mappings";
+       }
+
+       public String getArgumentSyntax() {
+               return "[application-id]";
+       }
+
+       public int execute(StratosCommandContext context, String[] args) throws 
CommandException {
+               if (logger.isDebugEnabled()) {
+                       logger.debug("Executing command: ", getName());
+               }
+               if ((args == null) || (args.length == 0)) {
+                       context.getStratosApplication().printUsage(getName());
+                       return CliConstants.COMMAND_FAILED;
+               } else {
+                       String applicationId = args[0];
+                       
RestCommandLineService.getInstance().listDomainMappings(applicationId);
+                       return CliConstants.COMMAND_SUCCESSFULL;
+               }
+       }
+
+       public Options getOptions() {
+               return null;
+       }
+}

http://git-wip-us.apache.org/repos/asf/stratos/blob/0704df1f/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/RemoveDomainMappingsCommand.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/RemoveDomainMappingsCommand.java
 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/RemoveDomainMappingsCommand.java
new file mode 100644
index 0000000..b711008
--- /dev/null
+++ 
b/components/org.apache.stratos.cli/src/main/java/org/apache/stratos/cli/commands/RemoveDomainMappingsCommand.java
@@ -0,0 +1,76 @@
+/**
+ *  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;
+
+/**
+ * Remove domain mappings command.
+ */
+public class RemoveDomainMappingsCommand implements 
Command<StratosCommandContext> {
+
+    private static final Logger logger = 
LoggerFactory.getLogger(RemoveDomainMappingsCommand.class);
+
+    public RemoveDomainMappingsCommand() {
+    }
+
+    @Override
+    public String getName() {
+        return "remove-domain-mappings";
+    }
+
+    @Override
+    public String getDescription() {
+        return "Remove domain mappings";
+    }
+
+    @Override
+    public String getArgumentSyntax() {
+        return "[application-id]";
+    }
+
+    @Override
+    public Options getOptions() {
+        return null;
+    }
+
+    @Override
+    public int execute(StratosCommandContext context, String[] args) throws 
CommandException {
+        if (logger.isDebugEnabled()) {
+            logger.debug("Executing command: ", getName());
+        }
+
+        if ((args == null) || (args.length <= 0)) {
+            context.getStratosApplication().printUsage(getName());
+            return CliConstants.COMMAND_FAILED;
+        }
+
+        String applicationId = args[0];
+        
RestCommandLineService.getInstance().removeDomainMappings(applicationId);
+        return CliConstants.COMMAND_SUCCESSFULL;
+    }
+}

Reply via email to