Added new commands Signed-off-by: Imesh Gunaratne <[email protected]>
Project: http://git-wip-us.apache.org/repos/asf/stratos/repo Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/96d7501e Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/96d7501e Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/96d7501e Branch: refs/heads/stratos-4.1.x Commit: 96d7501e5d7bc4fa84011dbec4714510d0b90565 Parents: 7701003 Author: Milindu Sanoj Kumarage <[email protected]> Authored: Fri Aug 7 19:32:33 2015 +0530 Committer: Imesh Gunaratne <[email protected]> Committed: Tue Oct 13 16:32:48 2015 +0530 ---------------------------------------------------------------------- .../src/main/python/cli/CLI.py | 410 ++++++++++++------- .../src/main/python/cli/Stratos.py | 142 +++++-- 2 files changed, 366 insertions(+), 186 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/stratos/blob/96d7501e/components/org.apache.stratos.python.cli/src/main/python/cli/CLI.py ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/CLI.py b/components/org.apache.stratos.python.cli/src/main/python/cli/CLI.py index cd9ccb1..b595882 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 @@ class CLI(Cmd): """ 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 @@ class CLI(Cmd): 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 @@ class CLI(Cmd): @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 @@ class CLI(Cmd): 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"), @@ -154,11 +141,67 @@ class CLI(Cmd): 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") + ]) + @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 cartrige groups found") + print("No cartridge groups found") else: table = PrintableTable() rows = [["Name", "No. of cartridges", "No of groups", "Dependency scaling"]] @@ -173,6 +216,186 @@ class CLI(Cmd): 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") + ]) + @auth def do_list_applications(self, line , opts=None): """Illustrate the base class method use.""" applications = Stratos.list_applications() @@ -263,105 +486,6 @@ class CLI(Cmd): 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") http://git-wip-us.apache.org/repos/asf/stratos/blob/96d7501e/components/org.apache.stratos.python.cli/src/main/python/cli/Stratos.py ---------------------------------------------------------------------- diff --git a/components/org.apache.stratos.python.cli/src/main/python/cli/Stratos.py b/components/org.apache.stratos.python.cli/src/main/python/cli/Stratos.py index f1c2e30..f753c33 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 @@ class Stratos: """ # Users + * list-users + * add-users + * remove-user """ @staticmethod @@ -36,17 +39,19 @@ class Stratos: @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 @@ class Stratos: """ # Cartridges + * list-cartridges + * describe-cartridge + * remove-cartridges """ @staticmethod @@ -77,50 +85,90 @@ class Stratos: 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 @@ class Stratos: 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 @@ class Stratos: 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 @@ class Stratos: 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 @@ class Stratos: 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()
