mirror of
https://github.com/ansible/awx.git
synced 2026-01-10 15:32:07 -03:30
add special code for formatting metrics and settings with -f human
see: https://github.com/ansible/awx/issues/4566
This commit is contained in:
parent
ba8b876dd3
commit
064f871fff
@ -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'
|
||||
)
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user