Example #1
0
def map_obj_to_commands(updates, module):
    commands = list()
    want, have = updates
    for w in want:
        name = w["name"]
        ipv4 = w["ipv4"]
        ipv6 = w["ipv6"]
        state = w["state"]
        interface = "interface " + name
        commands.append(interface)
        obj_in_have = search_obj_in_list(name, have)
        if state == "absent" and obj_in_have:
            if obj_in_have["ipv4"]:
                if ipv4:
                    address = ipv4.split("/")
                    if len(address) == 2:
                        ipv4 = "{0} {1}".format(address[0],
                                                to_netmask(address[1]))
                    commands.append("no ip address {0}".format(ipv4))
                else:
                    commands.append("no ip address")
            if obj_in_have["ipv6"]:
                if ipv6:
                    commands.append("no ipv6 address {0}".format(ipv6))
                else:
                    commands.append("no ipv6 address")
                    if "dhcp" in obj_in_have["ipv6"]:
                        commands.append("no ipv6 address dhcp")
        elif state == "present":
            if ipv4:
                if (obj_in_have is None or obj_in_have.get("ipv4") is None
                        or ipv4 != obj_in_have["ipv4"]):
                    address = ipv4.split("/")
                    if len(address) == 2:
                        ipv4 = "{0} {1}".format(address[0],
                                                to_netmask(address[1]))
                    commands.append("ip address {0}".format(ipv4))
            if ipv6:
                if (obj_in_have is None or obj_in_have.get("ipv6") is None
                        or ipv6.lower()
                        not in [addr.lower() for addr in obj_in_have["ipv6"]]):
                    commands.append("ipv6 address {0}".format(ipv6))
        if commands[-1] == interface:
            commands.pop(-1)
    return commands
Example #2
0
def validate_n_expand_ipv4(module, want):
    # Check if input IPV4 is valid IP and expand IPV4 with its subnet mask
    ip_addr_want = want.get("address")
    if len(ip_addr_want.split(" ")) > 1:
        return ip_addr_want
    validate_ipv4(ip_addr_want, module)
    ip = ip_addr_want.split("/")
    if len(ip) == 2:
        ip_addr_want = "{0} {1}".format(ip[0], to_netmask(ip[1]))

    return ip_addr_want
Example #3
0
def map_obj_to_commands(updates, module):
    commands = list()
    want, have = updates
    for w in want:
        name = w['name']
        ipv4 = w['ipv4']
        ipv6 = w['ipv6']
        state = w['state']
        if 'replace' in w:
            replace = w['replace'] == 'yes'
        else:
            replace = False
        if w['mode'] is not None:
            mode = ' ' + w['mode']
        else:
            mode = ''
        if w['secondary'] is not None:
            secondary = w['secondary'] == 'yes'
        else:
            secondary = False

        interface = 'interface ' + name
        commands.append(interface)

        obj_in_have = search_obj_in_list(name, have)
        if state == 'absent' and have == []:
            if ipv4:
                address = ipv4.split('/')
                if len(address) == 2:
                    ipv4 = '{addr} {mask}'.format(addr=address[0], mask=to_netmask(address[1]))
                commands.append('no ip address {ip}'.format(ip=ipv4))
            if ipv6:
                commands.append('no ipv6 address {ip}'.format(ip=ipv6))

        elif state == 'absent' and obj_in_have:
            if obj_in_have['ipv4']:
                if ipv4:
                    address = ipv4.split('/')
                    if len(address) == 2:
                        ipv4 = '{addr} {mask}'.format(addr=address[0], mask=to_netmask(address[1]))
                    commands.append('no ip address {ip}'.format(ip=ipv4))
            if obj_in_have['ipv6']:
                if ipv6:
                    commands.append('no ipv6 address {ip}'.format(ip=ipv6))

        elif state == 'present':
            if ipv4:
                if obj_in_have is None or obj_in_have.get('ipv4') is None or ipv4 != obj_in_have['ipv4']:
                    address = ipv4.split('/')
                    if len(address) == 2:
                        ipv4 = '{0} {1}'.format(address[0], to_netmask(address[1]))
                    commands.append('ip address %s%s%s%s' % (format(ipv4), mode, ' replace' if (replace) else '', ' secondary' if (secondary) else ''))

            if ipv6:
                if obj_in_have is None or obj_in_have.get('ipv6') is None or ipv6.lower() not in [addr.lower() for addr in obj_in_have['ipv6']]:
                    commands.append('ipv6 address {ip}'.format(ip=ipv6))

        if commands[-1] == interface:
            commands.pop(-1)
        else:
            commands.append("exit")

    return commands