blob: 4a20ca94978ace7044d1f37a2ad406dfdb327f11 [file] [log] [blame]
hadik3r237d3f52016-06-27 17:57:49 +02001from requests import get,put
2from tabulate import tabulate
3import pprint
4import argparse
5import json
6
7pp = pprint.PrettyPrinter(indent=4)
8
9class 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
stevenvanrossem73efd192016-06-29 01:44:07 +020030 response = put("%s/restapi/compute/%s/%s/start" %
hadik3r237d3f52016-06-27 17:57:49 +020031 (args.get("endpoint"),
32 args.get("datacenter"),
33 args.get("name")),
34 json = json.dumps(req))
stevenvanrossem73efd192016-06-29 01:44:07 +020035 pp.pprint(response.json())
36
hadik3r237d3f52016-06-27 17:57:49 +020037 def stop(self, args):
38
stevenvanrossem73efd192016-06-29 01:44:07 +020039 response = get("%s/restapi/compute/%s/%s/stop" %
hadik3r237d3f52016-06-27 17:57:49 +020040 (args.get("endpoint"),
41 args.get("datacenter"),
42 args.get("name")))
stevenvanrossem73efd192016-06-29 01:44:07 +020043 pp.pprint(response.json())
hadik3r237d3f52016-06-27 17:57:49 +020044
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
101parser = argparse.ArgumentParser(description='son-emu datacenter')
102parser.add_argument(
103 "command",
hadik3rbe81adb2016-06-27 19:03:36 +0200104 choices=['start', 'stop', 'list', 'status'],
hadik3r237d3f52016-06-27 17:57:49 +0200105 help="Action to be executed.")
106parser.add_argument(
107 "--datacenter", "-d", dest="datacenter",
108 help="Data center to which the command should be applied.")
109parser.add_argument(
110 "--name", "-n", dest="name",
111 help="Name of compute instance e.g. 'vnf1'.")
112parser.add_argument(
113 "--image","-i", dest="image",
114 help="Name of container image to be used e.g. 'ubuntu:trusty'")
115parser.add_argument(
116 "--dcmd", "-c", dest="docker_command",
117 help="Startup command of the container e.g. './start.sh'")
118parser.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.")
122parser.add_argument(
hadik3r237d3f52016-06-27 17:57:49 +0200123 "--endpoint", "-e", dest="endpoint",
peusterm0a336cc2016-07-04 09:15:47 +0200124 default="http://127.0.0.1:5001",
hadik3r237d3f52016-06-27 17:57:49 +0200125 help="UUID of the plugin to be manipulated.")
126
127def main(argv):
128 args = vars(parser.parse_args(argv))
129 c = RestApiClient()
130 c.execute_command(args)