ipalib: add convenient Command method for adding messages

Call the add_message() method of Command from anywhere in the implementation
of a command to add a message to the result of the command.

Reviewed-By: Martin Basti <mbasti@redhat.com>
This commit is contained in:
Jan Cholasta 2016-03-02 12:44:15 +01:00 committed by Martin Basti
parent e5520dc347
commit 3c57c305ad

View File

@ -434,13 +434,17 @@ class Command(HasParam):
return self.__do_call(*args, **options)
def __do_call(self, *args, **options):
version_provided = 'version' in options
if version_provided:
self.context.__messages = []
if 'version' in options:
self.verify_client_version(unicode(options['version']))
elif self.api.env.skip_version_check and not self.api.env.in_server:
options['version'] = u'2.0'
else:
options['version'] = API_VERSION
if self.api.env.in_server:
# add message only on server side
self.add_message(
messages.VersionMissing(server_version=API_VERSION))
params = self.args_options_2_params(*args, **options)
self.debug(
'raw: %s(%s)', self.name, ', '.join(self._repr_iter(**params))
@ -454,12 +458,9 @@ class Command(HasParam):
self.validate(**params)
(args, options) = self.params_2_args_options(**params)
ret = self.run(*args, **options)
if (not version_provided and isinstance(ret, dict) and
self.api.env.in_server):
# add message only on server side
messages.add_message(
API_VERSION, ret,
messages.VersionMissing(server_version=API_VERSION))
if isinstance(ret, dict):
for message in self.context.__messages:
messages.add_message(options['version'], ret, message)
if (
isinstance(ret, dict)
and 'summary' in self.output
@ -470,6 +471,9 @@ class Command(HasParam):
self.validate_output(ret, options['version'])
return ret
def add_message(self, message):
self.context.__messages.append(message)
def soft_validate(self, values):
errors = dict()
for p in self.params():