Merge pull request #131 from stevenvanrossem/master
[osm/vim-emu.git] / src / emuvim / cli / rest / compute.py
1 from requests import get,put
2 from tabulate import tabulate
3 import pprint
4 import argparse
5 import json
6
7 pp = pprint.PrettyPrinter(indent=4)
8
9 class RestApiClient():
10
11 def __init__(self):
12 self.cmds = {}
13
14 def execute_command(self, args):
15 if getattr(self, args["command"]) is not None:
16 # call the local method with the same name as the command arg
17 getattr(self, args["command"])(args)
18 else:
19 print("Command not implemented.")
20
21 def start(self, args):
22
23 nw_list = list()
24 if args.get("network") is not None:
25 nw_list = self._parse_network(args.get("network"))
26 req = {'image':args.get("image"),
27 'command':args.get("docker_command"),
28 'network':nw_list}
29
30 response = put("%s/restapi/compute/%s/%s/start" %
31 (args.get("endpoint"),
32 args.get("datacenter"),
33 args.get("name")),
34 json = json.dumps(req))
35 pp.pprint(response.json())
36
37 def stop(self, args):
38
39 response = get("%s/restapi/compute/%s/%s/stop" %
40 (args.get("endpoint"),
41 args.get("datacenter"),
42 args.get("name")))
43 pp.pprint(response.json())
44
45 def list(self,args):
46
47 list = get('%s/restapi/compute/%s' % (args.get("endpoint"),args.get('datacenter'))).json()
48
49 table = []
50 for c in list:
51 # for each container add a line to the output table
52 if len(c) > 1:
53 name = c[0]
54 status = c[1]
55 eth0ip = None
56 eth0status = "down"
57 if len(status.get("network")) > 0:
58 eth0ip = status.get("network")[0].get("ip")
59 eth0status = "up" if status.get(
60 "network")[0].get("up") else "down"
61 table.append([status.get("datacenter"),
62 name,
63 status.get("image"),
64 eth0ip,
65 eth0status,
66 status.get("state").get("Status")])
67
68 headers = ["Datacenter",
69 "Container",
70 "Image",
71 "eth0 IP",
72 "eth0 status",
73 "Status"]
74 print(tabulate(table, headers=headers, tablefmt="grid"))
75
76 def status(self,args):
77
78 list = get("%s/restapi/compute/%s/%s" %
79 (args.get("endpoint"),
80 args.get("datacenter"),
81 args.get("name"))).json()
82 pp.pprint(list)
83
84
85
86 def _parse_network(self, network_str):
87 '''
88 parse the options for all network interfaces of the vnf
89 :param network_str: (id=x,ip=x.x.x.x/x), ...
90 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
91 '''
92 nw_list = list()
93 networks = network_str[1:-1].split('),(')
94 for nw in networks:
95 nw_dict = dict(tuple(e.split('=')) for e in nw.split(','))
96 nw_list.append(nw_dict)
97
98 return nw_list
99
100
101 parser = argparse.ArgumentParser(description='son-emu datacenter')
102 parser.add_argument(
103 "command",
104 choices=['start', 'stop', 'list', 'status'],
105 help="Action to be executed.")
106 parser.add_argument(
107 "--datacenter", "-d", dest="datacenter",
108 help="Data center to which the command should be applied.")
109 parser.add_argument(
110 "--name", "-n", dest="name",
111 help="Name of compute instance e.g. 'vnf1'.")
112 parser.add_argument(
113 "--image","-i", dest="image",
114 help="Name of container image to be used e.g. 'ubuntu:trusty'")
115 parser.add_argument(
116 "--dcmd", "-c", dest="docker_command",
117 help="Startup command of the container e.g. './start.sh'")
118 parser.add_argument(
119 "--net", dest="network",
120 help="Network properties of a compute instance e.g. \
121 '(id=input,ip=10.0.10.3/24),(id=output,ip=10.0.10.4/24)' for multiple interfaces.")
122 parser.add_argument(
123 "--endpoint", "-e", dest="endpoint",
124 default="http://127.0.0.1:5001",
125 help="UUID of the plugin to be manipulated.")
126
127 def main(argv):
128 args = vars(parser.parse_args(argv))
129 c = RestApiClient()
130 c.execute_command(args)