blob: 651c55ce1a8fdddcae94b74f00c2c04ab58be68b [file] [log] [blame]
peusterm79ef6ae2016-07-08 13:53:57 +02001"""
2Copyright (c) 2015 SONATA-NFV and Paderborn University
3ALL RIGHTS RESERVED.
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
18nor the names of its contributors may be used to endorse or promote
19products derived from this software without specific prior written
20permission.
21
22This work has been performed in the framework of the SONATA project,
23funded by the European Commission under Grant number 671517 through
24the Horizon 2020 and 5G-PPP programmes. The authors would like to
25acknowledge the contributions of their colleagues of the SONATA
26partner consortium (www.sonata-nfv.eu).
27"""
hadik3r237d3f52016-06-27 17:57:49 +020028from requests import get
29from tabulate import tabulate
30import pprint
31import argparse
32
33pp = pprint.PrettyPrinter(indent=4)
34
35class RestApiClient():
36
37 def __init__(self):
38 self.cmds = {}
39
40 def execute_command(self, args):
41 if getattr(self, args["command"]) is not None:
42 # call the local method with the same name as the command arg
43 getattr(self, args["command"])(args)
44 else:
45 print("Command not implemented.")
46
47 def list(self,args):
48 list = get('%s/restapi/datacenter' % args.get('endpoint')).json()
49 table = []
50 for d in list:
51 # for each dc add a line to the output table
52 if len(d) > 0:
53 table.append([d.get("label"),
54 d.get("internalname"),
55 d.get("switch"),
56 d.get("n_running_containers"),
57 len(d.get("metadata"))])
58 headers = ["Label",
59 "Internal Name",
60 "Switch",
61 "# Containers",
62 "# Metadata Items"]
63 print (tabulate(table, headers=headers, tablefmt="grid"))
64
65 def status(self,args):
66 list = get('%s/restapi/datacenter/%s' % ( args.get("endpoint"), args.get("datacenter"))).json()
67 table = []
68 table.append([list.get('label'),
69 list.get('internalname'),
70 list.get('switch'),
71 list.get('n_running_containers'),
72 len(list.get('metadata'))])
73
74 headers = ["Label",
75 "Internal Name",
76 "Switch",
77 "# Containers",
78 "# Metadata Items"]
79
80 print (tabulate(table, headers=headers, tablefmt="grid"))
81
82
stevenvanrossemf693a3b2017-06-01 15:15:59 +020083parser = argparse.ArgumentParser(description='son-emu-cli datacenter')
hadik3r237d3f52016-06-27 17:57:49 +020084parser.add_argument(
85 "command",
86 choices=['list', 'status'],
87 help="Action to be executed.")
88parser.add_argument(
89 "--datacenter", "-d", dest="datacenter",
90 help="Data center to which the command should be applied.")
91parser.add_argument(
92 "--endpoint", "-e", dest="endpoint",
peusterm0a336cc2016-07-04 09:15:47 +020093 default="http://127.0.0.1:5001",
stevenvanrossemf693a3b2017-06-01 15:15:59 +020094 help="REST API endpoint of son-emu (default:http://127.0.0.1:5001)")
hadik3r237d3f52016-06-27 17:57:49 +020095
96
97def main(argv):
98 args = vars(parser.parse_args(argv))
99 c = RestApiClient()
100 c.execute_command(args)
101