| hadik3r | 237d3f5 | 2016-06-27 17:57:49 +0200 | [diff] [blame] | 1 | from requests import get |
| 2 | from tabulate import tabulate |
| 3 | import pprint |
| 4 | import argparse |
| 5 | |
| 6 | pp = pprint.PrettyPrinter(indent=4) |
| 7 | |
| 8 | class RestApiClient(): |
| 9 | |
| 10 | def __init__(self): |
| 11 | self.cmds = {} |
| 12 | |
| 13 | def execute_command(self, args): |
| 14 | if getattr(self, args["command"]) is not None: |
| 15 | # call the local method with the same name as the command arg |
| 16 | getattr(self, args["command"])(args) |
| 17 | else: |
| 18 | print("Command not implemented.") |
| 19 | |
| 20 | def list(self,args): |
| 21 | list = get('%s/restapi/datacenter' % args.get('endpoint')).json() |
| 22 | table = [] |
| 23 | for d in list: |
| 24 | # for each dc add a line to the output table |
| 25 | if len(d) > 0: |
| 26 | table.append([d.get("label"), |
| 27 | d.get("internalname"), |
| 28 | d.get("switch"), |
| 29 | d.get("n_running_containers"), |
| 30 | len(d.get("metadata"))]) |
| 31 | headers = ["Label", |
| 32 | "Internal Name", |
| 33 | "Switch", |
| 34 | "# Containers", |
| 35 | "# Metadata Items"] |
| 36 | print (tabulate(table, headers=headers, tablefmt="grid")) |
| 37 | |
| 38 | def status(self,args): |
| 39 | list = get('%s/restapi/datacenter/%s' % ( args.get("endpoint"), args.get("datacenter"))).json() |
| 40 | table = [] |
| 41 | table.append([list.get('label'), |
| 42 | list.get('internalname'), |
| 43 | list.get('switch'), |
| 44 | list.get('n_running_containers'), |
| 45 | len(list.get('metadata'))]) |
| 46 | |
| 47 | headers = ["Label", |
| 48 | "Internal Name", |
| 49 | "Switch", |
| 50 | "# Containers", |
| 51 | "# Metadata Items"] |
| 52 | |
| 53 | print (tabulate(table, headers=headers, tablefmt="grid")) |
| 54 | |
| 55 | |
| 56 | parser = argparse.ArgumentParser(description='son-emu datacenter') |
| 57 | parser.add_argument( |
| 58 | "command", |
| 59 | choices=['list', 'status'], |
| 60 | help="Action to be executed.") |
| 61 | parser.add_argument( |
| 62 | "--datacenter", "-d", dest="datacenter", |
| 63 | help="Data center to which the command should be applied.") |
| 64 | parser.add_argument( |
| 65 | "--endpoint", "-e", dest="endpoint", |
| 66 | default="http://127.0.0.1:5000", |
| 67 | help="UUID of the plugin to be manipulated.") |
| 68 | |
| 69 | |
| 70 | def main(argv): |
| 71 | args = vars(parser.parse_args(argv)) |
| 72 | c = RestApiClient() |
| 73 | c.execute_command(args) |
| 74 | |