From 59c637ba59c8a54e22c5513c674bbd05d3289d9e Mon Sep 17 00:00:00 2001 From: Paco Gomez Date: Thu, 23 Jul 2015 19:48:01 -0500 Subject: [PATCH] exit codes --- changes.txt | 1 + tests/login-vca.sh | 2 ++ vca_cli/vca_cli.py | 30 +++++++++++++++++++++++------- vca_cli/vca_cli_blueprint.py | 4 +++- vca_cli/vca_cli_compute.py | 15 +++++++++++---- vca_cli/vca_cli_network.py | 4 +++- vca_cli/vca_cli_sql.py | 4 +++- vca_cli/vca_cli_vca.py | 8 +++++--- 8 files changed, 51 insertions(+), 17 deletions(-) diff --git a/changes.txt b/changes.txt index 9ad3a829..02b90d32 100644 --- a/changes.txt +++ b/changes.txt @@ -23,6 +23,7 @@ new features: - login with instance, org and vdc - no need to enter service type - use instance and vdc (vca type) +- commands exit with error code == 1 when fail pending work: - actual session logout on VC \ No newline at end of file diff --git a/tests/login-vca.sh b/tests/login-vca.sh index c81e15fc..2cb07b86 100755 --- a/tests/login-vca.sh +++ b/tests/login-vca.sh @@ -1,4 +1,6 @@ #!/bin/bash +set -e +vca logout vca login $VCA_CLI_USER --password $VCA_CLI_PASS --instance $VCA_CLI_INSTANCE vca status diff --git a/vca_cli/vca_cli.py b/vca_cli/vca_cli.py index 5ee90de3..2cb09ad4 100644 --- a/vca_cli/vca_cli.py +++ b/vca_cli/vca_cli.py @@ -13,6 +13,7 @@ # +import sys import os import operator import click @@ -182,6 +183,7 @@ def login(cmd_proc, user, host, password, do_not_save_password, "'%s', profile '%s'" % (vdc, cmd_proc.profile), cmd_proc) + sys.exit(1) cmd_proc.save_current_config() elif cmd_proc.vca.service_type in [VCA.VCA_SERVICE_TYPE_VCHS]: if instance is not None or org is not None: @@ -204,6 +206,7 @@ def login(cmd_proc, user, host, password, do_not_save_password, "'%s', profile '%s'" % (vdc, cmd_proc.profile), cmd_proc) + sys.exit(1) cmd_proc.save_current_config() elif (cmd_proc.vca.service_type in [VCA.VCA_SERVICE_TYPE_STANDALONE]): @@ -229,11 +232,14 @@ def login(cmd_proc, user, host, password, do_not_save_password, "'%s' , profile '%s'" % (vdc, cmd_proc.profile), cmd_proc) + sys.exit(1) cmd_proc.save_current_config() else: utils.print_error('Can\'t login', cmd_proc) + sys.exit(1) except Exception as e: utils.print_error('Can\'t login: ' + str(e), cmd_proc) + sys.exit(1) @cli.command() @@ -241,11 +247,15 @@ def login(cmd_proc, user, host, password, do_not_save_password, def logout(cmd_proc): """Logout from a vCloud service""" user = cmd_proc.vca.username + if user is None: + utils.print_message('Not logged in', cmd_proc) + exit(0) profile = cmd_proc.profile cmd_proc.logout() - utils.print_message("User '%s' logged out, profile '%s'" % - (user, profile), - cmd_proc) + if user is not None: + utils.print_message("User '%s' logged out, profile '%s'" % + (user, profile), + cmd_proc) def _use_instance(cmd_proc, instance): @@ -263,6 +273,7 @@ def _use_instance(cmd_proc, instance): ", profile '%s'" % (instance, cmd_proc.profile), cmd_proc) + sys.exit(1) return result @@ -323,11 +334,11 @@ def instance(cmd_proc, operation, instance, org, vdc): if cmd_proc.vca.service_type not in \ [VCA.VCA_SERVICE_TYPE_VCA, VCA.VCA_SERVICE_TYPE_VCHS]: utils.print_message('This service type doesn\'t support this command') - return + sys.exit(1) result = cmd_proc.re_login() if not result: utils.print_error('Not logged in', cmd_proc) - return + sys.exit(1) if 'list' == operation: headers = [] table = [] @@ -405,8 +416,10 @@ def instance(cmd_proc, operation, instance, org, vdc): "'%s' , profile '%s'" % (vdc, cmd_proc.profile), cmd_proc) + sys.exit(1) else: utils.print_message('Not implemented') + sys.exit(1) cmd_proc.save_current_config() @@ -424,13 +437,13 @@ def org(cmd_proc, operation, instance, org): result = cmd_proc.re_login() if not result: utils.print_error('Not logged in', cmd_proc) - return + sys.exit(1) if 'list' == operation: if cmd_proc.vca.service_type == VCA.VCA_SERVICE_TYPE_VCHS: if '' == instance: instance = cmd_proc.instance _list_orgs_in_instance(cmd_proc, instance) - return + sys.exit(1) headers = ['Org', 'Selected'] table = [] if cmd_proc.vca is not None and\ @@ -458,6 +471,7 @@ def org(cmd_proc, operation, instance, org): headers, table, cmd_proc) else: utils.print_error("Org not found '%s'" % org) + sys.exit(1) elif 'use' == operation: if cmd_proc.vca.service_type == VCA.VCA_SERVICE_TYPE_VCA: utils.print_message('Operation not supported in ' @@ -482,6 +496,7 @@ def org(cmd_proc, operation, instance, org): "'%s' , profile '%s'" % (vdc, cmd_proc.profile), cmd_proc) + sys.exit(1) elif cmd_proc.vca.service_type == VCA.VCA_SERVICE_TYPE_STANDALONE: utils.print_message('Operation not supported in ' 'this service type. ' @@ -490,6 +505,7 @@ def org(cmd_proc, operation, instance, org): else: utils.print_message('Operation not supported ' 'in this service type') + sys.exit(1) cmd_proc.save_current_config() diff --git a/vca_cli/vca_cli_blueprint.py b/vca_cli/vca_cli_blueprint.py index 282af2ea..1ff89b26 100644 --- a/vca_cli/vca_cli_blueprint.py +++ b/vca_cli/vca_cli_blueprint.py @@ -13,6 +13,7 @@ # +import sys import click from vca_cli import cli, utils @@ -21,4 +22,5 @@ @click.pass_obj def blueprint(cmd_proc): """Operations with Blueprints""" - utils.print_message('blueprint', cmd_proc) + utils.print_message('Not implemented') + sys.exit(1) diff --git a/vca_cli/vca_cli_compute.py b/vca_cli/vca_cli_compute.py index a552bfdb..de42b329 100644 --- a/vca_cli/vca_cli_compute.py +++ b/vca_cli/vca_cli_compute.py @@ -13,6 +13,7 @@ # +import sys import click import operator from vca_cli import cli, utils, default_operation @@ -30,7 +31,7 @@ def vdc(cmd_proc, operation, vdc): result = cmd_proc.re_login() if not result: utils.print_error('Not logged in', cmd_proc) - return + sys.exit(1) if 'list' == operation: headers = ['Virtual Data Center', "Selected"] table = ['', ''] @@ -57,6 +58,7 @@ def vdc(cmd_proc, operation, vdc): else: utils.print_error("Unable to select vdc '%s' in profile '%s'" % (vdc, cmd_proc.profile), cmd_proc) + sys.exit(1) elif 'info' == operation: if vdc is None: vdc = cmd_proc.vdc_name @@ -91,6 +93,7 @@ def vdc(cmd_proc, operation, vdc): else: utils.print_error("Unable to select vdc '%s' in profile '%s'" % (vdc, cmd_proc.profile), cmd_proc) + sys.exit(1) cmd_proc.save_current_config() @@ -143,7 +146,7 @@ def vapp(cmd_proc, operation, vdc, vapp, catalog, template, result = cmd_proc.re_login() if not result: utils.print_error('Not logged in', cmd_proc) - return + sys.exit(1) if vdc is None: vdc = cmd_proc.vdc_name the_vdc = cmd_proc.vca.get_vdc(vdc) @@ -184,7 +187,7 @@ def vapp(cmd_proc, operation, vdc, vapp, catalog, template, get_vcloud_headers()) else: utils.print_error("can't create the vApp", cmd_proc) - return + sys.exit(1) the_vdc = cmd_proc.vca.get_vdc(vdc) the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp_name) if ((vm_name is not None) and @@ -203,7 +206,7 @@ def vapp(cmd_proc, operation, vdc, vapp, catalog, template, get_vcloud_headers()) else: utils.print_error("can't set VM name", cmd_proc) - return + sys.exit(1) the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp_name) if vm_name is not None: utils.print_message( @@ -217,6 +220,7 @@ def vapp(cmd_proc, operation, vdc, vapp, catalog, template, get_vcloud_headers()) else: utils.print_error("can't set computer name", cmd_proc) + sys.exit(1) the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp_name) if cpu is not None: utils.print_message( @@ -229,6 +233,7 @@ def vapp(cmd_proc, operation, vdc, vapp, catalog, template, get_vcloud_headers()) else: utils.print_error("can't configure virtual CPUs", cmd_proc) + sys.exit(1) the_vapp = cmd_proc.vca.get_vapp(the_vdc, vapp_name) # if ram is not None: # print_message("configuring '%s' MB of memory" @@ -305,6 +310,8 @@ def vapp(cmd_proc, operation, vdc, vapp, catalog, template, get_vcloud_headers()) else: utils.print_error("can't delete the vApp", cmd_proc) + sys.exit(1) else: utils.print_message('not implemented', cmd_proc) + sys.exit(1) cmd_proc.save_current_config() diff --git a/vca_cli/vca_cli_network.py b/vca_cli/vca_cli_network.py index 7b47017d..4c78442e 100644 --- a/vca_cli/vca_cli_network.py +++ b/vca_cli/vca_cli_network.py @@ -13,6 +13,7 @@ # +import sys import click from vca_cli import cli, utils @@ -21,4 +22,5 @@ @click.pass_obj def gateway(cmd_proc): """Operations with Edge Gateway""" - utils.print_message('gateway', cmd_proc) + utils.print_message('Not implemented') + sys.exit(1) diff --git a/vca_cli/vca_cli_sql.py b/vca_cli/vca_cli_sql.py index 9d1fc967..e2724c55 100644 --- a/vca_cli/vca_cli_sql.py +++ b/vca_cli/vca_cli_sql.py @@ -13,6 +13,7 @@ # +import sys import click from vca_cli import cli, utils @@ -21,4 +22,5 @@ @click.pass_obj def sql(cmd_proc): """Operations with SQL Air Service""" - utils.print_message('sql', cmd_proc) + utils.print_message('Not implemented') + sys.exit(1) diff --git a/vca_cli/vca_cli_vca.py b/vca_cli/vca_cli_vca.py index 9ead16b4..3fd799bd 100644 --- a/vca_cli/vca_cli_vca.py +++ b/vca_cli/vca_cli_vca.py @@ -13,6 +13,7 @@ # +import sys import click import operator from vca_cli import cli, utils, default_operation @@ -33,12 +34,12 @@ def user(cmd_proc, operation, username, password): if cmd_proc.vca.service_type != VCA.VCA_SERVICE_TYPE_VCA: utils.print_message('Operation not supported ' 'in this service type') - return + sys.exit(1) # see https://wiki.eng.vmware.com/Praxis_IAM_API_Details result = cmd_proc.re_login() if not result: utils.print_error('Not logged in', cmd_proc) - return + sys.exit(1) if 'list' == operation: headers = ['User Name', 'First', 'Last', 'Email', 'State', 'Roles'] table = [] @@ -56,5 +57,6 @@ def user(cmd_proc, operation, username, password): headers, sorted_table, cmd_proc) elif 'info' == operation: - pass + utils.print_message('Not implemented') + sys.exit(1) cmd_proc.save_current_config()