From 33c66410b0c6e7dcb2a183b1d44cd6598b5cc083 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Fri, 1 May 2015 08:08:30 +0530 Subject: [PATCH 01/79] Create README.md --- components/org.apache.stratos.python.cli/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 components/org.apache.stratos.python.cli/README.md diff --git a/components/org.apache.stratos.python.cli/README.md b/components/org.apache.stratos.python.cli/README.md new file mode 100644 index 0000000000..d1596d7e5a --- /dev/null +++ b/components/org.apache.stratos.python.cli/README.md @@ -0,0 +1 @@ +#Python based CLI for Apache Stratos From 758555cdb874671bbae639c58c3bb6d6a3ec96ac Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sun, 3 May 2015 10:53:16 +0530 Subject: [PATCH 02/79] Directory structure added On branch python-cli new file: components/org.apache.stratos.python.cli/src/main/python/cli/__init__.py --- .../org.apache.stratos.python.cli/src/main/python/cli/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 components/org.apache.stratos.python.cli/src/main/python/cli/__init__.py diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/__init__.py b/components/org.apache.stratos.python.cli/src/main/python/cli/__init__.py new file mode 100644 index 0000000000..e69de29bb2 From 3020d25b53e800c98ece368dd6de0a30b13502e8 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sun, 3 May 2015 12:11:24 +0530 Subject: [PATCH 03/79] Setup file added, setup config added On branch python-cli new file: components/org.apache.stratos.python.cli/src/main/python/cli/setup.cfg new file: components/org.apache.stratos.python.cli/src/main/python/cli/setup.py --- .../src/main/python/cli/setup.cfg | 2 ++ .../src/main/python/cli/setup.py | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 components/org.apache.stratos.python.cli/src/main/python/cli/setup.cfg create mode 100644 components/org.apache.stratos.python.cli/src/main/python/cli/setup.py diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/setup.cfg b/components/org.apache.stratos.python.cli/src/main/python/cli/setup.cfg new file mode 100644 index 0000000000..5e4090017a --- /dev/null +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/setup.cfg @@ -0,0 +1,2 @@ +[wheel] +universal = 1 diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/setup.py b/components/org.apache.stratos.python.cli/src/main/python/cli/setup.py new file mode 100644 index 0000000000..21d34c8fc1 --- /dev/null +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/setup.py @@ -0,0 +1,33 @@ +import os +from setuptools import setup + +# Utility function to read the README file. +# Used for the long_description. It's nice, because now 1) we have a top level +# README file and 2) it's easier to type in the README file than to put a raw +# string in below ... +def read(fname): + return open(os.path.join(os.path.dirname(__file__), fname)).read() + +setup( + name = "stratos", + version = "0.0.5", + author = "Agentmilindu", + author_email = "Agentmilindu@gmail.com", + py_modules=['stratos'], + description = ("CLI for Apache Startos"), + keywords = "stratos", + url = "http://stratos.apache.org/", + packages=['stratos'], + install_requires=['cmd2','requests','texttable'], + long_description=read('README.md'), + classifiers=[ + "Development Status :: 1 - Planning", + "Topic :: Utilities", + "License :: OSI Approved :: Apache Software License", + ], + entry_points=''' + [console_scripts] + stratos=src.Main:main + ''', +) + From 4ef19f9bae3c4d79a80f024d86d371732b18af3b Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sun, 3 May 2015 15:45:56 +0530 Subject: [PATCH 04/79] Directory for Tests added On branch python-cli new file: components/org.apache.stratos.python.cli/src/main/python/tests/__init__.py --- .../src/main/python/tests/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 components/org.apache.stratos.python.cli/src/main/python/tests/__init__.py diff --git a/components/org.apache.stratos.python.cli/src/main/python/tests/__init__.py b/components/org.apache.stratos.python.cli/src/main/python/tests/__init__.py new file mode 100644 index 0000000000..e69de29bb2 From 6553b4604ea0de89eba936519c041337616bbd5d Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sun, 3 May 2015 15:47:29 +0530 Subject: [PATCH 05/79] Setup file and Setup config file moved to correct location On branch python-cli renamed: components/org.apache.stratos.python.cli/src/main/python/cli/setup.cfg -> components/org.apache.stratos.python.cli/src/main/python/setup.cfg renamed: components/org.apache.stratos.python.cli/src/main/python/cli/setup.py -> components/org.apache.stratos.python.cli/src/main/python/setup.py Refer https://pythonhosted.org/an_example_pypi_project/setuptools.html#directory-structure --- .../src/main/python/{cli => }/setup.cfg | 0 .../src/main/python/{cli => }/setup.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename components/org.apache.stratos.python.cli/src/main/python/{cli => }/setup.cfg (100%) rename components/org.apache.stratos.python.cli/src/main/python/{cli => }/setup.py (100%) diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/setup.cfg b/components/org.apache.stratos.python.cli/src/main/python/setup.cfg similarity index 100% rename from components/org.apache.stratos.python.cli/src/main/python/cli/setup.cfg rename to components/org.apache.stratos.python.cli/src/main/python/setup.cfg diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/setup.py b/components/org.apache.stratos.python.cli/src/main/python/setup.py similarity index 100% rename from components/org.apache.stratos.python.cli/src/main/python/cli/setup.py rename to components/org.apache.stratos.python.cli/src/main/python/setup.py From 3b9067d05a9e001a714606c4689b15b71aef5193 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sun, 3 May 2015 22:59:52 +0530 Subject: [PATCH 06/79] README added --- .../src/main/python/README.md | 75 +++++++++++++++++++ .../src/main/python/setup.py | 3 - 2 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 components/org.apache.stratos.python.cli/src/main/python/README.md diff --git a/components/org.apache.stratos.python.cli/src/main/python/README.md b/components/org.apache.stratos.python.cli/src/main/python/README.md new file mode 100644 index 0000000000..b3427906f4 --- /dev/null +++ b/components/org.apache.stratos.python.cli/src/main/python/README.md @@ -0,0 +1,75 @@ +================================================================================ + Apache Stratos CLI +================================================================================ + +Welcome to the Apache Stratos CLI. + +The command line interface (CLI) is a powerful tool that clients can use +to communicate with Stratos services. The CLI distribution only includes one +jar file containing all dependent java libraries. + +The tenant can use the CLI tool on two separate modes if needed, which +are namely single command line mode and interactive mode. + +If the user executes with a command as an argument, the CLI tool +will execute that command and exit with a status code. + +However, if there is no command as an argument, the CLI tool will +enter into an interactive mode. + +A user will be able to carryout all the following functions via the +CLI, with the exception of registering tenants and viewing logs. + +The CLI tool also supports command history and auto-completing features +in the interactive mode. + +Configuring CLI Tool +================================== +Configuration guide can be found at https://cwiki.apache.org/confluence/display/STRATOS/4.1.0+Configuring+CLI+Tool + +Help +================================== +usage: stratos [-debug] [-h] [-p ] [-trace] [-u ] + -debug Enable debug logging + -h,--help Display this help + -p,--password Password + -trace Enable trace logging + -u,--username Username + + +Available Commands: +create-tenant Add new tenant +list-autoscale-policies List available autoscale policies +subscribe-cartridge Subscribe to a cartridge +exit Exit from Stratos Client Tool +list-available-cartridges List available cartridges +help Help for commands +list-available-partitions List available partitions +deploy-cartridge Add new cartridge deployment +unsubscribe-cartridge Unsubscribe from a subscribed cartridge +list-subscribe-cartridges List subscribed cartridges with details +deploy-partition Add new partition deployment +deploy-autoscaling-policy Add new autoscaling policy deployment +deploy-deployment-policy Add new deployment policy + + +Support +================================== + +Any problem with this release can be reported to Apache Stratos mailing list +or in the JIRA issue tracker. If you are sending an email to the mailing +list make sure to add the [Apache Stratos] prefix to the subject. + +Mailing list subscription: + dev-subscribe@stratos.apache.org + + +Issue Tracker +================================== + +Jira: + https://issues.apache.org/jira/browse/stratos + + +Thank you for using Apache Stratos! +The Stratos Team diff --git a/components/org.apache.stratos.python.cli/src/main/python/setup.py b/components/org.apache.stratos.python.cli/src/main/python/setup.py index 21d34c8fc1..dcedff8f59 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/setup.py +++ b/components/org.apache.stratos.python.cli/src/main/python/setup.py @@ -2,9 +2,6 @@ from setuptools import setup # Utility function to read the README file. -# Used for the long_description. It's nice, because now 1) we have a top level -# README file and 2) it's easier to type in the README file than to put a raw -# string in below ... def read(fname): return open(os.path.join(os.path.dirname(__file__), fname)).read() From 867c9a8645c26fd82909604af87512c77f1bbb93 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sun, 3 May 2015 23:03:55 +0530 Subject: [PATCH 07/79] Main file added On branch python-cli new file: components/org.apache.stratos.python.cli/src/main/python/cli/Main.py Main file contains the main() method, the entry point of the CLI --- .../src/main/python/cli/Main.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 components/org.apache.stratos.python.cli/src/main/python/cli/Main.py diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py new file mode 100644 index 0000000000..e813078ba0 --- /dev/null +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py @@ -0,0 +1,8 @@ +def main(): + pass + + +if __name__ == '__main__': + main() + + From e8c46899aa5f0bef511b20358a6be50819cd5313 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Tue, 5 May 2015 18:09:01 +0530 Subject: [PATCH 08/79] Simple code style fixes as of the coding guides --- .../src/main/python/setup.py | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/setup.py b/components/org.apache.stratos.python.cli/src/main/python/setup.py index dcedff8f59..0f3944ce13 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/setup.py +++ b/components/org.apache.stratos.python.cli/src/main/python/setup.py @@ -1,21 +1,22 @@ import os from setuptools import setup + # Utility function to read the README file. -def read(fname): - return open(os.path.join(os.path.dirname(__file__), fname)).read() +def read(file_name): + return open(os.path.join(os.path.dirname(__file__), file_name)).read() setup( - name = "stratos", - version = "0.0.5", - author = "Agentmilindu", - author_email = "Agentmilindu@gmail.com", + name="stratos", + version="0.0.5", + author="Agentmilindu", + author_email="Agentmilindu@gmail.com", py_modules=['stratos'], - description = ("CLI for Apache Startos"), - keywords = "stratos", - url = "http://stratos.apache.org/", + description="CLI for Apache Stratos", + keywords="stratos", + url="http://stratos.apache.org/", packages=['stratos'], - install_requires=['cmd2','requests','texttable'], + install_requires=['cmd2', 'requests', 'texttable'], long_description=read('README.md'), classifiers=[ "Development Status :: 1 - Planning", From ea0987681884a9a13dbc80b1a05eb83c48e5f9d7 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Tue, 5 May 2015 18:13:08 +0530 Subject: [PATCH 09/79] CLI and Stratos scripts added On branch python-cli new file: components/org.apache.stratos.python.cli/src/main/python/cli/CLI.py new file: components/org.apache.stratos.python.cli/src/main/python/cli/Stratos.py --- .../src/main/python/cli/CLI.py | 7 +++++++ .../src/main/python/cli/Stratos.py | 10 ++++++++++ 2 files changed, 17 insertions(+) create mode 100644 components/org.apache.stratos.python.cli/src/main/python/cli/CLI.py create mode 100644 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/CLI.py b/components/org.apache.stratos.python.cli/src/main/python/cli/CLI.py new file mode 100644 index 0000000000..ff2226ad84 --- /dev/null +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/CLI.py @@ -0,0 +1,7 @@ +from cmd2 import * + + +class CLI(Cmd): + """Apache Stratos CLI""" + + prompt = 'stratos> ' 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 new file mode 100644 index 0000000000..0c128168a7 --- /dev/null +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Stratos.py @@ -0,0 +1,10 @@ +import requests + + +class Stratos: + """Apache Stratos Python API""" + + def __init__(self): + pass + + From cc0e57c5186922f45313caaa032f8f64ebf2f06f Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Wed, 6 May 2015 12:03:25 +0530 Subject: [PATCH 10/79] Initializing the CMD and started the loop in Main On branch python-cli modified: components/org.apache.stratos.python.cli/src/main/python/cli/Main.py --- .../src/main/python/cli/Main.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py index e813078ba0..ee21f469a7 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py @@ -1,8 +1,13 @@ -def main(): - pass +import sys +import CLI + +def main(): + cli = CLI() + if len(sys.argv) > 1: + cli.onecmd(' '.join(sys.argv[1:])) + else: + cli.cmdloop() if __name__ == '__main__': - main() - - + main() From 0e8c1377e9c280b7b9a62aaa77f6775f5bc3efb4 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Wed, 6 May 2015 13:34:47 +0530 Subject: [PATCH 11/79] Main and CLI linked On branch python-cli modified: cli/CLI.py modified: cli/Main.py modified: cli/Stratos.py modified: setup.py Now anyone build and install stratos CLI by, $ python setup.py build $ python setup.py install or use $ python setup.py develop instead `install` if developing --- .../org.apache.stratos.python.cli/src/main/python/cli/CLI.py | 0 .../src/main/python/cli/Main.py | 2 +- .../src/main/python/cli/Stratos.py | 0 .../org.apache.stratos.python.cli/src/main/python/setup.py | 5 ++--- 4 files changed, 3 insertions(+), 4 deletions(-) mode change 100644 => 100755 components/org.apache.stratos.python.cli/src/main/python/cli/CLI.py mode change 100644 => 100755 components/org.apache.stratos.python.cli/src/main/python/cli/Main.py mode change 100644 => 100755 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/CLI.py b/components/org.apache.stratos.python.cli/src/main/python/cli/CLI.py old mode 100644 new mode 100755 diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py old mode 100644 new mode 100755 index ee21f469a7..535837c947 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py @@ -1,5 +1,5 @@ import sys -import CLI +from CLI import CLI def main(): 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 old mode 100644 new mode 100755 diff --git a/components/org.apache.stratos.python.cli/src/main/python/setup.py b/components/org.apache.stratos.python.cli/src/main/python/setup.py index 0f3944ce13..7f472cd1fa 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/setup.py +++ b/components/org.apache.stratos.python.cli/src/main/python/setup.py @@ -11,11 +11,10 @@ def read(file_name): version="0.0.5", author="Agentmilindu", author_email="Agentmilindu@gmail.com", - py_modules=['stratos'], description="CLI for Apache Stratos", keywords="stratos", url="http://stratos.apache.org/", - packages=['stratos'], + packages=['cli'], install_requires=['cmd2', 'requests', 'texttable'], long_description=read('README.md'), classifiers=[ @@ -25,7 +24,7 @@ def read(file_name): ], entry_points=''' [console_scripts] - stratos=src.Main:main + stratos=cli.Main:main ''', ) From f67802be3adfd42574dd746dfe16465eb73c520d Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Thu, 7 May 2015 18:09:53 +0530 Subject: [PATCH 12/79] Made the README a rst file On branch python-cli renamed: README.md -> README.rst Using rst(reStructuredText) file as setup.py's description, this will become the description of stratos on Pip. Therefor adding the default Stratos CLI guide there. Python CLI specific details would reside on README.md --- .../src/main/python/{README.md => README.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename components/org.apache.stratos.python.cli/src/main/python/{README.md => README.rst} (100%) diff --git a/components/org.apache.stratos.python.cli/src/main/python/README.md b/components/org.apache.stratos.python.cli/src/main/python/README.rst similarity index 100% rename from components/org.apache.stratos.python.cli/src/main/python/README.md rename to components/org.apache.stratos.python.cli/src/main/python/README.rst From a04004568a8a937676e09635e94f5066eb84bcb4 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Thu, 7 May 2015 18:16:54 +0530 Subject: [PATCH 13/79] Git Ignore file updated On branch python-cli new file: .gitignore Ignoring build/ dist/ stratos.egg-info/ --- .../src/main/python/.gitignore | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 components/org.apache.stratos.python.cli/src/main/python/.gitignore diff --git a/components/org.apache.stratos.python.cli/src/main/python/.gitignore b/components/org.apache.stratos.python.cli/src/main/python/.gitignore new file mode 100644 index 0000000000..989ef909b4 --- /dev/null +++ b/components/org.apache.stratos.python.cli/src/main/python/.gitignore @@ -0,0 +1,20 @@ +# 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. + +build/ +dist/ +stratos.egg-info/ From 4f5740772cf97daa7dbb0e10fce3a3b854e9795c Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Thu, 7 May 2015 18:18:14 +0530 Subject: [PATCH 14/79] new README.md added On branch python-cli new file: README.md Python CLI specific README --- .../src/main/python/README.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 components/org.apache.stratos.python.cli/src/main/python/README.md diff --git a/components/org.apache.stratos.python.cli/src/main/python/README.md b/components/org.apache.stratos.python.cli/src/main/python/README.md new file mode 100644 index 0000000000..c12d1950f4 --- /dev/null +++ b/components/org.apache.stratos.python.cli/src/main/python/README.md @@ -0,0 +1,20 @@ + Apache Stratos CLI + ================== + +Build and install stratos CLI by, + + $ python setup.py build + $ python setup.py install + +or use + + $ python setup.py develop + +instead `install` if developing + +To start the Python CLI issue, + + $ stratos + + + From eeb5e1fb9547e7dffca295661e077886df053278 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Thu, 7 May 2015 18:24:05 +0530 Subject: [PATCH 15/79] Setup.py now use README.rst as description --- .../org.apache.stratos.python.cli/src/main/python/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/setup.py b/components/org.apache.stratos.python.cli/src/main/python/setup.py index 7f472cd1fa..ad3ccb91a0 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/setup.py +++ b/components/org.apache.stratos.python.cli/src/main/python/setup.py @@ -16,7 +16,7 @@ def read(file_name): url="http://stratos.apache.org/", packages=['cli'], install_requires=['cmd2', 'requests', 'texttable'], - long_description=read('README.md'), + long_description=read('README.rst'), classifiers=[ "Development Status :: 1 - Planning", "Topic :: Utilities", From a8b65235cb4ce80d0b312e8e28b2d6a63ce50042 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Thu, 14 May 2015 00:05:01 +0530 Subject: [PATCH 16/79] Added Apache details to the Setup.py On branch python-cli modified: setup.py --- .../org.apache.stratos.python.cli/src/main/python/setup.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/setup.py b/components/org.apache.stratos.python.cli/src/main/python/setup.py index ad3ccb91a0..444f8b7cb3 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/setup.py +++ b/components/org.apache.stratos.python.cli/src/main/python/setup.py @@ -9,9 +9,9 @@ def read(file_name): setup( name="stratos", version="0.0.5", - author="Agentmilindu", - author_email="Agentmilindu@gmail.com", - description="CLI for Apache Stratos", + author="Apache Stratos", + author_email="dev@stratos.apache.org", + description="CLI tool to interact with Apache Stratos", keywords="stratos", url="http://stratos.apache.org/", packages=['cli'], From 979c0ef7f68a2ab25f466b30f1dd979db04f38e0 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Thu, 14 May 2015 09:02:01 +0530 Subject: [PATCH 17/79] Utilities to print tables, trees added On branch python-cli new file: cli/Utils.py --- .../src/main/python/cli/Utils.py | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py new file mode 100644 index 0000000000..350b1eb4cc --- /dev/null +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py @@ -0,0 +1,49 @@ +from texttable import * + + +class PrintableTree: + + def __init__(self, tree_data): + self.tree_data = tree_data + pass + + def print_tree(self): + def _print(t, level=0, ups=""): + if isinstance(t, list): + print('|') + for element in t[:-1]: + print(ups + "+-") + _print(element, level + 1, ups + "| ") + else: + print(ups + "+-") + _print(t[-1], level + 1, ups + " ") + elif isinstance(t, dict): + print('|') + l = [] + for k, v in t.items(): + if isinstance(v, list) or isinstance(v, dict): + l.extend([k, v]) + else: + l.extend([k + " : " + v]) + t = l + for element in t[:-1]: + print(ups + "+-") + _print(element, level + 1, ups + "| ") + else: + print(ups + "+-") + _print(t[-1], level + 1, ups + " ") + else: + print(t) + + _print(self.tree_data) + + +class PrintableTable(Texttable): + + def __init__(self): + Texttable.__init__(self) + self.set_deco(Texttable.BORDER | Texttable.HEADER | Texttable.VLINES) + + def print_table(self): + print self.draw() + From c3dbb1756160fa3a268dc3f1d04aa36e1f8f0a18 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Thu, 14 May 2015 09:03:21 +0530 Subject: [PATCH 18/79] Seperate py to handle Logging related stuffs On branch python-cli new file: cli/Logging.py --- .../src/main/python/cli/Logging.py | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py new file mode 100644 index 0000000000..17090b0871 --- /dev/null +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py @@ -0,0 +1,4 @@ +import logging + + +logging.basicConfig(filename="~/debug.log", level=logging.DEBUG) From 05e3a721808f2c9bc11e944a2574365d2fe60ec9 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sat, 16 May 2015 11:29:42 +0530 Subject: [PATCH 19/79] Creating stratos folder for logging( if not exists ), logging in to stratos-cli.log --- .../src/main/python/cli/Logging.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py index 17090b0871..20583ade79 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py @@ -1,4 +1,16 @@ import logging +import os +stratos_dir = os.path.expanduser('~/.stratos') +log_file_path = stratos_dir+"stratos-cli.log" -logging.basicConfig(filename="~/debug.log", level=logging.DEBUG) + +if not os.path.exists(stratos_dir): + try: + os.makedirs(stratos_dir) + logging.info("Created directory: "+stratos_dir) + except OSError: + logging.warning("Failed to create directory: "+stratos_dir) + + +logging.basicConfig(filename=log_file_path, level=logging.DEBUG) From 999500c897b677ce1d683ab828047e2ebb0e01f0 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sat, 16 May 2015 11:31:28 +0530 Subject: [PATCH 20/79] Logging added to the Main py --- .../src/main/python/cli/CLI.py | 28 +++++++++++++++++++ .../src/main/python/cli/Main.py | 2 ++ .../src/main/python/setup.py | 2 +- 3 files changed, 31 insertions(+), 1 deletion(-) 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 ff2226ad84..4060e2c98e 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 @@ -1,7 +1,35 @@ from cmd2 import * +from Utils import * +from Stratos import * class CLI(Cmd): """Apache Stratos CLI""" prompt = 'stratos> ' + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user") + ]) + def do_repositories(self, line, opts=None): + """ Shows the git repositories of the user identified by given the username and password + eg: repositories -u agentmilindu -p agentmilindu123 """ + + if opts.username and opts.password: + r = requests.get('https://api.github.com/users/' + opts.username + '/repos?per_page=5', + auth=(opts.username, opts.password)) + repositories = r.json() + table = PrintableTable() + rows = [["Name", "language"]] + table.set_cols_align(["l", "r"]) + table.set_cols_valign(["t", "m"]) + + for repo in repositories: + rows.append([repo['name'], repo['language']]) + print(rows) + table.add_rows(rows) + table.print_table() + + else: + print("Some required argument(s) missing") \ No newline at end of file diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py index 535837c947..dc4f29175b 100755 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py @@ -1,9 +1,11 @@ import sys from CLI import CLI +import Logging def main(): cli = CLI() + if len(sys.argv) > 1: cli.onecmd(' '.join(sys.argv[1:])) else: diff --git a/components/org.apache.stratos.python.cli/src/main/python/setup.py b/components/org.apache.stratos.python.cli/src/main/python/setup.py index 444f8b7cb3..1c390e2494 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/setup.py +++ b/components/org.apache.stratos.python.cli/src/main/python/setup.py @@ -24,7 +24,7 @@ def read(file_name): ], entry_points=''' [console_scripts] - stratos=cli.Main:main + stratos-cli=cli.Main:main ''', ) From d1ab83c5df4a83217d62f83a7e67d66880bff7c3 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Fri, 22 May 2015 17:16:11 +0530 Subject: [PATCH 21/79] Configs.py added, Using the constants in Configs.py On branch python-cli modified: cli/CLI.py new file: cli/Configs.py modified: cli/Logging.py --- .../src/main/python/cli/CLI.py | 12 ++++++++++-- .../src/main/python/cli/Configs.py | 10 ++++++++++ .../src/main/python/cli/Logging.py | 13 +++++++------ 3 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 components/org.apache.stratos.python.cli/src/main/python/cli/Configs.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 4060e2c98e..eccd2ab89f 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 @@ -1,12 +1,16 @@ from cmd2 import * from Utils import * from Stratos import * +import Configs class CLI(Cmd): """Apache Stratos CLI""" - prompt = 'stratos> ' + prompt = Configs.stratos_prompt + # resolving the '-' issue + Cmd.legalChars += '-' + Cmd.shortcuts.update({'deploy-user': 'deploy_user'}) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -32,4 +36,8 @@ def do_repositories(self, line, opts=None): table.print_table() else: - print("Some required argument(s) missing") \ No newline at end of file + print("Some required argument(s) missing") + + def do_deploy_user(self, line , opts=None): + """Illustrate the base class method use.""" + print("hello User") \ No newline at end of file diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py new file mode 100644 index 0000000000..e98dcb6b33 --- /dev/null +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py @@ -0,0 +1,10 @@ +import os + +stratos_prompt = "stratos> " + +stratos_dir = "~/.stratos" +log_file_name = "stratos-cli.log" + +Stratos_url = os.getenv('STRATOS_URL', None) +Stratos_username = os.getenv('STRATOS_USERNAME', None) +Stratos_password = os.getenv('STRATOS_PASSWORD', None) \ No newline at end of file diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py index 20583ade79..f149913586 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py @@ -1,16 +1,17 @@ import logging import os +import Configs -stratos_dir = os.path.expanduser('~/.stratos') -log_file_path = stratos_dir+"stratos-cli.log" +stratos_dir_path = os.path.expanduser(Configs.stratos_dir) +log_file_path = stratos_dir_path+Configs.log_file_name -if not os.path.exists(stratos_dir): +if not os.path.exists(stratos_dir_path): try: - os.makedirs(stratos_dir) - logging.info("Created directory: "+stratos_dir) + os.makedirs(stratos_dir_path) + logging.info("Created directory: "+stratos_dir_path) except OSError: - logging.warning("Failed to create directory: "+stratos_dir) + logging.warning("Failed to create directory: "+stratos_dir_path) logging.basicConfig(filename=log_file_path, level=logging.DEBUG) From 4502eb63b881ed9f65d9dd3dbac29c34f80c6b4a Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Fri, 22 May 2015 17:39:16 +0530 Subject: [PATCH 22/79] Resolving the '-' issue --- .../src/main/python/README.md | 17 +++++++++++------ .../src/main/python/cli/CLI.py | 4 ++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/README.md b/components/org.apache.stratos.python.cli/src/main/python/README.md index c12d1950f4..8b734d1091 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/README.md +++ b/components/org.apache.stratos.python.cli/src/main/python/README.md @@ -1,20 +1,25 @@ - Apache Stratos CLI - ================== +Apache Stratos CLI +================== Build and install stratos CLI by, - $ python setup.py build - $ python setup.py install +``` +$ python setup.py build +$ python setup.py install +``` or use - +``` $ python setup.py develop +``` instead `install` if developing To start the Python CLI issue, - $ stratos +``` +$ stratos +``` 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 eccd2ab89f..7ba7dcd7d8 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 @@ -12,6 +12,10 @@ class CLI(Cmd): Cmd.legalChars += '-' Cmd.shortcuts.update({'deploy-user': 'deploy_user'}) + def completenames(self, text, *ignored): + 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") From ef36b7a0aa683fb3f91cf909244d90e39c432235 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sat, 13 Jun 2015 17:11:58 +0530 Subject: [PATCH 23/79] Log file location moved to COnfigs.py from Logger.py On branch python-cli modified: components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py modified: components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py --- .../src/main/python/cli/Configs.py | 3 +++ .../src/main/python/cli/Logging.py | 14 +++++--------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py index e98dcb6b33..f85b4ccccc 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py @@ -5,6 +5,9 @@ stratos_dir = "~/.stratos" log_file_name = "stratos-cli.log" +stratos_dir_path = os.path.expanduser(stratos_dir) +log_file_path = stratos_dir_path+log_file_name + Stratos_url = os.getenv('STRATOS_URL', None) Stratos_username = os.getenv('STRATOS_USERNAME', None) Stratos_password = os.getenv('STRATOS_PASSWORD', None) \ No newline at end of file diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py index f149913586..b843dfe258 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py @@ -2,16 +2,12 @@ import os import Configs -stratos_dir_path = os.path.expanduser(Configs.stratos_dir) -log_file_path = stratos_dir_path+Configs.log_file_name - -if not os.path.exists(stratos_dir_path): +if not os.path.exists(Configs.stratos_dir_path): try: - os.makedirs(stratos_dir_path) - logging.info("Created directory: "+stratos_dir_path) + os.makedirs(Configs.stratos_dir_path) + logging.info("Created directory: "+Configs.stratos_dir_path) except OSError: - logging.warning("Failed to create directory: "+stratos_dir_path) - + logging.warning("Failed to create directory: "+Configs.stratos_dir_path) -logging.basicConfig(filename=log_file_path, level=logging.DEBUG) +logging.basicConfig(filename=Configs.log_file_path, level=logging.DEBUG) From 72dd3a4b8753501b63241287dca07065e1742298 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sun, 14 Jun 2015 13:15:33 +0530 Subject: [PATCH 24/79] Some quick fixes Using python os.path.join() to avoid forward/back slash problems Setting empty strings as defaults for username and password evn vars instead of None --- .../src/main/python/cli/Configs.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py index f85b4ccccc..a665474737 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py @@ -6,8 +6,8 @@ log_file_name = "stratos-cli.log" stratos_dir_path = os.path.expanduser(stratos_dir) -log_file_path = stratos_dir_path+log_file_name +log_file_path = os.path.join(stratos_dir_path, log_file_name) -Stratos_url = os.getenv('STRATOS_URL', None) -Stratos_username = os.getenv('STRATOS_USERNAME', None) -Stratos_password = os.getenv('STRATOS_PASSWORD', None) \ No newline at end of file +stratos_url = os.getenv('STRATOS_URL', None) +stratos_username = os.getenv('STRATOS_USERNAME', "") +stratos_password = os.getenv('STRATOS_PASSWORD', "") \ No newline at end of file From 28a72d39bf321cbfc6baf524b91930449f504cbd Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sun, 14 Jun 2015 15:11:25 +0530 Subject: [PATCH 25/79] Pre Authentication decorator added --- .../src/main/python/cli/Utils.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py index 350b1eb4cc..2b67d8bd94 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py @@ -1,4 +1,6 @@ from texttable import * +import Configs +from Logging import logging class PrintableTree: @@ -47,3 +49,19 @@ def __init__(self): def print_table(self): print self.draw() +def auth(func): + """Authenticate""" + def auth_inner(self, *args, **kwargs): + + if len(args) > 1 and args[1].username is not None: + Configs.stratos_username = args[1].username + if len(args) > 1 and args[1].password is not None: + Configs.stratos_password = args[1].password + + if Configs.stratos_username is "" and Configs.stratos_password is "": + print("Pre authentication failed. Some authentication details are missing") + logging.warning("Pre authentication failed. Some authentication details are missing") + else: + return func(self, *args, **kwargs) + return auth_inner + From f9f246f6bf61a9a767cee7cb9e314052b3bad91c Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Thu, 18 Jun 2015 07:10:06 +0530 Subject: [PATCH 26/79] Default stratos URL added --- .../src/main/python/cli/Configs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py index a665474737..4d31b7420f 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py @@ -8,6 +8,6 @@ stratos_dir_path = os.path.expanduser(stratos_dir) log_file_path = os.path.join(stratos_dir_path, log_file_name) -stratos_url = os.getenv('STRATOS_URL', None) +stratos_url = os.getenv('STRATOS_URL', "https://localhost:9443/") stratos_username = os.getenv('STRATOS_USERNAME', "") stratos_password = os.getenv('STRATOS_PASSWORD', "") \ No newline at end of file From bf053e092e23f1d76bdc980048f78c4036ce2982 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Thu, 18 Jun 2015 22:07:56 +0530 Subject: [PATCH 27/79] Auth and Print tree improved --- .../src/main/python/cli/Utils.py | 34 ++++++++++--------- .../main/python/cli/exceptions/__init__.py | 1 + 2 files changed, 19 insertions(+), 16 deletions(-) create mode 100644 components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/__init__.py diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py index 2b67d8bd94..ade30f46b3 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py @@ -1,3 +1,5 @@ +from __future__ import print_function +import sys from texttable import * import Configs from Logging import logging @@ -10,15 +12,15 @@ def __init__(self, tree_data): pass def print_tree(self): - def _print(t, level=0, ups=""): + def _print_tree(t, level=0, ups=""): if isinstance(t, list): print('|') for element in t[:-1]: - print(ups + "+-") - _print(element, level + 1, ups + "| ") + print(ups + "+-", end='') + _print_tree(element, level + 1, ups + "| ") else: - print(ups + "+-") - _print(t[-1], level + 1, ups + " ") + print(ups + "+-", end='') + _print_tree(t[-1], level + 1, ups + " ") elif isinstance(t, dict): print('|') l = [] @@ -26,18 +28,18 @@ def _print(t, level=0, ups=""): if isinstance(v, list) or isinstance(v, dict): l.extend([k, v]) else: - l.extend([k + " : " + v]) + l.extend([str(k) + " : " + str(v)]) t = l for element in t[:-1]: - print(ups + "+-") - _print(element, level + 1, ups + "| ") + print(ups + "+-", end='') + _print_tree(element, level + 1, ups + "| ") else: - print(ups + "+-") - _print(t[-1], level + 1, ups + " ") + print(ups + "+-", end='') + _print_tree(t[-1], level + 1, ups + " ") else: - print(t) - - _print(self.tree_data) + print(str(t)) + print("_") + _print_tree(self.tree_data) class PrintableTable(Texttable): @@ -47,15 +49,15 @@ def __init__(self): self.set_deco(Texttable.BORDER | Texttable.HEADER | Texttable.VLINES) def print_table(self): - print self.draw() + print(self.draw()) def auth(func): """Authenticate""" def auth_inner(self, *args, **kwargs): - if len(args) > 1 and args[1].username is not None: + if len(args) > 1 and hasattr(args[1], 'username') and args[1].username is not None: Configs.stratos_username = args[1].username - if len(args) > 1 and args[1].password is not None: + if len(args) > 1 and hasattr(args[1], 'password') and args[1].password is not None: Configs.stratos_password = args[1].password if Configs.stratos_username is "" and Configs.stratos_password is "": diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/__init__.py b/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/__init__.py new file mode 100644 index 0000000000..f80b5be600 --- /dev/null +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/__init__.py @@ -0,0 +1 @@ +__author__ = 'agentmilindu' From 347010278fa50ec90cdacc6ab830be6fe58cfc70 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Thu, 18 Jun 2015 22:09:09 +0530 Subject: [PATCH 28/79] Prompting for user credentials when starting the interactive mode --- .../src/main/python/cli/Main.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py index dc4f29175b..f5ea323496 100755 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py @@ -1,6 +1,24 @@ import sys +import getpass from CLI import CLI -import Logging +import Configs + +# Fix Python 2.x. +try: + input = raw_input +except NameError: + pass + + +def prompt_for_credentials(): + """Prompt for user credentials""" + while True: + _username = input("Username: ") + _password = getpass.getpass("Password: ") + if _username is not "" and _password is not "": + Configs.stratos_username = _username + Configs.stratos_password = _password + break def main(): @@ -9,6 +27,7 @@ def main(): if len(sys.argv) > 1: cli.onecmd(' '.join(sys.argv[1:])) else: + prompt_for_credentials() cli.cmdloop() if __name__ == '__main__': From 23d9785dd95bb38c96f76647b7283069cef169b6 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Thu, 18 Jun 2015 22:11:09 +0530 Subject: [PATCH 29/79] Stratos API URL added --- .../org.apache.stratos.python.cli/src/main/python/cli/Configs.py | 1 + 1 file changed, 1 insertion(+) diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py index 4d31b7420f..07a3380a6c 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py @@ -9,5 +9,6 @@ log_file_path = os.path.join(stratos_dir_path, log_file_name) stratos_url = os.getenv('STRATOS_URL', "https://localhost:9443/") +stratos_api_url = stratos_url + "api/" stratos_username = os.getenv('STRATOS_USERNAME', "") stratos_password = os.getenv('STRATOS_PASSWORD', "") \ No newline at end of file From e66a7ee147467e7f9f286c0447f718f3baaf03ff Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sat, 25 Jul 2015 12:29:13 +0530 Subject: [PATCH 30/79] Passowrd and username checking now happens in @auth decorator. do_list_users and do_network_partitions added --- .../src/main/python/cli/CLI.py | 74 ++++++++++++++----- .../src/main/python/tox.ini | 0 2 files changed, 54 insertions(+), 20 deletions(-) create mode 100644 components/org.apache.stratos.python.cli/src/main/python/tox.ini 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 7ba7dcd7d8..554ba49532 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 @@ -10,37 +10,71 @@ class CLI(Cmd): prompt = Configs.stratos_prompt # resolving the '-' issue Cmd.legalChars += '-' - Cmd.shortcuts.update({'deploy-user': 'deploy_user'}) + Cmd.shortcuts.update({'list-user': 'list_user'}) def completenames(self, text, *ignored): - return [a[3:].replace('_','-') for a in self.get_names() if a.replace('_','-').startswith('do-'+text)] - + 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") ]) + @auth def do_repositories(self, line, opts=None): """ Shows the git repositories of the user identified by given the username and password eg: repositories -u agentmilindu -p agentmilindu123 """ - if opts.username and opts.password: - r = requests.get('https://api.github.com/users/' + opts.username + '/repos?per_page=5', - auth=(opts.username, opts.password)) - repositories = r.json() - table = PrintableTable() - rows = [["Name", "language"]] - table.set_cols_align(["l", "r"]) - table.set_cols_valign(["t", "m"]) - - for repo in repositories: - rows.append([repo['name'], repo['language']]) - print(rows) - table.add_rows(rows) - table.print_table() - - else: - print("Some required argument(s) missing") + r = requests.get('https://api.github.com/users/' + Configs.stratos_username + '/repos?per_page=5', + auth=(Configs.stratos_username, Configs.stratos_password)) + repositories = r.json() + print(r) + print(repositories) + table = PrintableTable() + rows = [["Name", "language"]] + table.set_cols_align(["l", "r"]) + table.set_cols_valign(["t", "m"]) + + for repo in repositories: + rows.append([repo['name'], repo['language']]) + print(rows) + 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_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() + 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() + + @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_networkpartitions(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() def do_deploy_user(self, line , opts=None): """Illustrate the base class method use.""" diff --git a/components/org.apache.stratos.python.cli/src/main/python/tox.ini b/components/org.apache.stratos.python.cli/src/main/python/tox.ini new file mode 100644 index 0000000000..e69de29bb2 From 50e85cb66f0d79079d48b69befca7404ea54dc0e Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sat, 25 Jul 2015 12:31:14 +0530 Subject: [PATCH 31/79] added list_users to Straos --- .../src/main/python/cli/Stratos.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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 0c128168a7..360353dbd7 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,4 +1,5 @@ import requests +import Configs class Stratos: @@ -7,4 +8,19 @@ class Stratos: def __init__(self): pass + @staticmethod + def list_users(): + r = requests.get(Configs.stratos_url + 'users', + auth=(Configs.stratos_username, Configs.stratos_password), verify=False) + if r.status_code is 200: + return r.json() + + elif r.status_code is 400: + raise requests.HTTPError() + + @staticmethod + def network_partitions(): + pass + + From 3657053fb4beefadd7beb66ad2d86ef18e123e89 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sat, 25 Jul 2015 12:31:38 +0530 Subject: [PATCH 32/79] Loggin added --- .../org.apache.stratos.python.cli/src/main/python/cli/Logging.py | 1 - 1 file changed, 1 deletion(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py index b843dfe258..3451bca2d6 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py @@ -2,7 +2,6 @@ import os import Configs - if not os.path.exists(Configs.stratos_dir_path): try: os.makedirs(Configs.stratos_dir_path) From 4a2b52f115423f8c3a99a73deee1fe10c00685f4 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sat, 25 Jul 2015 12:32:18 +0530 Subject: [PATCH 33/79] Print tree method's print style fixed a little --- .../org.apache.stratos.python.cli/src/main/python/cli/Utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py index ade30f46b3..aa4b8de33a 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py @@ -28,7 +28,7 @@ def _print_tree(t, level=0, ups=""): if isinstance(v, list) or isinstance(v, dict): l.extend([k, v]) else: - l.extend([str(k) + " : " + str(v)]) + l.extend([str(k) + ":" + str(v)]) t = l for element in t[:-1]: print(ups + "+-", end='') From 4578f233f5cca35f4a0ca77e356080adc89f115f Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sat, 25 Jul 2015 12:32:50 +0530 Subject: [PATCH 34/79] Tox ini added for Python testing on different version --- .../org.apache.stratos.python.cli/src/main/python/tox.ini | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/components/org.apache.stratos.python.cli/src/main/python/tox.ini b/components/org.apache.stratos.python.cli/src/main/python/tox.ini index e69de29bb2..827566e7ff 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/tox.ini +++ b/components/org.apache.stratos.python.cli/src/main/python/tox.ini @@ -0,0 +1,5 @@ +[tox] +envlist = py27, py34 +[testenv] +deps=pytest +commands=py.test \ No newline at end of file From 6a17cc26b97d76f4c2330b66f6df1ff5a5603f0b Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sat, 25 Jul 2015 12:34:40 +0530 Subject: [PATCH 35/79] Ignored .tox dir --- .../org.apache.stratos.python.cli/src/main/python/.gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/components/org.apache.stratos.python.cli/src/main/python/.gitignore b/components/org.apache.stratos.python.cli/src/main/python/.gitignore index 989ef909b4..3d24c23997 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/.gitignore +++ b/components/org.apache.stratos.python.cli/src/main/python/.gitignore @@ -18,3 +18,4 @@ build/ dist/ stratos.egg-info/ +.tox/ From 9cbc273a84644cb04733fb9af43d562a142260d7 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sat, 25 Jul 2015 12:36:29 +0530 Subject: [PATCH 36/79] Added testing files --- .../src/main/python/tests/test_print.py | 16 ++++++++++++++ .../python/tests/test_printable_tree_print.py | 21 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 components/org.apache.stratos.python.cli/src/main/python/tests/test_print.py create mode 100644 components/org.apache.stratos.python.cli/src/main/python/tests/test_printable_tree_print.py diff --git a/components/org.apache.stratos.python.cli/src/main/python/tests/test_print.py b/components/org.apache.stratos.python.cli/src/main/python/tests/test_print.py new file mode 100644 index 0000000000..72a1d705fb --- /dev/null +++ b/components/org.apache.stratos.python.cli/src/main/python/tests/test_print.py @@ -0,0 +1,16 @@ +import sys +import unittest +from cli import Utils + + +class MyTestCase(unittest.TestCase): + def test_something(self): + tree = Utils.PrintableTree( + """[{"id":"network-partition-2","partitions":[{"id":"partition-2","partitionMax":0,"property":[{"name":"region","value":"default"}],"public":false},{"id":"partition-3","partitionMax":0,"property":[{"name":"region","value":"default"}],"public":false}]},{"id":"network-partition-1","partitions":[{"id":"partition-1","partitionMax":0,"property":[{"name":"region","value":"default"}],"public":false}]}]""") + tree.print_tree() + output = "hello world!" + self.assertEqual(output, 'hello world!') + + +if __name__ == '__main__': + unittest.main() diff --git a/components/org.apache.stratos.python.cli/src/main/python/tests/test_printable_tree_print.py b/components/org.apache.stratos.python.cli/src/main/python/tests/test_printable_tree_print.py new file mode 100644 index 0000000000..33ef5ceae7 --- /dev/null +++ b/components/org.apache.stratos.python.cli/src/main/python/tests/test_printable_tree_print.py @@ -0,0 +1,21 @@ +import sys +from cli import Utils + + +class TestClass: + + def __init__(self): + pass + + @staticmethod + def test_zero(): + i = 1 + assert i == 1 + + @staticmethod + def test_one(): + tree = Utils.PrintableTree( + """[{"id":"network-partition-2","partitions":[{"id":"partition-2","partitionMax":0,"property":[{"name":"region","value":"default"}],"public":false},{"id":"partition-3","partitionMax":0,"property":[{"name":"region","value":"default"}],"public":false}]},{"id":"network-partition-1","partitions":[{"id":"partition-1","partitionMax":0,"property":[{"name":"region","value":"default"}],"public":false}]}]""") + tree.print_tree() + output = sys.stdout.getline().strip() # because stdout is an StringIO instance + assert output == 'hello world!' From 9a731a36b132f13013831cfb9555f63efe306ff1 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Wed, 29 Jul 2015 09:56:38 +0530 Subject: [PATCH 37/79] New Authentication Error --- .../src/main/python/cli/exceptions/AuthenticationError.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py b/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py new file mode 100644 index 0000000000..d0e10892fb --- /dev/null +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py @@ -0,0 +1,6 @@ +class AuthenticationError(Exception): + def __init__(self): + pass + + def __str__(self): + return "Authentication error" \ No newline at end of file From 936b2044459a370df719a066fa07afe3b00b34bb Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Wed, 29 Jul 2015 09:57:26 +0530 Subject: [PATCH 38/79] New CLI actions added --- .../src/main/python/cli/CLI.py | 96 +++++++++++++++---- .../src/main/python/cli/Stratos.py | 58 ++++++++++- 2 files changed, 130 insertions(+), 24 deletions(-) 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 554ba49532..deaf90c31c 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 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 @@ def do_repositories(self, line, opts=None): @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 @@ def do_list_users(self, line , opts=None): 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 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 360353dbd7..494bc21f55 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 @@ def __init__(self): @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 From 406d6e25120ad0dfd4e6ef55e73891747b1fc479 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Wed, 29 Jul 2015 09:58:37 +0530 Subject: [PATCH 39/79] Resolving the '-' issue --- .../org.apache.stratos.python.cli/src/main/python/cli/Main.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py index f5ea323496..d6ef2d5391 100755 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py @@ -1,4 +1,5 @@ import sys +import readline import getpass from CLI import CLI import Configs @@ -22,6 +23,9 @@ def prompt_for_credentials(): def main(): + # resolving the '-' issue + readline.set_completer_delims(readline.get_completer_delims().replace('-', '')) + cli = CLI() if len(sys.argv) > 1: From 4b86bb5f8bb1e7a060c8db70354c1226cda68450 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Thu, 30 Jul 2015 21:39:24 +0530 Subject: [PATCH 40/79] Added set of listing actions --- .../src/main/python/cli/CLI.py | 61 +++++++++++++++++-- .../src/main/python/cli/Stratos.py | 44 +++++++++++++ 2 files changed, 100 insertions(+), 5 deletions(-) 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 deaf90c31c..fd33d55952 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 @@ -48,7 +48,32 @@ def do_repositories(self, line, opts=None): table.add_rows(rows) table.print_table() + """ + # User Entity + """ + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-r', '--role_name', type="str", help="Role name of the user"), + make_option('-f', '--first_name', type="str", help="First name of the user"), + make_option('-l', '--last_name', type="str", help="Last name of the user"), + make_option('-e', '--email', type="str", help="Email of the user"), + make_option('-x', '--profile_name', type="str", help="Profile name of the user") + ]) + @auth + def do_add_user(self, line , opts=None): + """Add a new user to the system""" + try: + user = Stratos.list_users(opts.username, opts.password, opts.role_name, opts.first_name, opts.last_name, + opts.email, opts.profile_name) + if user: + print("User successfully created") + else: + print("Error creating the user") + except AuthenticationError as e: + self.perror("Authentication Error") @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -58,7 +83,7 @@ def do_repositories(self, line, opts=None): def do_list_users(self, line , opts=None): """Illustrate the base class method use.""" try: - users = Stratos.list_users() + users = Stratos.add_user() table = PrintableTable() rows = [["Name", "language"]] table.set_cols_align(["l", "r"]) @@ -68,7 +93,8 @@ def do_list_users(self, line , opts=None): table.add_rows(rows) table.print_table() except AuthenticationError as e: - self.perror("sdc") + self.perror("Authentication Error") + @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -77,9 +103,13 @@ def do_list_users(self, line , opts=None): @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() + network_partitions = Stratos.list_network_partitions() + table = PrintableTable() + rows = [["Network Partition ID", "Number of Partitions"]] + for network_partition in network_partitions: + rows.append([network_partition['id'], len(network_partition['partitions'])]) + table.add_rows(rows) + table.print_table() @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -128,6 +158,27 @@ def do_list_applications(self, line , opts=None): rows.append([application['type'], application['category'], application['displayName'], application['description'], application['version'], application['multiTenant']]) table.add_rows(rows) table.print_table() + """ + # Kubernetes Cluster/Host + + """ + @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_kubernetes_clusters(self, line , opts=None): + """Retrieve detailed information on all Kubernetes-CoreOS Clusters.""" + kubernetes_clusters = Stratos.list_kubernetes_clusters() + if not kubernetes_clusters: + print("No Kubernetes-CoreOS clusters found") + else: + table = PrintableTable() + rows = [["Group ID", "Description"]] + for kubernetes_cluster in kubernetes_clusters: + rows.append([kubernetes_cluster['clusterId'], kubernetes_cluster['description']]) + table.add_rows(rows) + table.print_table() @options([]) def do_deploy_user(self, line , opts=None): 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 494bc21f55..239e806d3e 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 @@ -10,6 +10,10 @@ class Stratos: def __init__(self): pass + """ + # User Entity + + """ @staticmethod def list_users(): r = requests.get(Configs.stratos_api_url + 'users', @@ -22,6 +26,12 @@ def list_users(): elif r.status_code == 401: raise AuthenticationError() + @staticmethod + def add_users(username, password, role_name, first_name, last_name, email, profile_name): + data = [username] + r = requests.post(Configs.stratos_api_url + 'users', data, + auth=(Configs.stratos_username, Configs.stratos_password), verify=False) + @staticmethod def list_network_partitions(): r = requests.get(Configs.stratos_api_url + 'networkPartitions', @@ -67,8 +77,42 @@ def list_cartridges_groups(): else: raise requests.HTTPError() + @staticmethod + def list_kubernetes_clusters(): + r = requests.get(Configs.stratos_api_url + 'kubernetesClusters', + 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 + @staticmethod + def get(resource, errorMessage): + r = requests.get(Configs.stratos_api_url + resource, + 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'] == errorMessage: + return [] + else: + raise requests.HTTPError() + + + From 18ed1932a7d60d1664a30108f5d804ab1f4c3be3 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Thu, 30 Jul 2015 21:53:30 +0530 Subject: [PATCH 41/79] Using generalized get method --- .../src/main/python/cli/CLI.py | 2 +- .../src/main/python/cli/Stratos.py | 53 +++++++++---------- 2 files changed, 25 insertions(+), 30 deletions(-) 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 fd33d55952..821e47fb26 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 @@ -83,7 +83,7 @@ def do_add_user(self, line , opts=None): def do_list_users(self, line , opts=None): """Illustrate the base class method use.""" try: - users = Stratos.add_user() + users = Stratos.list_users() table = PrintableTable() rows = [["Name", "language"]] table.set_cols_align(["l", "r"]) 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 239e806d3e..a063f1c4e1 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 @@ -11,20 +11,12 @@ def __init__(self): pass """ - # User Entity + # Users """ @staticmethod def list_users(): - r = requests.get(Configs.stratos_api_url + 'users', - 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() + return Stratos.get('users', errorMessage='No applications found') @staticmethod def add_users(username, password, role_name, first_name, last_name, email, profile_name): @@ -32,12 +24,18 @@ def add_users(username, password, role_name, first_name, last_name, email, profi r = requests.post(Configs.stratos_api_url + 'users', data, auth=(Configs.stratos_username, Configs.stratos_password), verify=False) + """ + # Network Partitions + + """ @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() + return Stratos.get('networkPartitions', errorMessage='No network partitions found') + + """ + # Applications + """ @staticmethod def list_applications(): r = requests.get(Configs.stratos_api_url + 'applications', @@ -54,7 +52,10 @@ def list_applications(): else: raise requests.HTTPError() + """ + # Cartridges + """ @staticmethod def list_cartridges(): r = requests.get(Configs.stratos_api_url + 'cartridges', @@ -77,30 +78,24 @@ def list_cartridges_groups(): else: raise requests.HTTPError() + """ + # Kubernetes Clusters + + """ @staticmethod def list_kubernetes_clusters(): - r = requests.get(Configs.stratos_api_url + 'kubernetesClusters', - 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() + return Stratos.get('kubernetesClusters', errorMessage='Kubernetes cluster not found') - @staticmethod - def deploy_user(): - raise ValueError + """ + # Utils + + """ @staticmethod def get(resource, errorMessage): r = requests.get(Configs.stratos_api_url + resource, auth=(Configs.stratos_username, Configs.stratos_password), verify=False) + print(r.text) if r.status_code == 200: return r.json() elif r.status_code == 400: From 49fd1f8b6fd86d3cf1b421ef140421e7152c8c0a Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sat, 1 Aug 2015 22:33:49 +0530 Subject: [PATCH 42/79] Added Apache licence header --- .../src/main/python/cli/CLI.py | 17 +++++++++++++++++ .../src/main/python/cli/Configs.py | 17 +++++++++++++++++ .../src/main/python/cli/Logging.py | 17 +++++++++++++++++ .../src/main/python/cli/Main.py | 17 +++++++++++++++++ .../src/main/python/cli/Stratos.py | 17 +++++++++++++++++ .../src/main/python/cli/Utils.py | 17 +++++++++++++++++ .../src/main/python/cli/__init__.py | 17 +++++++++++++++++ .../cli/exceptions/AuthenticationError.py | 17 +++++++++++++++++ .../src/main/python/cli/exceptions/__init__.py | 18 +++++++++++++++++- .../src/main/python/setup.py | 17 +++++++++++++++++ .../src/main/python/tests/__init__.py | 17 +++++++++++++++++ .../src/main/python/tests/test_print.py | 17 +++++++++++++++++ .../python/tests/test_printable_tree_print.py | 17 +++++++++++++++++ 13 files changed, 221 insertions(+), 1 deletion(-) 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 821e47fb26..3025f6bc6e 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 @@ -1,3 +1,20 @@ +# 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. + from cmd2 import * from Utils import * from Stratos import * diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py index 07a3380a6c..387b1296ed 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py @@ -1,3 +1,20 @@ +# 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. + import os stratos_prompt = "stratos> " diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py index 3451bca2d6..3caa892b1c 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Logging.py @@ -1,3 +1,20 @@ +# 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. + import logging import os import Configs diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py index d6ef2d5391..78b3572851 100755 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py @@ -1,3 +1,20 @@ +# 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. + import sys import readline import getpass 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 a063f1c4e1..72c1c71d53 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,3 +1,20 @@ +# 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. + import requests import Configs from cli.exceptions.AuthenticationError import AuthenticationError diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py index aa4b8de33a..bc33642246 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py @@ -1,3 +1,20 @@ +# 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. + from __future__ import print_function import sys from texttable import * diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/__init__.py b/components/org.apache.stratos.python.cli/src/main/python/cli/__init__.py index e69de29bb2..60d550d605 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/__init__.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/__init__.py @@ -0,0 +1,17 @@ +# 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. + diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py b/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py index d0e10892fb..23fe468704 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py @@ -1,3 +1,20 @@ +# 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. + class AuthenticationError(Exception): def __init__(self): pass diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/__init__.py b/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/__init__.py index f80b5be600..60d550d605 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/__init__.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/__init__.py @@ -1 +1,17 @@ -__author__ = 'agentmilindu' +# 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. + diff --git a/components/org.apache.stratos.python.cli/src/main/python/setup.py b/components/org.apache.stratos.python.cli/src/main/python/setup.py index 1c390e2494..f8fdccffa6 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/setup.py +++ b/components/org.apache.stratos.python.cli/src/main/python/setup.py @@ -1,3 +1,20 @@ +# 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. + import os from setuptools import setup diff --git a/components/org.apache.stratos.python.cli/src/main/python/tests/__init__.py b/components/org.apache.stratos.python.cli/src/main/python/tests/__init__.py index e69de29bb2..60d550d605 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/tests/__init__.py +++ b/components/org.apache.stratos.python.cli/src/main/python/tests/__init__.py @@ -0,0 +1,17 @@ +# 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. + diff --git a/components/org.apache.stratos.python.cli/src/main/python/tests/test_print.py b/components/org.apache.stratos.python.cli/src/main/python/tests/test_print.py index 72a1d705fb..8d0b5aab59 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/tests/test_print.py +++ b/components/org.apache.stratos.python.cli/src/main/python/tests/test_print.py @@ -1,3 +1,20 @@ +# 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. + import sys import unittest from cli import Utils diff --git a/components/org.apache.stratos.python.cli/src/main/python/tests/test_printable_tree_print.py b/components/org.apache.stratos.python.cli/src/main/python/tests/test_printable_tree_print.py index 33ef5ceae7..2a386031ae 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/tests/test_printable_tree_print.py +++ b/components/org.apache.stratos.python.cli/src/main/python/tests/test_printable_tree_print.py @@ -1,3 +1,20 @@ +# 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. + import sys from cli import Utils From d798f35eae877901b1108615a9cdf4b069924e4b Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sat, 1 Aug 2015 22:35:23 +0530 Subject: [PATCH 43/79] Added list-kubernetes-hosts command --- .../src/main/python/cli/CLI.py | 24 ++++++++++++++++++- .../src/main/python/cli/Stratos.py | 6 +++-- 2 files changed, 27 insertions(+), 3 deletions(-) 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 3025f6bc6e..6d73df7b73 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 @@ -188,7 +188,7 @@ def do_list_kubernetes_clusters(self, line , opts=None): """Retrieve detailed information on all Kubernetes-CoreOS Clusters.""" kubernetes_clusters = Stratos.list_kubernetes_clusters() if not kubernetes_clusters: - print("No Kubernetes-CoreOS clusters found") + print("No Kubernetes clusters found") else: table = PrintableTable() rows = [["Group ID", "Description"]] @@ -197,6 +197,28 @@ def do_list_kubernetes_clusters(self, line , opts=None): 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"), + make_option('-c', '--cluster_id', type="str", help="Cluster ID") + ]) + @auth + def do_list_kubernetes_hosts(self, line , opts=None): + """Retrieve detailed information on all Kubernetes-CoreOS Clusters.""" + if not opts.cluster_id: + print("usage: list-kubernetes-hosts [-c ]") + return + kubernetes_cluster_hosts = Stratos.list_kubernetes_hosts(opts.cluster_id) + if not kubernetes_cluster_hosts: + print("No kubernetes hosts found") + else: + table = PrintableTable() + rows = [["Host ID", "Hostname", "Private IP Address", "Public IP Address"]] + for kubernetes_cluster_host in kubernetes_cluster_hosts: + rows.append([kubernetes_cluster_host['hostId'], kubernetes_cluster_host['hostname'], kubernetes_cluster_host['privateIPAddress'], kubernetes_cluster_host['publicIPAddress']]) + table.add_rows(rows) + table.print_table() + @options([]) def do_deploy_user(self, line , opts=None): """Illustrate the base class method use.""" 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 72c1c71d53..af47b5093f 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 @@ -20,7 +20,6 @@ from cli.exceptions.AuthenticationError import AuthenticationError - class Stratos: """Apache Stratos Python API""" @@ -103,6 +102,10 @@ def list_cartridges_groups(): def list_kubernetes_clusters(): return Stratos.get('kubernetesClusters', errorMessage='Kubernetes cluster not found') + @staticmethod + def list_kubernetes_hosts(kubernetes_cluster_id): + return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id+'/hosts', errorMessage='Kubernetes cluster not found') + """ # Utils @@ -112,7 +115,6 @@ def list_kubernetes_clusters(): def get(resource, errorMessage): r = requests.get(Configs.stratos_api_url + resource, auth=(Configs.stratos_username, Configs.stratos_password), verify=False) - print(r.text) if r.status_code == 200: return r.json() elif r.status_code == 400: From 9c20de6d1398d00ec947724fefcd6340261875e4 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sat, 1 Aug 2015 22:39:32 +0530 Subject: [PATCH 44/79] Added new line at the end of file as of PEP8 recommendations --- .../org.apache.stratos.python.cli/src/main/python/cli/CLI.py | 2 +- .../src/main/python/cli/Configs.py | 2 +- .../src/main/python/cli/Stratos.py | 3 --- .../org.apache.stratos.python.cli/src/main/python/cli/Utils.py | 1 - .../src/main/python/cli/exceptions/AuthenticationError.py | 2 +- .../org.apache.stratos.python.cli/src/main/python/setup.py | 1 - 6 files changed, 3 insertions(+), 8 deletions(-) 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 6d73df7b73..a1de37da4a 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 @@ -226,4 +226,4 @@ def do_deploy_user(self, line , opts=None): try: Stratos.deploy_user() except ValueError as e: - self.perror("sdc") \ No newline at end of file + self.perror("sdc") diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py index 387b1296ed..bc813a48ad 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Configs.py @@ -28,4 +28,4 @@ stratos_url = os.getenv('STRATOS_URL', "https://localhost:9443/") stratos_api_url = stratos_url + "api/" stratos_username = os.getenv('STRATOS_USERNAME', "") -stratos_password = os.getenv('STRATOS_PASSWORD', "") \ No newline at end of file +stratos_password = os.getenv('STRATOS_PASSWORD', "") 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 af47b5093f..40d511f615 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 @@ -127,6 +127,3 @@ def get(resource, errorMessage): else: raise requests.HTTPError() - - - diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py index bc33642246..96cc5ff1c9 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py @@ -83,4 +83,3 @@ def auth_inner(self, *args, **kwargs): else: return func(self, *args, **kwargs) return auth_inner - diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py b/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py index 23fe468704..2d68bd7d0a 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py @@ -20,4 +20,4 @@ def __init__(self): pass def __str__(self): - return "Authentication error" \ No newline at end of file + return "Authentication error" diff --git a/components/org.apache.stratos.python.cli/src/main/python/setup.py b/components/org.apache.stratos.python.cli/src/main/python/setup.py index f8fdccffa6..fce7decc14 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/setup.py +++ b/components/org.apache.stratos.python.cli/src/main/python/setup.py @@ -44,4 +44,3 @@ def read(file_name): stratos-cli=cli.Main:main ''', ) - From 44bd72afffdc44ab256836107259479d0f5ddcd9 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sun, 2 Aug 2015 13:08:59 +0530 Subject: [PATCH 45/79] 180 char limit as of PEP8 --- .../src/main/python/cli/CLI.py | 11 +++++++---- .../src/main/python/cli/Stratos.py | 3 ++- 2 files changed, 9 insertions(+), 5 deletions(-) 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 a1de37da4a..ae083cf8a9 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 @@ -83,7 +83,7 @@ def do_repositories(self, line, opts=None): def do_add_user(self, line , opts=None): """Add a new user to the system""" try: - user = Stratos.list_users(opts.username, opts.password, opts.role_name, opts.first_name, opts.last_name, + user = Stratos.add_users(opts.username, opts.password, opts.role_name, opts.first_name, opts.last_name, opts.email, opts.profile_name) if user: print("User successfully created") @@ -139,7 +139,8 @@ def do_list_cartridges(self, line , opts=None): table = PrintableTable() 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']]) + rows.append([cartridge['type'], cartridge['category'], cartridge['displayName'], cartridge['description'], + cartridge['version'], cartridge['multiTenant']]) table.add_rows(rows) table.print_table() @@ -172,7 +173,8 @@ def do_list_applications(self, line , opts=None): 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']]) + rows.append([application['type'], application['category'], application['displayName'], + application['description'], application['version'], application['multiTenant']]) table.add_rows(rows) table.print_table() """ @@ -215,7 +217,8 @@ def do_list_kubernetes_hosts(self, line , opts=None): table = PrintableTable() rows = [["Host ID", "Hostname", "Private IP Address", "Public IP Address"]] for kubernetes_cluster_host in kubernetes_cluster_hosts: - rows.append([kubernetes_cluster_host['hostId'], kubernetes_cluster_host['hostname'], kubernetes_cluster_host['privateIPAddress'], kubernetes_cluster_host['publicIPAddress']]) + rows.append([kubernetes_cluster_host['hostId'], kubernetes_cluster_host['hostname'], + kubernetes_cluster_host['privateIPAddress'], kubernetes_cluster_host['publicIPAddress']]) table.add_rows(rows) table.print_table() 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 40d511f615..3cfea19400 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 @@ -104,7 +104,8 @@ def list_kubernetes_clusters(): @staticmethod def list_kubernetes_hosts(kubernetes_cluster_id): - return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id+'/hosts', errorMessage='Kubernetes cluster not found') + return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id+'/hosts', + errorMessage='Kubernetes cluster not found') """ From 2ba8708da8dc23287b5c87976724e683795fe4a1 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sun, 2 Aug 2015 13:10:03 +0530 Subject: [PATCH 46/79] Updated list-cartridges-group command --- .../src/main/python/cli/CLI.py | 9 +++++---- .../src/main/python/cli/Stratos.py | 16 ++-------------- 2 files changed, 7 insertions(+), 18 deletions(-) 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 ae083cf8a9..1bc773c336 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 @@ -149,13 +149,14 @@ def do_list_cartridges(self, line , opts=None): make_option('-p', '--password', type="str", help="Password of the user") ]) @auth - def do_list_cartridges_group(self, line , opts=None): + def do_list_cartridge_groups(self, line , opts=None): """Illustrate the base class method use.""" - cartridges_groups = Stratos.list_cartridges_group() + cartridge_groups = Stratos.list_cartridge_groups() 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']]) + for cartridge_group in cartridge_groups: + rows.append([cartridge_group['name'], len(cartridge_group['cartridges']), + len(cartridge_group['cartridges']), cartridge_group['groupScalingEnabled']]) table.add_rows(rows) table.print_table() 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 3cfea19400..5990b4c517 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 @@ -79,20 +79,8 @@ def list_cartridges(): 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() + def list_cartridge_groups(): + return Stratos.get('cartridgeGroups', errorMessage='No cartridge groups found') """ # Kubernetes Clusters From 4d9303aa765a176b5587c628181977710d85bbb9 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sun, 2 Aug 2015 13:10:18 +0530 Subject: [PATCH 47/79] Updated list-cartridges command --- .../src/main/python/cli/Stratos.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 5990b4c517..e7becf9297 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 @@ -74,9 +74,7 @@ def list_applications(): """ @staticmethod def list_cartridges(): - r = requests.get(Configs.stratos_api_url + 'cartridges', - auth=(Configs.stratos_username, Configs.stratos_password), verify=False) - return r.json() + return Stratos.get('cartridges', errorMessage='No cartridges found') @staticmethod def list_cartridge_groups(): @@ -104,6 +102,7 @@ def list_kubernetes_hosts(kubernetes_cluster_id): def get(resource, errorMessage): r = requests.get(Configs.stratos_api_url + resource, auth=(Configs.stratos_username, Configs.stratos_password), verify=False) + print(r.text) if r.status_code == 200: return r.json() elif r.status_code == 400: From bc57cc239a620856c613c25590d833e4083f6d13 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Tue, 4 Aug 2015 12:18:56 +0530 Subject: [PATCH 48/79] added activate-tenant command --- .../src/main/python/cli/CLI.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) 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 1bc773c336..1715b82f00 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 @@ -223,6 +223,28 @@ def do_list_kubernetes_hosts(self, line , opts=None): 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"), + make_option('-t', '--tenant_domain', type="str", help="Cluster ID") + ]) + @auth + def do_activate_tenant(self, line , opts=None): + """Retrieve detailed information on all Kubernetes-CoreOS Clusters.""" + if not opts.tenant_domain: + print("usage: list-kubernetes-hosts [-c ]") + return + kubernetes_cluster_hosts = Stratos.list_kubernetes_hosts(opts.cluster_id) + if not kubernetes_cluster_hosts: + print("No kubernetes hosts found") + else: + table = PrintableTable() + rows = [["Host ID", "Hostname", "Private IP Address", "Public IP Address"]] + for kubernetes_cluster_host in kubernetes_cluster_hosts: + rows.append([kubernetes_cluster_host['hostId'], kubernetes_cluster_host['hostname'], + kubernetes_cluster_host['privateIPAddress'], kubernetes_cluster_host['publicIPAddress']]) + table.add_rows(rows) + table.print_table() @options([]) def do_deploy_user(self, line , opts=None): """Illustrate the base class method use.""" From 3774fc206627be732d7b9a7db93e9e5b0de20111 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Tue, 4 Aug 2015 12:19:42 +0530 Subject: [PATCH 49/79] Added list-deploying-policies --- .../src/main/python/cli/CLI.py | 19 +++++++++++++++++++ .../src/main/python/cli/Stratos.py | 4 ++++ 2 files changed, 23 insertions(+) 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 1715b82f00..346b07312b 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 @@ -253,3 +253,22 @@ def do_deploy_user(self, line , opts=None): Stratos.deploy_user() except ValueError 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_deployment_policies(self, line , opts=None): + """Illustrate the base class method use.""" + deployment_policies = Stratos.list_deployment_policies() + if not deployment_policies: + print("No deployment policies found") + else: + table = PrintableTable() + rows = [["Id", "Accessibility"]] + for deployment_policy in deployment_policies: + rows.append([deployment_policy['id'], len(deployment_policy['networkPartitions'])]) + table.add_rows(rows) + table.print_table() 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 e7becf9297..db6ca31f59 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 @@ -93,6 +93,10 @@ def list_kubernetes_hosts(kubernetes_cluster_id): return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id+'/hosts', errorMessage='Kubernetes cluster not found') + @staticmethod + def list_deployment_policies(): + return Stratos.get('deploymentPolicies', + errorMessage='Deployment policies not found') """ # Utils From c7eb3a1fc192d95e9cf37a7579c46ea9e9c64a9c Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Tue, 4 Aug 2015 12:20:37 +0530 Subject: [PATCH 50/79] Added list-cartridge-groups --- .../src/main/python/cli/Stratos.py | 4 ++++ 1 file changed, 4 insertions(+) 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 db6ca31f59..809c4fa877 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 @@ -98,6 +98,10 @@ def list_deployment_policies(): return Stratos.get('deploymentPolicies', errorMessage='Deployment policies not found') + @staticmethod + def list_cartridge_groups(): + return Stratos.get('cartridgeGroups', + errorMessage='cartridge groups not found') """ # Utils From a9b7292f2fcc36986388243a6acedf163851d17b Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Tue, 4 Aug 2015 12:21:08 +0530 Subject: [PATCH 51/79] Added list-autoscaling-policies --- .../src/main/python/cli/CLI.py | 18 ++++++++++++++++++ .../src/main/python/cli/Stratos.py | 5 +++++ 2 files changed, 23 insertions(+) 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 346b07312b..83374c7ec9 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 @@ -272,3 +272,21 @@ def do_list_deployment_policies(self, line , opts=None): rows.append([deployment_policy['id'], len(deployment_policy['networkPartitions'])]) 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_autoscaling_policies(self, line , opts=None): + """Retrieve details of all the cartridge groups that have been added.""" + autoscaling_policies = Stratos.list_autoscaling_policies() + if not autoscaling_policies: + print("No autoscaling policies found") + else: + table = PrintableTable() + rows = [["Id", "Accessibility"]] + for autoscaling_policy in autoscaling_policies: + rows.append([autoscaling_policy['id'], "Public" if autoscaling_policy['isPublic'] else "Private"]) + table.add_rows(rows) + table.print_table() 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 809c4fa877..04ff6748bd 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 @@ -102,6 +102,11 @@ def list_deployment_policies(): def list_cartridge_groups(): return Stratos.get('cartridgeGroups', errorMessage='cartridge groups not found') + @staticmethod + def list_autoscaling_policies(): + return Stratos.get('autoscalingPolicies', + errorMessage='No Autoscaling policies found') + """ # Utils From de66b1f74903ddbc1ba1f2978780bbfe90429e79 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Thu, 6 Aug 2015 10:15:41 +0530 Subject: [PATCH 52/79] Added function for pretty printing JSON --- .../src/main/python/cli/Utils.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py index 96cc5ff1c9..43ad375bed 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py @@ -18,6 +18,7 @@ from __future__ import print_function import sys from texttable import * +import json import Configs from Logging import logging @@ -68,6 +69,15 @@ def __init__(self): def print_table(self): print(self.draw()) +class PrintableJSON(Texttable): + + def __init__(self, json): + self.json = json + + def pprint(self): + + print(json.dumps(self.json, indent=4, separators=(',', ': '))) + def auth(func): """Authenticate""" def auth_inner(self, *args, **kwargs): From 3377498cdc8fca2cbf71c572246ad96980f6046d Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Thu, 6 Aug 2015 10:16:57 +0530 Subject: [PATCH 53/79] Updated user related commands --- .../src/main/python/cli/CLI.py | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) 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 83374c7ec9..b91ba4b84b 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 @@ -66,10 +66,32 @@ def do_repositories(self, line, opts=None): table.print_table() """ - # User Entity + User + * list-users + * add-user """ + @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_users(self, line , opts=None): + """Illustrate the base class method use.""" + 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("Authentication Error") + @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user"), @@ -92,27 +114,6 @@ def do_add_user(self, line , opts=None): except AuthenticationError as e: self.perror("Authentication Error") - @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_users(self, line , opts=None): - """Illustrate the base class method use.""" - 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("Authentication Error") - - @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user") From 7177d7b1fff80f6535ec9eee899abdc6011ff31a Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Thu, 6 Aug 2015 10:17:41 +0530 Subject: [PATCH 54/79] added describe-* type commands and list-* type commands --- .../src/main/python/cli/CLI.py | 151 +++++++++++++++++- .../src/main/python/cli/Stratos.py | 83 ++++++++-- 2 files changed, 215 insertions(+), 19 deletions(-) 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 b91ba4b84b..cd9ccb1eca 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 @@ -38,7 +38,11 @@ def completenames(self, text, *ignored): # resolving the '-' issue return [a[3:].replace('_', '-') for a in self.get_names() if a.replace('_', '-').startswith('do-'+text)] + """ + + Stratos CLI specific methods + """ @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -153,13 +157,16 @@ def do_list_cartridges(self, line , opts=None): def do_list_cartridge_groups(self, line , opts=None): """Illustrate the base class method use.""" cartridge_groups = Stratos.list_cartridge_groups() - table = PrintableTable() - rows = [["Name", "No. of cartridges", "No of groups", "Dependency scaling"]] - for cartridge_group in cartridge_groups: - rows.append([cartridge_group['name'], len(cartridge_group['cartridges']), - len(cartridge_group['cartridges']), cartridge_group['groupScalingEnabled']]) - table.add_rows(rows) - table.print_table() + if not cartridge_groups: + print("No cartrige groups found") + else: + table = PrintableTable() + rows = [["Name", "No. of cartridges", "No of groups", "Dependency scaling"]] + for cartridge_group in cartridge_groups: + rows.append([cartridge_group['name'], len(cartridge_group['cartridges']), + len(cartridge_group['cartridges']), len(cartridge_group['dependencies'])]) + table.add_rows(rows) + table.print_table() @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -246,6 +253,7 @@ def do_activate_tenant(self, line , opts=None): kubernetes_cluster_host['privateIPAddress'], kubernetes_cluster_host['publicIPAddress']]) table.add_rows(rows) table.print_table() + @options([]) def do_deploy_user(self, line , opts=None): """Illustrate the base class method use.""" @@ -291,3 +299,132 @@ def do_list_autoscaling_policies(self, line , opts=None): rows.append([autoscaling_policy['id'], "Public" if autoscaling_policy['isPublic'] else "Private"]) 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_describe_cartridge(self, line , opts=None): + """Retrieve details of a specific cartridge.""" + print(line) + cartridge = Stratos.describe_cartridge(line) + if not cartridge: + print("Cartridge not found") + else: + print(""" +------------------------------------- +Cartridge Information: +-------------------------------------""") + + print("Type: "+cartridge[0]['type']) + print("Category: "+cartridge[0]['category']) + print("Name: "+cartridge[0]['displayName']) + print("Description: "+cartridge[0]['description']) + print("Version: "+str(cartridge[0]['version'])) + print("Multi-Tenant: "+str(cartridge[0]['multiTenant'])) + print("Host Name: "+cartridge[0]['host']) + print("-------------------------------------") + + + @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_describe_cartridge_group(self, line , opts=None): + """Retrieve details of a cartridge group.""" + if not line.split(): + print("usage: describe-cartridge-group [cartridge-group-name]") + return + cartridge_group = Stratos.describe_cartridge_group(line) + if not cartridge_group: + print("Cartridge group not found") + else: + PrintableJSON(cartridge_group).pprint() + + + + @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_describe_deployment_policy(self, line , opts=None): + """Retrieve details of a specific deployment policy.""" + if not line.split(): + print("usage: describe-deployment-policy [deployment-policy-id]") + return + deployment_policy = Stratos.describe_deployment_policy(line) + if not deployment_policy: + print("Deployment policy not found") + else: + PrintableJSON(deployment_policy).pprint() + + + @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_describe_kubernetes_cluster(self, line , opts=None): + """Retrieve detailed information on a specific Kubernetes-CoreOS group. +.""" + if not line.split(): + print("usage: describe-kubernetes-cluster [cluster-i]]") + return + kubernetes_cluster= Stratos.describe_kubernetes_cluster(line) + if not kubernetes_cluster: + print("Kubernetes cluster not found") + else: + PrintableJSON(kubernetes_cluster).pprint() + + @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_describe_kubernetes_master(self, line , opts=None): + """Retrieve detailed information on the master node in a specific Kubernetes-CoreOS group""" + if not line.split(): + print("usage: describe-kubernetes-master [cluster-id]") + return + kubernetes_master = Stratos.describe_kubernetes_master(line) + if not kubernetes_master: + print("Kubernetes master not found") + else: + PrintableJSON(kubernetes_master).pprint() + + + + @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_describe_autoscaling_policy(self, line , opts=None): + """Retrieve details of a specific auto-scaling policy.""" + if not line.split(): + print("usage: describe-autoscaling-policy [autoscaling-policy-id]") + return + autoscaling_policy = Stratos.describe_autoscaling_policy(line) + if not autoscaling_policy: + print("Network partition not found") + else: + PrintableJSON(autoscaling_policy).pprint() + + @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_describe_application_signup(self, line , opts=None): + """Retrieve details of a specific auto-scaling policy.""" + if not line.split(): + print("usage: describe-application-signup [application-id]") + return + application_signup = Stratos.describe_application_signup(line) + if not application_signup: + print("Application signup not found") + else: + PrintableJSON(application_signup).pprint() \ No newline at end of file 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 04ff6748bd..f1c2e30bb3 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 @@ -32,7 +32,7 @@ def __init__(self): """ @staticmethod def list_users(): - return Stratos.get('users', errorMessage='No applications found') + return Stratos.get('users', error_message='No applications found') @staticmethod def add_users(username, password, role_name, first_name, last_name, email, profile_name): @@ -46,7 +46,7 @@ def add_users(username, password, role_name, first_name, last_name, email, profi """ @staticmethod def list_network_partitions(): - return Stratos.get('networkPartitions', errorMessage='No network partitions found') + return Stratos.get('networkPartitions', error_message='No network partitions found') """ # Applications @@ -74,11 +74,11 @@ def list_applications(): """ @staticmethod def list_cartridges(): - return Stratos.get('cartridges', errorMessage='No cartridges found') + return Stratos.get('cartridges', error_message='No cartridges found') @staticmethod def list_cartridge_groups(): - return Stratos.get('cartridgeGroups', errorMessage='No cartridge groups found') + return Stratos.get('cartridgeGroups', error_message='No cartridge groups found') """ # Kubernetes Clusters @@ -86,36 +86,95 @@ def list_cartridge_groups(): """ @staticmethod def list_kubernetes_clusters(): - return Stratos.get('kubernetesClusters', errorMessage='Kubernetes cluster not found') + return Stratos.get('kubernetesClusters', error_message='Kubernetes cluster not found') @staticmethod def list_kubernetes_hosts(kubernetes_cluster_id): return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id+'/hosts', - errorMessage='Kubernetes cluster not found') + error_message='Kubernetes cluster not found') @staticmethod def list_deployment_policies(): return Stratos.get('deploymentPolicies', - errorMessage='Deployment policies not found') + error_message='Deployment policies not found') @staticmethod def list_cartridge_groups(): return Stratos.get('cartridgeGroups', - errorMessage='cartridge groups not found') + error_message='cartridge groups not found') @staticmethod def list_autoscaling_policies(): return Stratos.get('autoscalingPolicies', - errorMessage='No Autoscaling policies found') + error_message='No Autoscaling policies found') + + @staticmethod + def describe_cartridge(cartridge_type): + return Stratos.get('cartridges/'+cartridge_type, + error_message='No Autoscaling policies found') + + @staticmethod + def describe_cartridge_group(group_definition_name): + return Stratos.get('cartridgeGroups/'+group_definition_name, + error_message='No cartridge groups found') + + @staticmethod + def describe_deployment_policy(deployment_policy_name): + return Stratos.get('deploymentPolicies/'+ deployment_policy_name, + error_message='No deployment policies found') + + @staticmethod + def describe_kubernetes_cluster(kubernetes_cluster_id): + return Stratos.get('kubernetesClusters/'+ kubernetes_cluster_id, + error_message='No kubernetes clusters found') + + @staticmethod + def describe_kubernetes_master(kubernetes_cluster_id): + return Stratos.get('kubernetesClusters/'+ kubernetes_cluster_id+'/master', + error_message='No kubernetes clusters found') + + @staticmethod + def describe_network_partition(network_partition_id): + return Stratos.get('networkPartitions/'+ network_partition_id, + error_message='No nerwork partitions found') + + @staticmethod + def describe_autoscaling_policy(autoscaling_policy_id): + return Stratos.get('autoscalingPolicies/'+ autoscaling_policy_id, + error_message='No autoscaling policy found') + + @staticmethod + def describe_application_signup(application_id): + return Stratos.get('applications/'+ application_id + '/signup', + error_message='No signup application found') + + """ # Utils """ @staticmethod - def get(resource, errorMessage): + def get(resource, error_message): r = requests.get(Configs.stratos_api_url + resource, auth=(Configs.stratos_username, Configs.stratos_password), verify=False) - print(r.text) + #print(r.text) + 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'] == error_message: + return [] + else: + raise requests.HTTPError() + + @staticmethod + def post(resource, data, error_message): + r = requests.post(Configs.stratos_api_url + resource, data, + auth=(Configs.stratos_username, Configs.stratos_password), verify=False) + #print(r.text) if r.status_code == 200: return r.json() elif r.status_code == 400: @@ -123,7 +182,7 @@ def get(resource, errorMessage): elif r.status_code == 401: raise AuthenticationError() elif r.status_code == 404: - if r.json() and r.json()['errorMessage'] == errorMessage: + if r.json() and r.json()['errorMessage'] == error_message: return [] else: raise requests.HTTPError() From 9622f7a60fafae3b5b9e60c38efdb79852072429 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Thu, 6 Aug 2015 10:29:35 +0530 Subject: [PATCH 55/79] improved clarity --- .../org.apache.stratos.python.cli/src/main/python/cli/Utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py index 43ad375bed..4f64873ba1 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py @@ -69,6 +69,7 @@ def __init__(self): def print_table(self): print(self.draw()) + class PrintableJSON(Texttable): def __init__(self, json): @@ -78,6 +79,7 @@ def pprint(self): print(json.dumps(self.json, indent=4, separators=(',', ': '))) + def auth(func): """Authenticate""" def auth_inner(self, *args, **kwargs): From 7e88d7f3025b1d895fed18ca0f7bfd885b0ebe0b Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Fri, 7 Aug 2015 19:32:33 +0530 Subject: [PATCH 56/79] Added new commands --- .../src/main/python/cli/CLI.py | 410 ++++++++++++------ .../src/main/python/cli/Stratos.py | 142 ++++-- 2 files changed, 366 insertions(+), 186 deletions(-) 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 cd9ccb1eca..b5958822f2 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 @@ -41,38 +41,12 @@ def completenames(self, text, *ignored): """ Stratos CLI specific methods + ==================================================================================================================== - """ - - @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_repositories(self, line, opts=None): - """ Shows the git repositories of the user identified by given the username and password - eg: repositories -u agentmilindu -p agentmilindu123 """ - - r = requests.get('https://api.github.com/users/' + Configs.stratos_username + '/repos?per_page=5', - auth=(Configs.stratos_username, Configs.stratos_password)) - repositories = r.json() - print(r) - print(repositories) - table = PrintableTable() - rows = [["Name", "language"]] - table.set_cols_align(["l", "r"]) - table.set_cols_valign(["t", "m"]) - - for repo in repositories: - rows.append([repo['name'], repo['language']]) - print(rows) - table.add_rows(rows) - table.print_table() - - """ - User + # User * list-users * add-user + * remove-user """ @@ -86,11 +60,9 @@ def do_list_users(self, line , opts=None): try: users = Stratos.list_users() table = PrintableTable() - rows = [["Name", "language"]] - table.set_cols_align(["l", "r"]) - table.set_cols_valign(["t", "m"]) + rows = [["Username", "Role"]] for user in users: - rows.append([user['role'], user['userName']]) + rows.append([user['userName'], user['role']]) table.add_rows(rows) table.print_table() except AuthenticationError as e: @@ -99,11 +71,13 @@ def do_list_users(self, line , opts=None): @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-s', '--username_user', type="str", help="Username of the user"), + make_option('-a', '--password_user', type="str", help="Password of the user"), make_option('-r', '--role_name', type="str", help="Role name of the user"), make_option('-f', '--first_name', type="str", help="First name of the user"), make_option('-l', '--last_name', type="str", help="Last name of the user"), make_option('-e', '--email', type="str", help="Email of the user"), - make_option('-x', '--profile_name', type="str", help="Profile name of the user") + make_option('-o', '--profile_name', type="str", help="Profile name of the user") ]) @auth def do_add_user(self, line , opts=None): @@ -123,15 +97,28 @@ def do_add_user(self, line , opts=None): 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.""" - network_partitions = Stratos.list_network_partitions() - table = PrintableTable() - rows = [["Network Partition ID", "Number of Partitions"]] - for network_partition in network_partitions: - rows.append([network_partition['id'], len(network_partition['partitions'])]) - table.add_rows(rows) - table.print_table() + def do_remove_user(self, name , opts=None): + """Delete a specific user""" + try: + if not name: + print("usage: remove-user [username]") + else: + user_removed = Stratos.remove_user(name) + if user_removed: + print("You have successfully deleted user: "+name) + else: + print("Could not delete user: "+name) + except AuthenticationError as e: + self.perror("Authentication Error") + + """ + # Cartridges + * list-cartridges + * describe-cartridge + * add-cartridge + * remove-cartridge + + """ @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -149,6 +136,62 @@ def do_list_cartridges(self, line , opts=None): 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_describe_cartridge(self, cartridge_type , opts=None): + """Retrieve details of a specific cartridge.""" + if not cartridge_type: + print("usage: describe-cartridge [cartridge-type]") + else: + try: + cartridge = Stratos.describe_cartridge(cartridge_type) + if not cartridge: + print("Cartridge not found") + else: + print("-------------------------------------") + print("Cartridge Information:") + print("-------------------------------------") + print("Type: "+cartridge['type']) + print("Category: "+cartridge['category']) + print("Name: "+cartridge['displayName']) + print("Description: "+cartridge['description']) + print("Version: "+str(cartridge['version'])) + print("Multi-Tenant: "+str(cartridge['multiTenant'])) + print("Host Name: "+cartridge['host']) + print("-------------------------------------") + except requests.HTTPError as e: + self.perror("Error") + + @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_remove_cartridge(self, cartridge_type , opts=None): + """Delete a cartridge""" + try: + if not cartridge_type: + print("usage: remove-cartridge [cartridge-type]") + else: + cartridge_removed = Stratos.remove_cartridge(cartridge_type) + if cartridge_removed: + print("Successfully un-deployed cartridge : "+cartridge_type) + else: + print("Could not un-deployed cartridge : "+cartridge_type) + except AuthenticationError as e: + self.perror("Authentication Error") + + """ + # Cartridge groups + * list-cartridge-groups + * describe-cartridge-group + * remove-cartridge-group + + """ + @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user") @@ -158,7 +201,7 @@ def do_list_cartridge_groups(self, line , opts=None): """Illustrate the base class method use.""" cartridge_groups = Stratos.list_cartridge_groups() if not cartridge_groups: - print("No cartrige groups found") + print("No cartridge groups found") else: table = PrintableTable() rows = [["Name", "No. of cartridges", "No of groups", "Dependency scaling"]] @@ -168,6 +211,186 @@ def do_list_cartridge_groups(self, line , opts=None): 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_describe_cartridge_group(self, group_definition_name , opts=None): + """Retrieve details of a cartridge group.""" + if not group_definition_name: + print("usage: describe-cartridge-group [cartridge-group-name]") + return + cartridge_group = Stratos.describe_cartridge_group(group_definition_name) + if not cartridge_group: + print("Cartridge group not found") + else: + print("Service Group : "+group_definition_name) + PrintableJSON(cartridge_group).pprint() + + @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_remove_cartridge_group(self, group_definition_name , opts=None): + """Delete a cartridge""" + try: + if not group_definition_name: + print("usage: remove-cartridge-group [cartridge-group-name]") + else: + cartridge_removed = Stratos.remove_cartridge_group(group_definition_name) + if cartridge_removed: + print("Successfully un-deployed cartridge group : "+group_definition_name) + else: + print("Could not un-deployed cartridge group : "+group_definition_name) + except AuthenticationError as e: + self.perror("Authentication Error") + + """ + # Deployment Policies + * list-deployment-policies + * describe-deployment-policy + * remove-deployment-policy + + """ + + @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_deployment_policies(self, line , opts=None): + """Illustrate the base class method use.""" + deployment_policies = Stratos.list_deployment_policies() + if not deployment_policies: + print("No deployment policies found") + else: + table = PrintableTable() + rows = [["Id", "Accessibility"]] + for deployment_policy in deployment_policies: + rows.append([deployment_policy['id'], len(deployment_policy['networkPartitions'])]) + 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_describe_deployment_policy(self, line , opts=None): + """Retrieve details of a specific deployment policy.""" + if not line.split(): + print("usage: describe-deployment-policy [deployment-policy-id]") + return + deployment_policy = Stratos.describe_deployment_policy(line) + if not deployment_policy: + print("Deployment policy not found") + else: + PrintableJSON(deployment_policy).pprint() + + @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_remove_deployment_policy(self, deployment_policy_id , opts=None): + """Delete a cartridge""" + try: + if not deployment_policy_id: + print("usage: remove-deployment-policy [deployment-policy-id]") + else: + cartridge_removed = Stratos.remove_deployment_policy(deployment_policy_id) + if cartridge_removed: + print("Successfully deleted deployment policy : "+deployment_policy_id) + else: + print("Could not deleted deployment policy : "+deployment_policy_id) + except AuthenticationError as e: + self.perror("Authentication Error") + + """ + # Network Partitions + * list-deployment-policies + * describe-deployment-policy + * remove-deployment-policy + + """ + + @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.""" + network_partitions = Stratos.list_network_partitions() + table = PrintableTable() + rows = [["Network Partition ID", "Number of Partitions"]] + for network_partition in network_partitions: + rows.append([network_partition['id'], len(network_partition['partitions'])]) + 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_describe_network_partition(self, network_partition_id , opts=None): + """Retrieve details of a specific deployment policy.""" + if not network_partition_id: + print("usage: describe-network-partition [network-partition]") + return + deployment_policy = Stratos.describe_network_partition(network_partition_id) + if not deployment_policy: + print("Network partition not found: "+network_partition_id) + else: + print("Partition: "+network_partition_id) + PrintableJSON(deployment_policy).pprint() + + @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_remove_network_partition(self, network_partition_id, opts=None): + """Delete a cartridge""" + try: + if not network_partition_id: + print("usage: remove-network-partition [network-partition-id]") + else: + cartridge_removed = Stratos.remove_network_partition(network_partition_id) + if cartridge_removed: + print("Successfully deleted network-partition : "+network_partition_id) + else: + print("Could not deleted network-partition : "+network_partition_id) + except AuthenticationError as e: + self.perror("Authentication Error") + + """ + # Auto-scaling policies + * + """ + + + @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_autoscaling_policies(self, line , opts=None): + """Retrieve details of all the cartridge groups that have been added.""" + autoscaling_policies = Stratos.list_autoscaling_policies() + if not autoscaling_policies: + print("No autoscaling policies found") + else: + table = PrintableTable() + rows = [["Id", "Accessibility"]] + for autoscaling_policy in autoscaling_policies: + rows.append([autoscaling_policy['id'], "Public" if autoscaling_policy['isPublic'] else "Private"]) + 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") @@ -263,105 +486,6 @@ def do_deploy_user(self, line , opts=None): except ValueError 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_deployment_policies(self, line , opts=None): - """Illustrate the base class method use.""" - deployment_policies = Stratos.list_deployment_policies() - if not deployment_policies: - print("No deployment policies found") - else: - table = PrintableTable() - rows = [["Id", "Accessibility"]] - for deployment_policy in deployment_policies: - rows.append([deployment_policy['id'], len(deployment_policy['networkPartitions'])]) - 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_autoscaling_policies(self, line , opts=None): - """Retrieve details of all the cartridge groups that have been added.""" - autoscaling_policies = Stratos.list_autoscaling_policies() - if not autoscaling_policies: - print("No autoscaling policies found") - else: - table = PrintableTable() - rows = [["Id", "Accessibility"]] - for autoscaling_policy in autoscaling_policies: - rows.append([autoscaling_policy['id'], "Public" if autoscaling_policy['isPublic'] else "Private"]) - 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_describe_cartridge(self, line , opts=None): - """Retrieve details of a specific cartridge.""" - print(line) - cartridge = Stratos.describe_cartridge(line) - if not cartridge: - print("Cartridge not found") - else: - print(""" -------------------------------------- -Cartridge Information: --------------------------------------""") - - print("Type: "+cartridge[0]['type']) - print("Category: "+cartridge[0]['category']) - print("Name: "+cartridge[0]['displayName']) - print("Description: "+cartridge[0]['description']) - print("Version: "+str(cartridge[0]['version'])) - print("Multi-Tenant: "+str(cartridge[0]['multiTenant'])) - print("Host Name: "+cartridge[0]['host']) - print("-------------------------------------") - - - @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_describe_cartridge_group(self, line , opts=None): - """Retrieve details of a cartridge group.""" - if not line.split(): - print("usage: describe-cartridge-group [cartridge-group-name]") - return - cartridge_group = Stratos.describe_cartridge_group(line) - if not cartridge_group: - print("Cartridge group not found") - else: - PrintableJSON(cartridge_group).pprint() - - - - @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_describe_deployment_policy(self, line , opts=None): - """Retrieve details of a specific deployment policy.""" - if not line.split(): - print("usage: describe-deployment-policy [deployment-policy-id]") - return - deployment_policy = Stratos.describe_deployment_policy(line) - if not deployment_policy: - print("Deployment policy not found") - else: - PrintableJSON(deployment_policy).pprint() - - @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user") 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 f1c2e30bb3..f753c3347b 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 @@ -28,6 +28,9 @@ def __init__(self): """ # Users + * list-users + * add-users + * remove-user """ @staticmethod @@ -36,17 +39,19 @@ def list_users(): @staticmethod def add_users(username, password, role_name, first_name, last_name, email, profile_name): - data = [username] - r = requests.post(Configs.stratos_api_url + 'users', data, - auth=(Configs.stratos_username, Configs.stratos_password), verify=False) - - """ - # Network Partitions + data = { + "userName": username, + "credential": password, + "role": role_name, + "firstName": first_name, + "lastName": last_name, + "email": email + } + return Stratos.post('users', data, error_message='No applications found') - """ @staticmethod - def list_network_partitions(): - return Stratos.get('networkPartitions', error_message='No network partitions found') + def remove_user(name): + return Stratos.delete('users/'+name) """ # Applications @@ -70,6 +75,9 @@ def list_applications(): """ # Cartridges + * list-cartridges + * describe-cartridge + * remove-cartridges """ @staticmethod @@ -77,50 +85,90 @@ def list_cartridges(): return Stratos.get('cartridges', error_message='No cartridges found') @staticmethod - def list_cartridge_groups(): - return Stratos.get('cartridgeGroups', error_message='No cartridge groups found') + def describe_cartridge(cartridge_type): + return Stratos.get('cartridges/'+cartridge_type, error_message='No cartridge found') + + @staticmethod + def remove_cartridge(cartridge_type): + return Stratos.delete('cartridges/'+cartridge_type) """ - # Kubernetes Clusters + # Cartridge groups + * list-cartridge-groups + * describe-cartridge-group + * remove-cartridges-group """ + @staticmethod - def list_kubernetes_clusters(): - return Stratos.get('kubernetesClusters', error_message='Kubernetes cluster not found') + def list_cartridge_groups(): + return Stratos.get('cartridgeGroups', error_message='No cartridge groups found') @staticmethod - def list_kubernetes_hosts(kubernetes_cluster_id): - return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id+'/hosts', - error_message='Kubernetes cluster not found') + def describe_cartridge_group(group_definition_name): + return Stratos.get('cartridgeGroups/'+group_definition_name, error_message='No cartridge groups found') + + @staticmethod + def remove_cartridge_group(group_definition_name): + return Stratos.delete('cartridgeGroups/'+group_definition_name) + + """ + # Deployment Policy + * list-deployment-policies + * describe-deployment-policy + * remove-deployment-policy + """ @staticmethod def list_deployment_policies(): return Stratos.get('deploymentPolicies', error_message='Deployment policies not found') + @staticmethod + def describe_deployment_policy(deployment_policy_name): + return Stratos.get('deploymentPolicies/'+ deployment_policy_name, + error_message='No deployment policies found') + @staticmethod + def remove_deployment_policy(deployment_policy_id): + return Stratos.delete('deploymentPolicies/'+deployment_policy_id) + + """ + # Network partitions + * list-network-partitions + * describe-network-partition + * remove-network-partition + """ @staticmethod - def list_cartridge_groups(): - return Stratos.get('cartridgeGroups', - error_message='cartridge groups not found') + def list_network_partitions(): + return Stratos.get('networkPartitions', error_message='No network partitions found') + @staticmethod - def list_autoscaling_policies(): - return Stratos.get('autoscalingPolicies', - error_message='No Autoscaling policies found') + def describe_network_partition(network_partition_id): + return Stratos.get('networkPartitions/'+ network_partition_id, + error_message='No network partitions found') @staticmethod - def describe_cartridge(cartridge_type): - return Stratos.get('cartridges/'+cartridge_type, - error_message='No Autoscaling policies found') + def remove_network_partition(network_partition_id): + return Stratos.delete('networkPartitions/'+network_partition_id) + """ + # Kubernetes Clusters + + """ @staticmethod - def describe_cartridge_group(group_definition_name): - return Stratos.get('cartridgeGroups/'+group_definition_name, - error_message='No cartridge groups found') + def list_kubernetes_clusters(): + return Stratos.get('kubernetesClusters', error_message='Kubernetes cluster not found') @staticmethod - def describe_deployment_policy(deployment_policy_name): - return Stratos.get('deploymentPolicies/'+ deployment_policy_name, - error_message='No deployment policies found') + def list_kubernetes_hosts(kubernetes_cluster_id): + return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id+'/hosts', + error_message='Kubernetes cluster not found') + + + @staticmethod + def list_autoscaling_policies(): + return Stratos.get('autoscalingPolicies', + error_message='No Autoscaling policies found') @staticmethod def describe_kubernetes_cluster(kubernetes_cluster_id): @@ -131,12 +179,6 @@ def describe_kubernetes_cluster(kubernetes_cluster_id): def describe_kubernetes_master(kubernetes_cluster_id): return Stratos.get('kubernetesClusters/'+ kubernetes_cluster_id+'/master', error_message='No kubernetes clusters found') - - @staticmethod - def describe_network_partition(network_partition_id): - return Stratos.get('networkPartitions/'+ network_partition_id, - error_message='No nerwork partitions found') - @staticmethod def describe_autoscaling_policy(autoscaling_policy_id): return Stratos.get('autoscalingPolicies/'+ autoscaling_policy_id, @@ -157,7 +199,7 @@ def describe_application_signup(application_id): def get(resource, error_message): r = requests.get(Configs.stratos_api_url + resource, auth=(Configs.stratos_username, Configs.stratos_password), verify=False) - #print(r.text) + # print(r.text) if r.status_code == 200: return r.json() elif r.status_code == 400: @@ -165,16 +207,30 @@ def get(resource, error_message): elif r.status_code == 401: raise AuthenticationError() elif r.status_code == 404: - if r.json() and r.json()['errorMessage'] == error_message: + if r.text and r.json() and r.json()['errorMessage'] == error_message: return [] else: raise requests.HTTPError() + @staticmethod + def delete(resource): + r = requests.delete(Configs.stratos_api_url + resource, + auth=(Configs.stratos_username, Configs.stratos_password), verify=False) + print(r.text) + 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: + raise requests.HTTPError() + @staticmethod def post(resource, data, error_message): r = requests.post(Configs.stratos_api_url + resource, data, - auth=(Configs.stratos_username, Configs.stratos_password), verify=False) - #print(r.text) + auth=(Configs.stratos_username, Configs.stratos_password), verify=False) + print(r.text) if r.status_code == 200: return r.json() elif r.status_code == 400: @@ -182,7 +238,7 @@ def post(resource, data, error_message): elif r.status_code == 401: raise AuthenticationError() elif r.status_code == 404: - if r.json() and r.json()['errorMessage'] == error_message: + if r.text and r.json() and r.json()['errorMessage'] == error_message: return [] else: raise requests.HTTPError() From cb926c7778eb25b3877ce7564f611eaf336800db Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sat, 8 Aug 2015 07:26:23 +0530 Subject: [PATCH 57/79] Actions related to Auto-scaling policies and Kubernetes Clusters updated --- .../src/main/python/cli/CLI.py | 182 +++++++++++------- .../src/main/python/cli/Stratos.py | 50 +++-- 2 files changed, 147 insertions(+), 85 deletions(-) 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 b5958822f2..dd9c70a72a 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 @@ -369,9 +369,11 @@ def do_remove_network_partition(self, network_partition_id, opts=None): """ # Auto-scaling policies - * - """ + * list-autoscaling-policies + * describe-autoscaling-policy + * remove-autoscaling-policy + """ @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -390,29 +392,54 @@ def do_list_autoscaling_policies(self, line , opts=None): rows.append([autoscaling_policy['id'], "Public" if autoscaling_policy['isPublic'] else "Private"]) 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") + def do_describe_autoscaling_policy(self, autoscaling_policy_id , opts=None): + """Retrieve details of a specific auto-scaling policy.""" + if not autoscaling_policy_id: + print("usage: describe-autoscaling-policy [autoscaling-policy-id]") + return + autoscaling_policy = Stratos.describe_autoscaling_policy(autoscaling_policy_id) + if not autoscaling_policy: + print("Autoscaling policy not found : "+autoscaling_policy_id) 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() + print("Autoscaling policy : "+autoscaling_policy_id) + PrintableJSON(autoscaling_policy).pprint() + + @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_remove_autoscaling_policy(self, autoscaling_policy_id , opts=None): + """Delete a cartridge""" + try: + if not autoscaling_policy_id: + print("usage: remove-autoscaling-policy [application-id]") + else: + cartridge_removed = Stratos.remove_autoscaling_policy(autoscaling_policy_id) + if cartridge_removed: + print("Successfully deleted Auto-scaling policy : "+autoscaling_policy_id) + else: + print("Auto-scaling policy not found : "+autoscaling_policy_id) + except AuthenticationError as e: + self.perror("Authentication Error") + """ - # Kubernetes Cluster/Host + # Kubernetes clusters/hosts + * list-kubernetes-clusters + * describe-kubernetes-cluster + * list-kubernetes-hosts + * describe-kubernetes-master + * remove-kubernetes-cluster + * remove-kubernetes-host """ + @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user") @@ -431,6 +458,22 @@ def do_list_kubernetes_clusters(self, line , opts=None): 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_describe_kubernetes_cluster(self, kubernetes_cluster_id, opts=None): + """Retrieve detailed information on a specific Kubernetes-CoreOS group""" + if not kubernetes_cluster_id: + print("usage: describe-kubernetes-cluster [cluster-i]]") + return + kubernetes_cluster = Stratos.describe_kubernetes_cluster(kubernetes_cluster_id) + if not kubernetes_cluster: + print("Kubernetes cluster not found") + else: + PrintableJSON(kubernetes_cluster).pprint() + @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user"), @@ -454,6 +497,62 @@ def do_list_kubernetes_hosts(self, line , opts=None): 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_describe_kubernetes_master(self, kubernetes_cluster_id , opts=None): + """Retrieve detailed information on the master node in a specific Kubernetes-CoreOS group""" + if not kubernetes_cluster_id: + print("usage: describe-kubernetes-master [cluster-id]") + return + kubernetes_master = Stratos.describe_kubernetes_master(kubernetes_cluster_id) + if not kubernetes_master: + print("Kubernetes master not found in : "+kubernetes_cluster_id) + else: + print("Cluster : "+kubernetes_cluster_id) + PrintableJSON(kubernetes_master).pprint() + + @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_remove_kubernetes_cluster(self, kubernetes_cluster_id, opts=None): + """Delete a cartridge""" + try: + if not kubernetes_cluster_id: + print("usage: remove-kubernetes-cluster [cluster-id]") + else: + cartridge_removed = Stratos.remove_autoscaling_policy(kubernetes_cluster_id) + if cartridge_removed: + print("Successfully un-deployed kubernetes cluster : "+kubernetes_cluster_id) + else: + print("Kubernetes cluster not found : "+kubernetes_cluster_id) + except AuthenticationError as e: + self.perror("Authentication Error") + + @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([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user"), @@ -486,57 +585,6 @@ def do_deploy_user(self, line , opts=None): except ValueError 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_describe_kubernetes_cluster(self, line , opts=None): - """Retrieve detailed information on a specific Kubernetes-CoreOS group. -.""" - if not line.split(): - print("usage: describe-kubernetes-cluster [cluster-i]]") - return - kubernetes_cluster= Stratos.describe_kubernetes_cluster(line) - if not kubernetes_cluster: - print("Kubernetes cluster not found") - else: - PrintableJSON(kubernetes_cluster).pprint() - - @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_describe_kubernetes_master(self, line , opts=None): - """Retrieve detailed information on the master node in a specific Kubernetes-CoreOS group""" - if not line.split(): - print("usage: describe-kubernetes-master [cluster-id]") - return - kubernetes_master = Stratos.describe_kubernetes_master(line) - if not kubernetes_master: - print("Kubernetes master not found") - else: - PrintableJSON(kubernetes_master).pprint() - - - - @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_describe_autoscaling_policy(self, line , opts=None): - """Retrieve details of a specific auto-scaling policy.""" - if not line.split(): - print("usage: describe-autoscaling-policy [autoscaling-policy-id]") - return - autoscaling_policy = Stratos.describe_autoscaling_policy(line) - if not autoscaling_policy: - print("Network partition not found") - else: - PrintableJSON(autoscaling_policy).pprint() - @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user") 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 f753c3347b..2555d4ca25 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 @@ -152,37 +152,48 @@ def remove_network_partition(network_partition_id): return Stratos.delete('networkPartitions/'+network_partition_id) """ - # Kubernetes Clusters + # Auto-scaling policies + * list-autoscaling-policies + * describe-autoscaling-policy + * remove-autoscaling-policy """ @staticmethod - def list_kubernetes_clusters(): - return Stratos.get('kubernetesClusters', error_message='Kubernetes cluster not found') - + def list_autoscaling_policies(): + return Stratos.get('autoscalingPolicies', + error_message='No Autoscaling policies found') @staticmethod - def list_kubernetes_hosts(kubernetes_cluster_id): - return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id+'/hosts', - error_message='Kubernetes cluster not found') + def describe_autoscaling_policy(autoscaling_policy_id): + return Stratos.get('autoscalingPolicies/'+autoscaling_policy_id, + error_message='No autoscaling policy found') + @staticmethod + def remove_autoscaling_policy(autoscaling_policy_id): + return Stratos.delete('autoscalingPolicies/'+autoscaling_policy_id, error_message="Autoscaling policy not found") + """ + # Kubernetes clusters/hosts + * list-kubernetes-clusters + * describe-kubernetes-cluster + * list-kubernetes-hosts + * describe-kubernetes-master + """ @staticmethod - def list_autoscaling_policies(): - return Stratos.get('autoscalingPolicies', - error_message='No Autoscaling policies found') + def list_kubernetes_clusters(): + return Stratos.get('kubernetesClusters', error_message='Kubernetes cluster not found') @staticmethod def describe_kubernetes_cluster(kubernetes_cluster_id): return Stratos.get('kubernetesClusters/'+ kubernetes_cluster_id, error_message='No kubernetes clusters found') - + @staticmethod + def list_kubernetes_hosts(kubernetes_cluster_id): + return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id+'/hosts', + error_message='Kubernetes cluster not found') @staticmethod def describe_kubernetes_master(kubernetes_cluster_id): return Stratos.get('kubernetesClusters/'+ kubernetes_cluster_id+'/master', error_message='No kubernetes clusters found') - @staticmethod - def describe_autoscaling_policy(autoscaling_policy_id): - return Stratos.get('autoscalingPolicies/'+ autoscaling_policy_id, - error_message='No autoscaling policy found') @staticmethod def describe_application_signup(application_id): @@ -208,12 +219,12 @@ def get(resource, error_message): raise AuthenticationError() elif r.status_code == 404: if r.text and r.json() and r.json()['errorMessage'] == error_message: - return [] + return False else: raise requests.HTTPError() @staticmethod - def delete(resource): + def delete(resource, error_message=None): r = requests.delete(Configs.stratos_api_url + resource, auth=(Configs.stratos_username, Configs.stratos_password), verify=False) print(r.text) @@ -224,7 +235,10 @@ def delete(resource): elif r.status_code == 401: raise AuthenticationError() elif r.status_code == 404: - raise requests.HTTPError() + if r.text and r.json() and r.json()['errorMessage'] == error_message: + return [] + else: + raise requests.HTTPError() @staticmethod def post(resource, data, error_message): From 4c7811fd54f182f693c647306ca5cbea438be861 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Mon, 10 Aug 2015 01:08:52 +0530 Subject: [PATCH 58/79] remove-* commands, update-* commands added --- .../src/main/python/cli/CLI.py | 677 ++++++++++++++++-- .../src/main/python/cli/Stratos.py | 210 +++++- 2 files changed, 823 insertions(+), 64 deletions(-) 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 dd9c70a72a..e9960f9161 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 @@ -16,6 +16,7 @@ # under the License. from cmd2 import * +from rpm._rpm import te from Utils import * from Stratos import * import Configs @@ -46,6 +47,7 @@ def completenames(self, text, *ignored): # User * list-users * add-user + * update-user * remove-user """ @@ -83,7 +85,7 @@ def do_list_users(self, line , opts=None): def do_add_user(self, line , opts=None): """Add a new user to the system""" try: - user = Stratos.add_users(opts.username, opts.password, opts.role_name, opts.first_name, opts.last_name, + user = Stratos.add_users(opts.username_user, opts.password_user, opts.role_name, opts.first_name, opts.last_name, opts.email, opts.profile_name) if user: print("User successfully created") @@ -92,6 +94,30 @@ def do_add_user(self, line , opts=None): except AuthenticationError as e: self.perror("Authentication Error") + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-s', '--username_user', type="str", help="Username of the user"), + make_option('-a', '--password_user', type="str", help="Password of the user"), + make_option('-r', '--role_name', type="str", help="Role name of the user"), + make_option('-f', '--first_name', type="str", help="First name of the user"), + make_option('-l', '--last_name', type="str", help="Last name of the user"), + make_option('-e', '--email', type="str", help="Email of the user"), + make_option('-o', '--profile_name', type="str", help="Profile name of the user") + ]) + @auth + def do_update_user(self, line , opts=None): + """Add a new user to the system""" + try: + user = Stratos.update_user(opts.username_user, opts.password_user, opts.role_name, opts.first_name, opts.last_name, + opts.email, opts.profile_name) + if user: + print("User successfully updated") + else: + print("Error updating the user") + except AuthenticationError as e: + self.perror("Authentication Error") + @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user") @@ -111,6 +137,227 @@ def do_remove_user(self, name , opts=None): except AuthenticationError as e: self.perror("Authentication Error") + """ + # Applications + * list-applications + * add-application + * remove-application + + """ + + @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 = [["Application ID", "Alias", "Status"]] + for application in applications: + PrintableJSON(application).pprint() + rows.append([application['applicationId'], application['alias'], application['status']]) + 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"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_add_application(self, line , opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: add-application [-f ]") + else: + add_application = Stratos.add_application(open(opts.json_file_path, 'r').read()) + if add_application: + print("Application added successfully") + else: + print("Error adding application") + except AuthenticationError as e: + self.perror("Authentication Error") + + @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_remove_application(self, application , opts=None): + """Delete a specific user""" + try: + if not application: + print("usage: remove-application [application]") + else: + application_removed = Stratos.remove_application(application) + if application_removed: + print("You have successfully removed application: "+application) + else: + print("Could not delete application : "+application) + except AuthenticationError as e: + self.perror("Authentication Error") + + """ + # Tenants + * list-tenants + * list-tenants-by-partial-domain + * describe-tenant + * add-tenant + * activate-tenant + * deactivate-tenant + + """ + + @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_tenants(self, line , opts=None): + """Illustrate the base class method use.""" + tenants = Stratos.list_tenants() + table = PrintableTable() + rows = [["Domain", "Tenant ID", "Email", " State", "Created Date"]] + for tenant in tenants: + rows.append([tenant['tenantDomain'], tenant['tenantId'], tenant['email'], + "Active" if tenant['active'] else "De-Active", datetime.datetime.fromtimestamp(tenant['createdDate']/1000).strftime('%Y-%m-%d %H:%M:%S')]) + 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_tenants_by_partial_domain(self, partial_domain , opts=None): + """Illustrate the base class method use.""" + tenants = Stratos.list_tenants_by_partial_domain(partial_domain) + table = PrintableTable() + rows = [["Domain", "Tenant ID", "Email", " State", "Created Date"]] + for tenant in tenants: + rows.append([tenant['tenantDomain'], tenant['tenantId'], tenant['email'], + "Active" if tenant['active'] else "De-Active", datetime.datetime.fromtimestamp(tenant['createdDate']/1000).strftime('%Y-%m-%d %H:%M:%S')]) + 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_describe_tenant(self, tenant_domain_name, opts=None): + """Retrieve details of a specific tenant.""" + if not tenant_domain_name: + print("usage: describe-tenant [Domain-Name]") + else: + try: + tenant = Stratos.describe_tenant(tenant_domain_name) + if not tenant: + print("Tenant not found") + else: + print("-------------------------------------") + print("Tenant Information:") + print("-------------------------------------") + print("Tenant domain: "+tenant['tenantDomain']) + print("ID: "+str(tenant['tenantId'])) + print("Active: "+str(tenant['active'])) + print("Email: "+tenant['email']) + print("Created date: "+datetime.datetime.fromtimestamp(tenant['createdDate']/1000).strftime('%Y-%m-%d %H:%M:%S')) + print("-------------------------------------") + except requests.HTTPError as e: + self.perror("Error") + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-s', '--username_user', type="str", help="Username of the tenant"), + make_option('-a', '--password_user', type="str", help="Password of the tenant"), + make_option('-d', '--domain_name', type="str", help="domain name of the tenant"), + make_option('-f', '--first_name', type="str", help="First name of the tenant"), + make_option('-l', '--last_name', type="str", help="Last name of the tenant"), + make_option('-e', '--email', type="str", help="Email of the tenant") + ]) + @auth + def do_add_tenant(self, line , opts=None): + """Add a new user to the system""" + try: + tenant = Stratos.add_tenant(opts.username_user, opts.first_name, opts.last_name, opts.password_user, + opts.domain_name, opts.email) + if tenant: + print("Tenant added successfully : "+opts.domain_name) + else: + print("Error creating the tenant : "+opts.domain_name) + except AuthenticationError as e: + self.perror("Authentication Error") + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-s', '--username_user', type="str", help="Username of the tenant"), + make_option('-a', '--password_user', type="str", help="Password of the tenant"), + make_option('-d', '--domain_name', type="str", help="domain name of the tenant"), + make_option('-f', '--first_name', type="str", help="First name of the tenant"), + make_option('-l', '--last_name', type="str", help="Last name of the tenant"), + make_option('-e', '--email', type="str", help="Email of the tenant"), + make_option('-i', '--tenant_id', type="str", help="ID of the tenant") + ]) + @auth + def do_update_tenant(self, line , opts=None): + """Add a new user to the system""" + try: + tenant = Stratos.update_tenant(opts.username_user, opts.first_name, opts.last_name, opts.password_user, + opts.domain_name, opts.email, opts.tenant_id) + if tenant: + print("Tenant updated successfully : "+opts.domain_name) + else: + print("Error updating the tenant : "+opts.domain_name) + except AuthenticationError as e: + self.perror("Authentication Error") + + @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_activate_tenant(self, tenant_domain, opts=None): + """Add a new user to the system""" + try: + if not tenant_domain: + print("usage: activate-tenant ") + else: + activate_tenant = Stratos.activate_tenant(tenant_domain) + if activate_tenant: + print("You have successfully activated the tenant : "+tenant_domain) + else: + print("Could not activate tenant : "+tenant_domain) + except AuthenticationError as e: + self.perror("Authentication Error") + + @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_deactivate_tenant(self, tenant_domain, opts=None): + """Add a new user to the system""" + try: + if not tenant_domain: + print("usage: deactivate-tenant ") + else: + activate_tenant = Stratos.deactivate_tenant(tenant_domain) + if activate_tenant: + print("You have successfully deactivated the tenant : "+tenant_domain) + else: + print("Could not deactivate tenant : "+tenant_domain) + except AuthenticationError as e: + self.perror("Authentication Error") + """ # Cartridges * list-cartridges @@ -165,6 +412,46 @@ def do_describe_cartridge(self, cartridge_type , opts=None): except requests.HTTPError as e: self.perror("Error") + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_add_cartridge(self, line , opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: add-cartridge [-f ]") + else: + cartridge = Stratos.add_cartridge(open(opts.json_file_path, 'r').read()) + if cartridge: + print("Cartridge added successfully") + else: + print("Error adding Cartridge") + except AuthenticationError as e: + self.perror("Authentication Error") + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_update_cartridge(self, line , opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: update-cartridge [-f ]") + else: + cartridge = Stratos.update_cartridge(open(opts.json_file_path, 'r').read()) + if cartridge: + print("Cartridge updated successfully") + else: + print("Error updating Cartridge") + except AuthenticationError as e: + self.perror("Authentication Error") + @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user") @@ -188,6 +475,7 @@ def do_remove_cartridge(self, cartridge_type , opts=None): # Cartridge groups * list-cartridge-groups * describe-cartridge-group + * add-cartridge-group * remove-cartridge-group """ @@ -204,10 +492,10 @@ def do_list_cartridge_groups(self, line , opts=None): print("No cartridge groups found") else: table = PrintableTable() - rows = [["Name", "No. of cartridges", "No of groups", "Dependency scaling"]] + rows = [["Name", "No. of cartridges", "No of groups"]] for cartridge_group in cartridge_groups: rows.append([cartridge_group['name'], len(cartridge_group['cartridges']), - len(cartridge_group['cartridges']), len(cartridge_group['dependencies'])]) + len(cartridge_group['cartridges'])]) table.add_rows(rows) table.print_table() @@ -228,6 +516,47 @@ def do_describe_cartridge_group(self, group_definition_name , opts=None): print("Service Group : "+group_definition_name) PrintableJSON(cartridge_group).pprint() + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_add_cartridge_group(self, line , opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: add-cartridge-group [-f ]") + else: + cartridge_group = Stratos.add_cartridge_group(open(opts.json_file_path, 'r').read()) + if cartridge_group: + print("Cartridge group added successfully") + else: + print("Error adding Cartridge group") + except AuthenticationError as e: + self.perror("Authentication Error") + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_update_cartridge_group(self, line , opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: update-cartridge-group [-f ]") + else: + cartridge = Stratos.update_cartridge_group(open(opts.json_file_path, 'r').read()) + if cartridge: + print("Cartridge group updated successfully") + else: + print("Error updating Cartridge group") + except AuthenticationError as e: + self.perror("Authentication Error") + @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user") @@ -251,6 +580,7 @@ def do_remove_cartridge_group(self, group_definition_name , opts=None): # Deployment Policies * list-deployment-policies * describe-deployment-policy + * update-deployment-policy * remove-deployment-policy """ @@ -289,6 +619,27 @@ def do_describe_deployment_policy(self, line , opts=None): else: PrintableJSON(deployment_policy).pprint() + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_update_deployment_policy(self, line , opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: update-deployment-policy [-f ]") + else: + cartridge = Stratos.update_deployment_policy(open(opts.json_file_path, 'r').read()) + if cartridge: + print("Deployment policy updated successfully") + else: + print("Error updating Deployment policy") + except AuthenticationError as e: + self.perror("Authentication Error") + @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user") @@ -312,6 +663,7 @@ def do_remove_deployment_policy(self, deployment_policy_id , opts=None): # Network Partitions * list-deployment-policies * describe-deployment-policy + * update-deployment-policy * remove-deployment-policy """ @@ -348,6 +700,26 @@ def do_describe_network_partition(self, network_partition_id , opts=None): print("Partition: "+network_partition_id) PrintableJSON(deployment_policy).pprint() + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_update_network_partition(self, line , opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: update-network-partition [-f ]") + else: + cartridge = Stratos.update_network_partition(open(opts.json_file_path, 'r').read()) + if cartridge: + print("Network partition updated successfully") + else: + print("Error updating Network partition") + except AuthenticationError as e: + self.perror("Authentication Error") + @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user") @@ -371,6 +743,7 @@ def do_remove_network_partition(self, network_partition_id, opts=None): # Auto-scaling policies * list-autoscaling-policies * describe-autoscaling-policy + * update-autoscaling-policy * remove-autoscaling-policy """ @@ -410,19 +783,39 @@ def do_describe_autoscaling_policy(self, autoscaling_policy_id , opts=None): print("Autoscaling policy : "+autoscaling_policy_id) PrintableJSON(autoscaling_policy).pprint() + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_update_autoscaling_policy(self, line , opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: update-autoscaling-policy [-f ]") + else: + autoscaling_policy = Stratos.update_autoscaling_policy(open(opts.json_file_path, 'r').read()) + if autoscaling_policy: + print("Cartridge updated successfully") + else: + print("Error updating Cartridge") + except AuthenticationError as e: + self.perror("Authentication Error") + @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_remove_autoscaling_policy(self, autoscaling_policy_id , opts=None): - """Delete a cartridge""" + """Delete a autoscaling_policy""" try: if not autoscaling_policy_id: print("usage: remove-autoscaling-policy [application-id]") else: - cartridge_removed = Stratos.remove_autoscaling_policy(autoscaling_policy_id) - if cartridge_removed: + autoscaling_policy_removed = Stratos.remove_autoscaling_policy(autoscaling_policy_id) + if autoscaling_policy_removed: print("Successfully deleted Auto-scaling policy : "+autoscaling_policy_id) else: print("Auto-scaling policy not found : "+autoscaling_policy_id) @@ -472,6 +865,7 @@ def do_describe_kubernetes_cluster(self, kubernetes_cluster_id, opts=None): if not kubernetes_cluster: print("Kubernetes cluster not found") else: + print("Kubernetes cluster: "+kubernetes_cluster_id) PrintableJSON(kubernetes_cluster).pprint() @options([ @@ -520,13 +914,13 @@ def do_describe_kubernetes_master(self, kubernetes_cluster_id , opts=None): ]) @auth def do_remove_kubernetes_cluster(self, kubernetes_cluster_id, opts=None): - """Delete a cartridge""" + """Delete a kubernetes cluster""" try: if not kubernetes_cluster_id: print("usage: remove-kubernetes-cluster [cluster-id]") else: - cartridge_removed = Stratos.remove_autoscaling_policy(kubernetes_cluster_id) - if cartridge_removed: + kubernetes_cluster_removed = Stratos.remove_kubernetes_cluster(kubernetes_cluster_id) + if kubernetes_cluster_removed: print("Successfully un-deployed kubernetes cluster : "+kubernetes_cluster_id) else: print("Kubernetes cluster not found : "+kubernetes_cluster_id) @@ -535,46 +929,47 @@ def do_remove_kubernetes_cluster(self, kubernetes_cluster_id, opts=None): @options([ make_option('-u', '--username', type="str", help="Username of the user"), - make_option('-p', '--password', type="str", help="Password of the user") + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-c', '--cluster_id', type="str", help="Cluster id of Kubernets cluster"), + make_option('-o', '--host_id', type="str", help="Host id of Kubernets cluster") ]) @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() + def do_remove_kubernetes_host(self, line, opts=None): + """Delete a kubernetes host""" + try: + if not opts.cluster_id or not opts.host_id: + print("usage: remove-kubernetes-host [-c cluster-id] [-o host-id]") + else: + kubernetes_host_removed = Stratos.remove_kubernetes_host(opts.cluster_id, opts.host_id) + if kubernetes_host_removed: + print("Successfully un-deployed kubernetes host : "+opts.host_id) + else: + print("Kubernetes host not found : "+opts.cluster_id+"/"+opts.host_id) + except AuthenticationError as e: + self.perror("Authentication Error") + """ + # Domain Mapping + * list-domain-mappings + + """ @options([ make_option('-u', '--username', type="str", help="Username of the user"), - make_option('-p', '--password', type="str", help="Password of the user"), - make_option('-t', '--tenant_domain', type="str", help="Cluster ID") + make_option('-p', '--password', type="str", help="Password of the user") ]) @auth - def do_activate_tenant(self, line , opts=None): - """Retrieve detailed information on all Kubernetes-CoreOS Clusters.""" - if not opts.tenant_domain: - print("usage: list-kubernetes-hosts [-c ]") - return - kubernetes_cluster_hosts = Stratos.list_kubernetes_hosts(opts.cluster_id) - if not kubernetes_cluster_hosts: - print("No kubernetes hosts found") - else: - table = PrintableTable() - rows = [["Host ID", "Hostname", "Private IP Address", "Public IP Address"]] - for kubernetes_cluster_host in kubernetes_cluster_hosts: - rows.append([kubernetes_cluster_host['hostId'], kubernetes_cluster_host['hostname'], - kubernetes_cluster_host['privateIPAddress'], kubernetes_cluster_host['publicIPAddress']]) - table.add_rows(rows) - table.print_table() + def do_list_domain_mappings(self, application_id , opts=None): + """Illustrate the base class method use.""" + tenants = Stratos.list_domain_mappings(application_id) + table = PrintableTable() + rows = [["Domain", "Tenant ID", "Email", " State", "Created Date"]] + for tenant in tenants: + rows.append([tenant['tenantDomain'], tenant['tenantId'], tenant['email'], + "Active" if tenant['active'] else "De-Active", datetime.datetime.fromtimestamp(tenant['createdDate']/1000).strftime('%Y-%m-%d %H:%M:%S')]) + table.add_rows(rows) + table.print_table() + @options([]) def do_deploy_user(self, line , opts=None): @@ -599,4 +994,202 @@ def do_describe_application_signup(self, line , opts=None): if not application_signup: print("Application signup not found") else: - PrintableJSON(application_signup).pprint() \ No newline at end of file + PrintableJSON(application_signup).pprint() + + + + + + + + + + + + + + + + @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_remove_domain_mappings(self, domain , opts=None): + """Delete a specific user""" + try: + if not domain: + print("usage: remove-domain-mappings [domain]") + else: + domain_removed = Stratos.remove_domain_mappings(domain) + if domain_removed: + print("You have successfully deleted domain: "+domain) + else: + print("Could not delete domain: "+domain) + except AuthenticationError as e: + self.perror("Authentication Error") + + + @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_remove_application_signup(self, signup , opts=None): + """Delete a specific user""" + try: + if not signup: + print("usage: remove-application-signup [signup]") + else: + signup_removed = Stratos.remove_application_signup(signup) + if signup_removed: + print("You have successfully remove signup: "+signup) + else: + print("Could not delete application signup: "+signup) + except AuthenticationError as e: + self.perror("Authentication Error") + + @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_describe_application_runtime(self, line , opts=None): + """Retrieve details of a specific auto-scaling policy.""" + if not line.split(): + print("usage: describe-application-runtime [application-id]") + return + application_runtime = Stratos.describe_application_runtime(line) + if not application_runtime: + print("Application runtime not found") + else: + PrintableJSON(application_runtime).pprint() + + + @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_describe_application(self, line , opts=None): + """Retrieve details of a specific auto-scaling policy.""" + if not line.split(): + print("usage: describe-application [application-id]") + return + application = Stratos.describe_application(line) + if not application: + print("Application not found") + else: + PrintableJSON(application).pprint() + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_add_network_partition(self, line , opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: add-network-partition [-f ]") + else: + tenant = Stratos.add_network_partition(open(opts.json_file_path, 'r').read()) + if tenant: + print("Network partition added successfully") + else: + print("Error creating network partition") + except AuthenticationError as e: + self.perror("Authentication Error") + + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_add_kubernetes_cluster(self, line , opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: add-kubernetes-cluster [-f ]") + else: + tenant = Stratos.add_kubernetes_cluster(open(opts.json_file_path, 'r').read()) + if tenant: + print("Kubernertes cluster added successfully") + else: + print("Error creating network partition") + except AuthenticationError as e: + self.perror("Authentication Error") + + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_add_domain_mapping(self, application_id, opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: add-domain-mapping [-f ]") + else: + tenant = Stratos.add_domain_mapping(application_id, """{ + "domainMappings": [ + { + "cartridgeAlias": "tomcat", + "domainName": "agentmilindu.com", + "contextPath": "/abc/app" + } + ] +}""") + if tenant: + print(" Domain mapping added successfully") + else: + print("Error creating domain mapping") + except AuthenticationError as e: + self.perror("Authentication Error") + + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_add_deployment_policy(self, line , opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: add-deployment-policy [-f ]") + else: + deployment_policy = Stratos.add_deployment_policy(open(opts.json_file_path, 'r').read()) + if deployment_policy: + print("Deployment policy added successfully") + else: + print("Error creating deployment policy") + except AuthenticationError as e: + self.perror("Authentication Error") + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_add_autoscaling_policy(self, line , opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: add-autoscaling-policy [-f ]") + else: + autoscaling_policy = Stratos.add_autoscaling_policy(open(opts.json_file_path, 'r').read()) + if autoscaling_policy: + print("Autoscaling policy added successfully") + else: + print("Error adding autoscaling policy") + except AuthenticationError as e: + self.perror("Authentication Error") + 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 2555d4ca25..164833b111 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 @@ -16,6 +16,7 @@ # under the License. import requests +import json import Configs from cli.exceptions.AuthenticationError import AuthenticationError @@ -30,12 +31,13 @@ def __init__(self): # Users * list-users * add-users + * update-users * remove-user """ @staticmethod def list_users(): - return Stratos.get('users', error_message='No applications found') + return Stratos.get('users', error_message='No users found') @staticmethod def add_users(username, password, role_name, first_name, last_name, email, profile_name): @@ -47,7 +49,19 @@ def add_users(username, password, role_name, first_name, last_name, email, profi "lastName": last_name, "email": email } - return Stratos.post('users', data, error_message='No applications found') + return Stratos.post('users', json.dumps(data), error_message='No applications found') + + @staticmethod + def update_user(username, password, role_name, first_name, last_name, email, profile_name): + data = { + "userName": username, + "credential": password, + "role": role_name, + "firstName": first_name, + "lastName": last_name, + "email": email + } + return Stratos.put('users', json.dumps(data), error_message='No applications found') @staticmethod def remove_user(name): @@ -55,28 +69,81 @@ def remove_user(name): """ # Applications + * list-applications + * add-application + * remove-application """ @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() + return Stratos.get('applications', error_message='No applications found') + + @staticmethod + def remove_application(application): + return Stratos.delete('application/'+application) + + """ + # Tenants + * list-tenants + * list-tenants-by-partial-domain + * describe-tenant + * add-tenant + * activate-tenant + * deactivate-tenant + + """ + @staticmethod + def list_tenants(): + return Stratos.get('tenants', error_message='No cartridges found') + + @staticmethod + def list_tenants_by_partial_domain(partial_domain): + return Stratos.get('tenants/search/'+partial_domain, error_message='No cartridges found') + + @staticmethod + def describe_tenant(tenant_domain_name): + return Stratos.get('tenants/'+tenant_domain_name, error_message='No cartridge found') + + @staticmethod + def add_tenant(username, first_name, last_name, password, domain_name, email): + data = { + "admin": username, + "firstName": first_name, + "lastName": last_name, + "adminPassword": password, + "tenantDomain": domain_name, + "email": email, + "active": "true" + } + return Stratos.post('tenants', json.dumps(data), error_message='No tenant found') + + @staticmethod + def update_tenant(username, first_name, last_name, password, domain_name, email, tenant_id): + data = { + "tenantId": tenant_id, + "admin": username, + "firstName": first_name, + "lastName": last_name, + "adminPassword": password, + "tenantDomain": domain_name, + "email": email, + "active": "true" + } + return Stratos.put('tenants', json.dumps(data), error_message='No tenant found') + + @staticmethod + def activate_tenant(tenant_domain): + return Stratos.put('tenants/activate/'+tenant_domain, "", error_message='No tenant found') + + @staticmethod + def deactivate_tenant(tenant_domain): + return Stratos.put('tenants/deactivate/'+tenant_domain, "", error_message='No tenant found') """ # Cartridges * list-cartridges * describe-cartridge + * add-cartridge * remove-cartridges """ @@ -88,6 +155,14 @@ def list_cartridges(): def describe_cartridge(cartridge_type): return Stratos.get('cartridges/'+cartridge_type, error_message='No cartridge found') + @staticmethod + def add_cartridge(json): + return Stratos.post('cartridges', json, error_message='No cartridge found') + + @staticmethod + def update_cartridge(json): + return Stratos.put('cartridges', json, error_message='No cartridge found') + @staticmethod def remove_cartridge(cartridge_type): return Stratos.delete('cartridges/'+cartridge_type) @@ -96,6 +171,7 @@ def remove_cartridge(cartridge_type): # Cartridge groups * list-cartridge-groups * describe-cartridge-group + * update-cartridges-group * remove-cartridges-group """ @@ -108,6 +184,14 @@ def list_cartridge_groups(): def describe_cartridge_group(group_definition_name): return Stratos.get('cartridgeGroups/'+group_definition_name, error_message='No cartridge groups found') + @staticmethod + def add_cartridge_group(json): + return Stratos.post('cartridgeGroups', json, error_message='No cartridge group found') + + @staticmethod + def update_cartridge_group(json): + return Stratos.put('cartridgeGroups', json, error_message='No cartridge found') + @staticmethod def remove_cartridge_group(group_definition_name): return Stratos.delete('cartridgeGroups/'+group_definition_name) @@ -116,6 +200,7 @@ def remove_cartridge_group(group_definition_name): # Deployment Policy * list-deployment-policies * describe-deployment-policy + * update-deployment-policy * remove-deployment-policy """ @@ -128,6 +213,10 @@ def describe_deployment_policy(deployment_policy_name): return Stratos.get('deploymentPolicies/'+ deployment_policy_name, error_message='No deployment policies found') @staticmethod + def update_deployment_policy(json): + return Stratos.put('deploymentPolicies', json, error_message='No deployment policies found') + + @staticmethod def remove_deployment_policy(deployment_policy_id): return Stratos.delete('deploymentPolicies/'+deployment_policy_id) @@ -135,6 +224,7 @@ def remove_deployment_policy(deployment_policy_id): # Network partitions * list-network-partitions * describe-network-partition + * update-network-partition * remove-network-partition """ @@ -144,8 +234,12 @@ def list_network_partitions(): @staticmethod def describe_network_partition(network_partition_id): - return Stratos.get('networkPartitions/'+ network_partition_id, + return Stratos.get('networkPartitions/'+network_partition_id, error_message='No network partitions found') + @staticmethod + def update_network_partition(json): + return Stratos.put('networkPartitions', json, error_message='No cartridge found') + @staticmethod def remove_network_partition(network_partition_id): @@ -155,6 +249,7 @@ def remove_network_partition(network_partition_id): # Auto-scaling policies * list-autoscaling-policies * describe-autoscaling-policy + * update-autoscaling-policy * remove-autoscaling-policy """ @@ -192,16 +287,21 @@ def list_kubernetes_hosts(kubernetes_cluster_id): error_message='Kubernetes cluster not found') @staticmethod def describe_kubernetes_master(kubernetes_cluster_id): - return Stratos.get('kubernetesClusters/'+ kubernetes_cluster_id+'/master', - error_message='No kubernetes clusters found') + return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id+'/master', error_message='No kubernetes clusters found') @staticmethod + def remove_kubernetes_cluster(kubernetes_cluster_id): + return Stratos.delete('kubernetesClusters/'+kubernetes_cluster_id, + error_message="Autoscaling policy not found") + @staticmethod + def remove_kubernetes_host(kubernetes_cluster_id, host_id): + return Stratos.delete('kubernetesClusters/'+kubernetes_cluster_id+"/hosts/"+host_id, + error_message="Autoscaling policy not found") + @staticmethod def describe_application_signup(application_id): return Stratos.get('applications/'+ application_id + '/signup', error_message='No signup application found') - - """ # Utils @@ -210,7 +310,7 @@ def describe_application_signup(application_id): def get(resource, error_message): r = requests.get(Configs.stratos_api_url + resource, auth=(Configs.stratos_username, Configs.stratos_password), verify=False) - # print(r.text) + print(r.text) if r.status_code == 200: return r.json() elif r.status_code == 400: @@ -242,11 +342,36 @@ def delete(resource, error_message=None): @staticmethod def post(resource, data, error_message): - r = requests.post(Configs.stratos_api_url + resource, data, + headers = {'content-type': 'application/json'} + r = requests.post(Configs.stratos_api_url + resource, data, headers=headers, auth=(Configs.stratos_username, Configs.stratos_password), verify=False) + print(r) + print(r.text) + if r.status_code == 200: + return r.json() + elif r.status_code == 201: + return True + elif r.status_code == 400: + raise requests.HTTPError() + elif r.status_code == 401: + raise AuthenticationError() + elif r.status_code == 404: + if r.text and r.json() and r.json()['errorMessage'] == error_message: + return [] + else: + raise requests.HTTPError() + + @staticmethod + def put(resource, data, error_message): + headers = {'content-type': 'application/json'} + r = requests.put(Configs.stratos_api_url + resource, data, headers=headers, + auth=(Configs.stratos_username, Configs.stratos_password), verify=False) + print(r) print(r.text) if r.status_code == 200: return r.json() + elif r.status_code == 201: + return True elif r.status_code == 400: raise requests.HTTPError() elif r.status_code == 401: @@ -257,3 +382,44 @@ def post(resource, data, error_message): else: raise requests.HTTPError() + + #kithule is confused + + @staticmethod + def remove_domain_mappings(domain): + return Stratos.delete('domainMappings/'+domain) + + def remove_application_signup(signup): + return Stratos.delete('applicationSignup/'+signup) + + def describe_application_runtime(application): + return Stratos.get('applicationRuntime/'+application, error_message='No application runtime found') + + def describe_application(application): + return Stratos.get('application/'+application, error_message='No application found') + + @staticmethod + def add_network_partition(json): + return Stratos.post('networkPartitions', json, error_message='No network partition found') + + @staticmethod + def add_kubernetes_cluster(json): + return Stratos.post('kubernetesClusters', json, error_message='No kubernetes cluster found') + + @staticmethod + def add_domain_mapping(application_id, json): + return Stratos.post('applications/'+application_id+'/domainMappings', json, error_message='No domain mapping found') + + @staticmethod + def add_deployment_policy(json): + return Stratos.post('deploymentPolicies', json, error_message='No deployment policy found') + + @staticmethod + def add_autoscaling_policy(json): + return Stratos.post('autoscalingPolicies', json, error_message='No autoscaling policy found') + + @staticmethod + def add_application(json): + return Stratos.post('applications', json, error_message='No application found') + + From 77b4580be37eb5cd281719ffb4f82e54bd7220df Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Wed, 12 Aug 2015 07:20:03 +0530 Subject: [PATCH 59/79] Updated Kubernates commands and Applications commands --- .../src/main/python/cli/CLI.py | 173 +++++++++++------- .../src/main/python/cli/Stratos.py | 51 +++++- 2 files changed, 152 insertions(+), 72 deletions(-) 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 e9960f9161..8073d97449 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 @@ -140,6 +140,7 @@ def do_remove_user(self, name , opts=None): """ # Applications * list-applications + * describe-application * add-application * remove-application @@ -164,6 +165,24 @@ def do_list_applications(self, line , opts=None): 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_describe_application(self, application_id , opts=None): + """Retrieve detailed information on the master node in a specific Kubernetes-CoreOS group""" + if not application_id: + print("usage: describe-application [cluster-id]") + return + application = Stratos.describe_application(application_id) + if not application: + print("Application not found in : "+application_id) + else: + print("Application : "+application_id) + PrintableJSON(application).pprint() + @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user"), @@ -203,6 +222,53 @@ def do_remove_application(self, application , opts=None): except AuthenticationError as e: self.perror("Authentication Error") + """ + # Application deployment + * describe-application-runtime + * deploy-application + + """ + + @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_describe_application_runtime(self, application_id , opts=None): + """Retrieve details of a specific auto-scaling policy.""" + if not application_id: + print("usage: describe-application-runtime [application-id]") + return + application_runtime = Stratos.describe_application_runtime(application_id) + if not application_runtime: + print("Application runtime not found") + else: + print("Application : "+application_id) + PrintableJSON(application_runtime).pprint() + + """ + # Application signup + * describe-application-signup + + """ + @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_describe_application_signup(self, line , opts=None): + """Retrieve details of a specific auto-scaling policy.""" + if not line.split(): + print("usage: describe-application-signup [application-id]") + return + application_signup = Stratos.describe_application_signup(line) + if not application_signup: + print("Application signup not found") + else: + PrintableJSON(application_signup).pprint() + + + """ # Tenants * list-tenants @@ -797,9 +863,9 @@ def do_update_autoscaling_policy(self, line , opts=None): else: autoscaling_policy = Stratos.update_autoscaling_policy(open(opts.json_file_path, 'r').read()) if autoscaling_policy: - print("Cartridge updated successfully") + print("Autoscaling policy updated successfully:") else: - print("Error updating Cartridge") + print("Error updating Autoscaling policy") except AuthenticationError as e: self.perror("Authentication Error") @@ -828,6 +894,8 @@ def do_remove_autoscaling_policy(self, autoscaling_policy_id , opts=None): * describe-kubernetes-cluster * list-kubernetes-hosts * describe-kubernetes-master + * update-kubernetes-host + * update-kubernetes-master * remove-kubernetes-cluster * remove-kubernetes-host @@ -908,6 +976,47 @@ def do_describe_kubernetes_master(self, kubernetes_cluster_id , opts=None): print("Cluster : "+kubernetes_cluster_id) PrintableJSON(kubernetes_master).pprint() + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-c', '--cluster_id', type="str", help="Cluster id of the cluster"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_update_kubernetes_master(self, line , opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: update-kubernetes-master [-c ] [-p ]") + else: + cartridge = Stratos.update_kubernetes_master(opts.cluster_id, open(opts.json_file_path, 'r').read()) + if cartridge: + print("Kubernetes master updated successfully") + else: + print("Error updating Kubernetes master") + except AuthenticationError as e: + self.perror("Authentication Error") + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_update_kubernetes_host(self, line , opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: update-kubernetes-host [-f ]") + else: + cartridge = Stratos.update_kubernetes_host(open(opts.json_file_path, 'r').read()) + if cartridge: + print("Kubernetes host updated successfully") + else: + print("Error updating Kubernetes host") + except AuthenticationError as e: + self.perror("Authentication Error") + @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user") @@ -980,34 +1089,6 @@ def do_deploy_user(self, line , opts=None): except ValueError 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_describe_application_signup(self, line , opts=None): - """Retrieve details of a specific auto-scaling policy.""" - if not line.split(): - print("usage: describe-application-signup [application-id]") - return - application_signup = Stratos.describe_application_signup(line) - if not application_signup: - print("Application signup not found") - else: - PrintableJSON(application_signup).pprint() - - - - - - - - - - - - - @options([ @@ -1049,38 +1130,6 @@ def do_remove_application_signup(self, signup , opts=None): except AuthenticationError as e: self.perror("Authentication Error") - @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_describe_application_runtime(self, line , opts=None): - """Retrieve details of a specific auto-scaling policy.""" - if not line.split(): - print("usage: describe-application-runtime [application-id]") - return - application_runtime = Stratos.describe_application_runtime(line) - if not application_runtime: - print("Application runtime not found") - else: - PrintableJSON(application_runtime).pprint() - - - @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_describe_application(self, line , opts=None): - """Retrieve details of a specific auto-scaling policy.""" - if not line.split(): - print("usage: describe-application [application-id]") - return - application = Stratos.describe_application(line) - if not application: - print("Application not found") - else: - PrintableJSON(application).pprint() @options([ make_option('-u', '--username', type="str", help="Username of the user"), 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 164833b111..24358fb748 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 @@ -70,6 +70,7 @@ def remove_user(name): """ # Applications * list-applications + * describe-applications * add-application * remove-application @@ -78,11 +79,34 @@ def remove_user(name): def list_applications(): return Stratos.get('applications', error_message='No applications found') + @staticmethod + def describe_application(application_id): + return Stratos.get('applications/'+application_id, + error_message='No application found') + @staticmethod def remove_application(application): return Stratos.delete('application/'+application) """ + # Application deployment + * describe-application-runtime + + """ + @staticmethod + def describe_application_runtime(application_id): + return Stratos.get('applications/'+application_id+"/runtime", error_message='No application runtime found') + + """ + # Application signup + * describe-application-signup + + """ + @staticmethod + def describe_application_signup(application_id): + return Stratos.get('applications/'+application_id + '/signup', + error_message='No signup application found') + """ # Tenants * list-tenants * list-tenants-by-partial-domain @@ -262,6 +286,10 @@ def describe_autoscaling_policy(autoscaling_policy_id): return Stratos.get('autoscalingPolicies/'+autoscaling_policy_id, error_message='No autoscaling policy found') @staticmethod + def update_autoscaling_policy(json): + return Stratos.put('autoscalingPolicies', json, error_message='No cartridge found') + + @staticmethod def remove_autoscaling_policy(autoscaling_policy_id): return Stratos.delete('autoscalingPolicies/'+autoscaling_policy_id, error_message="Autoscaling policy not found") @@ -271,6 +299,10 @@ def remove_autoscaling_policy(autoscaling_policy_id): * describe-kubernetes-cluster * list-kubernetes-hosts * describe-kubernetes-master + * update-kubernetes-host + * update-kubernetes-master + * remove-kubernetes-cluster + * remove-kubernetes-host """ @staticmethod @@ -289,6 +321,14 @@ def list_kubernetes_hosts(kubernetes_cluster_id): def describe_kubernetes_master(kubernetes_cluster_id): return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id+'/master', error_message='No kubernetes clusters found') + @staticmethod + def update_kubernetes_master(cluster_id, json): + return Stratos.put('kubernetesClusters/'+cluster_id+'/master', json, error_message='No cartridge found') + + @staticmethod + def update_kubernetes_host(json): + return Stratos.put('kubernetesClusters/update/host/', json, error_message='No cartridge found') + @staticmethod def remove_kubernetes_cluster(kubernetes_cluster_id): return Stratos.delete('kubernetesClusters/'+kubernetes_cluster_id, @@ -297,10 +337,7 @@ def remove_kubernetes_cluster(kubernetes_cluster_id): def remove_kubernetes_host(kubernetes_cluster_id, host_id): return Stratos.delete('kubernetesClusters/'+kubernetes_cluster_id+"/hosts/"+host_id, error_message="Autoscaling policy not found") - @staticmethod - def describe_application_signup(application_id): - return Stratos.get('applications/'+ application_id + '/signup', - error_message='No signup application found') + """ # Utils @@ -392,12 +429,6 @@ def remove_domain_mappings(domain): def remove_application_signup(signup): return Stratos.delete('applicationSignup/'+signup) - def describe_application_runtime(application): - return Stratos.get('applicationRuntime/'+application, error_message='No application runtime found') - - def describe_application(application): - return Stratos.get('application/'+application, error_message='No application found') - @staticmethod def add_network_partition(json): return Stratos.post('networkPartitions', json, error_message='No network partition found') From f1aadea7e66cb4c0fa52b8df7ef6478c5e9d7efa Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sun, 16 Aug 2015 22:50:19 +0530 Subject: [PATCH 60/79] Re-organized in to proper groups --- .../src/main/python/cli/CLI.py | 467 ++++++++++++------ .../src/main/python/cli/Stratos.py | 168 +++++-- 2 files changed, 425 insertions(+), 210 deletions(-) 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 8073d97449..bf01e54f64 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 @@ -58,7 +58,7 @@ def completenames(self, text, *ignored): ]) @auth def do_list_users(self, line , opts=None): - """Illustrate the base class method use.""" + """List the users of Stratos""" try: users = Stratos.list_users() table = PrintableTable() @@ -81,16 +81,20 @@ def do_list_users(self, line , opts=None): make_option('-e', '--email', type="str", help="Email of the user"), make_option('-o', '--profile_name', type="str", help="Profile name of the user") ]) - @auth def do_add_user(self, line , opts=None): """Add a new user to the system""" try: - user = Stratos.add_users(opts.username_user, opts.password_user, opts.role_name, opts.first_name, opts.last_name, - opts.email, opts.profile_name) - if user: - print("User successfully created") + if not opts.username_user or not opts.password_user: + print("usage: add-user [-s ] [-a ] [-r ] [-e ] [-f ]" + + " [-l ] [-o ]") + return else: - print("Error creating the user") + user = Stratos.add_users(opts.username_user, opts.password_user, opts.role_name, opts.first_name, opts.last_name, + opts.email, opts.profile_name) + if user: + print("User successfully created") + else: + print("Error creating the user") except AuthenticationError as e: self.perror("Authentication Error") @@ -130,6 +134,7 @@ def do_remove_user(self, name , opts=None): print("usage: remove-user [username]") else: user_removed = Stratos.remove_user(name) + print(user_removed) if user_removed: print("You have successfully deleted user: "+name) else: @@ -160,7 +165,6 @@ def do_list_applications(self, line , opts=None): table = PrintableTable() rows = [["Application ID", "Alias", "Status"]] for application in applications: - PrintableJSON(application).pprint() rows.append([application['applicationId'], application['alias'], application['status']]) table.add_rows(rows) table.print_table() @@ -222,6 +226,28 @@ def do_remove_application(self, application , opts=None): except AuthenticationError as e: self.perror("Authentication Error") + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-a', '--application_id', type="str", help="Unique ID of the application"), + make_option('-o', '--application_policy_id', type="str", help="Unique ID of the application policy") + ]) + @auth + def do_deploy_application(self, line , opts=None): + """Deploy an application.""" + try: + if not opts.application_id or not opts.application_policy_id: + print("usage: deploy-application [-a ] [-o ]") + else: + application_removed = Stratos.deploy_application(opts.application_id, opts.application_policy_id) + if application_removed: + print("You have successfully deployed application: "+opts.application_id) + else: + print("Could not deployed application : "+opts.application_id) + except AuthenticationError as e: + self.perror("Authentication Error") + """ # Application deployment * describe-application-runtime @@ -249,6 +275,8 @@ def do_describe_application_runtime(self, application_id , opts=None): """ # Application signup * describe-application-signup + * add-application-signup + * remove-application-signup """ @options([ @@ -256,17 +284,55 @@ def do_describe_application_runtime(self, application_id , opts=None): make_option('-p', '--password', type="str", help="Password of the user") ]) @auth - def do_describe_application_signup(self, line , opts=None): - """Retrieve details of a specific auto-scaling policy.""" - if not line.split(): + def do_describe_application_signup(self, application_id , opts=None): + """Retrieve details of a specific application signup.""" + if not application_id: print("usage: describe-application-signup [application-id]") return - application_signup = Stratos.describe_application_signup(line) + application_signup = Stratos.describe_application_signup(application_id) if not application_signup: print("Application signup not found") else: PrintableJSON(application_signup).pprint() + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_add_application_signup(self, application_id, opts=None): + """Add a new application signup to the system""" + try: + if not opts.json_file_path: + print("usage: add-application-signup [-f ] [application_id]") + else: + application_signup = Stratos.add_application_signup(application_id, open(opts.json_file_path, 'r').read()) + if application_signup: + print("Application signup added successfully") + else: + print("Error creating application signup") + except AuthenticationError as e: + self.perror("Authentication Error") + + @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_remove_application_signup(self, signup , opts=None): + """Delete a specific user""" + try: + if not signup: + print("usage: remove-application-signup [signup]") + else: + signup_removed = Stratos.remove_application_signup(signup) + if signup_removed: + print("You have successfully remove signup: "+signup) + else: + print("Could not delete application signup: "+signup) + except AuthenticationError as e: + self.perror("Authentication Error") """ @@ -445,7 +511,7 @@ def do_list_cartridges(self, line , opts=None): 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']]) + cartridge['version'], "True" if cartridge['multiTenant'] == 1 else "False"]) table.add_rows(rows) table.print_table() @@ -485,7 +551,10 @@ def do_describe_cartridge(self, cartridge_type , opts=None): ]) @auth def do_add_cartridge(self, line , opts=None): - """Add a new user to the system""" + """ + Add a new cartridge to the system + Usage: add-cartridge [-f ] + """ try: if not opts.json_file_path: print("usage: add-cartridge [-f ]") @@ -496,7 +565,7 @@ def do_add_cartridge(self, line , opts=None): else: print("Error adding Cartridge") except AuthenticationError as e: - self.perror("Authentication Error") + print("Authentication Error") @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -646,6 +715,7 @@ def do_remove_cartridge_group(self, group_definition_name , opts=None): # Deployment Policies * list-deployment-policies * describe-deployment-policy + * add-deployment-policy * update-deployment-policy * remove-deployment-policy @@ -692,7 +762,27 @@ def do_describe_deployment_policy(self, line , opts=None): make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") ]) @auth - def do_update_deployment_policy(self, line , opts=None): + def do_add_deployment_policy(self, line , opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: add-deployment-policy [-f ]") + else: + deployment_policy = Stratos.add_deployment_policy(open(opts.json_file_path, 'r').read()) + if deployment_policy: + print("Deployment policy added successfully") + else: + print("Error creating deployment policy") + except AuthenticationError as e: + self.perror("Authentication Error") + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_update_application_policy(self, line , opts=None): """Add a new user to the system""" try: if not opts.json_file_path: @@ -725,12 +815,117 @@ def do_remove_deployment_policy(self, deployment_policy_id , opts=None): except AuthenticationError as e: self.perror("Authentication Error") + """ + # Deployment Policies + * list-application-policies + * describe-application-policy + * update-application-policy + * remove-application-policy + + """ + + @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_application_policies(self, line , opts=None): + """Illustrate the base class method use.""" + application_policies = Stratos.list_application_policies() + if not application_policies: + print("No application policies found") + else: + table = PrintableTable() + rows = [["Id", "Accessibility"]] + for application_policy in application_policies: + rows.append([application_policy['id'], len(application_policy['networkPartitions'])]) + 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_describe_application_policy(self, line , opts=None): + """Retrieve details of a specific application policy.""" + if not line.split(): + print("usage: describe-application-policy [application-policy-id]") + return + application_policy = Stratos.describe_application_policy(line) + if not application_policy: + print("Deployment policy not found") + else: + PrintableJSON(application_policy).pprint() + + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_add_application_policy(self, line , opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: add-application-policy [-f ]") + else: + application_policy = Stratos.add_application_policy(open(opts.json_file_path, 'r').read()) + if application_policy: + print("Deployment policy added successfully") + else: + print("Error creating application policy") + except AuthenticationError as e: + self.perror("Authentication Error") + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_update_application_policy(self, line , opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: update-application-policy [-f ]") + else: + cartridge = Stratos.update_application_policy(open(opts.json_file_path, 'r').read()) + if cartridge: + print("Deployment policy updated successfully") + else: + print("Error updating Deployment policy") + except AuthenticationError as e: + self.perror("Authentication Error") + + @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_remove_application_policy(self, application_policy_id , opts=None): + """Delete a cartridge""" + try: + if not application_policy_id: + print("usage: remove-application-policy [application-policy-id]") + else: + cartridge_removed = Stratos.remove_application_policy(application_policy_id) + if cartridge_removed: + print("Successfully deleted application policy : "+application_policy_id) + else: + print("Could not deleted application policy : "+application_policy_id) + except AuthenticationError as e: + self.perror("Authentication Error") + + """ # Network Partitions - * list-deployment-policies - * describe-deployment-policy - * update-deployment-policy - * remove-deployment-policy + * list-network-partitions + * describe-network-partition + * add-network-partition + * update-network-partition + * remove-network-partition """ @@ -766,6 +961,26 @@ def do_describe_network_partition(self, network_partition_id , opts=None): print("Partition: "+network_partition_id) PrintableJSON(deployment_policy).pprint() + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_add_network_partition(self, line , opts=None): + """Add a new user to the system""" + try: + if not opts.json_file_path: + print("usage: add-network-partition [-f ]") + else: + tenant = Stratos.add_network_partition(open(opts.json_file_path, 'r').read()) + if tenant: + print("Network partition added successfully") + else: + print("Error creating network partition") + except AuthenticationError as e: + self.perror("Authentication Error") + @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user"), @@ -892,8 +1107,10 @@ def do_remove_autoscaling_policy(self, autoscaling_policy_id , opts=None): # Kubernetes clusters/hosts * list-kubernetes-clusters * describe-kubernetes-cluster - * list-kubernetes-hosts * describe-kubernetes-master + * add-kubernetes-cluster + * add-kubernetes-host + * list-kubernetes-hosts * update-kubernetes-host * update-kubernetes-master * remove-kubernetes-cluster @@ -936,12 +1153,48 @@ def do_describe_kubernetes_cluster(self, kubernetes_cluster_id, opts=None): print("Kubernetes cluster: "+kubernetes_cluster_id) PrintableJSON(kubernetes_cluster).pprint() + @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_describe_kubernetes_master(self, kubernetes_cluster_id , opts=None): + """Retrieve detailed information on the master node in a specific Kubernetes-CoreOS group""" + if not kubernetes_cluster_id: + print("usage: describe-kubernetes-master [cluster-id]") + return + kubernetes_master = Stratos.describe_kubernetes_master(kubernetes_cluster_id) + if not kubernetes_master: + print("Kubernetes master not found in : "+kubernetes_cluster_id) + else: + print("Cluster : "+kubernetes_cluster_id) + PrintableJSON(kubernetes_master).pprint() + @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user"), - make_option('-c', '--cluster_id', type="str", help="Cluster ID") + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") ]) @auth + def do_add_kubernetes_host(self, kubernetes_cluster_id , opts=None): + """Add a host (slave) to an existing Kubernetes-CoreOS Cluster.""" + try: + if not kubernetes_cluster_id or not opts.json_file_path: + print("usage: add-kubernetes-cluster [-f ] [kubernetes cluster id]") + else: + kubernetes_host = Stratos.add_kubernetes_host(kubernetes_cluster_id, open(opts.json_file_path, 'r').read()) + if kubernetes_host: + print("You have successfully deployed host to Kubernetes cluster: "+kubernetes_cluster_id) + else: + print("Error deploying host to Kubernetes cluster: "+kubernetes_cluster_id) + except AuthenticationError as e: + self.perror("Authentication Error") + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-c', '--cluster_id', type="str", help="Cluster ID") + ]) def do_list_kubernetes_hosts(self, line , opts=None): """Retrieve detailed information on all Kubernetes-CoreOS Clusters.""" if not opts.cluster_id: @@ -959,23 +1212,6 @@ def do_list_kubernetes_hosts(self, line , opts=None): 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_describe_kubernetes_master(self, kubernetes_cluster_id , opts=None): - """Retrieve detailed information on the master node in a specific Kubernetes-CoreOS group""" - if not kubernetes_cluster_id: - print("usage: describe-kubernetes-master [cluster-id]") - return - kubernetes_master = Stratos.describe_kubernetes_master(kubernetes_cluster_id) - if not kubernetes_master: - print("Kubernetes master not found in : "+kubernetes_cluster_id) - else: - print("Cluster : "+kubernetes_cluster_id) - PrintableJSON(kubernetes_master).pprint() - @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user"), @@ -1011,7 +1247,8 @@ def do_update_kubernetes_host(self, line , opts=None): else: cartridge = Stratos.update_kubernetes_host(open(opts.json_file_path, 'r').read()) if cartridge: - print("Kubernetes host updated successfully") + print(cartridge) + print("You have succesfully updated host to Kubernetes cluster") else: print("Error updating Kubernetes host") except AuthenticationError as e: @@ -1061,6 +1298,8 @@ def do_remove_kubernetes_host(self, line, opts=None): """ # Domain Mapping * list-domain-mappings + * add-domain-mapping + * remove-domain-mapping """ @options([ @@ -1070,107 +1309,20 @@ def do_remove_kubernetes_host(self, line, opts=None): @auth def do_list_domain_mappings(self, application_id , opts=None): """Illustrate the base class method use.""" - tenants = Stratos.list_domain_mappings(application_id) - table = PrintableTable() - rows = [["Domain", "Tenant ID", "Email", " State", "Created Date"]] - for tenant in tenants: - rows.append([tenant['tenantDomain'], tenant['tenantId'], tenant['email'], - "Active" if tenant['active'] else "De-Active", datetime.datetime.fromtimestamp(tenant['createdDate']/1000).strftime('%Y-%m-%d %H:%M:%S')]) - 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") - try: - Stratos.deploy_user() - except ValueError 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_remove_domain_mappings(self, domain , opts=None): - """Delete a specific user""" - try: - if not domain: - print("usage: remove-domain-mappings [domain]") - else: - domain_removed = Stratos.remove_domain_mappings(domain) - if domain_removed: - print("You have successfully deleted domain: "+domain) - else: - print("Could not delete domain: "+domain) - except AuthenticationError as e: - self.perror("Authentication Error") - - - @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_remove_application_signup(self, signup , opts=None): - """Delete a specific user""" - try: - if not signup: - print("usage: remove-application-signup [signup]") - else: - signup_removed = Stratos.remove_application_signup(signup) - if signup_removed: - print("You have successfully remove signup: "+signup) - else: - print("Could not delete application signup: "+signup) - except AuthenticationError as e: - self.perror("Authentication Error") - - - @options([ - make_option('-u', '--username', type="str", help="Username of the user"), - make_option('-p', '--password', type="str", help="Password of the user"), - make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") - ]) - @auth - def do_add_network_partition(self, line , opts=None): - """Add a new user to the system""" - try: - if not opts.json_file_path: - print("usage: add-network-partition [-f ]") - else: - tenant = Stratos.add_network_partition(open(opts.json_file_path, 'r').read()) - if tenant: - print("Network partition added successfully") - else: - print("Error creating network partition") - except AuthenticationError as e: - self.perror("Authentication Error") - - - @options([ - make_option('-u', '--username', type="str", help="Username of the user"), - make_option('-p', '--password', type="str", help="Password of the user"), - make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") - ]) - @auth - def do_add_kubernetes_cluster(self, line , opts=None): - """Add a new user to the system""" - try: - if not opts.json_file_path: - print("usage: add-kubernetes-cluster [-f ]") + if not application_id: + print("usage: list-domain-mappings [application-id]") + else: + domain_mappings = Stratos.list_domain_mappings(application_id) + if domain_mappings: + table = PrintableTable() + rows = [["Domain", "Tenant ID", "Email", " State", "Created Date"]] + for domain_mapping in domain_mappings: + rows.append([domain_mapping['domain_mappingsDomain'], domain_mapping['domain_mappingId'], domain_mapping['email'], + "Active" if domain_mapping['active'] else "De-Active", datetime.datetime.fromtimestamp(domain_mapping['createdDate']/1000).strftime('%Y-%m-%d %H:%M:%S')]) + table.add_rows(rows) + table.print_table() else: - tenant = Stratos.add_kubernetes_cluster(open(opts.json_file_path, 'r').read()) - if tenant: - print("Kubernertes cluster added successfully") - else: - print("Error creating network partition") - except AuthenticationError as e: - self.perror("Authentication Error") + print("No domain mappings found in application: "+application_id) @options([ @@ -1182,19 +1334,11 @@ def do_add_kubernetes_cluster(self, line , opts=None): def do_add_domain_mapping(self, application_id, opts=None): """Add a new user to the system""" try: - if not opts.json_file_path: - print("usage: add-domain-mapping [-f ]") + if not application_id or not opts.json_file_path: + print("usage: add-domain-mapping [-f ] [application id]") else: - tenant = Stratos.add_domain_mapping(application_id, """{ - "domainMappings": [ - { - "cartridgeAlias": "tomcat", - "domainName": "agentmilindu.com", - "contextPath": "/abc/app" - } - ] -}""") - if tenant: + domain_mapping = Stratos.add_domain_mapping(application_id, open(opts.json_file_path, 'r').read()) + if domain_mapping: print(" Domain mapping added successfully") else: print("Error creating domain mapping") @@ -1204,21 +1348,20 @@ def do_add_domain_mapping(self, application_id, opts=None): @options([ make_option('-u', '--username', type="str", help="Username of the user"), - make_option('-p', '--password', type="str", help="Password of the user"), - make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + make_option('-p', '--password', type="str", help="Password of the user") ]) @auth - def do_add_deployment_policy(self, line , opts=None): - """Add a new user to the system""" + def do_remove_domain_mappings(self, domain , opts=None): + """Delete a specific user""" try: - if not opts.json_file_path: - print("usage: add-deployment-policy [-f ]") + if not domain: + print("usage: remove-domain-mappings [domain]") else: - deployment_policy = Stratos.add_deployment_policy(open(opts.json_file_path, 'r').read()) - if deployment_policy: - print("Deployment policy added successfully") + domain_removed = Stratos.remove_domain_mappings(domain) + if domain_removed: + print("You have successfully deleted domain: "+domain) else: - print("Error creating deployment policy") + print("Could not delete domain: "+domain) except AuthenticationError as e: self.perror("Authentication Error") 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 24358fb748..4fcbecb648 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 @@ -18,6 +18,7 @@ import requests import json import Configs +from Logging import logging from cli.exceptions.AuthenticationError import AuthenticationError @@ -65,7 +66,7 @@ def update_user(username, password, role_name, first_name, last_name, email, pro @staticmethod def remove_user(name): - return Stratos.delete('users/'+name) + return Stratos.delete('users/'+name, error_message="Requested user "+name+" does not exist") """ # Applications @@ -73,6 +74,8 @@ def remove_user(name): * describe-applications * add-application * remove-application + * deploy-application + * describe-application-runtime """ @staticmethod @@ -86,13 +89,12 @@ def describe_application(application_id): @staticmethod def remove_application(application): - return Stratos.delete('application/'+application) - - """ - # Application deployment - * describe-application-runtime + return Stratos.delete('applications/'+application) - """ + @staticmethod + def deploy_application(application_id, application_policy_id): + return Stratos.post('applications/'+application_id+'/deploy/'+application_policy_id, None, + error_message='No application found') @staticmethod def describe_application_runtime(application_id): return Stratos.get('applications/'+application_id+"/runtime", error_message='No application runtime found') @@ -100,12 +102,21 @@ def describe_application_runtime(application_id): """ # Application signup * describe-application-signup + * remove-application-signup """ @staticmethod def describe_application_signup(application_id): - return Stratos.get('applications/'+application_id + '/signup', + return Stratos.get('applications/'+application_id+'/signup', error_message='No signup application found') + @staticmethod + def add_application_signup(application_id, json): + return Stratos.post('applications/'+application_id+"/runtime", json, error_message='No application runtime found') + + @staticmethod + def remove_application_signup(application_id): + return Stratos.delete('applications/'+application_id + '/signup') + """ # Tenants * list-tenants @@ -177,7 +188,7 @@ def list_cartridges(): @staticmethod def describe_cartridge(cartridge_type): - return Stratos.get('cartridges/'+cartridge_type, error_message='No cartridge found') + return Stratos.get('cartridges/'+cartridge_type, error_message='Cartridge not found') @staticmethod def add_cartridge(json): @@ -224,6 +235,7 @@ def remove_cartridge_group(group_definition_name): # Deployment Policy * list-deployment-policies * describe-deployment-policy + * add-deployment-policy * update-deployment-policy * remove-deployment-policy @@ -237,6 +249,10 @@ def describe_deployment_policy(deployment_policy_name): return Stratos.get('deploymentPolicies/'+ deployment_policy_name, error_message='No deployment policies found') @staticmethod + def add_deployment_policy(json): + return Stratos.post('deploymentPolicies', json, error_message='No deployment policy found') + + @staticmethod def update_deployment_policy(json): return Stratos.put('deploymentPolicies', json, error_message='No deployment policies found') @@ -244,10 +260,35 @@ def update_deployment_policy(json): def remove_deployment_policy(deployment_policy_id): return Stratos.delete('deploymentPolicies/'+deployment_policy_id) + """ + # Application Policy + * list-application-policies + * describe-application-policy + * update-application-policy + * remove-application-policy + + """ + @staticmethod + def list_application_policies(): + return Stratos.get('applicationPolicies', + error_message='Deployment policies not found') + @staticmethod + def describe_application_policy(application_policy_name): + return Stratos.get('applicationPolicies/'+ application_policy_name, + error_message='No application policies found') + @staticmethod + def update_application_policy(json): + return Stratos.put('applicationPolicies', json, error_message='No application policies found') + + @staticmethod + def remove_application_policy(application_policy_id): + return Stratos.delete('applicationPolicies/'+application_policy_id) + """ # Network partitions * list-network-partitions * describe-network-partition + * add-network-partition * update-network-partition * remove-network-partition @@ -261,6 +302,9 @@ def describe_network_partition(network_partition_id): return Stratos.get('networkPartitions/'+network_partition_id, error_message='No network partitions found') @staticmethod + def add_network_partition(json): + return Stratos.post('networkPartitions', json, error_message='No network partition found') + @staticmethod def update_network_partition(json): return Stratos.put('networkPartitions', json, error_message='No cartridge found') @@ -297,8 +341,9 @@ def remove_autoscaling_policy(autoscaling_policy_id): # Kubernetes clusters/hosts * list-kubernetes-clusters * describe-kubernetes-cluster - * list-kubernetes-hosts * describe-kubernetes-master + * add-kubernetes-cluster + * list-kubernetes-hosts * update-kubernetes-host * update-kubernetes-master * remove-kubernetes-cluster @@ -311,23 +356,32 @@ def list_kubernetes_clusters(): @staticmethod def describe_kubernetes_cluster(kubernetes_cluster_id): - return Stratos.get('kubernetesClusters/'+ kubernetes_cluster_id, - error_message='No kubernetes clusters found') - @staticmethod - def list_kubernetes_hosts(kubernetes_cluster_id): - return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id+'/hosts', + return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id, error_message='Kubernetes cluster not found') @staticmethod def describe_kubernetes_master(kubernetes_cluster_id): return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id+'/master', error_message='No kubernetes clusters found') + @staticmethod + def add_kubernetes_cluster(json): + return Stratos.post('kubernetesClusters', json, error_message='No kubernetes cluster found') + + @staticmethod + def add_kubernetes_host(kubernetes_cluster_id, json): + return Stratos.post('kubernetesClusters/'+kubernetes_cluster_id+'/minion', json, error_message='No kubernetes cluster found') + + @staticmethod + def list_kubernetes_hosts(kubernetes_cluster_id): + return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id+'/hosts', + error_message='Kubernetes cluster not found') + @staticmethod def update_kubernetes_master(cluster_id, json): return Stratos.put('kubernetesClusters/'+cluster_id+'/master', json, error_message='No cartridge found') @staticmethod def update_kubernetes_host(json): - return Stratos.put('kubernetesClusters/update/host/', json, error_message='No cartridge found') + return Stratos.put('kubernetesClusters/update/host', json, error_message='No cartridge found') @staticmethod def remove_kubernetes_cluster(kubernetes_cluster_id): @@ -338,15 +392,46 @@ def remove_kubernetes_host(kubernetes_cluster_id, host_id): return Stratos.delete('kubernetesClusters/'+kubernetes_cluster_id+"/hosts/"+host_id, error_message="Autoscaling policy not found") + """ + # Domain Mapping + * list-domain-mappings + * add-domain-mapping + * remove-domain-mapping + + """ + + @staticmethod + def list_domain_mappings(application_id): + return Stratos.get('applications/'+application_id+'/domainMappings', + error_message='No domain mapping found') + + @staticmethod + def remove_domain_mappings(application_id): + return Stratos.delete('applications/'+application_id+'/domainMappings') + + @staticmethod + def add_domain_mapping(application_id, json): + return Stratos.post('applications/'+application_id+'/domainMappings', json, + error_message='No domain mapping found') """ # Utils """ + + @staticmethod + def authenticate(): + try: + Stratos.get('init') + return True + except AuthenticationError as e: + return False + @staticmethod - def get(resource, error_message): + def get(resource, error_message=""): r = requests.get(Configs.stratos_api_url + resource, auth=(Configs.stratos_username, Configs.stratos_password), verify=False) + print(r) print(r.text) if r.status_code == 200: return r.json() @@ -355,15 +440,18 @@ def get(resource, error_message): elif r.status_code == 401: raise AuthenticationError() elif r.status_code == 404: - if r.text and r.json() and r.json()['errorMessage'] == error_message: + if r.text and r.json() and r.json()['message'] == error_message: return False else: raise requests.HTTPError() + else: + logging.error(r.status_code+" : "+r.text) @staticmethod def delete(resource, error_message=None): r = requests.delete(Configs.stratos_api_url + resource, auth=(Configs.stratos_username, Configs.stratos_password), verify=False) + print(r) print(r.text) if r.status_code == 200: return r.json() @@ -372,13 +460,18 @@ def delete(resource, error_message=None): elif r.status_code == 401: raise AuthenticationError() elif r.status_code == 404: - if r.text and r.json() and r.json()['errorMessage'] == error_message: - return [] + if r.text and r.json() and r.json()['message']: + return False + elif r.status_code == 404: + if r.text and r.json() and r.json()['message']: + return False else: raise requests.HTTPError() + else: + logging.error(r.status_code+" : "+r.text) @staticmethod - def post(resource, data, error_message): + def post(resource, data, error_message=None): headers = {'content-type': 'application/json'} r = requests.post(Configs.stratos_api_url + resource, data, headers=headers, auth=(Configs.stratos_username, Configs.stratos_password), verify=False) @@ -393,10 +486,12 @@ def post(resource, data, error_message): elif r.status_code == 401: raise AuthenticationError() elif r.status_code == 404: - if r.text and r.json() and r.json()['errorMessage'] == error_message: + if r.text and r.json() and r.json()['message'] == error_message: return [] else: raise requests.HTTPError() + else: + logging.error(r.status_code+" : "+r.text) @staticmethod def put(resource, data, error_message): @@ -414,37 +509,14 @@ def put(resource, data, error_message): elif r.status_code == 401: raise AuthenticationError() elif r.status_code == 404: - if r.text and r.json() and r.json()['errorMessage'] == error_message: + if r.text and r.json() and r.json()['message'] == error_message: return [] else: raise requests.HTTPError() + else: + logging.error(r.status_code+" : "+r.text) - #kithule is confused - - @staticmethod - def remove_domain_mappings(domain): - return Stratos.delete('domainMappings/'+domain) - - def remove_application_signup(signup): - return Stratos.delete('applicationSignup/'+signup) - - @staticmethod - def add_network_partition(json): - return Stratos.post('networkPartitions', json, error_message='No network partition found') - - @staticmethod - def add_kubernetes_cluster(json): - return Stratos.post('kubernetesClusters', json, error_message='No kubernetes cluster found') - - @staticmethod - def add_domain_mapping(application_id, json): - return Stratos.post('applications/'+application_id+'/domainMappings', json, error_message='No domain mapping found') - - @staticmethod - def add_deployment_policy(json): - return Stratos.post('deploymentPolicies', json, error_message='No deployment policy found') - @staticmethod def add_autoscaling_policy(json): return Stratos.post('autoscalingPolicies', json, error_message='No autoscaling policy found') From 20a7a8c0fc0aa0b6875fad3125b73185743693c4 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sun, 16 Aug 2015 22:50:41 +0530 Subject: [PATCH 61/79] Better prompt_for_credentials() --- .../src/main/python/cli/Main.py | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py index 78b3572851..e826b07a14 100755 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py @@ -22,6 +22,8 @@ import Configs # Fix Python 2.x. +from cli.Stratos import Stratos + try: input = raw_input except NameError: @@ -30,13 +32,18 @@ def prompt_for_credentials(): """Prompt for user credentials""" - while True: - _username = input("Username: ") - _password = getpass.getpass("Password: ") - if _username is not "" and _password is not "": - Configs.stratos_username = _username - Configs.stratos_password = _password - break + while Configs.stratos_username is "" or Configs.stratos_password is "": + if Configs.stratos_username is "": + Configs.stratos_username = input("Username: ") + + if Configs.stratos_password is "": + Configs.stratos_password = getpass.getpass("Password: ") + if Stratos.authenticate(): + print("Successfully authenticated") + else: + print("Could not authenticate") + exit() + def main(): From 6e1b56588113872169d29cbaeb23d25c3386dfe1 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sun, 16 Aug 2015 22:50:57 +0530 Subject: [PATCH 62/79] Fixed doc string issue --- .../src/main/python/cli/Utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py index 4f64873ba1..9510273a37 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Utils.py @@ -83,7 +83,6 @@ def pprint(self): def auth(func): """Authenticate""" def auth_inner(self, *args, **kwargs): - if len(args) > 1 and hasattr(args[1], 'username') and args[1].username is not None: Configs.stratos_username = args[1].username if len(args) > 1 and hasattr(args[1], 'password') and args[1].password is not None: @@ -94,4 +93,7 @@ def auth_inner(self, *args, **kwargs): logging.warning("Pre authentication failed. Some authentication details are missing") else: return func(self, *args, **kwargs) + auth_inner.__name__ = func.__name__.replace('_', '-') + auth_inner.__doc__ = func.__doc__ return auth_inner + From 11d4cec1abf48d380ef8709f7b48071baca5d886 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sun, 16 Aug 2015 22:51:23 +0530 Subject: [PATCH 63/79] ignoring test output dirs --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 5fe1c804ad..355fa34e7a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/components/org.apache.stratos.python.cli/src/main/python/tests/scratch/.scripttest-test-dir.txt # 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 From 3868c5118c7e2fd4d820f415a6a7d8b6752c6be2 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sun, 16 Aug 2015 22:51:40 +0530 Subject: [PATCH 64/79] Better Exception handling --- .../src/main/python/cli/exceptions/AuthenticationError.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py b/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py index 2d68bd7d0a..5bcd5d8ac1 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py @@ -16,7 +16,7 @@ # under the License. class AuthenticationError(Exception): - def __init__(self): + def __init__(self, message=""): pass def __str__(self): From 736626c8abe0e442774eb910045720330448f796 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sun, 16 Aug 2015 22:51:54 +0530 Subject: [PATCH 65/79] CLI output testing --- .../src/main/python/tests/CLITest.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 components/org.apache.stratos.python.cli/src/main/python/tests/CLITest.py diff --git a/components/org.apache.stratos.python.cli/src/main/python/tests/CLITest.py b/components/org.apache.stratos.python.cli/src/main/python/tests/CLITest.py new file mode 100644 index 0000000000..b0ad3cc089 --- /dev/null +++ b/components/org.apache.stratos.python.cli/src/main/python/tests/CLITest.py @@ -0,0 +1,11 @@ +from scripttest import TestFileEnvironment + +env = TestFileEnvironment('./scratch') + + +def test_list_users(): + env.clear() + result = env.run('stratos-cli list-users') + assert result.stdout.startswith("") + +test_list_users() \ No newline at end of file From cc2ef20cc718edd7e2a1d3a7dc8583fe0308bfee Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Sun, 16 Aug 2015 23:42:12 +0530 Subject: [PATCH 66/79] Generalized Errors --- .../src/main/python/cli/CLI.py | 197 +++++++++++------- .../src/main/python/cli/Stratos.py | 70 +++---- ...enticationError.py => BadResponseError.py} | 9 +- 3 files changed, 158 insertions(+), 118 deletions(-) rename components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/{AuthenticationError.py => BadResponseError.py} (81%) 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 bf01e54f64..1fa0a46a9d 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 @@ -20,7 +20,8 @@ from Utils import * from Stratos import * import Configs -from cli.exceptions import AuthenticationError +from Logging import logging +from cli.exceptions import BadResponseError class CLI(Cmd): @@ -67,8 +68,9 @@ def do_list_users(self, line , opts=None): rows.append([user['userName'], user['role']]) table.add_rows(rows) table.print_table() - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -95,8 +97,9 @@ def do_add_user(self, line , opts=None): print("User successfully created") else: print("Error creating the user") - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -119,8 +122,9 @@ def do_update_user(self, line , opts=None): print("User successfully updated") else: print("Error updating the user") - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -139,8 +143,9 @@ def do_remove_user(self, name , opts=None): print("You have successfully deleted user: "+name) else: print("Could not delete user: "+name) - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) """ # Applications @@ -204,8 +209,9 @@ def do_add_application(self, line , opts=None): print("Application added successfully") else: print("Error adding application") - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -223,8 +229,9 @@ def do_remove_application(self, application , opts=None): print("You have successfully removed application: "+application) else: print("Could not delete application : "+application) - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ @@ -245,8 +252,9 @@ def do_deploy_application(self, line , opts=None): print("You have successfully deployed application: "+opts.application_id) else: print("Could not deployed application : "+opts.application_id) - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) """ # Application deployment @@ -312,8 +320,9 @@ def do_add_application_signup(self, application_id, opts=None): print("Application signup added successfully") else: print("Error creating application signup") - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -331,8 +340,9 @@ def do_remove_application_signup(self, signup , opts=None): print("You have successfully remove signup: "+signup) else: print("Could not delete application signup: "+signup) - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) """ @@ -403,7 +413,8 @@ def do_describe_tenant(self, tenant_domain_name, opts=None): print("Created date: "+datetime.datetime.fromtimestamp(tenant['createdDate']/1000).strftime('%Y-%m-%d %H:%M:%S')) print("-------------------------------------") except requests.HTTPError as e: - self.perror("Error") + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -425,8 +436,9 @@ def do_add_tenant(self, line , opts=None): print("Tenant added successfully : "+opts.domain_name) else: print("Error creating the tenant : "+opts.domain_name) - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -449,8 +461,9 @@ def do_update_tenant(self, line , opts=None): print("Tenant updated successfully : "+opts.domain_name) else: print("Error updating the tenant : "+opts.domain_name) - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -468,8 +481,9 @@ def do_activate_tenant(self, tenant_domain, opts=None): print("You have successfully activated the tenant : "+tenant_domain) else: print("Could not activate tenant : "+tenant_domain) - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -487,8 +501,9 @@ def do_deactivate_tenant(self, tenant_domain, opts=None): print("You have successfully deactivated the tenant : "+tenant_domain) else: print("Could not deactivate tenant : "+tenant_domain) - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) """ # Cartridges @@ -542,7 +557,8 @@ def do_describe_cartridge(self, cartridge_type , opts=None): print("Host Name: "+cartridge['host']) print("-------------------------------------") except requests.HTTPError as e: - self.perror("Error") + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -564,7 +580,7 @@ def do_add_cartridge(self, line , opts=None): print("Cartridge added successfully") else: print("Error adding Cartridge") - except AuthenticationError as e: + except BadResponseError as e: print("Authentication Error") @options([ @@ -584,8 +600,9 @@ def do_update_cartridge(self, line , opts=None): print("Cartridge updated successfully") else: print("Error updating Cartridge") - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -603,8 +620,9 @@ def do_remove_cartridge(self, cartridge_type , opts=None): print("Successfully un-deployed cartridge : "+cartridge_type) else: print("Could not un-deployed cartridge : "+cartridge_type) - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) """ # Cartridge groups @@ -669,8 +687,9 @@ def do_add_cartridge_group(self, line , opts=None): print("Cartridge group added successfully") else: print("Error adding Cartridge group") - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -689,8 +708,9 @@ def do_update_cartridge_group(self, line , opts=None): print("Cartridge group updated successfully") else: print("Error updating Cartridge group") - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -708,8 +728,9 @@ def do_remove_cartridge_group(self, group_definition_name , opts=None): print("Successfully un-deployed cartridge group : "+group_definition_name) else: print("Could not un-deployed cartridge group : "+group_definition_name) - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) """ # Deployment Policies @@ -773,8 +794,9 @@ def do_add_deployment_policy(self, line , opts=None): print("Deployment policy added successfully") else: print("Error creating deployment policy") - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -793,8 +815,9 @@ def do_update_application_policy(self, line , opts=None): print("Deployment policy updated successfully") else: print("Error updating Deployment policy") - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -812,8 +835,9 @@ def do_remove_deployment_policy(self, deployment_policy_id , opts=None): print("Successfully deleted deployment policy : "+deployment_policy_id) else: print("Could not deleted deployment policy : "+deployment_policy_id) - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) """ # Deployment Policies @@ -876,8 +900,9 @@ def do_add_application_policy(self, line , opts=None): print("Deployment policy added successfully") else: print("Error creating application policy") - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -896,8 +921,9 @@ def do_update_application_policy(self, line , opts=None): print("Deployment policy updated successfully") else: print("Error updating Deployment policy") - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -915,8 +941,9 @@ def do_remove_application_policy(self, application_policy_id , opts=None): print("Successfully deleted application policy : "+application_policy_id) else: print("Could not deleted application policy : "+application_policy_id) - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) """ @@ -978,8 +1005,9 @@ def do_add_network_partition(self, line , opts=None): print("Network partition added successfully") else: print("Error creating network partition") - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -998,8 +1026,9 @@ def do_update_network_partition(self, line , opts=None): print("Network partition updated successfully") else: print("Error updating Network partition") - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1017,8 +1046,9 @@ def do_remove_network_partition(self, network_partition_id, opts=None): print("Successfully deleted network-partition : "+network_partition_id) else: print("Could not deleted network-partition : "+network_partition_id) - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) """ # Auto-scaling policies @@ -1081,8 +1111,9 @@ def do_update_autoscaling_policy(self, line , opts=None): print("Autoscaling policy updated successfully:") else: print("Error updating Autoscaling policy") - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1100,8 +1131,9 @@ def do_remove_autoscaling_policy(self, autoscaling_policy_id , opts=None): print("Successfully deleted Auto-scaling policy : "+autoscaling_policy_id) else: print("Auto-scaling policy not found : "+autoscaling_policy_id) - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) """ # Kubernetes clusters/hosts @@ -1187,8 +1219,9 @@ def do_add_kubernetes_host(self, kubernetes_cluster_id , opts=None): print("You have successfully deployed host to Kubernetes cluster: "+kubernetes_cluster_id) else: print("Error deploying host to Kubernetes cluster: "+kubernetes_cluster_id) - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1230,8 +1263,9 @@ def do_update_kubernetes_master(self, line , opts=None): print("Kubernetes master updated successfully") else: print("Error updating Kubernetes master") - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1251,8 +1285,9 @@ def do_update_kubernetes_host(self, line , opts=None): print("You have succesfully updated host to Kubernetes cluster") else: print("Error updating Kubernetes host") - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1270,8 +1305,9 @@ def do_remove_kubernetes_cluster(self, kubernetes_cluster_id, opts=None): print("Successfully un-deployed kubernetes cluster : "+kubernetes_cluster_id) else: print("Kubernetes cluster not found : "+kubernetes_cluster_id) - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1291,8 +1327,9 @@ def do_remove_kubernetes_host(self, line, opts=None): print("Successfully un-deployed kubernetes host : "+opts.host_id) else: print("Kubernetes host not found : "+opts.cluster_id+"/"+opts.host_id) - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) """ @@ -1342,8 +1379,9 @@ def do_add_domain_mapping(self, application_id, opts=None): print(" Domain mapping added successfully") else: print("Error creating domain mapping") - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) @options([ @@ -1362,8 +1400,9 @@ def do_remove_domain_mappings(self, domain , opts=None): print("You have successfully deleted domain: "+domain) else: print("Could not delete domain: "+domain) - except AuthenticationError as e: - self.perror("Authentication Error") + except BadResponseError as e: + self.perror(str(e)) + logging.error("HTTP "+e.error_code+" : "+str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1382,6 +1421,6 @@ def do_add_autoscaling_policy(self, line , opts=None): print("Autoscaling policy added successfully") else: print("Error adding autoscaling policy") - except AuthenticationError as e: - self.perror("Authentication Error") - + except BadResponseError as e: + self.perror(str(e)) + logging.error(str(e)) \ No newline at end of file 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 4fcbecb648..d1a300d501 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 @@ -18,8 +18,7 @@ import requests import json import Configs -from Logging import logging -from cli.exceptions.AuthenticationError import AuthenticationError +from cli.exceptions.BadResponseError import BadResponseError class Stratos: @@ -424,29 +423,9 @@ def authenticate(): try: Stratos.get('init') return True - except AuthenticationError as e: + except BadResponseError as e: return False - @staticmethod - def get(resource, error_message=""): - r = requests.get(Configs.stratos_api_url + resource, - auth=(Configs.stratos_username, Configs.stratos_password), verify=False) - print(r) - print(r.text) - 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.text and r.json() and r.json()['message'] == error_message: - return False - else: - raise requests.HTTPError() - else: - logging.error(r.status_code+" : "+r.text) - @staticmethod def delete(resource, error_message=None): r = requests.delete(Configs.stratos_api_url + resource, @@ -456,9 +435,9 @@ def delete(resource, error_message=None): if r.status_code == 200: return r.json() elif r.status_code == 400: - raise requests.HTTPError() + raise BadResponseError(str(r.status_code), r.json()['message']) elif r.status_code == 401: - raise AuthenticationError() + raise BadResponseError(str(r.status_code), r.json()['message']) elif r.status_code == 404: if r.text and r.json() and r.json()['message']: return False @@ -466,9 +445,9 @@ def delete(resource, error_message=None): if r.text and r.json() and r.json()['message']: return False else: - raise requests.HTTPError() + raise BadResponseError(str(r.status_code), r.json()['message']) else: - logging.error(r.status_code+" : "+r.text) + raise BadResponseError(str(r.status_code), r.json()['message']) @staticmethod def post(resource, data, error_message=None): @@ -482,16 +461,16 @@ def post(resource, data, error_message=None): elif r.status_code == 201: return True elif r.status_code == 400: - raise requests.HTTPError() + raise BadResponseError(str(r.status_code), r.json()['message']) elif r.status_code == 401: - raise AuthenticationError() + raise BadResponseError(str(r.status_code), r.json()['message']) elif r.status_code == 404: if r.text and r.json() and r.json()['message'] == error_message: return [] else: - raise requests.HTTPError() + raise BadResponseError(str(r.status_code), r.text) else: - logging.error(r.status_code+" : "+r.text) + raise BadResponseError(str(r.status_code), r.json()['message']) @staticmethod def put(resource, data, error_message): @@ -505,16 +484,16 @@ def put(resource, data, error_message): elif r.status_code == 201: return True elif r.status_code == 400: - raise requests.HTTPError() + raise BadResponseError(str(r.status_code), r.json()['message']) elif r.status_code == 401: - raise AuthenticationError() + raise BadResponseError(str(r.status_code), r.json()['message']) elif r.status_code == 404: if r.text and r.json() and r.json()['message'] == error_message: return [] else: - raise requests.HTTPError() + raise BadResponseError(str(r.status_code), r.json()['message']) else: - logging.error(r.status_code+" : "+r.text) + raise BadResponseError(str(r.status_code), r.json()['message']) @staticmethod @@ -526,3 +505,24 @@ def add_application(json): return Stratos.post('applications', json, error_message='No application found') +def get(resource, error_message=""): + r = requests.get(Configs.stratos_api_url + resource, + auth=(Configs.stratos_username, Configs.stratos_password), verify=False) + print(r) + print(r.text) + if r.status_code == 200: + return r.json() + elif r.status_code == 400: + raise BadResponseError(str(r.status_code), r.json()['message']) + elif r.status_code == 401: + raise BadResponseError(str(r.status_code), r.json()['message']) + elif r.status_code == 404: + if r.text and r.json() and r.json()['message'] == error_message: + return False + else: + raise BadResponseError(str(r.status_code), " : "+r.text) + else: + raise BadResponseError(str(r.status_code), r.json()['message']) + return response(r) + + diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py b/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/BadResponseError.py similarity index 81% rename from components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py rename to components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/BadResponseError.py index 5bcd5d8ac1..775ec65e35 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/AuthenticationError.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/exceptions/BadResponseError.py @@ -15,9 +15,10 @@ # specific language governing permissions and limitations # under the License. -class AuthenticationError(Exception): - def __init__(self, message=""): - pass +class BadResponseError(Exception): + def __init__(self, error_code, message=""): + self.error_code = error_code + self.message = message def __str__(self): - return "Authentication error" + return self.message From 85d5d9b7adff0fe249264a946bf16584e0a956a0 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Mon, 17 Aug 2015 11:06:52 +0530 Subject: [PATCH 67/79] removed unwanted stuffs --- .../src/main/python/cli/CLI.py | 701 ++++++++++-------- .../src/main/python/cli/Main.py | 6 +- .../src/main/python/cli/Stratos.py | 227 +++--- 3 files changed, 477 insertions(+), 457 deletions(-) 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 1fa0a46a9d..88d6b924e4 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 @@ -16,11 +16,9 @@ # under the License. from cmd2 import * -from rpm._rpm import te from Utils import * from Stratos import * import Configs -from Logging import logging from cli.exceptions import BadResponseError @@ -59,7 +57,7 @@ def completenames(self, text, *ignored): ]) @auth def do_list_users(self, line , opts=None): - """List the users of Stratos""" + """Retrieve details of all users.""" try: users = Stratos.list_users() table = PrintableTable() @@ -70,7 +68,6 @@ def do_list_users(self, line , opts=None): table.print_table() except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -84,7 +81,7 @@ def do_list_users(self, line , opts=None): make_option('-o', '--profile_name', type="str", help="Profile name of the user") ]) def do_add_user(self, line , opts=None): - """Add a new user to the system""" + """Add a user.""" try: if not opts.username_user or not opts.password_user: print("usage: add-user [-s ] [-a ] [-r ] [-e ] [-f ]" + @@ -99,7 +96,6 @@ def do_add_user(self, line , opts=None): print("Error creating the user") except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -114,7 +110,7 @@ def do_add_user(self, line , opts=None): ]) @auth def do_update_user(self, line , opts=None): - """Add a new user to the system""" + """Update a specific user.""" try: user = Stratos.update_user(opts.username_user, opts.password_user, opts.role_name, opts.first_name, opts.last_name, opts.email, opts.profile_name) @@ -124,7 +120,6 @@ def do_update_user(self, line , opts=None): print("Error updating the user") except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -132,7 +127,7 @@ def do_update_user(self, line , opts=None): ]) @auth def do_remove_user(self, name , opts=None): - """Delete a specific user""" + """Delete a user.""" try: if not name: print("usage: remove-user [username]") @@ -145,7 +140,6 @@ def do_remove_user(self, name , opts=None): print("Could not delete user: "+name) except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) """ # Applications @@ -163,17 +157,19 @@ def do_remove_user(self, name , opts=None): @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 = [["Application ID", "Alias", "Status"]] - for application in applications: - rows.append([application['applicationId'], application['alias'], application['status']]) - table.add_rows(rows) - table.print_table() - + try: + applications = Stratos.list_applications() + if not applications: + print("No applications found") + else: + table = PrintableTable() + rows = [["Application ID", "Alias", "Status"]] + for application in applications: + rows.append([application['applicationId'], application['alias'], application['status']]) + table.add_rows(rows) + table.print_table() + except BadResponseError as e: + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -182,15 +178,18 @@ def do_list_applications(self, line , opts=None): @auth def do_describe_application(self, application_id , opts=None): """Retrieve detailed information on the master node in a specific Kubernetes-CoreOS group""" - if not application_id: - print("usage: describe-application [cluster-id]") - return - application = Stratos.describe_application(application_id) - if not application: - print("Application not found in : "+application_id) - else: - print("Application : "+application_id) - PrintableJSON(application).pprint() + try: + if not application_id: + print("usage: describe-application [cluster-id]") + return + application = Stratos.describe_application(application_id) + if not application: + print("Application not found in : "+application_id) + else: + print("Application : "+application_id) + PrintableJSON(application).pprint() + except BadResponseError as e: + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -211,7 +210,7 @@ def do_add_application(self, line , opts=None): print("Error adding application") except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) + @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -231,8 +230,6 @@ def do_remove_application(self, application , opts=None): print("Could not delete application : "+application) except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) - @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -254,7 +251,6 @@ def do_deploy_application(self, line , opts=None): print("Could not deployed application : "+opts.application_id) except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) """ # Application deployment @@ -270,15 +266,18 @@ def do_deploy_application(self, line , opts=None): @auth def do_describe_application_runtime(self, application_id , opts=None): """Retrieve details of a specific auto-scaling policy.""" - if not application_id: - print("usage: describe-application-runtime [application-id]") - return - application_runtime = Stratos.describe_application_runtime(application_id) - if not application_runtime: - print("Application runtime not found") - else: - print("Application : "+application_id) - PrintableJSON(application_runtime).pprint() + try: + if not application_id: + print("usage: describe-application-runtime [application-id]") + return + application_runtime = Stratos.describe_application_runtime(application_id) + if not application_runtime: + print("Application runtime not found") + else: + print("Application : "+application_id) + PrintableJSON(application_runtime).pprint() + except BadResponseError as e: + self.perror(str(e)) """ # Application signup @@ -294,14 +293,17 @@ def do_describe_application_runtime(self, application_id , opts=None): @auth def do_describe_application_signup(self, application_id , opts=None): """Retrieve details of a specific application signup.""" - if not application_id: - print("usage: describe-application-signup [application-id]") - return - application_signup = Stratos.describe_application_signup(application_id) - if not application_signup: - print("Application signup not found") - else: - PrintableJSON(application_signup).pprint() + try: + if not application_id: + print("usage: describe-application-signup [application-id]") + return + application_signup = Stratos.describe_application_signup(application_id) + if not application_signup: + print("Application signup not found") + else: + PrintableJSON(application_signup).pprint() + except BadResponseError as e: + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -322,7 +324,6 @@ def do_add_application_signup(self, application_id, opts=None): print("Error creating application signup") except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -342,8 +343,6 @@ def do_remove_application_signup(self, signup , opts=None): print("Could not delete application signup: "+signup) except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) - """ # Tenants @@ -362,15 +361,18 @@ def do_remove_application_signup(self, signup , opts=None): ]) @auth def do_list_tenants(self, line , opts=None): - """Illustrate the base class method use.""" - tenants = Stratos.list_tenants() - table = PrintableTable() - rows = [["Domain", "Tenant ID", "Email", " State", "Created Date"]] - for tenant in tenants: - rows.append([tenant['tenantDomain'], tenant['tenantId'], tenant['email'], - "Active" if tenant['active'] else "De-Active", datetime.datetime.fromtimestamp(tenant['createdDate']/1000).strftime('%Y-%m-%d %H:%M:%S')]) - table.add_rows(rows) - table.print_table() + """Retrieve details of all tenants.""" + try: + tenants = Stratos.list_tenants() + table = PrintableTable() + rows = [["Domain", "Tenant ID", "Email", " State", "Created Date"]] + for tenant in tenants: + rows.append([tenant['tenantDomain'], tenant['tenantId'], tenant['email'], + "Active" if tenant['active'] else "De-Active", datetime.datetime.fromtimestamp(tenant['createdDate']/1000).strftime('%Y-%m-%d %H:%M:%S')]) + table.add_rows(rows) + table.print_table() + except BadResponseError as e: + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -378,15 +380,18 @@ def do_list_tenants(self, line , opts=None): ]) @auth def do_list_tenants_by_partial_domain(self, partial_domain , opts=None): - """Illustrate the base class method use.""" - tenants = Stratos.list_tenants_by_partial_domain(partial_domain) - table = PrintableTable() - rows = [["Domain", "Tenant ID", "Email", " State", "Created Date"]] - for tenant in tenants: - rows.append([tenant['tenantDomain'], tenant['tenantId'], tenant['email'], - "Active" if tenant['active'] else "De-Active", datetime.datetime.fromtimestamp(tenant['createdDate']/1000).strftime('%Y-%m-%d %H:%M:%S')]) - table.add_rows(rows) - table.print_table() + """Search for tenants based on the partial domain value entered.""" + try: + tenants = Stratos.list_tenants_by_partial_domain(partial_domain) + table = PrintableTable() + rows = [["Domain", "Tenant ID", "Email", " State", "Created Date"]] + for tenant in tenants: + rows.append([tenant['tenantDomain'], tenant['tenantId'], tenant['email'], + "Active" if tenant['active'] else "De-Active", datetime.datetime.fromtimestamp(tenant['createdDate']/1000).strftime('%Y-%m-%d %H:%M:%S')]) + table.add_rows(rows) + table.print_table() + except BadResponseError as e: + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -412,9 +417,8 @@ def do_describe_tenant(self, tenant_domain_name, opts=None): print("Email: "+tenant['email']) print("Created date: "+datetime.datetime.fromtimestamp(tenant['createdDate']/1000).strftime('%Y-%m-%d %H:%M:%S')) print("-------------------------------------") - except requests.HTTPError as e: + except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -428,7 +432,7 @@ def do_describe_tenant(self, tenant_domain_name, opts=None): ]) @auth def do_add_tenant(self, line , opts=None): - """Add a new user to the system""" + """Add a tenant.""" try: tenant = Stratos.add_tenant(opts.username_user, opts.first_name, opts.last_name, opts.password_user, opts.domain_name, opts.email) @@ -438,7 +442,6 @@ def do_add_tenant(self, line , opts=None): print("Error creating the tenant : "+opts.domain_name) except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -453,7 +456,7 @@ def do_add_tenant(self, line , opts=None): ]) @auth def do_update_tenant(self, line , opts=None): - """Add a new user to the system""" + """Update a specific tenant.""" try: tenant = Stratos.update_tenant(opts.username_user, opts.first_name, opts.last_name, opts.password_user, opts.domain_name, opts.email, opts.tenant_id) @@ -463,7 +466,6 @@ def do_update_tenant(self, line , opts=None): print("Error updating the tenant : "+opts.domain_name) except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -471,7 +473,7 @@ def do_update_tenant(self, line , opts=None): ]) @auth def do_activate_tenant(self, tenant_domain, opts=None): - """Add a new user to the system""" + """Activate a tenant.""" try: if not tenant_domain: print("usage: activate-tenant ") @@ -483,7 +485,6 @@ def do_activate_tenant(self, tenant_domain, opts=None): print("Could not activate tenant : "+tenant_domain) except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -491,7 +492,7 @@ def do_activate_tenant(self, tenant_domain, opts=None): ]) @auth def do_deactivate_tenant(self, tenant_domain, opts=None): - """Add a new user to the system""" + """Deactivate a tenant.""" try: if not tenant_domain: print("usage: deactivate-tenant ") @@ -503,11 +504,11 @@ def do_deactivate_tenant(self, tenant_domain, opts=None): print("Could not deactivate tenant : "+tenant_domain) except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) """ # Cartridges * list-cartridges + * list-cartridges-by-filter * describe-cartridge * add-cartridge * remove-cartridge @@ -520,22 +521,47 @@ def do_deactivate_tenant(self, tenant_domain, opts=None): ]) @auth def do_list_cartridges(self, line , opts=None): - """Illustrate the base class method use.""" - cartridges = Stratos.list_cartridges() - table = PrintableTable() - rows = [["Type", "Category", "Name", "Description", "Version", "Multi-Tenant"]] - for cartridge in cartridges: - rows.append([cartridge['type'], cartridge['category'], cartridge['displayName'], cartridge['description'], - cartridge['version'], "True" if cartridge['multiTenant'] == 1 else "False"]) - table.add_rows(rows) - table.print_table() + """Retrieve details of available cartridges.""" + try: + cartridges = Stratos.list_cartridges() + table = PrintableTable() + rows = [["Type", "Category", "Name", "Description", "Version", "Multi-Tenant"]] + for cartridge in cartridges: + rows.append([cartridge['type'], cartridge['category'], cartridge['displayName'], cartridge['description'], + cartridge['version'], "True" if cartridge['multiTenant'] == 1 else "False"]) + table.add_rows(rows) + table.print_table() + except BadResponseError as e: + self.perror(str(e)) @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_describe_cartridge(self, cartridge_type , opts=None): + def do_list_cartridges_by_filter(self, filter_text , opts=None): + """Retrieve details of available cartridges.""" + try: + if not filter_text: + print("") + else: + cartridges = Stratos.list_cartridges_by_filter(filter_text) + table = PrintableTable() + rows = [["Type", "Category", "Name", "Description", "Version", "Multi-Tenant"]] + for cartridge in cartridges: + rows.append([cartridge['type'], cartridge['category'], cartridge['displayName'], cartridge['description'], + cartridge['version'], "True" if cartridge['multiTenant'] == 1 else "False"]) + table.add_rows(rows) + table.print_table() + except BadResponseError as e: + self.perror(str(e)) + + @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_describe_cartridge(self, cartridge_type, opts=None): """Retrieve details of a specific cartridge.""" if not cartridge_type: print("usage: describe-cartridge [cartridge-type]") @@ -558,7 +584,7 @@ def do_describe_cartridge(self, cartridge_type , opts=None): print("-------------------------------------") except requests.HTTPError as e: self.perror(str(e)) - logging.error(str(e)) + @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -567,10 +593,7 @@ def do_describe_cartridge(self, cartridge_type , opts=None): ]) @auth def do_add_cartridge(self, line , opts=None): - """ - Add a new cartridge to the system - Usage: add-cartridge [-f ] - """ + """Add a cartridge definition.""" try: if not opts.json_file_path: print("usage: add-cartridge [-f ]") @@ -581,7 +604,7 @@ def do_add_cartridge(self, line , opts=None): else: print("Error adding Cartridge") except BadResponseError as e: - print("Authentication Error") + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -602,7 +625,7 @@ def do_update_cartridge(self, line , opts=None): print("Error updating Cartridge") except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) + @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -622,7 +645,6 @@ def do_remove_cartridge(self, cartridge_type , opts=None): print("Could not un-deployed cartridge : "+cartridge_type) except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) """ # Cartridge groups @@ -640,17 +662,20 @@ def do_remove_cartridge(self, cartridge_type , opts=None): @auth def do_list_cartridge_groups(self, line , opts=None): """Illustrate the base class method use.""" - cartridge_groups = Stratos.list_cartridge_groups() - if not cartridge_groups: - print("No cartridge groups found") - else: - table = PrintableTable() - rows = [["Name", "No. of cartridges", "No of groups"]] - for cartridge_group in cartridge_groups: - rows.append([cartridge_group['name'], len(cartridge_group['cartridges']), - len(cartridge_group['cartridges'])]) - table.add_rows(rows) - table.print_table() + try: + cartridge_groups = Stratos.list_cartridge_groups() + if not cartridge_groups: + print("No cartridge groups found") + else: + table = PrintableTable() + rows = [["Name", "No. of cartridges", "No of groups"]] + for cartridge_group in cartridge_groups: + rows.append([cartridge_group['name'], len(cartridge_group['cartridges']), + len(cartridge_group['cartridges'])]) + table.add_rows(rows) + table.print_table() + except BadResponseError as e: + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -659,16 +684,18 @@ def do_list_cartridge_groups(self, line , opts=None): @auth def do_describe_cartridge_group(self, group_definition_name , opts=None): """Retrieve details of a cartridge group.""" - if not group_definition_name: - print("usage: describe-cartridge-group [cartridge-group-name]") - return - cartridge_group = Stratos.describe_cartridge_group(group_definition_name) - if not cartridge_group: - print("Cartridge group not found") - else: - print("Service Group : "+group_definition_name) - PrintableJSON(cartridge_group).pprint() - + try: + if not group_definition_name: + print("usage: describe-cartridge-group [cartridge-group-name]") + return + cartridge_group = Stratos.describe_cartridge_group(group_definition_name) + if not cartridge_group: + print("Cartridge group not found") + else: + print("Service Group : "+group_definition_name) + PrintableJSON(cartridge_group).pprint() + except BadResponseError as e: + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -689,7 +716,6 @@ def do_add_cartridge_group(self, line , opts=None): print("Error adding Cartridge group") except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -710,7 +736,6 @@ def do_update_cartridge_group(self, line , opts=None): print("Error updating Cartridge group") except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -730,7 +755,6 @@ def do_remove_cartridge_group(self, group_definition_name , opts=None): print("Could not un-deployed cartridge group : "+group_definition_name) except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) """ # Deployment Policies @@ -748,17 +772,20 @@ def do_remove_cartridge_group(self, group_definition_name , opts=None): ]) @auth def do_list_deployment_policies(self, line , opts=None): - """Illustrate the base class method use.""" - deployment_policies = Stratos.list_deployment_policies() - if not deployment_policies: - print("No deployment policies found") - else: - table = PrintableTable() - rows = [["Id", "Accessibility"]] - for deployment_policy in deployment_policies: - rows.append([deployment_policy['id'], len(deployment_policy['networkPartitions'])]) - table.add_rows(rows) - table.print_table() + """Retrieve details of a deployment policy.""" + try: + deployment_policies = Stratos.list_deployment_policies() + if not deployment_policies: + print("No deployment policies found") + else: + table = PrintableTable() + rows = [["Id", "Accessibility"]] + for deployment_policy in deployment_policies: + rows.append([deployment_policy['id'], len(deployment_policy['networkPartitions'])]) + table.add_rows(rows) + table.print_table() + except BadResponseError as e: + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -766,15 +793,18 @@ def do_list_deployment_policies(self, line , opts=None): ]) @auth def do_describe_deployment_policy(self, line , opts=None): - """Retrieve details of a specific deployment policy.""" - if not line.split(): - print("usage: describe-deployment-policy [deployment-policy-id]") - return - deployment_policy = Stratos.describe_deployment_policy(line) - if not deployment_policy: - print("Deployment policy not found") - else: - PrintableJSON(deployment_policy).pprint() + """Describe a deployment policy.""" + try: + if not line: + print("usage: describe-deployment-policy [deployment-policy-id]") + return + deployment_policy = Stratos.describe_deployment_policy(line) + if not deployment_policy: + print("Deployment policy not found") + else: + PrintableJSON(deployment_policy).pprint() + except BadResponseError as e: + self.perror(str(e)) @options([ @@ -784,7 +814,7 @@ def do_describe_deployment_policy(self, line , opts=None): ]) @auth def do_add_deployment_policy(self, line , opts=None): - """Add a new user to the system""" + """Add a deployment policy definition.""" try: if not opts.json_file_path: print("usage: add-deployment-policy [-f ]") @@ -796,7 +826,7 @@ def do_add_deployment_policy(self, line , opts=None): print("Error creating deployment policy") except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) + @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -805,7 +835,7 @@ def do_add_deployment_policy(self, line , opts=None): ]) @auth def do_update_application_policy(self, line , opts=None): - """Add a new user to the system""" + """Update a deployment policy.""" try: if not opts.json_file_path: print("usage: update-deployment-policy [-f ]") @@ -817,7 +847,7 @@ def do_update_application_policy(self, line , opts=None): print("Error updating Deployment policy") except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) + @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -825,7 +855,7 @@ def do_update_application_policy(self, line , opts=None): ]) @auth def do_remove_deployment_policy(self, deployment_policy_id , opts=None): - """Delete a cartridge""" + """Delete a deployment policy.""" try: if not deployment_policy_id: print("usage: remove-deployment-policy [deployment-policy-id]") @@ -837,7 +867,7 @@ def do_remove_deployment_policy(self, deployment_policy_id , opts=None): print("Could not deleted deployment policy : "+deployment_policy_id) except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) + """ # Deployment Policies @@ -855,32 +885,38 @@ def do_remove_deployment_policy(self, deployment_policy_id , opts=None): @auth def do_list_application_policies(self, line , opts=None): """Illustrate the base class method use.""" - application_policies = Stratos.list_application_policies() - if not application_policies: - print("No application policies found") - else: - table = PrintableTable() - rows = [["Id", "Accessibility"]] - for application_policy in application_policies: - rows.append([application_policy['id'], len(application_policy['networkPartitions'])]) - table.add_rows(rows) - table.print_table() + try: + application_policies = Stratos.list_application_policies() + if not application_policies: + print("No application policies found") + else: + table = PrintableTable() + rows = [["Id", "Accessibility"]] + for application_policy in application_policies: + rows.append([application_policy['id'], len(application_policy['networkPartitions'])]) + table.add_rows(rows) + table.print_table() + except BadResponseError as e: + self.perror(str(e)) @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_describe_application_policy(self, line , opts=None): + def do_describe_application_policy(self, application_policy_id , opts=None): """Retrieve details of a specific application policy.""" - if not line.split(): - print("usage: describe-application-policy [application-policy-id]") - return - application_policy = Stratos.describe_application_policy(line) - if not application_policy: - print("Deployment policy not found") - else: - PrintableJSON(application_policy).pprint() + try: + if not application_policy_id: + print("usage: describe-application-policy [application-policy-id]") + return + application_policy = Stratos.describe_application_policy(application_policy_id) + if not application_policy: + print("Deployment policy not found") + else: + PrintableJSON(application_policy).pprint() + except BadResponseError as e: + self.perror(str(e)) @options([ @@ -902,7 +938,6 @@ def do_add_application_policy(self, line , opts=None): print("Error creating application policy") except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -923,7 +958,6 @@ def do_update_application_policy(self, line , opts=None): print("Error updating Deployment policy") except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -943,8 +977,6 @@ def do_remove_application_policy(self, application_policy_id , opts=None): print("Could not deleted application policy : "+application_policy_id) except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) - """ # Network Partitions @@ -962,14 +994,17 @@ def do_remove_application_policy(self, application_policy_id , opts=None): ]) @auth def do_list_network_partitions(self, line , opts=None): - """Illustrate the base class method use.""" - network_partitions = Stratos.list_network_partitions() - table = PrintableTable() - rows = [["Network Partition ID", "Number of Partitions"]] - for network_partition in network_partitions: - rows.append([network_partition['id'], len(network_partition['partitions'])]) - table.add_rows(rows) - table.print_table() + """Retrieve details of all the network partitions.""" + try: + network_partitions = Stratos.list_network_partitions() + table = PrintableTable() + rows = [["Network Partition ID", "Number of Partitions"]] + for network_partition in network_partitions: + rows.append([network_partition['id'], len(network_partition['partitions'])]) + table.add_rows(rows) + table.print_table() + except BadResponseError as e: + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -977,16 +1012,19 @@ def do_list_network_partitions(self, line , opts=None): ]) @auth def do_describe_network_partition(self, network_partition_id , opts=None): - """Retrieve details of a specific deployment policy.""" - if not network_partition_id: - print("usage: describe-network-partition [network-partition]") - return - deployment_policy = Stratos.describe_network_partition(network_partition_id) - if not deployment_policy: - print("Network partition not found: "+network_partition_id) - else: - print("Partition: "+network_partition_id) - PrintableJSON(deployment_policy).pprint() + """Describe a network partition.""" + try: + if not network_partition_id: + print("usage: describe-network-partition [network-partition]") + return + deployment_policy = Stratos.describe_network_partition(network_partition_id) + if not deployment_policy: + print("Network partition not found: "+network_partition_id) + else: + print("Partition: "+network_partition_id) + PrintableJSON(deployment_policy).pprint() + except BadResponseError as e: + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -995,7 +1033,7 @@ def do_describe_network_partition(self, network_partition_id , opts=None): ]) @auth def do_add_network_partition(self, line , opts=None): - """Add a new user to the system""" + """Add a new network partition.""" try: if not opts.json_file_path: print("usage: add-network-partition [-f ]") @@ -1007,7 +1045,6 @@ def do_add_network_partition(self, line , opts=None): print("Error creating network partition") except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1016,7 +1053,7 @@ def do_add_network_partition(self, line , opts=None): ]) @auth def do_update_network_partition(self, line , opts=None): - """Add a new user to the system""" + """Update a specific network partition.""" try: if not opts.json_file_path: print("usage: update-network-partition [-f ]") @@ -1028,7 +1065,6 @@ def do_update_network_partition(self, line , opts=None): print("Error updating Network partition") except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1036,7 +1072,7 @@ def do_update_network_partition(self, line , opts=None): ]) @auth def do_remove_network_partition(self, network_partition_id, opts=None): - """Delete a cartridge""" + """Delete a network partition.""" try: if not network_partition_id: print("usage: remove-network-partition [network-partition-id]") @@ -1048,12 +1084,12 @@ def do_remove_network_partition(self, network_partition_id, opts=None): print("Could not deleted network-partition : "+network_partition_id) except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) """ # Auto-scaling policies * list-autoscaling-policies * describe-autoscaling-policy + * add-autoscaling-policy * update-autoscaling-policy * remove-autoscaling-policy @@ -1065,17 +1101,20 @@ def do_remove_network_partition(self, network_partition_id, opts=None): ]) @auth def do_list_autoscaling_policies(self, line , opts=None): - """Retrieve details of all the cartridge groups that have been added.""" - autoscaling_policies = Stratos.list_autoscaling_policies() - if not autoscaling_policies: - print("No autoscaling policies found") - else: - table = PrintableTable() - rows = [["Id", "Accessibility"]] - for autoscaling_policy in autoscaling_policies: - rows.append([autoscaling_policy['id'], "Public" if autoscaling_policy['isPublic'] else "Private"]) - table.add_rows(rows) - table.print_table() + """Retrieve details of auto-scaling policies.""" + try: + autoscaling_policies = Stratos.list_autoscaling_policies() + if not autoscaling_policies: + print("No autoscaling policies found") + else: + table = PrintableTable() + rows = [["Id", "Accessibility"]] + for autoscaling_policy in autoscaling_policies: + rows.append([autoscaling_policy['id'], "Public" if autoscaling_policy['isPublic'] else "Private"]) + table.add_rows(rows) + table.print_table() + except BadResponseError as e: + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1084,15 +1123,38 @@ def do_list_autoscaling_policies(self, line , opts=None): @auth def do_describe_autoscaling_policy(self, autoscaling_policy_id , opts=None): """Retrieve details of a specific auto-scaling policy.""" - if not autoscaling_policy_id: - print("usage: describe-autoscaling-policy [autoscaling-policy-id]") - return - autoscaling_policy = Stratos.describe_autoscaling_policy(autoscaling_policy_id) - if not autoscaling_policy: - print("Autoscaling policy not found : "+autoscaling_policy_id) - else: - print("Autoscaling policy : "+autoscaling_policy_id) - PrintableJSON(autoscaling_policy).pprint() + try: + if not autoscaling_policy_id: + print("usage: describe-autoscaling-policy [autoscaling-policy-id]") + return + autoscaling_policy = Stratos.describe_autoscaling_policy(autoscaling_policy_id) + if not autoscaling_policy: + print("Autoscaling policy not found : "+autoscaling_policy_id) + else: + print("Autoscaling policy : "+autoscaling_policy_id) + PrintableJSON(autoscaling_policy).pprint() + except BadResponseError as e: + self.perror(str(e)) + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_add_autoscaling_policy(self, line , opts=None): + """Add an auto-scaling policy definition.""" + try: + if not opts.json_file_path: + print("usage: add-autoscaling-policy [-f ]") + else: + autoscaling_policy = Stratos.add_autoscaling_policy(open(opts.json_file_path, 'r').read()) + if autoscaling_policy: + print("Autoscaling policy added successfully") + else: + print("Error adding autoscaling policy") + except BadResponseError as e: + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1101,7 +1163,7 @@ def do_describe_autoscaling_policy(self, autoscaling_policy_id , opts=None): ]) @auth def do_update_autoscaling_policy(self, line , opts=None): - """Add a new user to the system""" + """Update an auto-scaling policy.""" try: if not opts.json_file_path: print("usage: update-autoscaling-policy [-f ]") @@ -1113,7 +1175,7 @@ def do_update_autoscaling_policy(self, line , opts=None): print("Error updating Autoscaling policy") except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) + @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1121,7 +1183,7 @@ def do_update_autoscaling_policy(self, line , opts=None): ]) @auth def do_remove_autoscaling_policy(self, autoscaling_policy_id , opts=None): - """Delete a autoscaling_policy""" + """Delete an autoscaling_policy.""" try: if not autoscaling_policy_id: print("usage: remove-autoscaling-policy [application-id]") @@ -1133,7 +1195,7 @@ def do_remove_autoscaling_policy(self, autoscaling_policy_id , opts=None): print("Auto-scaling policy not found : "+autoscaling_policy_id) except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) + """ # Kubernetes clusters/hosts @@ -1156,17 +1218,20 @@ def do_remove_autoscaling_policy(self, autoscaling_policy_id , opts=None): ]) @auth def do_list_kubernetes_clusters(self, line , opts=None): - """Retrieve detailed information on all Kubernetes-CoreOS Clusters.""" - kubernetes_clusters = Stratos.list_kubernetes_clusters() - if not kubernetes_clusters: - print("No Kubernetes clusters found") - else: - table = PrintableTable() - rows = [["Group ID", "Description"]] - for kubernetes_cluster in kubernetes_clusters: - rows.append([kubernetes_cluster['clusterId'], kubernetes_cluster['description']]) - table.add_rows(rows) - table.print_table() + """Retrieving details of all Kubernetes-CoreOS Clusters.""" + try: + kubernetes_clusters = Stratos.list_kubernetes_clusters() + if not kubernetes_clusters: + print("No Kubernetes clusters found") + else: + table = PrintableTable() + rows = [["Group ID", "Description"]] + for kubernetes_cluster in kubernetes_clusters: + rows.append([kubernetes_cluster['clusterId'], kubernetes_cluster['description']]) + table.add_rows(rows) + table.print_table() + except BadResponseError as e: + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1174,16 +1239,19 @@ def do_list_kubernetes_clusters(self, line , opts=None): ]) @auth def do_describe_kubernetes_cluster(self, kubernetes_cluster_id, opts=None): - """Retrieve detailed information on a specific Kubernetes-CoreOS group""" - if not kubernetes_cluster_id: - print("usage: describe-kubernetes-cluster [cluster-i]]") - return - kubernetes_cluster = Stratos.describe_kubernetes_cluster(kubernetes_cluster_id) - if not kubernetes_cluster: - print("Kubernetes cluster not found") - else: - print("Kubernetes cluster: "+kubernetes_cluster_id) - PrintableJSON(kubernetes_cluster).pprint() + """Describe a Kubernetes-CoreOS Cluster.""" + try: + if not kubernetes_cluster_id: + print("usage: describe-kubernetes-cluster [cluster-i]]") + return + kubernetes_cluster = Stratos.describe_kubernetes_cluster(kubernetes_cluster_id) + if not kubernetes_cluster: + print("Kubernetes cluster not found") + else: + print("Kubernetes cluster: "+kubernetes_cluster_id) + PrintableJSON(kubernetes_cluster).pprint() + except BadResponseError as e: + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1191,16 +1259,39 @@ def do_describe_kubernetes_cluster(self, kubernetes_cluster_id, opts=None): ]) @auth def do_describe_kubernetes_master(self, kubernetes_cluster_id , opts=None): - """Retrieve detailed information on the master node in a specific Kubernetes-CoreOS group""" - if not kubernetes_cluster_id: - print("usage: describe-kubernetes-master [cluster-id]") - return - kubernetes_master = Stratos.describe_kubernetes_master(kubernetes_cluster_id) - if not kubernetes_master: - print("Kubernetes master not found in : "+kubernetes_cluster_id) - else: - print("Cluster : "+kubernetes_cluster_id) - PrintableJSON(kubernetes_master).pprint() + """Retrieve details of the master in a Kubernetes-CoreOS Cluster.""" + try: + if not kubernetes_cluster_id: + print("usage: describe-kubernetes-master [cluster-id]") + return + kubernetes_master = Stratos.describe_kubernetes_master(kubernetes_cluster_id) + if not kubernetes_master: + print("Kubernetes master not found in : "+kubernetes_cluster_id) + else: + print("Cluster : "+kubernetes_cluster_id) + PrintableJSON(kubernetes_master).pprint() + except BadResponseError as e: + self.perror(str(e)) + + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_add_kubernetes_cluster(self, opts=None): + """Add a Kubernetes-CoreOS Cluster.""" + try: + if not opts.json_file_path: + print("usage: add-kubernetes-cluster [-f ]") + else: + kubernetes_cluster = Stratos.add_kubernetes_cluster(open(opts.json_file_path, 'r').read()) + if kubernetes_cluster: + print("You have successfully deployed the Kubernetes cluster") + else: + print("Error deploying the Kubernetes cluster ") + except BadResponseError as e: + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1209,10 +1300,10 @@ def do_describe_kubernetes_master(self, kubernetes_cluster_id , opts=None): ]) @auth def do_add_kubernetes_host(self, kubernetes_cluster_id , opts=None): - """Add a host (slave) to an existing Kubernetes-CoreOS Cluster.""" + """Add a host to a Kubernetes-CoreOS Cluster.""" try: if not kubernetes_cluster_id or not opts.json_file_path: - print("usage: add-kubernetes-cluster [-f ] [kubernetes cluster id]") + print("usage: add-kubernetes-host [-f ] [kubernetes cluster id]") else: kubernetes_host = Stratos.add_kubernetes_host(kubernetes_cluster_id, open(opts.json_file_path, 'r').read()) if kubernetes_host: @@ -1221,7 +1312,6 @@ def do_add_kubernetes_host(self, kubernetes_cluster_id , opts=None): print("Error deploying host to Kubernetes cluster: "+kubernetes_cluster_id) except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1229,21 +1319,24 @@ def do_add_kubernetes_host(self, kubernetes_cluster_id , opts=None): make_option('-c', '--cluster_id', type="str", help="Cluster ID") ]) def do_list_kubernetes_hosts(self, line , opts=None): - """Retrieve detailed information on all Kubernetes-CoreOS Clusters.""" - if not opts.cluster_id: - print("usage: list-kubernetes-hosts [-c ]") - return - kubernetes_cluster_hosts = Stratos.list_kubernetes_hosts(opts.cluster_id) - if not kubernetes_cluster_hosts: - print("No kubernetes hosts found") - else: - table = PrintableTable() - rows = [["Host ID", "Hostname", "Private IP Address", "Public IP Address"]] - for kubernetes_cluster_host in kubernetes_cluster_hosts: - rows.append([kubernetes_cluster_host['hostId'], kubernetes_cluster_host['hostname'], - kubernetes_cluster_host['privateIPAddress'], kubernetes_cluster_host['publicIPAddress']]) - table.add_rows(rows) - table.print_table() + """Retrieve details of all hosts of a Kubernetes-CoreOS Cluster.""" + try: + if not opts.cluster_id: + print("usage: list-kubernetes-hosts [-c ]") + return + kubernetes_cluster_hosts = Stratos.list_kubernetes_hosts(opts.cluster_id) + if not kubernetes_cluster_hosts: + print("No kubernetes hosts found") + else: + table = PrintableTable() + rows = [["Host ID", "Hostname", "Private IP Address", "Public IP Address"]] + for kubernetes_cluster_host in kubernetes_cluster_hosts: + rows.append([kubernetes_cluster_host['hostId'], kubernetes_cluster_host['hostname'], + kubernetes_cluster_host['privateIPAddress'], kubernetes_cluster_host['publicIPAddress']]) + table.add_rows(rows) + table.print_table() + except BadResponseError as e: + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1253,7 +1346,7 @@ def do_list_kubernetes_hosts(self, line , opts=None): ]) @auth def do_update_kubernetes_master(self, line , opts=None): - """Add a new user to the system""" + """Update the master node of the Kubernetes-CoreOS Cluster.""" try: if not opts.json_file_path: print("usage: update-kubernetes-master [-c ] [-p ]") @@ -1265,16 +1358,15 @@ def do_update_kubernetes_master(self, line , opts=None): print("Error updating Kubernetes master") except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user"), - make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") ]) @auth def do_update_kubernetes_host(self, line , opts=None): - """Add a new user to the system""" + """Update the host of a Kubernetes-CoreOS Cluster.""" try: if not opts.json_file_path: print("usage: update-kubernetes-host [-f ]") @@ -1287,7 +1379,6 @@ def do_update_kubernetes_host(self, line , opts=None): print("Error updating Kubernetes host") except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1295,7 +1386,7 @@ def do_update_kubernetes_host(self, line , opts=None): ]) @auth def do_remove_kubernetes_cluster(self, kubernetes_cluster_id, opts=None): - """Delete a kubernetes cluster""" + """Delete a Kubernetes-CoreOS Cluster.""" try: if not kubernetes_cluster_id: print("usage: remove-kubernetes-cluster [cluster-id]") @@ -1307,7 +1398,6 @@ def do_remove_kubernetes_cluster(self, kubernetes_cluster_id, opts=None): print("Kubernetes cluster not found : "+kubernetes_cluster_id) except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1317,7 +1407,7 @@ def do_remove_kubernetes_cluster(self, kubernetes_cluster_id, opts=None): ]) @auth def do_remove_kubernetes_host(self, line, opts=None): - """Delete a kubernetes host""" + """Delete the host of a Kubernetes-CoreOS Cluster.""" try: if not opts.cluster_id or not opts.host_id: print("usage: remove-kubernetes-host [-c cluster-id] [-o host-id]") @@ -1329,8 +1419,6 @@ def do_remove_kubernetes_host(self, line, opts=None): print("Kubernetes host not found : "+opts.cluster_id+"/"+opts.host_id) except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) - """ # Domain Mapping @@ -1346,21 +1434,23 @@ def do_remove_kubernetes_host(self, line, opts=None): @auth def do_list_domain_mappings(self, application_id , opts=None): """Illustrate the base class method use.""" - if not application_id: - print("usage: list-domain-mappings [application-id]") - else: - domain_mappings = Stratos.list_domain_mappings(application_id) - if domain_mappings: - table = PrintableTable() - rows = [["Domain", "Tenant ID", "Email", " State", "Created Date"]] - for domain_mapping in domain_mappings: - rows.append([domain_mapping['domain_mappingsDomain'], domain_mapping['domain_mappingId'], domain_mapping['email'], - "Active" if domain_mapping['active'] else "De-Active", datetime.datetime.fromtimestamp(domain_mapping['createdDate']/1000).strftime('%Y-%m-%d %H:%M:%S')]) - table.add_rows(rows) - table.print_table() + try: + if not application_id: + print("usage: list-domain-mappings [application-id]") else: - print("No domain mappings found in application: "+application_id) - + domain_mappings = Stratos.list_domain_mappings(application_id) + if domain_mappings: + table = PrintableTable() + rows = [["Domain", "Tenant ID", "Email", " State", "Created Date"]] + for domain_mapping in domain_mappings: + rows.append([domain_mapping['domain_mappingsDomain'], domain_mapping['domain_mappingId'], domain_mapping['email'], + "Active" if domain_mapping['active'] else "De-Active", datetime.datetime.fromtimestamp(domain_mapping['createdDate']/1000).strftime('%Y-%m-%d %H:%M:%S')]) + table.add_rows(rows) + table.print_table() + else: + print("No domain mappings found in application: "+application_id) + except BadResponseError as e: + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1381,8 +1471,6 @@ def do_add_domain_mapping(self, application_id, opts=None): print("Error creating domain mapping") except BadResponseError as e: self.perror(str(e)) - logging.error(str(e)) - @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -1403,24 +1491,3 @@ def do_remove_domain_mappings(self, domain , opts=None): except BadResponseError as e: self.perror(str(e)) logging.error("HTTP "+e.error_code+" : "+str(e)) - - @options([ - make_option('-u', '--username', type="str", help="Username of the user"), - make_option('-p', '--password', type="str", help="Password of the user"), - make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") - ]) - @auth - def do_add_autoscaling_policy(self, line , opts=None): - """Add a new user to the system""" - try: - if not opts.json_file_path: - print("usage: add-autoscaling-policy [-f ]") - else: - autoscaling_policy = Stratos.add_autoscaling_policy(open(opts.json_file_path, 'r').read()) - if autoscaling_policy: - print("Autoscaling policy added successfully") - else: - print("Error adding autoscaling policy") - except BadResponseError as e: - self.perror(str(e)) - logging.error(str(e)) \ No newline at end of file diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py index e826b07a14..707e4a91e6 100755 --- a/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py +++ b/components/org.apache.stratos.python.cli/src/main/python/cli/Main.py @@ -23,6 +23,7 @@ # Fix Python 2.x. from cli.Stratos import Stratos +from cli.exceptions import BadResponseError try: input = raw_input @@ -53,7 +54,10 @@ def main(): cli = CLI() if len(sys.argv) > 1: - cli.onecmd(' '.join(sys.argv[1:])) + try: + cli.onecmd(' '.join(sys.argv[1:])) + except BadResponseError as e: + print(str(e)) else: prompt_for_credentials() cli.cmdloop() 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 d1a300d501..d69df86eb0 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 @@ -18,6 +18,7 @@ import requests import json import Configs +from Logging import logging from cli.exceptions.BadResponseError import BadResponseError @@ -37,7 +38,7 @@ def __init__(self): """ @staticmethod def list_users(): - return Stratos.get('users', error_message='No users found') + return Stratos.get('users') @staticmethod def add_users(username, password, role_name, first_name, last_name, email, profile_name): @@ -49,7 +50,7 @@ def add_users(username, password, role_name, first_name, last_name, email, profi "lastName": last_name, "email": email } - return Stratos.post('users', json.dumps(data), error_message='No applications found') + return Stratos.post('users', json.dumps(data)) @staticmethod def update_user(username, password, role_name, first_name, last_name, email, profile_name): @@ -61,11 +62,11 @@ def update_user(username, password, role_name, first_name, last_name, email, pro "lastName": last_name, "email": email } - return Stratos.put('users', json.dumps(data), error_message='No applications found') + return Stratos.put('users', json.dumps(data)) @staticmethod def remove_user(name): - return Stratos.delete('users/'+name, error_message="Requested user "+name+" does not exist") + return Stratos.delete('users/'+name) """ # Applications @@ -79,12 +80,11 @@ def remove_user(name): """ @staticmethod def list_applications(): - return Stratos.get('applications', error_message='No applications found') + return Stratos.get('applications') @staticmethod def describe_application(application_id): - return Stratos.get('applications/'+application_id, - error_message='No application found') + return Stratos.get('applications/'+application_id) @staticmethod def remove_application(application): @@ -93,10 +93,10 @@ def remove_application(application): @staticmethod def deploy_application(application_id, application_policy_id): return Stratos.post('applications/'+application_id+'/deploy/'+application_policy_id, None, - error_message='No application found') + ) @staticmethod def describe_application_runtime(application_id): - return Stratos.get('applications/'+application_id+"/runtime", error_message='No application runtime found') + return Stratos.get('applications/'+application_id+"/runtime") """ # Application signup @@ -106,11 +106,10 @@ def describe_application_runtime(application_id): """ @staticmethod def describe_application_signup(application_id): - return Stratos.get('applications/'+application_id+'/signup', - error_message='No signup application found') + return Stratos.get('applications/'+application_id+'/signup') @staticmethod def add_application_signup(application_id, json): - return Stratos.post('applications/'+application_id+"/runtime", json, error_message='No application runtime found') + return Stratos.post('applications/'+application_id+"/runtime", json) @staticmethod def remove_application_signup(application_id): @@ -128,15 +127,15 @@ def remove_application_signup(application_id): """ @staticmethod def list_tenants(): - return Stratos.get('tenants', error_message='No cartridges found') + return Stratos.get('tenants') @staticmethod def list_tenants_by_partial_domain(partial_domain): - return Stratos.get('tenants/search/'+partial_domain, error_message='No cartridges found') + return Stratos.get('tenants/search/'+partial_domain) @staticmethod def describe_tenant(tenant_domain_name): - return Stratos.get('tenants/'+tenant_domain_name, error_message='No cartridge found') + return Stratos.get('tenants/'+tenant_domain_name) @staticmethod def add_tenant(username, first_name, last_name, password, domain_name, email): @@ -149,7 +148,7 @@ def add_tenant(username, first_name, last_name, password, domain_name, email): "email": email, "active": "true" } - return Stratos.post('tenants', json.dumps(data), error_message='No tenant found') + return Stratos.post('tenants', json.dumps(data)) @staticmethod def update_tenant(username, first_name, last_name, password, domain_name, email, tenant_id): @@ -163,15 +162,15 @@ def update_tenant(username, first_name, last_name, password, domain_name, email, "email": email, "active": "true" } - return Stratos.put('tenants', json.dumps(data), error_message='No tenant found') + return Stratos.put('tenants', json.dumps(data)) @staticmethod def activate_tenant(tenant_domain): - return Stratos.put('tenants/activate/'+tenant_domain, "", error_message='No tenant found') + return Stratos.put('tenants/activate/'+tenant_domain, "") @staticmethod def deactivate_tenant(tenant_domain): - return Stratos.put('tenants/deactivate/'+tenant_domain, "", error_message='No tenant found') + return Stratos.put('tenants/deactivate/'+tenant_domain, "") """ # Cartridges @@ -183,19 +182,23 @@ def deactivate_tenant(tenant_domain): """ @staticmethod def list_cartridges(): - return Stratos.get('cartridges', error_message='No cartridges found') + return Stratos.get('cartridges') + + @staticmethod + def list_cartridges_by_filter(filter_text): + return Stratos.get('cartridges/filter/'+filter_text) @staticmethod def describe_cartridge(cartridge_type): - return Stratos.get('cartridges/'+cartridge_type, error_message='Cartridge not found') + return Stratos.get('cartridges/'+cartridge_type) @staticmethod def add_cartridge(json): - return Stratos.post('cartridges', json, error_message='No cartridge found') + return Stratos.post('cartridges', json) @staticmethod def update_cartridge(json): - return Stratos.put('cartridges', json, error_message='No cartridge found') + return Stratos.put('cartridges', json) @staticmethod def remove_cartridge(cartridge_type): @@ -212,19 +215,19 @@ def remove_cartridge(cartridge_type): @staticmethod def list_cartridge_groups(): - return Stratos.get('cartridgeGroups', error_message='No cartridge groups found') + return Stratos.get('cartridgeGroups') @staticmethod def describe_cartridge_group(group_definition_name): - return Stratos.get('cartridgeGroups/'+group_definition_name, error_message='No cartridge groups found') + return Stratos.get('cartridgeGroups/'+group_definition_name) @staticmethod def add_cartridge_group(json): - return Stratos.post('cartridgeGroups', json, error_message='No cartridge group found') + return Stratos.post('cartridgeGroups', json) @staticmethod def update_cartridge_group(json): - return Stratos.put('cartridgeGroups', json, error_message='No cartridge found') + return Stratos.put('cartridgeGroups', json) @staticmethod def remove_cartridge_group(group_definition_name): @@ -241,19 +244,17 @@ def remove_cartridge_group(group_definition_name): """ @staticmethod def list_deployment_policies(): - return Stratos.get('deploymentPolicies', - error_message='Deployment policies not found') + return Stratos.get('deploymentPolicies') @staticmethod def describe_deployment_policy(deployment_policy_name): - return Stratos.get('deploymentPolicies/'+ deployment_policy_name, - error_message='No deployment policies found') + return Stratos.get('deploymentPolicies/'+ deployment_policy_name) @staticmethod def add_deployment_policy(json): - return Stratos.post('deploymentPolicies', json, error_message='No deployment policy found') + return Stratos.post('deploymentPolicies', json) @staticmethod def update_deployment_policy(json): - return Stratos.put('deploymentPolicies', json, error_message='No deployment policies found') + return Stratos.put('deploymentPolicies', json) @staticmethod def remove_deployment_policy(deployment_policy_id): @@ -269,15 +270,13 @@ def remove_deployment_policy(deployment_policy_id): """ @staticmethod def list_application_policies(): - return Stratos.get('applicationPolicies', - error_message='Deployment policies not found') + return Stratos.get('applicationPolicies') @staticmethod def describe_application_policy(application_policy_name): - return Stratos.get('applicationPolicies/'+ application_policy_name, - error_message='No application policies found') + return Stratos.get('applicationPolicies/'+ application_policy_name) @staticmethod def update_application_policy(json): - return Stratos.put('applicationPolicies', json, error_message='No application policies found') + return Stratos.put('applicationPolicies', json) @staticmethod def remove_application_policy(application_policy_id): @@ -294,18 +293,17 @@ def remove_application_policy(application_policy_id): """ @staticmethod def list_network_partitions(): - return Stratos.get('networkPartitions', error_message='No network partitions found') + return Stratos.get('networkPartitions') @staticmethod def describe_network_partition(network_partition_id): - return Stratos.get('networkPartitions/'+network_partition_id, - error_message='No network partitions found') + return Stratos.get('networkPartitions/'+network_partition_id) @staticmethod def add_network_partition(json): - return Stratos.post('networkPartitions', json, error_message='No network partition found') + return Stratos.post('networkPartitions', json) @staticmethod def update_network_partition(json): - return Stratos.put('networkPartitions', json, error_message='No cartridge found') + return Stratos.put('networkPartitions', json) @staticmethod @@ -316,25 +314,29 @@ def remove_network_partition(network_partition_id): # Auto-scaling policies * list-autoscaling-policies * describe-autoscaling-policy + * add-autoscaling-policy * update-autoscaling-policy * remove-autoscaling-policy """ @staticmethod def list_autoscaling_policies(): - return Stratos.get('autoscalingPolicies', - error_message='No Autoscaling policies found') + return Stratos.get('autoscalingPolicies') @staticmethod def describe_autoscaling_policy(autoscaling_policy_id): - return Stratos.get('autoscalingPolicies/'+autoscaling_policy_id, - error_message='No autoscaling policy found') + return Stratos.get('autoscalingPolicies/'+autoscaling_policy_id) + + @staticmethod + def add_autoscaling_policy(json): + return Stratos.post('autoscalingPolicies', json) + @staticmethod def update_autoscaling_policy(json): - return Stratos.put('autoscalingPolicies', json, error_message='No cartridge found') + return Stratos.put('autoscalingPolicies', json) @staticmethod def remove_autoscaling_policy(autoscaling_policy_id): - return Stratos.delete('autoscalingPolicies/'+autoscaling_policy_id, error_message="Autoscaling policy not found") + return Stratos.delete('autoscalingPolicies/'+autoscaling_policy_id) """ # Kubernetes clusters/hosts @@ -351,45 +353,43 @@ def remove_autoscaling_policy(autoscaling_policy_id): """ @staticmethod def list_kubernetes_clusters(): - return Stratos.get('kubernetesClusters', error_message='Kubernetes cluster not found') + return Stratos.get('kubernetesClusters') @staticmethod def describe_kubernetes_cluster(kubernetes_cluster_id): - return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id, - error_message='Kubernetes cluster not found') + return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id) @staticmethod def describe_kubernetes_master(kubernetes_cluster_id): - return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id+'/master', error_message='No kubernetes clusters found') + return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id+'/master') @staticmethod def add_kubernetes_cluster(json): - return Stratos.post('kubernetesClusters', json, error_message='No kubernetes cluster found') + return Stratos.post('kubernetesClusters', json) @staticmethod def add_kubernetes_host(kubernetes_cluster_id, json): - return Stratos.post('kubernetesClusters/'+kubernetes_cluster_id+'/minion', json, error_message='No kubernetes cluster found') + return Stratos.post('kubernetesClusters/'+kubernetes_cluster_id+'/minion', json) @staticmethod def list_kubernetes_hosts(kubernetes_cluster_id): - return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id+'/hosts', - error_message='Kubernetes cluster not found') + return Stratos.get('kubernetesClusters/'+kubernetes_cluster_id+'/hosts') @staticmethod def update_kubernetes_master(cluster_id, json): - return Stratos.put('kubernetesClusters/'+cluster_id+'/master', json, error_message='No cartridge found') + return Stratos.put('kubernetesClusters/'+cluster_id+'/master', json) @staticmethod def update_kubernetes_host(json): - return Stratos.put('kubernetesClusters/update/host', json, error_message='No cartridge found') + return Stratos.put('kubernetesClusters/update/host', json) @staticmethod def remove_kubernetes_cluster(kubernetes_cluster_id): return Stratos.delete('kubernetesClusters/'+kubernetes_cluster_id, - error_message="Autoscaling policy not found") + ) @staticmethod def remove_kubernetes_host(kubernetes_cluster_id, host_id): return Stratos.delete('kubernetesClusters/'+kubernetes_cluster_id+"/hosts/"+host_id, - error_message="Autoscaling policy not found") + ) """ # Domain Mapping @@ -401,8 +401,7 @@ def remove_kubernetes_host(kubernetes_cluster_id, host_id): @staticmethod def list_domain_mappings(application_id): - return Stratos.get('applications/'+application_id+'/domainMappings', - error_message='No domain mapping found') + return Stratos.get('applications/'+application_id+'/domainMappings') @staticmethod def remove_domain_mappings(application_id): @@ -411,7 +410,7 @@ def remove_domain_mappings(application_id): @staticmethod def add_domain_mapping(application_id, json): return Stratos.post('applications/'+application_id+'/domainMappings', json, - error_message='No domain mapping found') + ) """ # Utils @@ -427,102 +426,52 @@ def authenticate(): return False @staticmethod - def delete(resource, error_message=None): + def get(resource): + r = requests.get(Configs.stratos_api_url + resource, + auth=(Configs.stratos_username, Configs.stratos_password), verify=False) + return Stratos.response(r) + + @staticmethod + def delete(resource): r = requests.delete(Configs.stratos_api_url + resource, auth=(Configs.stratos_username, Configs.stratos_password), verify=False) - print(r) - print(r.text) - if r.status_code == 200: - return r.json() - elif r.status_code == 400: - raise BadResponseError(str(r.status_code), r.json()['message']) - elif r.status_code == 401: - raise BadResponseError(str(r.status_code), r.json()['message']) - elif r.status_code == 404: - if r.text and r.json() and r.json()['message']: - return False - elif r.status_code == 404: - if r.text and r.json() and r.json()['message']: - return False - else: - raise BadResponseError(str(r.status_code), r.json()['message']) - else: - raise BadResponseError(str(r.status_code), r.json()['message']) + return Stratos.response(r) @staticmethod - def post(resource, data, error_message=None): + def post(resource, data): headers = {'content-type': 'application/json'} r = requests.post(Configs.stratos_api_url + resource, data, headers=headers, auth=(Configs.stratos_username, Configs.stratos_password), verify=False) - print(r) - print(r.text) - if r.status_code == 200: - return r.json() - elif r.status_code == 201: - return True - elif r.status_code == 400: - raise BadResponseError(str(r.status_code), r.json()['message']) - elif r.status_code == 401: - raise BadResponseError(str(r.status_code), r.json()['message']) - elif r.status_code == 404: - if r.text and r.json() and r.json()['message'] == error_message: - return [] - else: - raise BadResponseError(str(r.status_code), r.text) - else: - raise BadResponseError(str(r.status_code), r.json()['message']) + return Stratos.response(r) @staticmethod - def put(resource, data, error_message): + def put(resource, data): headers = {'content-type': 'application/json'} r = requests.put(Configs.stratos_api_url + resource, data, headers=headers, auth=(Configs.stratos_username, Configs.stratos_password), verify=False) + return Stratos.response(r) + + + @staticmethod + def response(r): print(r) print(r.text) if r.status_code == 200: return r.json() elif r.status_code == 201: return True - elif r.status_code == 400: - raise BadResponseError(str(r.status_code), r.json()['message']) - elif r.status_code == 401: - raise BadResponseError(str(r.status_code), r.json()['message']) - elif r.status_code == 404: - if r.text and r.json() and r.json()['message'] == error_message: - return [] - else: + elif r.status_code == 202: + return True + elif r.status_code >= 400: + if r.text and r.json() and r.json()['message']: + logging.error("HTTP "+str(r.status_code)+" : "+r.json()['message']) raise BadResponseError(str(r.status_code), r.json()['message']) - else: - raise BadResponseError(str(r.status_code), r.json()['message']) - - - @staticmethod - def add_autoscaling_policy(json): - return Stratos.post('autoscalingPolicies', json, error_message='No autoscaling policy found') + else: + logging.error("HTTP "+str(r.status_code)+" : Could not connect to Stratos server") + raise BadResponseError(str(r.status_code), "Could not connect to Stratos server") @staticmethod def add_application(json): - return Stratos.post('applications', json, error_message='No application found') - - -def get(resource, error_message=""): - r = requests.get(Configs.stratos_api_url + resource, - auth=(Configs.stratos_username, Configs.stratos_password), verify=False) - print(r) - print(r.text) - if r.status_code == 200: - return r.json() - elif r.status_code == 400: - raise BadResponseError(str(r.status_code), r.json()['message']) - elif r.status_code == 401: - raise BadResponseError(str(r.status_code), r.json()['message']) - elif r.status_code == 404: - if r.text and r.json() and r.json()['message'] == error_message: - return False - else: - raise BadResponseError(str(r.status_code), " : "+r.text) - else: - raise BadResponseError(str(r.status_code), r.json()['message']) - return response(r) + return Stratos.post('applications', json) From 675ae2fcc1daa55d1a1146d9fc27e3028563279b Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Mon, 17 Aug 2015 11:07:07 +0530 Subject: [PATCH 68/79] mock object for tests --- .../python/tests/test_printable_tree_print.py | 98 ++++++++++++++++--- 1 file changed, 87 insertions(+), 11 deletions(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/tests/test_printable_tree_print.py b/components/org.apache.stratos.python.cli/src/main/python/tests/test_printable_tree_print.py index 2a386031ae..d22139bdb7 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/tests/test_printable_tree_print.py +++ b/components/org.apache.stratos.python.cli/src/main/python/tests/test_printable_tree_print.py @@ -15,8 +15,9 @@ # specific language governing permissions and limitations # under the License. -import sys -from cli import Utils +from cli import Stratos +from cli import Configs +import responses class TestClass: @@ -25,14 +26,89 @@ def __init__(self): pass @staticmethod - def test_zero(): - i = 1 - assert i == 1 + @responses.activate + def test_http_get_handler_on_200(): + responses.add(responses.GET, Configs.stratos_api_url, + body='{"keyOne": "valueOne"}', status=200, + content_type='application/json') + + r = Stratos.get("") + + assert r == {"keyOne": "valueOne"} + + @staticmethod + @responses.activate + def test_http_get_handler_on_400(): + responses.add(responses.GET, Configs.stratos_api_url, + body='', status=400, + content_type='application/json') + + r = Stratos.get("") + + assert r == {"keyOne": "valueOne"} + + @staticmethod + @responses.activate + def test_http_get_handler_on_200(): + responses.add(responses.GET, Configs.stratos_api_url, + body='{"keyOne": "valueOne"}', status=200, + content_type='application/json') + + r = Stratos.get("") + + assert r == {"keyOne": "valueOne"} + + @staticmethod + @responses.activate + def test_http_get_handler_on_200(): + responses.add(responses.GET, Configs.stratos_api_url, + body='{"keyOne": "valueOne"}', status=200, + content_type='application/json') + + r = Stratos.get("") + + assert r == {"keyOne": "valueOne"} @staticmethod - def test_one(): - tree = Utils.PrintableTree( - """[{"id":"network-partition-2","partitions":[{"id":"partition-2","partitionMax":0,"property":[{"name":"region","value":"default"}],"public":false},{"id":"partition-3","partitionMax":0,"property":[{"name":"region","value":"default"}],"public":false}]},{"id":"network-partition-1","partitions":[{"id":"partition-1","partitionMax":0,"property":[{"name":"region","value":"default"}],"public":false}]}]""") - tree.print_tree() - output = sys.stdout.getline().strip() # because stdout is an StringIO instance - assert output == 'hello world!' + @responses.activate + def test_http_get_handler_on_200(): + responses.add(responses.GET, Configs.stratos_api_url, + body='{"keyOne": "valueOne"}', status=200, + content_type='application/json') + + r = Stratos.get("") + + assert r == {"keyOne": "valueOne"} + + @staticmethod + @responses.activate + def test_http_get_handler_on_200(): + responses.add(responses.GET, Configs.stratos_api_url, + body='{"keyOne": "valueOne"}', status=200, + content_type='application/json') + + r = Stratos.get("") + + assert r == {"keyOne": "valueOne"} + + @staticmethod + @responses.activate + def test_http_get_handler_on_200(): + responses.add(responses.GET, Configs.stratos_api_url, + body='{"keyOne": "valueOne"}', status=200, + content_type='application/json') + + r = Stratos.get("") + + assert r == {"keyOne": "valueOne"} + + @staticmethod + @responses.activate + def test_http_get_handler_on_200(): + responses.add(responses.GET, Configs.stratos_api_url, + body='{"keyOne": "valueOne"}', status=200, + content_type='application/json') + + r = Stratos.get("") + + assert r == {"keyOne": "valueOne"} From 41e53ef6ce08c7c5515ef64b3bbf4b9ae4a96bbd Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Mon, 17 Aug 2015 23:45:39 +0530 Subject: [PATCH 69/79] Fixed doc strings --- .../src/main/python/cli/CLI.py | 85 +++++++++++++------ .../src/main/python/cli/Stratos.py | 19 +++-- 2 files changed, 74 insertions(+), 30 deletions(-) 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 88d6b924e4..6dc22018df 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 @@ -146,7 +146,10 @@ def do_remove_user(self, name , opts=None): * list-applications * describe-application * add-application + * update-application * remove-application + * describe-application-runtime + * deploy-application """ @@ -156,7 +159,7 @@ def do_remove_user(self, name , opts=None): ]) @auth def do_list_applications(self, line , opts=None): - """Illustrate the base class method use.""" + """Retrieve details of all the applications.""" try: applications = Stratos.list_applications() if not applications: @@ -177,7 +180,7 @@ def do_list_applications(self, line , opts=None): ]) @auth def do_describe_application(self, application_id , opts=None): - """Retrieve detailed information on the master node in a specific Kubernetes-CoreOS group""" + """Describe an application.""" try: if not application_id: print("usage: describe-application [cluster-id]") @@ -198,7 +201,7 @@ def do_describe_application(self, application_id , opts=None): ]) @auth def do_add_application(self, line , opts=None): - """Add a new user to the system""" + """Add an application.""" try: if not opts.json_file_path: print("usage: add-application [-f ]") @@ -211,6 +214,26 @@ def do_add_application(self, line , opts=None): except BadResponseError as e: self.perror(str(e)) + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") + ]) + @auth + def do_update_application(self, application , opts=None): + """Update an application.""" + try: + if not opts.json_file_path: + print("usage: update-application [-f ] [application]") + else: + update_application = Stratos.update_application(application, open(opts.json_file_path, 'r').read()) + if update_application: + print("Application updated successfully") + else: + print("Error updating application") + except BadResponseError as e: + self.perror(str(e)) + @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -218,7 +241,7 @@ def do_add_application(self, line , opts=None): ]) @auth def do_remove_application(self, application , opts=None): - """Delete a specific user""" + """Delete an application.""" try: if not application: print("usage: remove-application [application]") @@ -252,12 +275,26 @@ def do_deploy_application(self, line , opts=None): except BadResponseError as e: self.perror(str(e)) - """ - # Application deployment - * describe-application-runtime - * deploy-application - - """ + @options([ + make_option('-u', '--username', type="str", help="Username of the user"), + make_option('-p', '--password', type="str", help="Password of the user"), + make_option('-a', '--application_id', type="str", help="Unique ID of the application"), + make_option('-o', '--application_policy_id', type="str", help="Unique ID of the application policy") + ]) + @auth + def do_undeploy_application(self, line , opts=None): + """Undeploy an application.""" + try: + if not opts.application_id or not opts.application_policy_id: + print("usage: undeploy-application [-a ] [-o ]") + else: + application_removed = Stratos.undeploy_application(opts.application_id, opts.application_policy_id) + if application_removed: + print("You have successfully undeployed application: "+opts.application_id) + else: + print("Could not undeployed application : "+opts.application_id) + except BadResponseError as e: + self.perror(str(e)) @options([ make_option('-u', '--username', type="str", help="Username of the user"), @@ -265,7 +302,7 @@ def do_deploy_application(self, line , opts=None): ]) @auth def do_describe_application_runtime(self, application_id , opts=None): - """Retrieve details of a specific auto-scaling policy.""" + """Describe the runtime topology of an application.""" try: if not application_id: print("usage: describe-application-runtime [application-id]") @@ -331,7 +368,7 @@ def do_add_application_signup(self, application_id, opts=None): ]) @auth def do_remove_application_signup(self, signup , opts=None): - """Delete a specific user""" + """Delete an application sign up.""" try: if not signup: print("usage: remove-application-signup [signup]") @@ -613,7 +650,7 @@ def do_add_cartridge(self, line , opts=None): ]) @auth def do_update_cartridge(self, line , opts=None): - """Add a new user to the system""" + """Update a cartridge""" try: if not opts.json_file_path: print("usage: update-cartridge [-f ]") @@ -661,7 +698,7 @@ def do_remove_cartridge(self, cartridge_type , opts=None): ]) @auth def do_list_cartridge_groups(self, line , opts=None): - """Illustrate the base class method use.""" + """Retrieve details of all the cartridge groups.""" try: cartridge_groups = Stratos.list_cartridge_groups() if not cartridge_groups: @@ -704,7 +741,7 @@ def do_describe_cartridge_group(self, group_definition_name , opts=None): ]) @auth def do_add_cartridge_group(self, line , opts=None): - """Add a new user to the system""" + """Add a cartridge group.""" try: if not opts.json_file_path: print("usage: add-cartridge-group [-f ]") @@ -743,7 +780,7 @@ def do_update_cartridge_group(self, line , opts=None): ]) @auth def do_remove_cartridge_group(self, group_definition_name , opts=None): - """Delete a cartridge""" + """Delete a cartridge group.""" try: if not group_definition_name: print("usage: remove-cartridge-group [cartridge-group-name]") @@ -834,7 +871,7 @@ def do_add_deployment_policy(self, line , opts=None): make_option('-f', '--json_file_path', type="str", help="Path of the JSON file") ]) @auth - def do_update_application_policy(self, line , opts=None): + def do_update_deployment_policy(self, line , opts=None): """Update a deployment policy.""" try: if not opts.json_file_path: @@ -884,7 +921,7 @@ def do_remove_deployment_policy(self, deployment_policy_id , opts=None): ]) @auth def do_list_application_policies(self, line , opts=None): - """Illustrate the base class method use.""" + """Retrieve details of all the application policies.""" try: application_policies = Stratos.list_application_policies() if not application_policies: @@ -926,7 +963,7 @@ def do_describe_application_policy(self, application_policy_id , opts=None): ]) @auth def do_add_application_policy(self, line , opts=None): - """Add a new user to the system""" + """Add an application policy.""" try: if not opts.json_file_path: print("usage: add-application-policy [-f ]") @@ -946,7 +983,7 @@ def do_add_application_policy(self, line , opts=None): ]) @auth def do_update_application_policy(self, line , opts=None): - """Add a new user to the system""" + """Update an application policy.""" try: if not opts.json_file_path: print("usage: update-application-policy [-f ]") @@ -965,7 +1002,7 @@ def do_update_application_policy(self, line , opts=None): ]) @auth def do_remove_application_policy(self, application_policy_id , opts=None): - """Delete a cartridge""" + """Delete an application policy.""" try: if not application_policy_id: print("usage: remove-application-policy [application-policy-id]") @@ -1433,7 +1470,7 @@ def do_remove_kubernetes_host(self, line, opts=None): ]) @auth def do_list_domain_mappings(self, application_id , opts=None): - """Illustrate the base class method use.""" + """Retrieve details of domain mappings of an application.""" try: if not application_id: print("usage: list-domain-mappings [application-id]") @@ -1459,7 +1496,7 @@ def do_list_domain_mappings(self, application_id , opts=None): ]) @auth def do_add_domain_mapping(self, application_id, opts=None): - """Add a new user to the system""" + """Map domain to a subscribed cartridge.""" try: if not application_id or not opts.json_file_path: print("usage: add-domain-mapping [-f ] [application id]") @@ -1478,7 +1515,7 @@ def do_add_domain_mapping(self, application_id, opts=None): ]) @auth def do_remove_domain_mappings(self, domain , opts=None): - """Delete a specific user""" + """Remove domain mappings of an application.""" try: if not domain: print("usage: remove-domain-mappings [domain]") 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 d69df86eb0..e26e133379 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 @@ -86,14 +86,25 @@ def list_applications(): def describe_application(application_id): return Stratos.get('applications/'+application_id) + @staticmethod + def add_application(json): + return Stratos.post('applications', json) + + @staticmethod + def update_application(application_id, json): + return Stratos.put('applications/'+application_id, json) + @staticmethod def remove_application(application): return Stratos.delete('applications/'+application) @staticmethod def deploy_application(application_id, application_policy_id): - return Stratos.post('applications/'+application_id+'/deploy/'+application_policy_id, None, - ) + return Stratos.post('applications/'+application_id+'/deploy/'+application_policy_id, None) + + @staticmethod + def deploy_application(application_id, application_policy_id): + return Stratos.post('applications/'+application_id+'/undeploy/'+application_policy_id, None) @staticmethod def describe_application_runtime(application_id): return Stratos.get('applications/'+application_id+"/runtime") @@ -470,8 +481,4 @@ def response(r): logging.error("HTTP "+str(r.status_code)+" : Could not connect to Stratos server") raise BadResponseError(str(r.status_code), "Could not connect to Stratos server") - @staticmethod - def add_application(json): - return Stratos.post('applications', json) - From 0140a6cb87a67f8f68f5fd7c9eaff52d51213607 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Tue, 18 Aug 2015 00:02:23 +0530 Subject: [PATCH 70/79] tests updated --- .../src/main/python/tests/test_print.py | 33 -------- ...le_tree_print.py => test_stratos_utils.py} | 79 ++++++++----------- 2 files changed, 31 insertions(+), 81 deletions(-) delete mode 100644 components/org.apache.stratos.python.cli/src/main/python/tests/test_print.py rename components/org.apache.stratos.python.cli/src/main/python/tests/{test_printable_tree_print.py => test_stratos_utils.py} (54%) diff --git a/components/org.apache.stratos.python.cli/src/main/python/tests/test_print.py b/components/org.apache.stratos.python.cli/src/main/python/tests/test_print.py deleted file mode 100644 index 8d0b5aab59..0000000000 --- a/components/org.apache.stratos.python.cli/src/main/python/tests/test_print.py +++ /dev/null @@ -1,33 +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. - -import sys -import unittest -from cli import Utils - - -class MyTestCase(unittest.TestCase): - def test_something(self): - tree = Utils.PrintableTree( - """[{"id":"network-partition-2","partitions":[{"id":"partition-2","partitionMax":0,"property":[{"name":"region","value":"default"}],"public":false},{"id":"partition-3","partitionMax":0,"property":[{"name":"region","value":"default"}],"public":false}]},{"id":"network-partition-1","partitions":[{"id":"partition-1","partitionMax":0,"property":[{"name":"region","value":"default"}],"public":false}]}]""") - tree.print_tree() - output = "hello world!" - self.assertEqual(output, 'hello world!') - - -if __name__ == '__main__': - unittest.main() diff --git a/components/org.apache.stratos.python.cli/src/main/python/tests/test_printable_tree_print.py b/components/org.apache.stratos.python.cli/src/main/python/tests/test_stratos_utils.py similarity index 54% rename from components/org.apache.stratos.python.cli/src/main/python/tests/test_printable_tree_print.py rename to components/org.apache.stratos.python.cli/src/main/python/tests/test_stratos_utils.py index d22139bdb7..85acb4b548 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/tests/test_printable_tree_print.py +++ b/components/org.apache.stratos.python.cli/src/main/python/tests/test_stratos_utils.py @@ -15,100 +15,83 @@ # specific language governing permissions and limitations # under the License. +import unittest from cli import Stratos from cli import Configs +from cli.exceptions import BadResponseError import responses -class TestClass: +class MyTestCase(unittest.TestCase): - def __init__(self): - pass - @staticmethod @responses.activate - def test_http_get_handler_on_200(): + def test_http_get_handler_on_200(self): responses.add(responses.GET, Configs.stratos_api_url, body='{"keyOne": "valueOne"}', status=200, content_type='application/json') - r = Stratos.get("") + r = Stratos.post("") assert r == {"keyOne": "valueOne"} - @staticmethod @responses.activate - def test_http_get_handler_on_400(): + def test_http_get_handler_on_400(self): responses.add(responses.GET, Configs.stratos_api_url, body='', status=400, content_type='application/json') - r = Stratos.get("") + self.assertRaises(BadResponseError, Stratos.get("")) - assert r == {"keyOne": "valueOne"} - - @staticmethod @responses.activate - def test_http_get_handler_on_200(): + def test_http_get_handler_on_404(self): responses.add(responses.GET, Configs.stratos_api_url, - body='{"keyOne": "valueOne"}', status=200, + body='', status=404, content_type='application/json') - r = Stratos.get("") + self.assertRaises(BadResponseError, Stratos.get("")) - assert r == {"keyOne": "valueOne"} - - @staticmethod @responses.activate - def test_http_get_handler_on_200(): + def test_http_get_handler_on_500(self): responses.add(responses.GET, Configs.stratos_api_url, - body='{"keyOne": "valueOne"}', status=200, + body='', status=500, content_type='application/json') - r = Stratos.get("") + self.assertRaises(BadResponseError, Stratos.get("")) - assert r == {"keyOne": "valueOne"} - - @staticmethod @responses.activate - def test_http_get_handler_on_200(): - responses.add(responses.GET, Configs.stratos_api_url, + def test_http_post_handler_on_200(self): + responses.add(responses.POST, Configs.stratos_api_url, body='{"keyOne": "valueOne"}', status=200, content_type='application/json') - r = Stratos.get("") + r = Stratos.post("") - assert r == {"keyOne": "valueOne"} + assert r is True - @staticmethod @responses.activate - def test_http_get_handler_on_200(): - responses.add(responses.GET, Configs.stratos_api_url, - body='{"keyOne": "valueOne"}', status=200, + def test_http_post_handler_on_400(self): + responses.add(responses.POST, Configs.stratos_api_url, + body='', status=400, content_type='application/json') - r = Stratos.get("") - - assert r == {"keyOne": "valueOne"} + self.assertRaises(BadResponseError, Stratos.post("")) - @staticmethod @responses.activate - def test_http_get_handler_on_200(): - responses.add(responses.GET, Configs.stratos_api_url, - body='{"keyOne": "valueOne"}', status=200, + def test_http_post_handler_on_404(self): + responses.add(responses.POST, Configs.stratos_api_url, + body='', status=404, content_type='application/json') - r = Stratos.get("") + self.assertRaises(BadResponseError, Stratos.post("")) - assert r == {"keyOne": "valueOne"} - - @staticmethod @responses.activate - def test_http_get_handler_on_200(): - responses.add(responses.GET, Configs.stratos_api_url, - body='{"keyOne": "valueOne"}', status=200, + def test_http_post_handler_on_500(self): + responses.add(responses.POST, Configs.stratos_api_url, + body='', status=500, content_type='application/json') - r = Stratos.get("") + self.assertRaises(BadResponseError, Stratos.post("")) - assert r == {"keyOne": "valueOne"} +if __name__ == '__main__': + unittest.main() \ No newline at end of file From 3816639eb5498589559ad4deea3eefa060ff72a8 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Tue, 18 Aug 2015 21:37:38 +0530 Subject: [PATCH 71/79] fixed print statements --- .../org.apache.stratos.python.cli/src/main/python/cli/CLI.py | 3 ++- .../src/main/python/cli/Stratos.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) 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 6dc22018df..5be93dcbbd 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 @@ -80,6 +80,7 @@ def do_list_users(self, line , opts=None): make_option('-e', '--email', type="str", help="Email of the user"), make_option('-o', '--profile_name', type="str", help="Profile name of the user") ]) + @auth def do_add_user(self, line , opts=None): """Add a user.""" try: @@ -580,7 +581,7 @@ def do_list_cartridges_by_filter(self, filter_text , opts=None): """Retrieve details of available cartridges.""" try: if not filter_text: - print("") + print("usage: describe-cartridge-by-filter [filter]") else: cartridges = Stratos.list_cartridges_by_filter(filter_text) table = PrintableTable() 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 e26e133379..2f16d83698 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 @@ -465,8 +465,8 @@ def put(resource, data): @staticmethod def response(r): - print(r) - print(r.text) + # print(r) + # print(r.text) if r.status_code == 200: return r.json() elif r.status_code == 201: From 993ce64a31dd578ad775cc64a46a6256bf8bd155 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Tue, 25 Aug 2015 06:49:08 +0530 Subject: [PATCH 72/79] README updated --- .../src/main/python/README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/README.md b/components/org.apache.stratos.python.cli/src/main/python/README.md index 8b734d1091..4b523fe50d 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/README.md +++ b/components/org.apache.stratos.python.cli/src/main/python/README.md @@ -18,8 +18,19 @@ instead `install` if developing To start the Python CLI issue, ``` -$ stratos +$ stratos-cli ``` + +Install with Pip + +``` +pip install stratos +``` + +or to install a specific version use +``` +pip install stratos==1.0 +``` From 364f5cf4140f85f1c920549ca08a17211bf37011 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Tue, 25 Aug 2015 06:52:23 +0530 Subject: [PATCH 73/79] Changes to the help text of user --- .../src/main/python/cli/CLI.py | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) 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 5be93dcbbd..d4965f3de3 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 @@ -89,8 +89,8 @@ def do_add_user(self, line , opts=None): " [-l ] [-o ]") return else: - user = Stratos.add_users(opts.username_user, opts.password_user, opts.role_name, opts.first_name, opts.last_name, - opts.email, opts.profile_name) + user = Stratos.add_users(opts.username_user, opts.password_user, opts.role_name, opts.first_name, + opts.last_name, opts.email, opts.profile_name) if user: print("User successfully created") else: @@ -101,20 +101,20 @@ def do_add_user(self, line , opts=None): @options([ make_option('-u', '--username', type="str", help="Username of the user"), make_option('-p', '--password', type="str", help="Password of the user"), - make_option('-s', '--username_user', type="str", help="Username of the user"), - make_option('-a', '--password_user', type="str", help="Password of the user"), - make_option('-r', '--role_name', type="str", help="Role name of the user"), - make_option('-f', '--first_name', type="str", help="First name of the user"), - make_option('-l', '--last_name', type="str", help="Last name of the user"), - make_option('-e', '--email', type="str", help="Email of the user"), - make_option('-o', '--profile_name', type="str", help="Profile name of the user") + make_option('-s', '--username_user', type="str", help="Username of the user to be created"), + make_option('-a', '--password_user', type="str", help="Password of the user to be created"), + make_option('-r', '--role_name', type="str", help="Role name of the user to be created"), + make_option('-f', '--first_name', type="str", help="First name of the user to be created"), + make_option('-l', '--last_name', type="str", help="Last name of the user to be created"), + make_option('-e', '--email', type="str", help="Email of the user to be created"), + make_option('-o', '--profile_name', type="str", help="Profile name of the user to be created") ]) @auth def do_update_user(self, line , opts=None): """Update a specific user.""" try: - user = Stratos.update_user(opts.username_user, opts.password_user, opts.role_name, opts.first_name, opts.last_name, - opts.email, opts.profile_name) + user = Stratos.update_user(opts.username_user, opts.password_user, opts.role_name, opts.first_name, + opts.last_name, opts.email, opts.profile_name) if user: print("User successfully updated") else: @@ -134,7 +134,6 @@ def do_remove_user(self, name , opts=None): print("usage: remove-user [username]") else: user_removed = Stratos.remove_user(name) - print(user_removed) if user_removed: print("You have successfully deleted user: "+name) else: @@ -1528,4 +1527,4 @@ def do_remove_domain_mappings(self, domain , opts=None): print("Could not delete domain: "+domain) except BadResponseError as e: self.perror(str(e)) - logging.error("HTTP "+e.error_code+" : "+str(e)) + From 543198c9b03fae897015c013d5ff8c1b57634ca6 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Tue, 25 Aug 2015 06:52:45 +0530 Subject: [PATCH 74/79] improved formatting --- .../src/main/python/cli/Stratos.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) 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 2f16d83698..ebb3b8eddf 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 @@ -395,12 +395,11 @@ def update_kubernetes_host(json): @staticmethod def remove_kubernetes_cluster(kubernetes_cluster_id): - return Stratos.delete('kubernetesClusters/'+kubernetes_cluster_id, - ) + return Stratos.delete('kubernetesClusters/'+kubernetes_cluster_id) + @staticmethod def remove_kubernetes_host(kubernetes_cluster_id, host_id): - return Stratos.delete('kubernetesClusters/'+kubernetes_cluster_id+"/hosts/"+host_id, - ) + return Stratos.delete('kubernetesClusters/'+kubernetes_cluster_id+"/hosts/"+host_id) """ # Domain Mapping @@ -420,8 +419,7 @@ def remove_domain_mappings(application_id): @staticmethod def add_domain_mapping(application_id, json): - return Stratos.post('applications/'+application_id+'/domainMappings', json, - ) + return Stratos.post('applications/'+application_id+'/domainMappings', json) """ # Utils From 15b8dc80781b48513b1c03c041b61864085f746c Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Tue, 25 Aug 2015 06:52:58 +0530 Subject: [PATCH 75/79] removed universal flag for pip --- .../org.apache.stratos.python.cli/src/main/python/setup.cfg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/setup.cfg b/components/org.apache.stratos.python.cli/src/main/python/setup.cfg index 5e4090017a..a731f96fa9 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/setup.cfg +++ b/components/org.apache.stratos.python.cli/src/main/python/setup.cfg @@ -1,2 +1,2 @@ [wheel] -universal = 1 + From df0ce829a077e70b915fbb5cf7f95bdd19170421 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Tue, 25 Aug 2015 06:53:15 +0530 Subject: [PATCH 76/79] updated version --- .../org.apache.stratos.python.cli/src/main/python/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/setup.py b/components/org.apache.stratos.python.cli/src/main/python/setup.py index fce7decc14..e5f7a35e16 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/setup.py +++ b/components/org.apache.stratos.python.cli/src/main/python/setup.py @@ -25,7 +25,7 @@ def read(file_name): setup( name="stratos", - version="0.0.5", + version="1.0", author="Apache Stratos", author_email="dev@stratos.apache.org", description="CLI tool to interact with Apache Stratos", From 94da2243cc0ba5cbf9d06b9cbb8443805edab874 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Tue, 25 Aug 2015 06:53:28 +0530 Subject: [PATCH 77/79] bug fixed in test --- .../src/main/python/tests/test_stratos_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/tests/test_stratos_utils.py b/components/org.apache.stratos.python.cli/src/main/python/tests/test_stratos_utils.py index 85acb4b548..1f20c4b78c 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/tests/test_stratos_utils.py +++ b/components/org.apache.stratos.python.cli/src/main/python/tests/test_stratos_utils.py @@ -31,7 +31,7 @@ def test_http_get_handler_on_200(self): body='{"keyOne": "valueOne"}', status=200, content_type='application/json') - r = Stratos.post("") + r = Stratos.get("") assert r == {"keyOne": "valueOne"} From 60d1d8efe2d3ce3b3325f1b921f96da31c4cde85 Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Tue, 25 Aug 2015 07:14:25 +0530 Subject: [PATCH 78/79] changed the package name to stratos-cli --- .../org.apache.stratos.python.cli/src/main/python/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/org.apache.stratos.python.cli/src/main/python/setup.py b/components/org.apache.stratos.python.cli/src/main/python/setup.py index e5f7a35e16..07156a2ace 100644 --- a/components/org.apache.stratos.python.cli/src/main/python/setup.py +++ b/components/org.apache.stratos.python.cli/src/main/python/setup.py @@ -24,7 +24,7 @@ def read(file_name): return open(os.path.join(os.path.dirname(__file__), file_name)).read() setup( - name="stratos", + name="stratos-cli", version="1.0", author="Apache Stratos", author_email="dev@stratos.apache.org", From 985433a2b67ad20070266d3a6214726570215a7e Mon Sep 17 00:00:00 2001 From: Milindu Sanoj Kumarage Date: Tue, 25 Aug 2015 07:33:44 +0530 Subject: [PATCH 79/79] Printing Tree structure for describe-application command --- .../org.apache.stratos.python.cli/src/main/python/cli/CLI.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 d4965f3de3..ea66bd380f 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 @@ -190,7 +190,7 @@ def do_describe_application(self, application_id , opts=None): print("Application not found in : "+application_id) else: print("Application : "+application_id) - PrintableJSON(application).pprint() + PrintableTree(application).print_tree() except BadResponseError as e: self.perror(str(e))