2 Copyright (c) 2015 SONATA-NFV and Paderborn University
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
17 Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
18 nor the names of its contributors may be used to endorse or promote
19 products derived from this software without specific prior written
22 This work has been performed in the framework of the SONATA project,
23 funded by the European Commission under Grant number 671517 through
24 the Horizon 2020 and 5G-PPP programmes. The authors would like to
25 acknowledge the contributions of their colleagues of the SONATA
26 partner consortium (www.sonata-nfv.eu).
28 from requests
import get
29 from tabulate
import tabulate
33 pp
= pprint
.PrettyPrinter(indent
=4)
35 class RestApiClient():
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
)
45 print("Command not implemented.")
48 list = get('%s/restapi/datacenter' % args
.get('endpoint')).json()
51 # for each dc add a line to the output table
53 table
.append([d
.get("label"),
54 d
.get("internalname"),
56 d
.get("n_running_containers"),
57 len(d
.get("metadata"))])
63 print (tabulate(table
, headers
=headers
, tablefmt
="grid"))
65 def status(self
,args
):
66 list = get('%s/restapi/datacenter/%s' % ( args
.get("endpoint"), args
.get("datacenter"))).json()
68 table
.append([list.get('label'),
69 list.get('internalname'),
71 list.get('n_running_containers'),
72 len(list.get('metadata'))])
80 print (tabulate(table
, headers
=headers
, tablefmt
="grid"))
83 parser
= argparse
.ArgumentParser(description
='son-emu-cli datacenter')
86 choices
=['list', 'status'],
87 help="Action to be executed.")
89 "--datacenter", "-d", dest
="datacenter",
90 help="Data center to which the command should be applied.")
92 "--endpoint", "-e", dest
="endpoint",
93 default
="http://127.0.0.1:5001",
94 help="REST API endpoint of son-emu (default:http://127.0.0.1:5001)")
98 args
= vars(parser
.parse_args(argv
))
100 c
.execute_command(args
)