fixed api to use recent version of flask-restless-ng
This commit is contained in:
88
faafo/bin/faafo
Normal file → Executable file
88
faafo/bin/faafo
Normal file → Executable file
@ -12,7 +12,6 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import copy
|
||||
import json
|
||||
import random
|
||||
import uuid
|
||||
@ -75,13 +74,19 @@ def get_random_task():
|
||||
float(CONF.command.max_yb))
|
||||
|
||||
task = {
|
||||
'uuid': str(uuid.uuid4()),
|
||||
'width': width,
|
||||
'height': height,
|
||||
'iterations': iterations, 'xa': xa,
|
||||
'xb': xb,
|
||||
'ya': ya,
|
||||
'yb': yb
|
||||
'data': {
|
||||
'type': 'fractal',
|
||||
'attributes': {
|
||||
'uuid': str(uuid.uuid4()),
|
||||
'width': width,
|
||||
'height': height,
|
||||
'iterations': iterations,
|
||||
'xa': xa,
|
||||
'xb': xb,
|
||||
'ya': ya,
|
||||
'yb': yb
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return task
|
||||
@ -93,25 +98,31 @@ def do_get_fractal():
|
||||
|
||||
def do_show_fractal():
|
||||
LOG.info("showing fractal %s" % CONF.command.uuid)
|
||||
headers = {'Content-Type': 'application/vnd.api+json',
|
||||
'Accept': 'application/vnd.api+json'}
|
||||
result = requests.get("%s/v1/fractal/%s" %
|
||||
(CONF.endpoint_url, CONF.command.uuid))
|
||||
(CONF.endpoint_url, CONF.command.uuid),
|
||||
headers=headers)
|
||||
LOG.debug("result: %s" % result.text)
|
||||
|
||||
if result.status_code == 200:
|
||||
data = json.loads(result.text)
|
||||
fractal_data = data['data']['attributes']
|
||||
output = PrettyTable(["Parameter", "Value"])
|
||||
output.align["Parameter"] = "l"
|
||||
output.align["Value"] = "l"
|
||||
output.add_row(["uuid", data['uuid']])
|
||||
output.add_row(["duration", "%f seconds" % data['duration']])
|
||||
output.add_row(["uuid", fractal_data['uuid']])
|
||||
output.add_row(["duration", "%f seconds" % fractal_data['duration']])
|
||||
output.add_row(["dimensions", "%d x %d pixels" %
|
||||
(data['width'], data['height'])])
|
||||
output.add_row(["iterations", data['iterations']])
|
||||
output.add_row(["xa", data['xa']])
|
||||
output.add_row(["xb", data['xb']])
|
||||
output.add_row(["ya", data['ya']])
|
||||
output.add_row(["yb", data['yb']])
|
||||
output.add_row(["size", "%d bytes" % data['size']])
|
||||
output.add_row(["checksum", data['checksum']])
|
||||
output.add_row(["generated_by", data['generated_by']])
|
||||
(fractal_data['width'], fractal_data['height'])])
|
||||
output.add_row(["iterations", fractal_data['iterations']])
|
||||
output.add_row(["xa", fractal_data['xa']])
|
||||
output.add_row(["xb", fractal_data['xb']])
|
||||
output.add_row(["ya", fractal_data['ya']])
|
||||
output.add_row(["yb", fractal_data['yb']])
|
||||
output.add_row(["size", "%d bytes" % fractal_data['size']])
|
||||
output.add_row(["checksum", fractal_data['checksum']])
|
||||
output.add_row(["generated_by", fractal_data['generated_by']])
|
||||
print(output)
|
||||
else:
|
||||
LOG.error("fractal '%s' not found" % CONF.command.uuid)
|
||||
@ -123,34 +134,43 @@ def do_list_fractals():
|
||||
fractals = get_fractals()
|
||||
output = PrettyTable(["UUID", "Dimensions", "Filesize"])
|
||||
for fractal in fractals:
|
||||
fractal_data = fractal['attributes']
|
||||
output.add_row([
|
||||
fractal["uuid"],
|
||||
"%d x %d pixels" % (fractal["width"], fractal["height"]),
|
||||
"%d bytes" % (fractal["size"] or 0),
|
||||
fractal_data["uuid"],
|
||||
"%d x %d pixels" % (fractal_data["width"], fractal_data["height"]),
|
||||
"%d bytes" % (fractal_data["size"] or 0),
|
||||
])
|
||||
print(output)
|
||||
|
||||
|
||||
def get_fractals(page=1):
|
||||
result = requests.get("%s/v1/fractal?page=%d" %
|
||||
(CONF.endpoint_url, page))
|
||||
headers = {'Content-Type': 'application/vnd.api+json',
|
||||
'Accept': 'application/vnd.api+json'}
|
||||
result = requests.get("%s/v1/fractal?page=%d&page[size]=10" %
|
||||
(CONF.endpoint_url, page),
|
||||
headers=headers)
|
||||
LOG.debug("result: %s" % result.text)
|
||||
|
||||
|
||||
fractals = []
|
||||
if result.status_code == 200:
|
||||
data = json.loads(result.text)
|
||||
if page < data['total_pages']:
|
||||
fractals = data['objects'] + get_fractals(page + 1)
|
||||
if (page * 10) < data['meta']['total']:
|
||||
fractals = data['data'] + get_fractals(page + 1)
|
||||
else:
|
||||
return data['objects']
|
||||
return data['data']
|
||||
|
||||
return fractals
|
||||
|
||||
|
||||
def do_delete_fractal():
|
||||
LOG.info("deleting fractal %s" % CONF.command.uuid)
|
||||
headers = {'Content-Type': 'application/vnd.api+json',
|
||||
'Accept': 'application/vnd.api+json'}
|
||||
result = requests.delete("%s/v1/fractal/%s" %
|
||||
(CONF.endpoint_url, CONF.command.uuid))
|
||||
LOG.debug("result: %s" %result)
|
||||
(CONF.endpoint_url, CONF.command.uuid),
|
||||
headers=headers)
|
||||
LOG.debug("result: %s" % result.text)
|
||||
|
||||
|
||||
def do_create_fractal():
|
||||
@ -164,11 +184,11 @@ def do_create_fractal():
|
||||
for i in range(0, number):
|
||||
task = get_random_task()
|
||||
LOG.debug("created task %s" % task)
|
||||
# NOTE(berendt): only necessary when using requests < 2.4.2
|
||||
headers = {'Content-type': 'application/json',
|
||||
'Accept': 'text/plain'}
|
||||
requests.post("%s/v1/fractal" % CONF.endpoint_url,
|
||||
headers = {'Content-Type': 'application/vnd.api+json',
|
||||
'Accept': 'application/vnd.api+json'}
|
||||
resp = requests.post("%s/v1/fractal" % CONF.endpoint_url,
|
||||
json.dumps(task), headers=headers)
|
||||
LOG.debug("resp: %s" % resp.text)
|
||||
|
||||
|
||||
def add_command_parsers(subparsers):
|
||||
|
Reference in New Issue
Block a user