mirror of
https://github.com/ansible/awx.git
synced 2026-03-19 09:57:33 -02:30
Merge pull request #4648 from ryanpetrello/format-metrics-human
cli: add special code for formatting metrics and settings with -f human Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
@@ -12,7 +12,7 @@ from .custom import handle_custom_actions
|
|||||||
from .format import (add_authentication_arguments,
|
from .format import (add_authentication_arguments,
|
||||||
add_output_formatting_arguments,
|
add_output_formatting_arguments,
|
||||||
FORMATTERS, format_response)
|
FORMATTERS, format_response)
|
||||||
from .options import ResourceOptionsParser
|
from .options import ResourceOptionsParser, UNIQUENESS_RULES
|
||||||
from .resource import parse_resource, is_control_resource
|
from .resource import parse_resource, is_control_resource
|
||||||
from awxkit import api, config, utils, exceptions, WSClient # noqa
|
from awxkit import api, config, utils, exceptions, WSClient # noqa
|
||||||
from awxkit.cli.utils import HelpfulArgumentParser, cprint, disable_color
|
from awxkit.cli.utils import HelpfulArgumentParser, cprint, disable_color
|
||||||
@@ -159,10 +159,34 @@ class CLI(object):
|
|||||||
response = getattr(resource, self.method)()
|
response = getattr(resource, self.method)()
|
||||||
else:
|
else:
|
||||||
response = self.parse_action(resource)
|
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(
|
formatted = format_response(
|
||||||
response,
|
response,
|
||||||
fmt=self.get_config('format'),
|
fmt=self.get_config('format'),
|
||||||
filter=self.get_config('filter'),
|
filter=_filter,
|
||||||
changed=self.original_action in (
|
changed=self.original_action in (
|
||||||
'modify', 'create', 'associate', 'disassociate'
|
'modify', 'create', 'associate', 'disassociate'
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -13,6 +13,13 @@ from .format import add_output_formatting_arguments
|
|||||||
from .resource import DEPRECATED_RESOURCES_REVERSE
|
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):
|
def pk_or_name(v2, model_name, value, page=None):
|
||||||
if isinstance(value, int):
|
if isinstance(value, int):
|
||||||
return value
|
return value
|
||||||
@@ -27,10 +34,8 @@ def pk_or_name(v2, model_name, value, page=None):
|
|||||||
if model_name in DEPRECATED_RESOURCES_REVERSE:
|
if model_name in DEPRECATED_RESOURCES_REVERSE:
|
||||||
model_name = DEPRECATED_RESOURCES_REVERSE[model_name]
|
model_name = DEPRECATED_RESOURCES_REVERSE[model_name]
|
||||||
|
|
||||||
if model_name == 'users':
|
if model_name in UNIQUENESS_RULES:
|
||||||
identity = 'username'
|
identity = UNIQUENESS_RULES[model_name][-1]
|
||||||
elif model_name == 'instances':
|
|
||||||
model_name = 'hostname'
|
|
||||||
|
|
||||||
if hasattr(v2, model_name):
|
if hasattr(v2, model_name):
|
||||||
page = getattr(v2, model_name)
|
page = getattr(v2, model_name)
|
||||||
|
|||||||
@@ -168,10 +168,23 @@ def parse_resource(client, skip_deprecated=False):
|
|||||||
command = CustomCommand.registry[resource]()
|
command = CustomCommand.registry[resource]()
|
||||||
response = command.handle(client, parser)
|
response = command.handle(client, parser)
|
||||||
if response:
|
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(
|
formatted = format_response(
|
||||||
Page.from_json(response),
|
Page.from_json(response),
|
||||||
fmt=client.get_config('format'),
|
fmt=client.get_config('format'),
|
||||||
filter=client.get_config('filter'),
|
filter=_filter
|
||||||
)
|
)
|
||||||
print(formatted)
|
print(formatted)
|
||||||
raise SystemExit()
|
raise SystemExit()
|
||||||
|
|||||||
Reference in New Issue
Block a user