Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Jul 29, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 32 additions & 27 deletions lib/ansible/modules/network/cloudengine/ce_file_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@
import time
from xml.etree import ElementTree
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:
import paramiko
Expand Down Expand Up @@ -144,13 +146,9 @@

CE_NC_GET_SCP_ENABLE = """
<filter type="subtree">
<sshs xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
<sshServerEnable>
<scpIpv4Enable></scpIpv4Enable>
<scpIpv6Enable></scpIpv6Enable>
</sshServerEnable>
</sshs>
</filter>
<sshs xmlns="http://www.huawei.com/netconf/vrp" content-version="1.0" format-version="1.0">
</sshs>
</filter>
"""


Expand Down Expand Up @@ -202,6 +200,7 @@ def __init__(self, argument_spec):
self.local_file = self.module.params['local_file']
self.remote_file = self.module.params['remote_file']
self.file_system = self.module.params['file_system']
self.host_is_ipv6 = validate_ip_v6_address(self.module.params['provider']['host'])

# state
self.transfer_result = None
Expand Down Expand Up @@ -309,26 +308,31 @@ def transfer_file(self, dest):
def get_scp_enable(self):
"""Get scp enable state"""

xml_str = CE_NC_GET_SCP_ENABLE
scp_enable = dict()
ret_xml = get_nc_config(self.module, xml_str)
ret_xml = ''
try:
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:
return scp_enable
return False

xml_str = ret_xml.replace('\r', '').replace('\n', '').\
replace('xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"', "").\
replace('xmlns="http://www.huawei.com/netconf/vrp"', "")

# get file info
root = ElementTree.fromstring(xml_str)
topo = root.find("sshs/sshServerEnable")
if topo is None:
return scp_enable

for eles in topo:
scp_enable[eles.tag] = eles.text

return scp_enable
topo1 = root.find("sshs/sshServer/scpEnable")
topo2 = root.find("sshs/sshServerEnable/scpIpv4Enable")
topo3 = root.find("sshs/sshServerEnable/scpIpv6Enable")
if topo1 is not None:
return str(topo1.text).strip().lower() == 'enable'
elif self.host_is_ipv6 and topo3 is not None:
return str(topo3.text).strip().lower() == 'enable'
elif topo2 is not None:
return str(topo2.text).strip().lower() == 'enable'
return False

def work(self):
"""Excute task """
Expand All @@ -349,13 +353,14 @@ def work(self):
self.module.fail_json(
msg="'Error: The maximum length of remote_file is 4096.'")

cur_state = self.get_scp_enable()
if len(cur_state) > 0 and (cur_state.get('scpIpv4Enable').lower() == 'disable' or cur_state.get('scpIpv6Enable').lower() == 'disable'):
self.module.fail_json(
msg="'Error: Please ensure ipv4 and ipv6 SCP server are enabled.'")
elif len(cur_state) == 0:
self.module.fail_json(
msg="'Error: Please ensure ipv4 and ipv6 SCP server are enabled.'")
scp_enable = self.get_scp_enable()
if not scp_enable:
if self.host_is_ipv6:
self.module.fail_json(
msg="'Error: Please ensure ipv6 SCP server are enabled.'")
else:
self.module.fail_json(
msg="'Error: Please ensure ipv4 SCP server are enabled.'")

if not os.path.isfile(self.local_file):
self.module.fail_json(
Expand Down