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