Beispiel #1
0
def validate_ipv4(value, module):
    if value:
        address = value.split('/')
        if len(address) != 2:
            module.fail_json(msg='address format is <ipv4 address>/<mask>, got invalid format %s' % value)
        else:
            if not is_masklen(address[1]):
                module.fail_json(msg='invalid value for mask: %s, mask should be in range 0-32' % address[1])
Beispiel #2
0
def validate_ipv4(value, module):
    if value:
        address = value.split("/")
        if len(address) != 2:
            module.fail_json(
                msg="address format is <ipv4 address>/<mask>, got invalid format %s" % value,
            )
        if not is_masklen(address[1]):
            module.fail_json(
                msg="invalid value for mask: %s, mask should be in range 0-32" % address[1],
            )
Beispiel #3
0
    def _set_config(self, want, have):
        # Set the interface config based on the want and have config
        commands = []
        interface = 'interface ' + want['name']

        if want == have:
            return

        if want.get('ipv4'):
            haddr = have.get('ipv4')
            if not haddr:  # if interface has no address, have empty list
                haddr = []
            for waddr in want.get('ipv4'):
                waddr = remove_empties(waddr)
                if waddr not in haddr:  # if wanted address not yet configured, add
                    if waddr.get('address') != 'dhcp':
                        ipv4_addr = waddr['address'].split('/')
                        if len(ipv4_addr) != 2:  # validate ip address
                            self._module.fail_json(msg='Invalid format')
                        if not validate_ip_address(
                                ipv4_addr[0]) or not is_masklen(ipv4_addr[1]):
                            self._module.fail_json(msg='Invalid IP address')
                        cmd = 'ip address {0}'.format(waddr['address'])
                        if waddr.get('secondary'):
                            cmd += ' secondary'
                    elif waddr.get('address') == 'dhcp':
                        cmd = 'ip address dhcp'
                        if waddr.get('dhcp_client'):
                            cmd += ' client-id vlan{0}'.format(
                                waddr.get('dhcp_client'))
                        if waddr.get('dhcp_hostname'):
                            cmd += ' hostname {0}'.format(
                                waddr.get('dhcp_hostname'))
                    add_command_to_config_list(interface, cmd, commands)

        if want.get('ipv6'):
            haddr = have.get('ipv6')
            if not haddr:
                haddr = []
            for waddr in want.get('ipv6'):
                waddr = remove_empties(waddr)
                if waddr not in haddr:
                    ipv6_addr = waddr['address'].split('/')
                    if len(ipv6_addr) != 2:  # validate ipv6 address
                        self._module.fail_json(msg='Invalid format')
                    if not validate_ip_v6_address(
                            ipv6_addr[0]) or int(ipv6_addr[1]) > 64:
                        self._module.fail_json(msg='Invalid IP address')
                    cmd = 'ipv6 address {0}'.format(waddr['address'])
                    add_command_to_config_list(interface, cmd, commands)

        return commands
Beispiel #4
0
def validate_ipv4(value, module):
    if value:
        address = value.split("/")
        if len(address) != 2:
            module.fail_json(
                msg=
                "address format is <ipv4 address>/<mask>, got invalid format {0}"
                .format(value))

        if not is_masklen(address[1]):
            module.fail_json(
                msg="invalid value for mask: {0}, mask should be in range 0-128"
                .format(address[1]))
Beispiel #5
0
def is_address(value):
    if value:
        address = value.split("/")
        if is_masklen(address[1]) and validate_ip_address(address[0]):
            return True
    return False