Changed the name of call_rpc to rpc and changed the way rpc call is made

This commit is contained in:
rajvidhimar 2016-10-19 16:07:27 +05:30
parent 2fef4b6504
commit 65529d1f35
3 changed files with 41 additions and 37 deletions

View File

@ -8,9 +8,6 @@ from __future__ import absolute_import
import logging import logging
import json import json
# Import Salt libs
import salt.ext.six as six
try: try:
from lxml import etree from lxml import etree
except ImportError: except ImportError:
@ -25,17 +22,12 @@ try:
from jnpr.junos.utils.scp import SCP from jnpr.junos.utils.scp import SCP
import jnpr.junos.utils import jnpr.junos.utils
import jnpr.junos.cfg import jnpr.junos.cfg
import jxmlease
# pylint: enable=W0611 # pylint: enable=W0611
HAS_JUNOS = True HAS_JUNOS = True
except ImportError: except ImportError:
HAS_JUNOS = False HAS_JUNOS = False
try:
import jxmlease
except:
print "jxmlease not found"
# Set up logging # Set up logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -56,7 +48,7 @@ def __virtual__():
return __virtualname__ return __virtualname__
else: else:
return (False, 'The junos module could not be \ return (False, 'The junos module could not be \
loaded: junos-eznc or proxy could not be loaded.') loaded: junos-eznc or jxmlease or proxy could not be loaded.')
def facts_refresh(): def facts_refresh():
@ -102,7 +94,7 @@ def facts():
return ret return ret
def call_rpc(cmd=None, dest=None, *args, **kwargs): def rpc(cmd=None, dest=None, format='xml', *args, **kwargs):
''' '''
This function executes the rpc provided as arguments on the junos device. This function executes the rpc provided as arguments on the junos device.
The returned data can be stored in a file whose destination can be The returned data can be stored in a file whose destination can be
@ -122,6 +114,7 @@ def call_rpc(cmd=None, dest=None, *args, **kwargs):
* args: other arguments as taken by rpc call of PyEZ * args: other arguments as taken by rpc call of PyEZ
* kwargs: keyworded arguments taken by rpc call of PyEZ * kwargs: keyworded arguments taken by rpc call of PyEZ
''' '''
conn = __proxy__['junos.conn']() conn = __proxy__['junos.conn']()
ret = dict() ret = dict()
ret['out'] = True ret['out'] = True
@ -133,7 +126,9 @@ def call_rpc(cmd=None, dest=None, *args, **kwargs):
else: else:
op.update(kwargs) op.update(kwargs)
if dest is None and format != 'xml':
log.error(
'Format can only be specified when the rpc output is stored in a file mentioned in dest.')
write_response = '' write_response = ''
try: try:
@ -142,46 +137,51 @@ def call_rpc(cmd=None, dest=None, *args, **kwargs):
if len(args) > 0: if len(args) > 0:
filter_reply = etree.XML(args[0]) filter_reply = etree.XML(args[0])
if dest is not None and 'format' in op: xml_reply = getattr(
rpc_reply = getattr(conn.rpc, cmd.replace('-', '_'))(filter_reply, options=op) conn.rpc,
if op['format']=='json': cmd.replace('-',
'_'))(filter_reply,
options=op)
ret['message'] = jxmlease.parse(etree.tostring(xml_reply))
write_response = etree.tostring(xml_reply)
if format != 'xml':
op.update({'format': format})
rpc_reply = getattr(
conn.rpc,
cmd.replace('-',
'_'))(filter_reply,
options=op)
if format == 'json':
write_response = json.dumps(rpc_reply, indent=1) write_response = json.dumps(rpc_reply, indent=1)
else: else:
write_response = etree.tostring(rpc_reply) write_response = etree.tostring(rpc_reply)
op.pop('format')
ret['message'] = jxmlease.parse(etree.tostring(getattr(conn.rpc, cmd.replace('-', '_'))(filter_reply, options=op)))
else:
ret['message'] = jxmlease.parse(etree.tostring(getattr(conn.rpc, cmd.replace('-', '_'))(filter_reply, options=op)))
else: else:
d = dict() xml_reply = getattr(conn.rpc, cmd.replace('-', '_'))(**op)
is_json = False ret['message'] = jxmlease.parse(etree.tostring(xml_reply))
write_response = etree.tostring(xml_reply)
if dest is not None and 'format' in op:
if op['format']=='json':
is_json = True
d = {'format': op.pop('format')}
rpc_reply = getattr(conn.rpc, cmd.replace('-', '_'))(d, **op) if format != 'xml':
if is_json: rpc_reply = getattr(
conn.rpc,
cmd.replace('-',
'_'))({'format': format},
**op)
if format == 'json':
write_response = json.dumps(rpc_reply, indent=1) write_response = json.dumps(rpc_reply, indent=1)
else: else:
write_response = etree.tostring(rpc_reply) write_response = etree.tostring(rpc_reply)
ret['message'] = jxmlease.parse(etree.tostring(getattr(conn.rpc, cmd.replace('-', '_'))(**op)))
else:
ret['message'] = jxmlease.parse(etree.tostring(getattr(conn.rpc, cmd.replace('-', '_'))(**op)))
except Exception as exception: except Exception as exception:
ret['message'] = 'Execution failed due to "{0}"'.format(exception) ret['message'] = 'Execution failed due to "{0}"'.format(exception)
ret['out'] = False ret['out'] = False
if dest is not None: if dest is not None:
with open(dest, 'a') as file: with open(dest, 'w') as fp:
file.write(write_response) fp.write(write_response)
return ret return ret

View File

@ -75,7 +75,11 @@ def id(opts):
def grains(): def grains():
thisproxy['grains'] = copy.deepcopy(thisproxy['conn'].facts) thisproxy['grains'] = copy.deepcopy(thisproxy['conn'].facts)
thisproxy['grains']['version_info'] = thisproxy['grains']['version_info'].v_dict thisproxy[
'grains'][
'version_info'] = thisproxy[
'grains'][
'version_info'].v_dict
return thisproxy['grains'] return thisproxy['grains']

View File

@ -80,7 +80,7 @@ def commit(name):
def rollback(name): def rollback(name):
''' '''
Rollbacks the committed changes. Rollbacks the committed changes.
.. code-block:: yaml .. code-block:: yaml
rollback the changes: rollback the changes: