mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Add --yaml-out, --json-out, and --raw-out outputters to salt-key CLI (retain color output as default)
This commit is contained in:
parent
1fa592bf0e
commit
c0f8e07bd7
3 changed files with 112 additions and 16 deletions
|
@ -617,6 +617,24 @@ class SaltKey(object):
|
|||
dest='conf_file',
|
||||
default='/etc/salt/master',
|
||||
help='Pass in an alternative configuration file')
|
||||
|
||||
parser.add_option('--raw-out',
|
||||
default=False,
|
||||
action='store_true',
|
||||
dest='raw_out',
|
||||
help=('Print the output from the salt-key command in raw python '
|
||||
'form, this is suitable for re-reading the output into '
|
||||
'an executing python script with eval.'))
|
||||
parser.add_option('--yaml-out',
|
||||
default=False,
|
||||
action='store_true',
|
||||
dest='yaml_out',
|
||||
help='Print the output from the salt-key command in yaml.')
|
||||
parser.add_option('--json-out',
|
||||
default=False,
|
||||
action='store_true',
|
||||
dest='json_out',
|
||||
help='Print the output from the salt-key command in json.')
|
||||
|
||||
options, args = parser.parse_args()
|
||||
|
||||
|
|
|
@ -55,53 +55,88 @@ class Key(object):
|
|||
if not self.opts['quiet']:
|
||||
print(message)
|
||||
|
||||
def _list_pre(self, header=True):
|
||||
def _list_pre(self, header=True, printer=None):
|
||||
'''
|
||||
List the unaccepted keys
|
||||
'''
|
||||
if header == True:
|
||||
self._log(utils.LIGHT_RED + 'Unaccepted Keys:' + utils.ENDC)
|
||||
for key in sorted(self._keys('pre')):
|
||||
output = utils.RED + key + utils.ENDC
|
||||
self._log(output)
|
||||
keys = self._keys('pre')
|
||||
if printer is None:
|
||||
for key in sorted(keys):
|
||||
output = utils.RED + key + utils.ENDC
|
||||
self._log(output)
|
||||
else:
|
||||
printer(list(keys))
|
||||
|
||||
def _list_accepted(self, header=True):
|
||||
def _list_accepted(self, header=True, printer=None):
|
||||
'''
|
||||
List the accepted public keys
|
||||
'''
|
||||
if header == True:
|
||||
self._log(utils.LIGHT_GREEN + 'Accepted Keys:' + utils.ENDC)
|
||||
for key in sorted(self._keys('acc')):
|
||||
self._log(utils.GREEN + key + utils.ENDC)
|
||||
keys = self._keys('acc')
|
||||
if printer is None:
|
||||
for key in sorted(keys):
|
||||
self._log(utils.GREEN + key + utils.ENDC)
|
||||
else:
|
||||
printer(list(keys))
|
||||
|
||||
def _list_rejected(self, header=True):
|
||||
def _list_rejected(self, header=True, printer=None):
|
||||
'''
|
||||
List the unaccepted keys
|
||||
'''
|
||||
if header == True:
|
||||
self._log(utils.LIGHT_BLUE + 'Rejected:' + utils.ENDC)
|
||||
for key in sorted(self._keys('rej')):
|
||||
self._log(utils.BLUE + key + utils.ENDC)
|
||||
keys = self._keys('rej')
|
||||
if printer is None:
|
||||
for key in sorted(keys):
|
||||
self._log(utils.BLUE + key + utils.ENDC)
|
||||
else:
|
||||
printer(list(keys))
|
||||
|
||||
def _list(self, name):
|
||||
'''
|
||||
List keys
|
||||
'''
|
||||
printout = self._get_outputter()
|
||||
if 'json_out' in self.opts and self.opts['json_out']:
|
||||
printout.indent = 2
|
||||
if name in ('pre', 'un', 'unaccept', 'unaccepted'):
|
||||
self._list_pre(False)
|
||||
self._list_pre(header=False, printer=printout)
|
||||
elif name in ('acc', 'accept', 'accepted'):
|
||||
self._list_accepted(False)
|
||||
self._list_accepted(header=False, printer=printout)
|
||||
elif name in ('rej', 'reject', 'rejected'):
|
||||
self._list_rejected(False)
|
||||
self._list_rejected(header=False, printer=printout)
|
||||
elif name in ('all',):
|
||||
self._list_pre()
|
||||
self._list_accepted()
|
||||
self._list_rejected()
|
||||
if printout is not None:
|
||||
keys = {
|
||||
'rejected': list(self._keys('rej')),
|
||||
'accepted': list(self._keys('acc')),
|
||||
'unaccepted': list(self._keys('pre')),
|
||||
}
|
||||
printout(keys)
|
||||
else:
|
||||
self._list_pre(printer=printout)
|
||||
self._list_accepted(printer=printout)
|
||||
self._list_rejected(printer=printout)
|
||||
else:
|
||||
err = ('Unrecognized key type "{0}". Run with -h for options.'
|
||||
).format(name)
|
||||
self._log(err, level='error')
|
||||
|
||||
def _get_outputter(self):
|
||||
get_outputter = salt.output.get_outputter
|
||||
if self.opts['raw_out']:
|
||||
printout = get_outputter('raw')
|
||||
elif self.opts['json_out']:
|
||||
printout = get_outputter('json')
|
||||
elif self.opts['yaml_out']:
|
||||
printout = get_outputter('yaml')
|
||||
else:
|
||||
printout = None # use default color output
|
||||
return printout
|
||||
|
||||
def _print_key(self, name):
|
||||
'''
|
||||
Print out the specified public key
|
||||
|
|
|
@ -24,6 +24,49 @@ class KeyTest(integration.ShellCase):
|
|||
'\x1b[1;34mRejected:\x1b[0m', '']
|
||||
self.assertEqual(data, expect)
|
||||
|
||||
def test_list_json_out(self):
|
||||
'''
|
||||
test salt-key -L --json-out
|
||||
'''
|
||||
data = self.run_key('-L --json-out')
|
||||
expect = [
|
||||
'{',
|
||||
' "unaccepted": [], ',
|
||||
' "accepted": [',
|
||||
' "minion", ',
|
||||
' "sub_minion"',
|
||||
' ], ',
|
||||
' "rejected": []',
|
||||
'}',
|
||||
''
|
||||
]
|
||||
self.assertEqual(data, expect)
|
||||
|
||||
def test_list_yaml_out(self):
|
||||
'''
|
||||
test salt-key -L --yaml-out
|
||||
'''
|
||||
data = self.run_key('-L --yaml-out')
|
||||
expect = [
|
||||
'accepted: [minion, sub_minion]',
|
||||
'rejected: []',
|
||||
'unaccepted: []',
|
||||
'',
|
||||
''
|
||||
]
|
||||
self.assertEqual(data, expect)
|
||||
|
||||
def test_list_raw_out(self):
|
||||
'''
|
||||
test salt-key -L --raw-out
|
||||
'''
|
||||
data = self.run_key('-L --raw-out')
|
||||
expect = [
|
||||
"{'unaccepted': [], 'accepted': ['minion', 'sub_minion'], 'rejected': []}",
|
||||
''
|
||||
]
|
||||
self.assertEqual(data, expect)
|
||||
|
||||
def test_list_acc(self):
|
||||
'''
|
||||
test salt-key -l
|
||||
|
|
Loading…
Add table
Reference in a new issue