ce_file_copy: update to Compatible with multiple version of NETCONF API. (#59450)

* update to Compatible with multiple version of NETCONF API.

* update for  shippable.

* Update ce_file_copy.py

* Update ce_file_copy.py

* Update ce_file_copy.py

* Update ce_file_copy.py

* Update ce_file_copy.py
This commit is contained in:
Xu Yuandong 2019-07-29 14:02:32 +08:00 committed by ansibot
parent a589cac18a
commit 86937e06e3

View file

@ -99,7 +99,9 @@ import sys
import time import time
from xml.etree import ElementTree from xml.etree import ElementTree
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network.cloudengine.ce import ce_argument_spec, run_commands, get_nc_config from ansible.module_utils.network.cloudengine.ce import ce_argument_spec, get_nc_config
from ansible.module_utils.connection import ConnectionError
from ansible.module_utils.network.common.utils import validate_ip_v6_address
try: try:
import paramiko import paramiko
@ -144,13 +146,9 @@ CE_NC_GET_FILE_INFO = """
CE_NC_GET_SCP_ENABLE = """ CE_NC_GET_SCP_ENABLE = """
<filter type="subtree"> <filter type="subtree">
<sshs xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0"> <sshs xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
<sshServerEnable> </sshs>
<scpIpv4Enable></scpIpv4Enable> </filter>
<scpIpv6Enable></scpIpv6Enable>
</sshServerEnable>
</sshs>
</filter>
""" """
@ -202,6 +200,7 @@ class FileCopy(object):
self.local_file = self.module.params['local_file'] self.local_file = self.module.params['local_file']
self.remote_file = self.module.params['remote_file'] self.remote_file = self.module.params['remote_file']
self.file_system = self.module.params['file_system'] self.file_system = self.module.params['file_system']
self.host_is_ipv6 = validate_ip_v6_address(self.module.params['provider']['host'])
# state # state
self.transfer_result = None self.transfer_result = None
@ -309,11 +308,14 @@ class FileCopy(object):
def get_scp_enable(self): def get_scp_enable(self):
"""Get scp enable state""" """Get scp enable state"""
xml_str = CE_NC_GET_SCP_ENABLE ret_xml = ''
scp_enable = dict() try:
ret_xml = get_nc_config(self.module, xml_str) ret_xml = get_nc_config(self.module, CE_NC_GET_SCP_ENABLE)
except ConnectionError:
self.module.fail_json(msg='Error: The NETCONF API of scp_enable is not supported.')
if "<data/>" in ret_xml: if "<data/>" in ret_xml:
return scp_enable return False
xml_str = ret_xml.replace('\r', '').replace('\n', '').\ xml_str = ret_xml.replace('\r', '').replace('\n', '').\
replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\ replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\
@ -321,14 +323,16 @@ class FileCopy(object):
# get file info # get file info
root = ElementTree.fromstring(xml_str) root = ElementTree.fromstring(xml_str)
topo = root.find("sshs/sshServerEnable") topo1 = root.find("sshs/sshServer/scpEnable")
if topo is None: topo2 = root.find("sshs/sshServerEnable/scpIpv4Enable")
return scp_enable topo3 = root.find("sshs/sshServerEnable/scpIpv6Enable")
if topo1 is not None:
for eles in topo: return str(topo1.text).strip().lower() == 'enable'
scp_enable[eles.tag] = eles.text elif self.host_is_ipv6 and topo3 is not None:
return str(topo3.text).strip().lower() == 'enable'
return scp_enable elif topo2 is not None:
return str(topo2.text).strip().lower() == 'enable'
return False
def work(self): def work(self):
"""Excute task """ """Excute task """
@ -349,13 +353,14 @@ class FileCopy(object):
self.module.fail_json( self.module.fail_json(
msg="'Error: The maximum length of remote_file is 4096.'") msg="'Error: The maximum length of remote_file is 4096.'")
cur_state = self.get_scp_enable() scp_enable = self.get_scp_enable()
if len(cur_state) > 0 and (cur_state.get('scpIpv4Enable').lower() == 'disable' or cur_state.get('scpIpv6Enable').lower() == 'disable'): if not scp_enable:
self.module.fail_json( if self.host_is_ipv6:
msg="'Error: Please ensure ipv4 and ipv6 SCP server are enabled.'") self.module.fail_json(
elif len(cur_state) == 0: msg="'Error: Please ensure ipv6 SCP server are enabled.'")
self.module.fail_json( else:
msg="'Error: Please ensure ipv4 and ipv6 SCP server are enabled.'") self.module.fail_json(
msg="'Error: Please ensure ipv4 SCP server are enabled.'")
if not os.path.isfile(self.local_file): if not os.path.isfile(self.local_file):
self.module.fail_json( self.module.fail_json(