Michael Pasternak has uploaded a new change for review. Change subject: cli: introducing new state UNAUTHORIZED ......................................................................
cli: introducing new state UNAUTHORIZED AUTHENTICATION_ERROR can occur when authentication session expires, this is yet another recoverable error that exposed via own state - UNAUTHORIZED Change-Id: I287743f00ba34997d7e2bbc1aaf5a65c86038ade Signed-off-by: Michael pasternak <[email protected]> --- M src/cli/context.py M src/ovirtcli/command/connect.py M src/ovirtcli/prompt.py M src/ovirtcli/settings.py M src/ovirtcli/shell/engineshell.py 5 files changed, 38 insertions(+), 7 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-cli refs/changes/45/20245/1 diff --git a/src/cli/context.py b/src/cli/context.py index 8bbaed4..6ded31c 100644 --- a/src/cli/context.py +++ b/src/cli/context.py @@ -48,6 +48,7 @@ INTERRUPTED = 3 UNKNOWN_ERROR = 4 COMMUNICATION_ERROR = 5 + AUTHENTICATION_ERROR = 6 Parser = Parser Terminal = Terminal @@ -228,8 +229,15 @@ if isinstance(e, KeyboardInterrupt): self.status = self.INTERRUPTED sys.stdout.write('\n') - elif isinstance(e, CommandError) or isinstance(e, RequestError) \ - or isinstance(e, AmbiguousQueryError): + elif isinstance(e, RequestError): + if hasattr(e, 'status') and e.status == 401: + self.status = self.AUTHENTICATION_ERROR + else: + self.status = getattr(e, 'status', self.COMMAND_ERROR) + self.__pint_error(e) + if hasattr(e, 'help'): + sys.stderr.write('%s\n' % e.help) + elif isinstance(e, CommandError) or isinstance(e, AmbiguousQueryError): self.status = getattr(e, 'status', self.COMMAND_ERROR) self.__pint_error(e) if hasattr(e, 'help'): diff --git a/src/ovirtcli/command/connect.py b/src/ovirtcli/command/connect.py index 85cdd80..1b361ac 100644 --- a/src/ovirtcli/command/connect.py +++ b/src/ovirtcli/command/connect.py @@ -84,8 +84,9 @@ filter_ = settings.get('ovirt-shell:filter') if self.context.connection is not None and \ - self.__test_connectivity() and \ - self.context.status != self.context.COMMUNICATION_ERROR: + self.context.status != self.context.COMMUNICATION_ERROR and \ + self.context.status != self.context.AUTHENTICATION_ERROR and \ + self.__test_connectivity(): stdout.write(Messages.Warning.ALREADY_CONNECTED) return if len(args) == 3: diff --git a/src/ovirtcli/prompt.py b/src/ovirtcli/prompt.py index 0ca8d8f..9faa571 100644 --- a/src/ovirtcli/prompt.py +++ b/src/ovirtcli/prompt.py @@ -16,7 +16,7 @@ class PromptMode(): - Original, Multiline, Disconnected, Connected, Default = range(5) + Original, Multiline, Disconnected, Connected, Unauthorized, Default = range(6) def __init__(self, Type): self.value = Type @@ -30,6 +30,8 @@ return 'Disconnected' if self.value == PromptMode.Connected: return 'Connected' + if self.value == PromptMode.Unauthorized: + return 'Unauthorized' if self.value == PromptMode.Default: return 'Default' diff --git a/src/ovirtcli/settings.py b/src/ovirtcli/settings.py index 974b2bf..49e66bb 100644 --- a/src/ovirtcli/settings.py +++ b/src/ovirtcli/settings.py @@ -69,6 +69,7 @@ ('ovirt-shell:ps2.connected', str, '[' + PRODUCT + ' shell (connected)]# '), ('ovirt-shell:ps3.connected', str, '[' + PRODUCT + ' shell (connected@%(host)s)]# '), ('ovirt-shell:ps1.disconnected', str, '[%s shell (disconnected)]# ' % PRODUCT), + ('ovirt-shell:ps1.unauthorized', str, '[%s shell (unauthorized)]# ' % PRODUCT), ('ovirt-shell:commands', str, '%s shell commands:' % PRODUCT), ('ovirt-shell:misc_commands', str, '%s shell commands:' % PRODUCT), ('ovirt-shell:version', str, ''), diff --git a/src/ovirtcli/shell/engineshell.py b/src/ovirtcli/shell/engineshell.py index e575961..e4e06aa 100644 --- a/src/ovirtcli/shell/engineshell.py +++ b/src/ovirtcli/shell/engineshell.py @@ -138,9 +138,13 @@ # if communication error occurred, change prompt state if self.context.status == self.context.COMMUNICATION_ERROR: - self.__last_status = self.context.COMMUNICATION_ERROR + self.__last_status = self.context.status self.owner._set_prompt(mode=PromptMode.Disconnected) - elif self.__last_status == self.context.COMMUNICATION_ERROR: + elif self.context.status == self.context.AUTHENTICATION_ERROR: + self.__last_status = self.context.status + self.owner._set_prompt(mode=PromptMode.Unauthorized) + elif self.__last_status == self.context.COMMUNICATION_ERROR or \ + self.__last_status == self.context.AUTHENTICATION_ERROR: self.__last_status = -1 self.owner._set_prompt(mode=PromptMode.Original) @@ -161,6 +165,21 @@ self.prompt = self.__get_disconnected_prompt() elif mode == PromptMode.Connected: self.prompt = self.__get_connected_prompt() + elif mode == PromptMode.Unauthorized: + self.prompt = self.__get_unauthorized_prompt() + + def __get_unauthorized_prompt(self): + dprompt = self.context.settings.get('ovirt-shell:ps1.unauthorized') + if self.context.mode != ExecutionMode.SCRIPT \ + and self.context.interactive: + dprompt = dprompt.replace( + "unauthorized", + ColorHelper.color( + "unauthorized", + color_=ColorHelper.RED + ) + ) + return dprompt def __get_disconnected_prompt(self): dprompt = self.context.settings.get('ovirt-shell:ps1.disconnected') -- To view, visit http://gerrit.ovirt.org/20245 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I287743f00ba34997d7e2bbc1aaf5a65c86038ade Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine-cli Gerrit-Branch: master Gerrit-Owner: Michael Pasternak <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
