Michael Pasternak has uploaded a new change for review. Change subject: cli: expose api capabilities in cli #891227 ......................................................................
cli: expose api capabilities in cli #891227 == Usage == capabilities [command options] == Description == Displaying system capabilities for the given version. == Arguments == * [features] - Lists version features. Bug-Url: https://bugzilla.redhat.com/show_bug.cgi?id=891227 Change-Id: I501739820951cdccdad16013b0de761ca2e276cd Signed-off-by: Michael pasternak <[email protected]> --- A src/ovirtcli/command/capabilities.py M src/ovirtcli/context.py A src/ovirtcli/shell/capabilitiescmdshell.py M src/ovirtcli/shell/engineshell.py 4 files changed, 153 insertions(+), 1 deletion(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-cli refs/changes/22/20022/1 diff --git a/src/ovirtcli/command/capabilities.py b/src/ovirtcli/command/capabilities.py new file mode 100644 index 0000000..9203fb2 --- /dev/null +++ b/src/ovirtcli/command/capabilities.py @@ -0,0 +1,96 @@ +# +# Copyright (c) 2013 Red Hat, Inc. +# +# Licensed 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 pkg_resources + +from ovirtcli.shell.capabilitiescmdshell import CapabilitiesCmdShell +from ovirtcli.command.command import OvirtCommand + +from ovirtsdk.xml.params import VersionCaps + +class CapabilitiesCommand(OvirtCommand): + + name = 'capabilities' + description = 'displaying system capabilities' + args_check = (0, 1) + valid_options = [ ( + '--' + item, str + ) + for item in CapabilitiesCmdShell.OPTIONS + ] + + helptext = """\ + == Usage == + + capabilities [command options] + + == Description == + + Displaying system capabilities. + + == Arguments == + + * [features] - Lists version features. + """ + + def execute(self): + """executes "capabilities".""" + opts = self.options + + for capabilities in self.context.get_capabilities(): + capabilities_version = pkg_resources.parse_version( + "%s.%s.%s.%s" % ( + capabilities.major, + capabilities.minor, + capabilities.version if hasattr( + capabilities, 'version' + ) else '0', + capabilities.revision if hasattr( + capabilities, 'revision' + ) else '0' + ) + ) + + if capabilities_version[0] == self.context.backend_version[0] and \ + capabilities_version[1] == self.context.backend_version[1]: + if '--features' in opts: + self.context.formatter.format( + self.context, + VersionCaps(features=capabilities.features) + ) + else: + # backup and restore unrelevant data to this context + # saves deepcopy of the object + caps_features = capabilities.features + caps_id = capabilities.id + + capabilities.features = None + capabilities.id = None + + self.context.formatter.format( + self.context, + capabilities + ) + + capabilities.features = caps_features + capabilities.id = caps_id + + def show_help(self): + """Show help for "capabilities".""" + + stdout = self.context.terminal.stdout + stdout.write(self.helptext) diff --git a/src/ovirtcli/context.py b/src/ovirtcli/context.py index 93df58d..d332c0e 100644 --- a/src/ovirtcli/context.py +++ b/src/ovirtcli/context.py @@ -27,6 +27,7 @@ from ovirtcli.command.info import InfoCommand from ovirtcli.historymanager import HistoryManager from ovirtcli.command.summary import SummaryCommand +from ovirtcli.command.capabilities import CapabilitiesCommand class OvirtCliExecutionContext(ExecutionContext): @@ -52,11 +53,26 @@ self.product_info = None self.sdk_version, self.cli_version, self.backend_version = self.__get_version_info() self.history = HistoryManager() + self.__capabilities = None def set_connection(self, connection, url=None): self.connection = connection self.url = url self.__update_backend_metadata() + + def get_capabilities(self): + """ + Fetches system capabilities + + @return: Capabilities + """ + + # cache capabilities cause it won't change + # during application lifetime and deserializing + # this object is pretty expensive. + if not self.__capabilities: + self.__capabilities = self.connection.capabilities.list() + return self.__capabilities def __get_version_info(self): SNAPSHOT_SUFFIX = '-SNAPSHOT' @@ -111,6 +127,7 @@ self.add_command(InfoCommand) self.add_command(HistoryCommand) self.add_command(SummaryCommand) + self.add_command(CapabilitiesCommand) def __update_backend_metadata(self): """Return a dict with prompt variables.""" diff --git a/src/ovirtcli/shell/capabilitiescmdshell.py b/src/ovirtcli/shell/capabilitiescmdshell.py new file mode 100644 index 0000000..8380407 --- /dev/null +++ b/src/ovirtcli/shell/capabilitiescmdshell.py @@ -0,0 +1,37 @@ +# +# Copyright (c) 2010 Red Hat, Inc. +# +# Licensed 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 ovirtcli.shell.cmdshell import CmdShell +from ovirtcli.utils.autocompletionhelper import AutoCompletionHelper + + +class CapabilitiesCmdShell(CmdShell): + NAME = 'capabilities' + OPTIONS = [ + 'features' + ] + + def __init__(self, context, parser): + CmdShell.__init__(self, context, parser) + + def do_capabilities(self, args): + return self.context.execute_string(CapabilitiesCmdShell.NAME + ' ' + args + '\n') + + def complete_capabilities(self, text, line, begidx, endidx): + return AutoCompletionHelper.complete(line=line, text=text, + args={}.fromkeys(CapabilitiesCmdShell.OPTIONS), + all_options=True) diff --git a/src/ovirtcli/shell/engineshell.py b/src/ovirtcli/shell/engineshell.py index 138ea52..ac6323e 100644 --- a/src/ovirtcli/shell/engineshell.py +++ b/src/ovirtcli/shell/engineshell.py @@ -38,6 +38,7 @@ from ovirtcli.shell.filecmdshell import FileCmdShell from ovirtcli.shell.historycmdshell import HistoryCmdShell from ovirtcli.shell.infocmdshell import InfoCmdShell +from ovirtcli.shell.capabilitiescmdshell import CapabilitiesCmdShell from ovirtcli.settings import OvirtCliSettings from ovirtcli.prompt import PromptMode @@ -53,7 +54,7 @@ RemoveCmdShell, AddCmdShell, DisconnectCmdShell, \ ConsoleCmdShell, PingCmdShell, StatusCmdShell, \ ClearCmdShell, FileCmdShell, HistoryCmdShell, \ - InfoCmdShell, SummaryCmdShell): + InfoCmdShell, SummaryCmdShell, CapabilitiesCmdShell): OFF_LINE_CONTENT = [ConnectCmdShell.NAME, HelpCommand.name, 'exit', "EOF"] ############################# INIT ################################# def __init__(self, context, parser, completekey='tab', stdin=None, stdout=None): @@ -74,6 +75,7 @@ HistoryCmdShell.__init__(self, context, parser) InfoCmdShell.__init__(self, context, parser) SummaryCmdShell.__init__(self, context, parser) + CapabilitiesCmdShell.__init__(self, context, parser) self.last_output = '' self.__input_buffer = '' -- To view, visit http://gerrit.ovirt.org/20022 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I501739820951cdccdad16013b0de761ca2e276cd 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
