1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Move redudant check_ip_addr method to common ce (#25575)

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2017-06-15 17:51:18 +05:30 committed by John R Barker
parent a51d64dfe3
commit 8ad04d6348
10 changed files with 32 additions and 212 deletions

View file

@ -29,6 +29,8 @@
#
import re
import socket
import sys
from ansible.module_utils.basic import env_fallback
from ansible.module_utils.network_common import to_list, ComplexList
@ -443,3 +445,21 @@ def execute_nc_cli(module, xml_str):
conn = get_nc_connection(module)
return conn.execute_cli(xml_str)
def check_ip_addr(ipaddr):
""" check ip address, Supports IPv4 and IPv6 """
if not ipaddr or '\x00' in ipaddr:
return False
try:
res = socket.getaddrinfo(ipaddr, 0, socket.AF_UNSPEC,
socket.SOCK_STREAM,
0, socket.AI_NUMERICHOST)
return bool(res)
except socket.gaierror:
err = sys.exc_info()[1]
if err.args[0] == socket.EAI_NONAME:
return False
raise

View file

@ -282,12 +282,9 @@ updates:
"hwtacacs server authorization 10.135.182.157 vpn-instance test_vpn public-net"]
'''
import sys
import socket
from xml.etree import ElementTree
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec
from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec, check_ip_addr
SUCCESS = """success"""
FAILED = """failed"""
@ -2193,25 +2190,6 @@ class AaaServerHost(object):
return cmds
def check_ip_addr(ipaddr):
""" check_ip_addr, Supports IPv4 and IPv6 """
if not ipaddr or '\x00' in ipaddr:
return False
try:
res = socket.getaddrinfo(ipaddr, 0, socket.AF_UNSPEC,
socket.SOCK_STREAM,
0, socket.AI_NUMERICHOST)
return bool(res)
except socket.gaierror:
err = sys.exc_info()[1]
if err.args[0] == socket.EAI_NONAME:
return False
raise
return True
def check_name(**kwargs):
""" Check invalid name """

View file

@ -210,12 +210,9 @@ updates:
sample: ["undo acl name test"]
'''
import socket
import sys
from xml.etree import ElementTree
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec
from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec, check_ip_addr
# get acl
CE_GET_ACL_HEADER = """
@ -319,25 +316,6 @@ CE_DELETE_ACL_BASE_RULE_TAIL = """
"""
def check_ip_addr(ipaddr):
""" check_ip_addr, Supports IPv4 and IPv6 """
if not ipaddr or '\x00' in ipaddr:
return False
try:
res = socket.getaddrinfo(ipaddr, 0, socket.AF_UNSPEC,
socket.SOCK_STREAM,
0, socket.AI_NUMERICHOST)
return bool(res)
except socket.gaierror:
err = sys.exc_info()[1]
if err.args[0] == socket.EAI_NONAME:
return False
raise
return True
class BaseAcl(object):
""" Manages base acl configuration """

View file

@ -356,11 +356,9 @@ updates:
sample: ["undo acl name test"]
'''
import socket
import sys
from xml.etree import ElementTree
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec
from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec, check_ip_addr
# get acl
@ -486,25 +484,6 @@ IGMP_TYPE_NUM = {"host-query": "17",
"v3host-report": "34"}
def check_ip_addr(ipaddr):
""" check ip address, Supports IPv4 and IPv6 """
if not ipaddr or '\x00' in ipaddr:
return False
try:
res = socket.getaddrinfo(ipaddr, 0, socket.AF_UNSPEC,
socket.SOCK_STREAM,
0, socket.AI_NUMERICHOST)
return bool(res)
except socket.gaierror:
err = sys.exc_info()[1]
if err.args[0] == socket.EAI_NONAME:
return False
raise
return True
def get_wildcard_mask(mask):
""" convert mask length to ip address wildcard mask, i.e. 24 to 0.0.0.255 """

View file

@ -497,11 +497,8 @@ updates:
'''
import re
import sys
import socket
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec
from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec, check_ip_addr
# get bgp address family
CE_GET_BGP_ADDRESS_FAMILY_HEADER = """
@ -882,27 +879,6 @@ CE_BGP_DELETE_NETWORK_UNIT = """
"""
def check_ip_addr(**kwargs):
""" check_ip_addr, Supports IPv4 and IPv6"""
ipaddr = kwargs["ipaddr"]
if not ipaddr or '\x00' in ipaddr:
return False
try:
res = socket.getaddrinfo(ipaddr, 0, socket.AF_UNSPEC,
socket.SOCK_STREAM,
0, socket.AI_NUMERICHOST)
return bool(res)
except socket.gaierror:
err = sys.exc_info()[1]
if err.args[0] == socket.EAI_NONAME:
return False
raise
return True
class BgpAf(object):
""" Manages BGP Address-family configuration """

View file

@ -293,10 +293,8 @@ updates:
'''
import re
import sys
import socket
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec
from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec, check_ip_addr
# get bgp peer
@ -461,27 +459,6 @@ CE_DELETE_PEER_BFD_TAIL = """
"""
def check_ip_addr(**kwargs):
""" check_ip_addr, Supports IPv4 and IPv6 """
ipaddr = kwargs["ipaddr"]
if not ipaddr or '\x00' in ipaddr:
return False
try:
res = socket.getaddrinfo(ipaddr, 0, socket.AF_UNSPEC,
socket.SOCK_STREAM,
0, socket.AI_NUMERICHOST)
return bool(res)
except socket.gaierror:
err = sys.exc_info()[1]
if err.args[0] == socket.EAI_NONAME:
return False
raise
return True
class BgpNeighbor(object):
""" Manages BGP peer configuration """

View file

@ -460,11 +460,8 @@ updates:
'''
import re
import sys
import socket
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec
from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec, check_ip_addr
# get bgp peer af
CE_GET_BGP_PEER_AF_HEADER = """
@ -571,27 +568,6 @@ CE_DELETE_BGP_PEER_AF = """
"""
def check_ip_addr(**kwargs):
""" check_ip_addr, Supports IPv4 and IPv6 """
ipaddr = kwargs["ipaddr"]
if not ipaddr or '\x00' in ipaddr:
return False
try:
res = socket.getaddrinfo(ipaddr, 0, socket.AF_UNSPEC,
socket.SOCK_STREAM,
0, socket.AI_NUMERICHOST)
return bool(res)
except socket.gaierror:
err = sys.exc_info()[1]
if err.args[0] == socket.EAI_NONAME:
return False
raise
return True
class BgpNeighborAf(object):
""" Manages BGP neighbor Address-family configuration """

View file

@ -326,11 +326,10 @@ changed:
type: boolean
sample: true
'''
import socket
import sys
from xml.etree import ElementTree
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ce import ce_argument_spec, get_nc_config, set_nc_config
from ansible.module_utils.ce import ce_argument_spec, get_nc_config, set_nc_config, check_ip_addr
CE_NC_GET_CENTER_GLOBAL_INFO_HEADER = """
@ -571,25 +570,6 @@ CE_NC_DELETE_SERVER_DNS_INFO_TAIL = """
"""
def is_valid_address(address):
""" check ip address, Supports IPv4 and IPv6"""
if not address or '\x00' in address:
return False
try:
res = socket.getaddrinfo(address, 0, socket.AF_UNSPEC,
socket.SOCK_STREAM,
0, socket.AI_NUMERICHOST)
return bool(res)
except socket.gaierror:
err = sys.exc_info()[1]
if err.args[0] == socket.EAI_NONAME:
return False
raise
return True
def get_out_direct_default(out_direct):
"""get default out direct"""
@ -1433,12 +1413,12 @@ class InfoCenterGlobal(object):
# server_ip check
if self.server_ip:
if not is_valid_address(self.server_ip):
if not check_ip_addr(self.server_ip):
self.module.fail_json(
msg='Error: The %s is not a valid ip address' % self.server_ip)
# source_ip check
if self.source_ip:
if not is_valid_address(self.source_ip):
if not check_ip_addr(self.source_ip):
self.module.fail_json(
msg='Error: The %s is not a valid ip address' % self.source_ip)

View file

@ -243,12 +243,10 @@ changed:
sample: true
'''
import sys
import re
import socket
from xml.etree import ElementTree
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec
from ansible.module_utils.ce import get_nc_config, set_nc_config, ce_argument_spec, check_ip_addr
from ansible.module_utils.ce import get_config, load_config
CE_NC_GET_SFLOW = """
@ -331,26 +329,6 @@ def is_valid_ip_vpn(vpname):
return True
def check_ip_addr(ipaddr):
"""check ip address, Supports IPv4 and IPv6"""
if not ipaddr or '\x00' in ipaddr:
return False
try:
res = socket.getaddrinfo(ipaddr, 0, socket.AF_UNSPEC,
socket.SOCK_STREAM,
0, socket.AI_NUMERICHOST)
return bool(res)
except socket.gaierror:
err = sys.exc_info()[1]
if err.args[0] == socket.EAI_NONAME:
return False
raise
return True
def get_ip_version(address):
"""get ip version fast"""

View file

@ -170,13 +170,10 @@ updates:
sample: ["snmp-agent target-host host-name test2 trap address udp-domain 10.135.182.158 vpn-instance js params securityname wdz v3 authentication"]
'''
import sys
import socket
from xml.etree import ElementTree
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ce import get_nc_config, set_nc_config, \
ce_argument_spec, get_config, load_config
ce_argument_spec, get_config, load_config, check_ip_addr
# get snmp version
CE_GET_SNMP_VERSION = """
@ -263,25 +260,6 @@ INTERFACE_TYPE = ['ethernet', 'eth-trunk', 'tunnel', 'null', 'loopback',
'vlanif', '100ge', '40ge', 'mtunnel', '10ge', 'ge', 'meth', 'vbdif', 'nve']
def check_ip_addr(ipaddr):
""" check_ip_addr, Supports IPv4 and IPv6 """
if not ipaddr or '\x00' in ipaddr:
return False
try:
res = socket.getaddrinfo(ipaddr, 0, socket.AF_UNSPEC,
socket.SOCK_STREAM,
0, socket.AI_NUMERICHOST)
return bool(res)
except socket.gaierror:
err = sys.exc_info()[1]
if err.args[0] == socket.EAI_NONAME:
return False
raise
return True
class SnmpTargetHost(object):
""" Manages SNMP target host configuration """