add special code for formatting metrics and settings with -f human

see: https://github.com/ansible/awx/issues/4566
This commit is contained in:
Ryan Petrello 2019-09-03 16:56:13 -04:00
parent ba8b876dd3
commit 064f871fff
No known key found for this signature in database
GPG Key ID: F2AA5F2122351777
3 changed files with 49 additions and 7 deletions

View File

@ -12,7 +12,7 @@ from .custom import handle_custom_actions
from .format import (add_authentication_arguments,
add_output_formatting_arguments,
FORMATTERS, format_response)
from .options import ResourceOptionsParser
from .options import ResourceOptionsParser, UNIQUENESS_RULES
from .resource import parse_resource, is_control_resource
from awxkit import api, config, utils, exceptions, WSClient # noqa
from awxkit.cli.utils import HelpfulArgumentParser, cprint, disable_color
@ -159,10 +159,34 @@ class CLI(object):
response = getattr(resource, self.method)()
else:
response = self.parse_action(resource)
_filter = self.get_config('filter')
# human format for metrics, settings is special
if (
self.resource in ('metrics', 'settings') and
self.get_config('format') == 'human'
):
response.json = {
'count': len(response.json),
'results': [
{'key': k, 'value': v}
for k, v in response.json.items()
]
}
_filter = 'key, value'
if (
self.get_config('format') == 'human' and
_filter == '.' and
self.resource in UNIQUENESS_RULES
):
_filter = UNIQUENESS_RULES[self.resource]
formatted = format_response(
response,
fmt=self.get_config('format'),
filter=self.get_config('filter'),
filter=_filter,
changed=self.original_action in (
'modify', 'create', 'associate', 'disassociate'
)

View File

@ -12,6 +12,13 @@ from .format import add_output_formatting_arguments
from .resource import DEPRECATED_RESOURCES_REVERSE
UNIQUENESS_RULES = {
'me': 'id, username',
'users': 'id, username',
'instances': 'id, hostname',
}
def pk_or_name(v2, model_name, value, page=None):
if isinstance(value, int):
return value
@ -26,10 +33,8 @@ def pk_or_name(v2, model_name, value, page=None):
if model_name in DEPRECATED_RESOURCES_REVERSE:
model_name = DEPRECATED_RESOURCES_REVERSE[model_name]
if model_name == 'users':
identity = 'username'
elif model_name == 'instances':
model_name = 'hostname'
if model_name in UNIQUENESS_RULES:
identity = UNIQUENESS_RULES[model_name][-1]
if hasattr(v2, model_name):
page = getattr(v2, model_name)

View File

@ -167,10 +167,23 @@ def parse_resource(client, skip_deprecated=False):
command = CustomCommand.registry[resource]()
response = command.handle(client, parser)
if response:
_filter = client.get_config('filter')
if (
resource == 'config' and
client.get_config('format') == 'human'
):
response = {
'count': len(response),
'results': [
{'key': k, 'value': v}
for k, v in response.items()
]
}
_filter = 'key, value'
formatted = format_response(
Page.from_json(response),
fmt=client.get_config('format'),
filter=client.get_config('filter'),
filter=_filter
)
print(formatted)
raise SystemExit()