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:
parent
cc95d679bd
commit
2d60240fec
@ -156,13 +156,16 @@ class NodePoolCmd(NodepoolApp):
|
|||||||
def list(self, node_id=None, detail=False):
|
def list(self, node_id=None, detail=False):
|
||||||
if hasattr(self.args, 'detail'):
|
if hasattr(self.args, 'detail'):
|
||||||
detail = 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):
|
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):
|
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):
|
def image_build(self, diskimage=None):
|
||||||
diskimage = diskimage or self.args.image
|
diskimage = diskimage or self.args.image
|
||||||
@ -334,7 +337,8 @@ class NodePoolCmd(NodepoolApp):
|
|||||||
# TODO(asselin,yolanda): add validation of secure.conf
|
# TODO(asselin,yolanda): add validation of secure.conf
|
||||||
|
|
||||||
def request_list(self):
|
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):
|
def _wait_for_threads(self, threads):
|
||||||
for t in threads:
|
for t in threads:
|
||||||
|
@ -20,6 +20,29 @@ import time
|
|||||||
|
|
||||||
from prettytable import PrettyTable
|
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):
|
def age(timestamp):
|
||||||
now = time.time()
|
now = time.time()
|
||||||
@ -53,7 +76,18 @@ def _to_pretty_table(objs, headers_table):
|
|||||||
return t
|
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 = [
|
headers_table = [
|
||||||
("id", "ID"),
|
("id", "ID"),
|
||||||
("provider", "Provider"),
|
("provider", "Provider"),
|
||||||
@ -126,16 +160,10 @@ def node_list(zk, node_id=None, detail=False, format='pretty'):
|
|||||||
objs.append(dict(zip(headers_table.keys(),
|
objs.append(dict(zip(headers_table.keys(),
|
||||||
values)))
|
values)))
|
||||||
|
|
||||||
if format == 'pretty':
|
return (objs, headers_table)
|
||||||
t = _to_pretty_table(objs, headers_table)
|
|
||||||
return str(t)
|
|
||||||
elif format == 'json':
|
|
||||||
return json.dumps(objs)
|
|
||||||
else:
|
|
||||||
raise ValueError('Unknown format "%s"' % format)
|
|
||||||
|
|
||||||
|
|
||||||
def dib_image_list(zk, format='pretty'):
|
def dib_image_list(zk):
|
||||||
headers_table = OrderedDict([
|
headers_table = OrderedDict([
|
||||||
("id", "ID"),
|
("id", "ID"),
|
||||||
("image", "Image"),
|
("image", "Image"),
|
||||||
@ -154,16 +182,10 @@ def dib_image_list(zk, format='pretty'):
|
|||||||
'state': build.state,
|
'state': build.state,
|
||||||
'age': int(build.state_time)
|
'age': int(build.state_time)
|
||||||
})
|
})
|
||||||
if format == 'pretty':
|
return (objs, headers_table)
|
||||||
t = _to_pretty_table(objs, headers_table)
|
|
||||||
return str(t)
|
|
||||||
elif format == 'json':
|
|
||||||
return json.dumps(objs)
|
|
||||||
else:
|
|
||||||
raise ValueError('Unknown format "%s"' % format)
|
|
||||||
|
|
||||||
|
|
||||||
def image_list(zk, format='pretty'):
|
def image_list(zk):
|
||||||
headers_table = OrderedDict([
|
headers_table = OrderedDict([
|
||||||
("id", "Build ID"),
|
("id", "Build ID"),
|
||||||
("upload_id", "Upload ID"),
|
("upload_id", "Upload ID"),
|
||||||
@ -188,16 +210,10 @@ def image_list(zk, format='pretty'):
|
|||||||
int(upload.state_time)]
|
int(upload.state_time)]
|
||||||
objs.append(dict(zip(headers_table.keys(),
|
objs.append(dict(zip(headers_table.keys(),
|
||||||
values)))
|
values)))
|
||||||
if format == 'pretty':
|
return (objs, headers_table)
|
||||||
t = _to_pretty_table(objs, headers_table)
|
|
||||||
return str(t)
|
|
||||||
elif format == 'json':
|
|
||||||
return json.dumps(objs)
|
|
||||||
else:
|
|
||||||
raise ValueError('Unknown format "%s"' % format)
|
|
||||||
|
|
||||||
|
|
||||||
def request_list(zk, format='pretty'):
|
def request_list(zk):
|
||||||
headers_table = OrderedDict([
|
headers_table = OrderedDict([
|
||||||
("id", "Request ID"),
|
("id", "Request ID"),
|
||||||
("state", "State"),
|
("state", "State"),
|
||||||
@ -213,10 +229,4 @@ def request_list(zk, format='pretty'):
|
|||||||
req.declined_by]
|
req.declined_by]
|
||||||
objs.append(dict(zip(headers_table.keys(),
|
objs.append(dict(zip(headers_table.keys(),
|
||||||
values)))
|
values)))
|
||||||
if format == 'pretty':
|
return (objs, headers_table)
|
||||||
t = _to_pretty_table(objs, headers_table)
|
|
||||||
return str(t)
|
|
||||||
elif format == 'json':
|
|
||||||
return json.dumps(objs)
|
|
||||||
else:
|
|
||||||
raise ValueError('Unknown format "%s"' % format)
|
|
||||||
|
@ -84,34 +84,29 @@ class WebApp(threading.Thread):
|
|||||||
result = self.cache.get(index)
|
result = self.cache.get(index)
|
||||||
if result:
|
if result:
|
||||||
return 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':
|
if path == '/image-list':
|
||||||
output = status.image_list(self.nodepool.getZK(),
|
results = status.image_list(zk)
|
||||||
format='pretty')
|
|
||||||
elif path == '/image-list.json':
|
|
||||||
output = status.image_list(self.nodepool.getZK(),
|
|
||||||
format='json')
|
|
||||||
elif path == '/dib-image-list':
|
elif path == '/dib-image-list':
|
||||||
output = status.dib_image_list(self.nodepool.getZK(),
|
results = status.dib_image_list(zk)
|
||||||
format='pretty')
|
|
||||||
elif path == '/dib-image-list.json':
|
|
||||||
output = status.dib_image_list(self.nodepool.getZK(),
|
|
||||||
format='json')
|
|
||||||
elif path == '/node-list':
|
elif path == '/node-list':
|
||||||
output = status.node_list(self.nodepool.getZK(),
|
results = status.node_list(zk,
|
||||||
format='pretty',
|
node_id=params.get('node_id'))
|
||||||
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'))
|
|
||||||
elif path == '/request-list':
|
elif path == '/request-list':
|
||||||
output = status.request_list(self.nodepool.getZK(),
|
results = status.request_list(zk)
|
||||||
format='pretty')
|
|
||||||
elif path == '/request-list.json':
|
|
||||||
output = status.request_list(self.nodepool.getZK(),
|
|
||||||
format='json')
|
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
output = status.output(results, out_fmt)
|
||||||
|
|
||||||
return self.cache.put(index, output)
|
return self.cache.put(index, output)
|
||||||
|
|
||||||
def app(self, request):
|
def app(self, request):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user