bdef0ec10c3f6ae7d6f5c6a016d1a4138d7286f9
[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 responce = 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(responce.json())
36 def stop(self, args):
37
38 responce = get("%s/restapi/compute/%s/%s/stop" %
39 (args.get("endpoint"),
40 args.get("datacenter"),
41 args.get("name")))
42 pp.pprint(responce.json())
43
44 def list(self,args):
45
46 list = get('%s/restapi/compute/%s' % (args.get("endpoint"),args.get('datacenter'))).json()
47
48 table = []
49 for c in list:
50 # for each container add a line to the output table
51 if len(c) > 1:
52 name = c[0]
53 status = c[1]
54 eth0ip = None
55 eth0status = "down"
56 if len(status.get("network")) > 0:
57 eth0ip = status.get("network")[0].get("ip")
58 eth0status = "up" if status.get(
59 "network")[0].get("up") else "down"
60 table.append([status.get("datacenter"),
61 name,
62 status.get("image"),
63 eth0ip,
64 eth0status,
65 status.get("state").get("Status")])
66
67 headers = ["Datacenter",
68 "Container",
69 "Image",
70 "eth0 IP",
71 "eth0 status",
72 "Status"]
73 print(tabulate(table, headers=headers, tablefmt="grid"))
74
75 def status(self,args):
76
77 list = get("%s/restapi/compute/%s/%s" %
78 (args.get("endpoint"),
79 args.get("datacenter"),
80 args.get("name"))).json()
81 pp.pprint(list)
82
83
84
85 def _parse_network(self, network_str):
86 '''
87 parse the options for all network interfaces of the vnf
88 :param network_str: (id=x,ip=x.x.x.x/x), ...
89 :return: list of dicts [{"id":x,"ip":"x.x.x.x/x"}, ...]
90 '''
91 nw_list = list()
92 networks = network_str[1:-1].split('),(')
93 for nw in networks:
94 nw_dict = dict(tuple(e.split('=')) for e in nw.split(','))
95 nw_list.append(nw_dict)
96
97 return nw_list
98
99
100 parser = argparse.ArgumentParser(description='son-emu datacenter')
101 parser.add_argument(
102 "command",
103 choices=['start', 'stop', 'list', 'status'],
104 help="Action to be executed.")
105 parser.add_argument(
106 "--datacenter", "-d", dest="datacenter",
107 help="Data center to which the command should be applied.")
108 parser.add_argument(
109 "--name", "-n", dest="name",
110 help="Name of compute instance e.g. 'vnf1'.")
111 parser.add_argument(
112 "--image","-i", dest="image",
113 help="Name of container image to be used e.g. 'ubuntu:trusty'")
114 parser.add_argument(
115 "--dcmd", "-c", dest="docker_command",
116 help="Startup command of the container e.g. './start.sh'")
117 parser.add_argument(
118 "--net", dest="network",
119 help="Network properties of a compute instance e.g. \
120 '(id=input,ip=10.0.10.3/24),(id=output,ip=10.0.10.4/24)' for multiple interfaces.")
121 parser.add_argument(
122 "--endpoint", "-e", dest="endpoint",
123 default="http://127.0.0.1:5000",
124 help="UUID of the plugin to be manipulated.")
125
126 def main(argv):
127 args = vars(parser.parse_args(argv))
128 c = RestApiClient()
129 c.execute_command(args)