Consolidate status results

All the _list functions return the same thing; save the results and
use a common output function to generate the json or text output.

Change-Id: I9cb44b09de2cb948e7381ef10302b21040433a2c
This commit is contained in:
Ian Wienand 2018-02-27 11:08:26 +11:00
parent cc95d679bd
commit 2d60240fec
3 changed files with 67 additions and 58 deletions

View File

@ -156,13 +156,16 @@ class NodePoolCmd(NodepoolApp):
def list(self, node_id=None, detail=False):
if hasattr(self.args, 'detail'):
detail = self.args.detail
print(status.node_list(self.zk, node_id, detail))
results = status.node_list(self.zk, node_id, detail)
print(status.output(results, 'pretty'))
def dib_image_list(self):
print(status.dib_image_list(self.zk))
results = status.dib_image_list(self.zk)
print(status.output(results, 'pretty'))
def image_list(self):
print(status.image_list(self.zk))
results = status.image_list(self.zk)
print(status.output(results, 'pretty'))
def image_build(self, diskimage=None):
diskimage = diskimage or self.args.image
@ -334,7 +337,8 @@ class NodePoolCmd(NodepoolApp):
# TODO(asselin,yolanda): add validation of secure.conf
def request_list(self):
print(status.request_list(self.zk))
results = status.request_list(self.zk)
print(status.output(results, 'pretty'))
def _wait_for_threads(self, threads):
for t in threads:

View File

@ -20,6 +20,29 @@ import time
from prettytable import PrettyTable
# General notes:
#
# All the _list functions should return a tuple
#
# ([ {obj}, {obj}, ...], headers_table)
#
# The headers_table is an OrderedDict that maps the fields in the
# returned objs to pretty-printable headers. Each obj in the list
# should be a dictionary with fields as described by the
# headers_table.
#
# e.g.
#
# headers_table = OrderedDict({
# 'key1': 'Key One',
# 'key2': 'Key Two'})
# objs = [ { 'key1': 'value', 'key2': 123 },
# { 'key1': 'value2', 'key2': 456 } ]
# return(objs, headers_table)
#
# The output() function takes this tuple result and a format to
# produce results for consumption by the caller.
def age(timestamp):
now = time.time()
@ -53,7 +76,18 @@ def _to_pretty_table(objs, headers_table):
return t
def node_list(zk, node_id=None, detail=False, format='pretty'):
def output(results, fmt):
objs, headers_table = results
if fmt == 'pretty':
t = _to_pretty_table(objs, headers_table)
return str(t)
elif fmt == 'json':
return json.dumps(objs)
else:
raise ValueError('Unknown format "%s"' % fmt)
def node_list(zk, node_id=None, detail=False):
headers_table = [
("id", "ID"),
("provider", "Provider"),
@ -126,16 +160,10 @@ def node_list(zk, node_id=None, detail=False, format='pretty'):
objs.append(dict(zip(headers_table.keys(),
values)))
if format == 'pretty':
t = _to_pretty_table(objs, headers_table)
return str(t)
elif format == 'json':
return json.dumps(objs)
else:
raise ValueError('Unknown format "%s"' % format)
return (objs, headers_table)
def dib_image_list(zk, format='pretty'):
def dib_image_list(zk):
headers_table = OrderedDict([
("id", "ID"),
("image", "Image"),
@ -154,16 +182,10 @@ def dib_image_list(zk, format='pretty'):
'state': build.state,
'age': int(build.state_time)
})
if format == 'pretty':
t = _to_pretty_table(objs, headers_table)
return str(t)
elif format == 'json':
return json.dumps(objs)
else:
raise ValueError('Unknown format "%s"' % format)
return (objs, headers_table)
def image_list(zk, format='pretty'):
def image_list(zk):
headers_table = OrderedDict([
("id", "Build ID"),
("upload_id", "Upload ID"),
@ -188,16 +210,10 @@ def image_list(zk, format='pretty'):
int(upload.state_time)]
objs.append(dict(zip(headers_table.keys(),
values)))
if format == 'pretty':
t = _to_pretty_table(objs, headers_table)
return str(t)
elif format == 'json':
return json.dumps(objs)
else:
raise ValueError('Unknown format "%s"' % format)
return (objs, headers_table)
def request_list(zk, format='pretty'):
def request_list(zk):
headers_table = OrderedDict([
("id", "Request ID"),
("state", "State"),
@ -213,10 +229,4 @@ def request_list(zk, format='pretty'):
req.declined_by]
objs.append(dict(zip(headers_table.keys(),
values)))
if format == 'pretty':
t = _to_pretty_table(objs, headers_table)
return str(t)
elif format == 'json':
return json.dumps(objs)
else:
raise ValueError('Unknown format "%s"' % format)
return (objs, headers_table)

View File

@ -84,34 +84,29 @@ class WebApp(threading.Thread):
result = self.cache.get(index)
if result:
return result
if path.endswith('.json'):
out_fmt = 'json'
path = path[:-5]
else:
out_fmt = 'pretty'
zk = self.nodepool.getZK()
if path == '/image-list':
output = status.image_list(self.nodepool.getZK(),
format='pretty')
elif path == '/image-list.json':
output = status.image_list(self.nodepool.getZK(),
format='json')
results = status.image_list(zk)
elif path == '/dib-image-list':
output = status.dib_image_list(self.nodepool.getZK(),
format='pretty')
elif path == '/dib-image-list.json':
output = status.dib_image_list(self.nodepool.getZK(),
format='json')
results = status.dib_image_list(zk)
elif path == '/node-list':
output = status.node_list(self.nodepool.getZK(),
format='pretty',
node_id=params.get('node_id'))
elif path == '/node-list.json':
output = status.node_list(self.nodepool.getZK(),
format='json',
node_id=params.get('node_id'))
results = status.node_list(zk,
node_id=params.get('node_id'))
elif path == '/request-list':
output = status.request_list(self.nodepool.getZK(),
format='pretty')
elif path == '/request-list.json':
output = status.request_list(self.nodepool.getZK(),
format='json')
results = status.request_list(zk)
else:
return None
output = status.output(results, out_fmt)
return self.cache.put(index, output)
def app(self, request):