Skip to content

Commit

Permalink
Merge "Taking care of some earlier comments"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and opencontrail-ci-admin committed Dec 15, 2016
2 parents f527cb4 + 77d148b commit ca0a8eb
Show file tree
Hide file tree
Showing 31 changed files with 161 additions and 101 deletions.
2 changes: 1 addition & 1 deletion src/utils/SConscript
@@ -1,6 +1,6 @@
# -*- mode: python; -*-
#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#

Import('BuildEnv')
Expand Down
4 changes: 1 addition & 3 deletions src/utils/contrail-cli/SConscript
@@ -1,7 +1,5 @@

# -*- mode: python; -*-
#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#

Import('BuildEnv')
Expand Down
1 change: 0 additions & 1 deletion src/utils/contrail-cli/commandlist.py

This file was deleted.

3 changes: 1 addition & 2 deletions src/utils/contrail-cli/contrail_analytics_cli/SConscript
@@ -1,6 +1,5 @@
# -*- mode: python; -*-
#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#

Import('BuildEnv')
Expand Down
3 changes: 3 additions & 0 deletions src/utils/contrail-cli/contrail_analytics_cli/commandlist.py
@@ -0,0 +1,3 @@
#
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#
4 changes: 4 additions & 0 deletions src/utils/contrail-cli/contrail_analytics_cli/entry_points.py
@@ -1 +1,5 @@
#
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#

entry_points_dict = dict()
3 changes: 3 additions & 0 deletions src/utils/contrail-cli/contrail_analytics_cli/main.py
@@ -1,3 +1,6 @@
#
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#
from ContrailCli.main import ContrailCliApp
from commandlist import commands_list
import sys, os
Expand Down
11 changes: 5 additions & 6 deletions src/utils/contrail-cli/contrail_analytics_cli/setup.py
@@ -1,21 +1,20 @@
#!/usr/bin/env python
#
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#

PROJECT = 'ContrailAnalyticsCli'

VERSION = '0.1'

from setuptools import setup, find_packages
from setuptools import setup

from entry_points import entry_points_dict

setup( name=PROJECT,
version=VERSION,
description='Contrail Analytics Command Line Interfae',
description='Contrail Analytics Command Line Interface',
platforms=['Any'],
install_requires=['cliff>=2.2.0'],
packages=find_packages(),
package_data={'': ['*.html', '*.css', '*.xml']},
include_package_data=True,
entry_points=entry_points_dict,
zip_safe=False,
)
Expand Down
3 changes: 1 addition & 2 deletions src/utils/contrail-cli/contrail_cli/SConscript
@@ -1,6 +1,5 @@
# -*- mode: python; -*-
#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#

Import('BuildEnv')
Expand Down
3 changes: 3 additions & 0 deletions src/utils/contrail-cli/contrail_cli/commandlist.py
@@ -0,0 +1,3 @@
#
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#
4 changes: 0 additions & 4 deletions src/utils/contrail-cli/contrail_cli/commandmanager.py
Expand Up @@ -15,15 +15,11 @@

"""Modify cliff.CommandManager"""

import logging
import pkg_resources

import cliff.commandmanager
from cliff.commandmanager import EntryPointWrapper

LOG = logging.getLogger(__name__)


class CommandManager(cliff.commandmanager.CommandManager):
"""Add additional functionality to cliff.CommandManager
Expand Down
69 changes: 37 additions & 32 deletions src/utils/contrail-cli/contrail_cli/contrailCli.py
@@ -1,9 +1,12 @@
#
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#

import sys
if 'threading' in sys.modules:
del sys.modules['threading']
from opserver.opserver_util import OpServerUtils
import json
import subprocess
from cliff.command import Command
import xmltodict
import requests
Expand All @@ -29,23 +32,23 @@ def get_description(self, cmd_name=None):
if cmd_name == None:
return
for command in self.cmd_list:
if cmd_name in command[1].keys():
return command[1].values()[0].keys()[0]
if cmd_name == command.cli_name:
return command.cli_help
return
#end get_description

def _add_fields_to_parser(self, parser, command):
for fields in command.cli_params:
parser.add_argument("--"+fields.param_name, nargs=1, help=fields.param_help, metavar='\b')
#end _add_fields_to_parser

def get_parser(self, prog_name):
self.prog_name = prog_name
parser = super(ContrailCli, self).get_parser(prog_name)
cmd_name = None
if self.cmd_name:
cmd_name = '_'.join(self.cmd_name.split(' '))
cmd_name = self.cmd_name
for command in self.cmd_list:
if cmd_name in command[1].keys():
for fields in command[1].values()[0].values():
for args in fields:
parser.add_argument("--"+args[0], nargs=1, help=args[1], metavar='\b')
if cmd_name == command.cli_name:
self._add_fields_to_parser(parser, command)
return parser

def set_http_port(self, port, http_ip):
Expand All @@ -55,29 +58,31 @@ def set_http_port(self, port, http_ip):
def set_cmd_list(self, cmd_list):
self.cmd_list = cmd_list

def take_action(self, parsed_args):
def _prepare_query_url(self, parsed_args, command):
ip_addr = self.http_ip
http_port = self.http_port
url = "http://" + ip_addr + ":" + http_port + "/Snh_" + command.struct_name
arg_count = 0
for params in command.cli_params:
if arg_count > 0:
url = url + "&"
else:
url = url + "?"
url = url + params.param_name + "="
if hasattr(parsed_args, params.param_name) == True:
if getattr(parsed_args, params.param_name) != None:
url = url + getattr(parsed_args, params.param_name)[0]
arg_count = arg_count + 1
return url
#end _prepare_query_url

def take_action(self, parsed_args):
cmd_name = self.cmd_name
for command in self.cmd_list:
if cmd_name in command[1].keys():
tab_url = "http://" + ip_addr + ":" +\
http_port + "/Snh_" + command[0].encode("utf-8")
arg_count = 0
if len(command[1].values()[0].values()):
tab_url = tab_url + "?"
for params in command[1].values()[0].values():
for args in params:
args[0] = args[0].encode("utf-8")
if arg_count > 0:
tab_url = tab_url + "&"
tab_url = tab_url + args[0] + "="
if hasattr(parsed_args, args[0]) == True:
if getattr(parsed_args, args[0]) != None:
tab_url = tab_url + getattr(parsed_args, args[0])[0]
arg_count = arg_count + 1
tables = self.web_invoke(tab_url)
if tables:
doc = xmltodict.parse(tables)
OpServerUtils.messages_dict_scrub(doc)
print json.dumps(doc, indent=4)
if cmd_name == command.cli_name:
url = self._prepare_query_url(parsed_args, command)
result = self.web_invoke(url)
if result:
output = xmltodict.parse(result)
OpServerUtils.messages_dict_scrub(output)
print json.dumps(output, indent=4)
4 changes: 4 additions & 0 deletions src/utils/contrail-cli/contrail_cli/entry_points.py
@@ -1 +1,5 @@
#
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#

entry_points_dict = dict()
30 changes: 17 additions & 13 deletions src/utils/contrail-cli/contrail_cli/help.py
@@ -1,7 +1,11 @@
#
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#

import argparse
import sys
import traceback
import os
from prettytable import PrettyTable
from prettytable import PLAIN_COLUMNS

from cliff.command import Command

Expand All @@ -15,21 +19,21 @@ class HelpAction(argparse.Action):
"""
def __call__(self, parser, namespace, commands, option_string=None):

rows, columns = os.popen('stty size', 'r').read().split()
app = self.default
app.stdout.write('\nCommands:\n')
command_manager = app.command_manager
table = PrettyTable(["cli_name", "cli_help"])
table.set_style(PLAIN_COLUMNS)
table.border = False
table.header = False
table.align = "l"
table.left_padding_width = 2
for name, ep in sorted(command_manager):
if name == "complete":
continue
for command in commands:
if name in command[1].keys():
one_liner = command[1].values()[0].keys()[0]
if len(name) > 35:
app.stdout.write('%s\n' % (name))
app.stdout.write(' %s\n' % (one_liner))
elif 35 + len(one_liner) > columns:
app.stdout.write('%s\n' % (name))
app.stdout.write(' %s\n' % (one_liner))
else:
app.stdout.write(' %-33s %s\n' % (name, one_liner))
if name == command.cli_name:
one_liner = command.cli_help
table.add_row([name, one_liner])
break
app.stdout.write('%s\n' %table)
35 changes: 28 additions & 7 deletions src/utils/contrail-cli/contrail_cli/main.py 100755 → 100644
@@ -1,12 +1,22 @@
#
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#

import sys
import argparse
import inspect

from cliff.app import App
import logging
logging.basicConfig()
from .contrailCli import ContrailCli
from .help import HelpAction
from .commandmanager import CommandManager
from sandesh_common.vns.constants import ServiceHttpPortMap
from collections import namedtuple
cli_mapping = namedtuple("cli_mapping", ("struct_name", "cli_name",
"cli_help", "cli_params"))
cli_params = namedtuple("cli_params", ("param_name", "param_help"))

class ContrailCliApp(App):

Expand All @@ -17,9 +27,20 @@ def __init__(self, commands_list):
command_manager=CommandManager(self.NAME),
deferred_help=True,
)
self.cmd_list = commands_list
self.cmd_list = []
for command in commands_list:
cps_list = []
for fields in command[1].values()[0].values():
for args in fields:
cps = cli_params(param_name=args[0], param_help=args[1])
cps_list.append(cps)
scm = cli_mapping(struct_name=command[0],
cli_name=command[1].keys()[0],
cli_help=command[1].values()[0].keys()[0],
cli_params=cps_list)
self.cmd_list.append(scm)
for command in self.cmd_list:
self.command_manager.add_command(command[1].keys()[0], None)
self.command_manager.add_command(command.cli_name, None)
self.command_manager.del_command('help')
self.port = None
self.mod_name = self.NAME
Expand All @@ -30,7 +51,7 @@ def __init__(self, commands_list):
self.port = ServiceHttpPortMap[self.mod_name]
if self.port == None:
print "No port specified, exiting"
exit(0)
exit(-1)

def run_subcommand(self, argv):
try:
Expand Down Expand Up @@ -58,7 +79,6 @@ def run_subcommand(self, argv):
cmd.set_http_port(self.port if self.options.http_server_port is
None else self.options.http_server_port, self.options.http_server_ip)
cmd.set_cmd_list(self.cmd_list)
result = 1
self.prepare_to_run_command(cmd)
full_name = (cmd_name
if self.interactive_mode
Expand Down Expand Up @@ -100,7 +120,7 @@ def get_matching_commands(self, cmd):
"""
all_cmds = []
for command in self.cmd_list:
all_cmds.append(command[1].keys()[0])
all_cmds.append(command.cli_name)
dist = []
for candidate in sorted(all_cmds):
if candidate.startswith(cmd):
Expand All @@ -109,8 +129,9 @@ def get_matching_commands(self, cmd):
return dist

def clean_up(self, cmd, result, err):
if err:
self.LOG.debug('got an error: %s', err)
if result:
self.LOG.error('got an error: %s while executing cmd %s, \
exception %s', result, cmd.cmd_name, err)

def build_option_parser(self, description, version,
argparse_kwargs=None):
Expand Down
11 changes: 5 additions & 6 deletions src/utils/contrail-cli/contrail_cli/setup.py
@@ -1,21 +1,20 @@
#!/usr/bin/env python
#
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#

PROJECT = 'ContrailCli'

VERSION = '0.1'

from setuptools import setup, find_packages
from setuptools import setup

from entry_points import entry_points_dict

setup( name=PROJECT,
version=VERSION,
description='Contrail Command Line Interfae',
description='Contrail Command Line Interface',
platforms=['Any'],
install_requires=['cliff>=2.2.0'],
packages=find_packages(),
package_data={'': ['*.html', '*.css', '*.xml']},
include_package_data=True,
entry_points=entry_points_dict,
zip_safe=False,
)
Expand Down
3 changes: 1 addition & 2 deletions src/utils/contrail-cli/contrail_config_cli/SConscript
@@ -1,6 +1,5 @@
# -*- mode: python; -*-
#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#

Import('BuildEnv')
Expand Down
4 changes: 4 additions & 0 deletions src/utils/contrail-cli/contrail_config_cli/commandlist.py
@@ -0,0 +1,4 @@
#
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#

4 changes: 4 additions & 0 deletions src/utils/contrail-cli/contrail_config_cli/entry_points.py
@@ -1 +1,5 @@
#
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#

entry_points_dict = dict()
4 changes: 4 additions & 0 deletions src/utils/contrail-cli/contrail_config_cli/main.py
@@ -1,3 +1,7 @@
#
# Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
#

from ContrailCli.main import ContrailCliApp
from commandlist import commands_list
import sys, os
Expand Down

0 comments on commit ca0a8eb

Please sign in to comment.