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/10/13 13:06:43 UTC

[20/50] stratos git commit: New CLI actions added

New CLI actions added

Signed-off-by: Imesh Gunaratne <im...@apache.org>


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

Branch: refs/heads/stratos-4.1.x
Commit: 7a78a9886fa7057577ba6e6c2715f18942f9f265
Parents: ea42e1e
Author: Milindu Sanoj Kumarage <ag...@gmail.com>
Authored: Wed Jul 29 09:57:26 2015 +0530
Committer: Imesh Gunaratne <im...@apache.org>
Committed: Tue Oct 13 16:32:46 2015 +0530

----------------------------------------------------------------------
 .../src/main/python/cli/CLI.py                  | 96 ++++++++++++++++----
 .../src/main/python/cli/Stratos.py              | 58 +++++++++++-
 2 files changed, 130 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/7a78a988/components/org.apache.stratos.python.cli/src/main/python/cli/CLI.py
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/CLI.py b/components/org.apache.stratos.python.cli/src/main/python/cli/CLI.py
index 554ba49..deaf90c 100755
--- a/components/org.apache.stratos.python.cli/src/main/python/cli/CLI.py
+++ b/components/org.apache.stratos.python.cli/src/main/python/cli/CLI.py
@@ -2,6 +2,7 @@ from cmd2 import *
 from Utils import *
 from Stratos import *
 import Configs
+from cli.exceptions import AuthenticationError
 
 
 class CLI(Cmd):
@@ -9,12 +10,19 @@ class CLI(Cmd):
 
     prompt = Configs.stratos_prompt
     # resolving the '-' issue
-    Cmd.legalChars += '-'
-    Cmd.shortcuts.update({'list-user': 'list_user'})
+    Cmd.legalChars = '-' + Cmd.legalChars
+
+    def __init__(self):
+        # resolving the '-' issue
+        [Cmd.shortcuts.update({a[3:].replace('_', '-'): a[3:]}) for a in self.get_names() if a.startswith('do_')]
+        Cmd.__init__(self)
 
     def completenames(self, text, *ignored):
+        # resolving the '-' issue
         return [a[3:].replace('_', '-') for a in self.get_names() if a.replace('_', '-').startswith('do-'+text)]
 
+
+
     @options([
         make_option('-u', '--username', type="str", help="Username of the user"),
         make_option('-p', '--password', type="str", help="Password of the user")
@@ -49,15 +57,42 @@ class CLI(Cmd):
     @auth
     def do_list_users(self, line , opts=None):
         """Illustrate the base class method use."""
-        r = requests.get(Configs.stratos_api_url + 'users',
-                         auth=(Configs.stratos_username, Configs.stratos_password), verify=False)
-        users = r.json()
+        try:
+            users = Stratos.list_users()
+            table = PrintableTable()
+            rows = [["Name", "language"]]
+            table.set_cols_align(["l", "r"])
+            table.set_cols_valign(["t", "m"])
+            for user in users:
+                rows.append([user['role'], user['userName']])
+            table.add_rows(rows)
+            table.print_table()
+        except AuthenticationError as e:
+            self.perror("sdc")
+
+    @options([
+        make_option('-u', '--username', type="str", help="Username of the user"),
+        make_option('-p', '--password', type="str", help="Password of the user")
+    ])
+    @auth
+    def do_list_network_partitions(self, line , opts=None):
+        """Illustrate the base class method use."""
+        repositories = Stratos.list_network_partitions()
+        tree = PrintableTree(repositories)
+        tree.print_tree()
+
+    @options([
+        make_option('-u', '--username', type="str", help="Username of the user"),
+        make_option('-p', '--password', type="str", help="Password of the user")
+    ])
+    @auth
+    def do_list_cartridges(self, line , opts=None):
+        """Illustrate the base class method use."""
+        cartridges = Stratos.list_cartridges()
         table = PrintableTable()
-        rows = [["Name", "language"]]
-        table.set_cols_align(["l", "r"])
-        table.set_cols_valign(["t", "m"])
-        for user in users:
-                    rows.append([user['role'], user['userName']])
+        rows = [["Type", "Category", "Name", "Description", "Version", "Multi-Tenant"]]
+        for cartridge in cartridges:
+            rows.append([cartridge['type'], cartridge['category'], cartridge['displayName'], cartridge['description'], cartridge['version'], cartridge['multiTenant']])
         table.add_rows(rows)
         table.print_table()
 
@@ -66,16 +101,39 @@ class CLI(Cmd):
         make_option('-p', '--password', type="str", help="Password of the user")
     ])
     @auth
-    def do_networkpartitions(self, line , opts=None):
+    def do_list_cartridges_group(self, line , opts=None):
         """Illustrate the base class method use."""
-        r = requests.get(Configs.stratos_api_url + 'networkPartitions',
-                         auth=(Configs.stratos_username, Configs.stratos_password), verify=False)
-        print(r)
-        print(r.text)
-        repositories = r.json()
-        tree = PrintableTree(repositories)
-        tree.print_tree()
+        cartridges_groups = Stratos.list_cartridges_group()
+        table = PrintableTable()
+        rows = [["Name", "No. of cartridges", "No of groups", "Dependency scaling"]]
+        for cartridges_group in cartridges_groups:
+            rows.append([cartridges_group['name'], cartridges_group['category'], cartridges_group['displayName'], cartridge['description'], cartridge['version'], cartridge['multiTenant']])
+        table.add_rows(rows)
+        table.print_table()
+
+    @options([
+        make_option('-u', '--username', type="str", help="Username of the user"),
+        make_option('-p', '--password', type="str", help="Password of the user")
+    ])
+    @auth
+    def do_list_applications(self, line , opts=None):
+        """Illustrate the base class method use."""
+        applications = Stratos.list_applications()
+        if not applications:
+            print("No applications found")
+        else:
+            table = PrintableTable()
+            rows = [["Type", "Category", "Name", "Description", "Version", "Multi-Tenant"]]
+            for application in applications:
+                rows.append([application['type'], application['category'], application['displayName'], application['description'], application['version'], application['multiTenant']])
+            table.add_rows(rows)
+            table.print_table()
 
+    @options([])
     def do_deploy_user(self, line , opts=None):
         """Illustrate the base class method use."""
-        print("hello User")
\ No newline at end of file
+        print("hello User")
+        try:
+            Stratos.deploy_user()
+        except ValueError as e:
+            self.perror("sdc")
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/stratos/blob/7a78a988/components/org.apache.stratos.python.cli/src/main/python/cli/Stratos.py
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Stratos.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Stratos.py
index 360353d..494bc21 100755
--- a/components/org.apache.stratos.python.cli/src/main/python/cli/Stratos.py
+++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Stratos.py
@@ -1,5 +1,7 @@
 import requests
 import Configs
+from cli.exceptions.AuthenticationError import AuthenticationError
+
 
 
 class Stratos:
@@ -10,17 +12,63 @@ class Stratos:
 
     @staticmethod
     def list_users():
-        r = requests.get(Configs.stratos_url + 'users',
+        r = requests.get(Configs.stratos_api_url + 'users',
                          auth=(Configs.stratos_username, Configs.stratos_password), verify=False)
-        if r.status_code is 200:
+
+        if r.status_code == 200:
             return r.json()
+        elif r.status_code == 400:
+            raise requests.HTTPError()
+        elif r.status_code == 401:
+            raise AuthenticationError()
 
-        elif r.status_code is 400:
+    @staticmethod
+    def list_network_partitions():
+        r = requests.get(Configs.stratos_api_url + 'networkPartitions',
+                         auth=(Configs.stratos_username, Configs.stratos_password), verify=False)
+        return r.json()
+
+    @staticmethod
+    def list_applications():
+        r = requests.get(Configs.stratos_api_url + 'applications',
+                         auth=(Configs.stratos_username, Configs.stratos_password), verify=False)
+        if r.status_code == 200:
+            return r.json()
+        elif r.status_code == 400:
             raise requests.HTTPError()
+        elif r.status_code == 401:
+            raise AuthenticationError()
+        elif r.status_code == 404:
+            if r.json() and r.json()['errorMessage'] == "No applications found":
+                return []
+            else:
+                raise requests.HTTPError()
+
 
     @staticmethod
-    def network_partitions():
-        pass
+    def list_cartridges():
+        r = requests.get(Configs.stratos_api_url + 'cartridges',
+                         auth=(Configs.stratos_username, Configs.stratos_password), verify=False)
+        return r.json()
 
+    @staticmethod
+    def list_cartridges_groups():
+        r = requests.get(Configs.stratos_api_url + 'cartridgeGroups',
+                         auth=(Configs.stratos_username, Configs.stratos_password), verify=False)
+        if r.status_code == 200:
+            return r.json()
+        elif r.status_code == 400:
+            raise requests.HTTPError()
+        elif r.status_code == 401:
+            raise AuthenticationError()
+        elif r.status_code == 404:
+            if r.json() and r.json()['errorMessage'] == "No cartridges found":
+                return []
+            else:
+                raise requests.HTTPError()
+
+    @staticmethod
+    def deploy_user():
+        raise ValueError