def _render_interface_del_commands(self, want, have):
        """ The command generator for delete commands
            w.r.t member interfaces
        :rtype: A list
        :returns: the commands necessary to update member
                  interfaces
        """
        commands = []
        if not want:
            want = {}
        have_members = have.get("members")

        if have_members:
            have_members = param_list_to_dict(deepcopy(have_members),
                                              unique_key="member")
            want_members = param_list_to_dict(deepcopy(want).get(
                "members", []),
                                              unique_key="member")

            for key in have_members:
                if key not in want_members:
                    member_cmd = ["no bundle id"]
                    pad_commands(member_cmd, key)
                    commands.extend(member_cmd)

        return commands
    def set_state(self, want, have):
        """Select the appropriate function based on the state provided

        :param want: the desired configuration as a dictionary
        :param have: the current configuration as a dictionary
        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        state = self._module.params["state"]
        if (
            state in ("merged", "replaced", "overridden", "rendered")
            and not want
        ):
            self._module.fail_json(
                msg="value of config parameter must not be empty for state {0}".format(
                    state
                )
            )
        want = param_list_to_dict(want, remove_key=False)
        have = param_list_to_dict(have, remove_key=False)
        if state == "overridden":
            commands = self._state_overridden(want, have)
        elif state == "deleted":
            commands = self._state_deleted(want, have)
        elif state == "merged" or state == "rendered":
            commands = self._state_merged(want, have)
        elif state == "replaced":
            commands = self._state_replaced(want, have)
        return commands
    def set_state(self, want, have):
        """ Select the appropriate function based on the state provided

        :param want: the desired configuration as a dictionary
        :param have: the current configuration as a dictionary
        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        state = self._module.params['state']

        want = param_list_to_dict(want) if want else dict()
        have = param_list_to_dict(have) if have else dict()

        if state == 'overridden':
            kwargs = {'self': self, 'want': want, 'have': have}
            commands = self._state_overridden(**kwargs)
        elif state == 'deleted':
            kwargs = {'self': self, 'want': want, 'have': have}
            commands = self._state_deleted(**kwargs)
        elif state == 'merged':
            kwargs = {'self': self, 'want': want, 'have': have}
            commands = self._state_merged(**kwargs)
        elif state == 'replaced':
            kwargs = {'self': self, 'want': want, 'have': have}
            commands = self._state_replaced(**kwargs)
        return commands
Exemple #4
0
def generate_addrfam_commands(want, have):
    commands = []
    want = param_list_to_dict(want, unique_key='vrf')
    if have:
        have = param_list_to_dict(have, unique_key='vrf')
    else:
        have = dict()

    for vrf, stats in iteritems(want):
        vrf_commands = []

        if stats.get('redistribute'):
            vrf_commands.extend(
                generate_redistribute_commands(
                    stats['redistribute'],
                    have.get(vrf, {}).get('redistribute', [])))

        if stats.get('networks'):
            network_commands = generate_network_commands(
                stats['networks'],
                have.get(vrf, {}).get('networks', []))
            vrf_commands.extend(network_commands)

        if stats.get('neighbors'):
            neighbor_commands = generate_af_neighbor_commands(
                stats['neighbors'],
                have.get(vrf, {}).get('neighbors'))
            vrf_commands.extend(neighbor_commands)

        if vrf_commands or vrf not in have:
            vrf_commands.insert(0, 'address-family ipv4 vrf {}'.format(vrf))
            vrf_commands.append('exit-address-family')
        commands.extend(vrf_commands)

    return commands
    def _purge_attribs(self, intf):
        """ The command generator for purging attributes
        :rtype: A list
        :returns: the commands necessary to purge attributes
        """
        commands = []
        have_copy = deepcopy(intf)
        members = have_copy.pop("members", [])

        to_delete = dict_delete(have_copy,
                                remove_empties({"name": have_copy["name"]}))
        if to_delete:
            for key, value in iteritems(flatten_dict(
                    remove_empties(to_delete))):
                commands.append(
                    self._compute_commands(key=key, value=value, remove=True))

        if commands:
            pad_commands(commands, intf["name"])

        if members:
            members = param_list_to_dict(deepcopy(members),
                                         unique_key="member")
            for key in members:
                member_cmd = ["no bundle id"]
                pad_commands(member_cmd, key)
                commands.extend(member_cmd)

        return commands
Exemple #6
0
def generate_neighbor_commands(want, have):
    commands = []

    w_neighbors = param_list_to_dict(want, unique_key='neighbor')
    if have:
        h_neighbors = param_list_to_dict(have, unique_key='neighbor')
    else:
        h_neighbors = dict()

    diff = dict_diff(h_neighbors, w_neighbors)

    for w_neighbor, stats in iteritems(diff):
        if w_neighbor not in h_neighbors:
            commands.append('neighbor {} remote-as {}'.format(
                w_neighbor, stats['remote_as']))
        for key, value in iteritems(stats):
            if key == 'remote_as' or value is None:
                continue

            if key == 'enabled':
                if value is False and value != h_neighbors.get(w_neighbor).get(
                        'enabled', True):
                    commands.append('neighbor {} shutdown'.format(w_neighbor))
                if value and value != h_neighbors.get(w_neighbor).get(
                        'enabled', True):
                    commands.append(
                        'no neighbor {} shutdown'.format(w_neighbor))
            elif key == 'timers':
                if value.get('connect'):
                    commands.append('neighbor {} timers connect {}'.format(
                        w_neighbor, value['connect']))
                else:
                    if value.get('keepalive') is None or value.get(
                            'holdtime') is None:
                        raise ValueError(
                            'keepalive and holdtime required together')
                    commands.append('neighbor {} timers {} {}'.format(
                        w_neighbor, value['keepalive'], value['holdtime']))

            else:
                commands.append('neighbor {} {} {}'.format(
                    w_neighbor, key.replace('_', '-'), value))

    return commands
Exemple #7
0
    def set_state(self, want, have):
        """ Select the appropriate function based on the state provided

        :param want: the desired configuration as a dictionary
        :param have: the current configuration as a dictionary
        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        state = self._module.params['state']
        want = param_list_to_dict(want, "vlan_id", remove_key=False)
        have = param_list_to_dict(have, "vlan_id", remove_key=False)
        if state == 'overridden':
            commands = self._state_overridden(want, have)
        elif state == 'deleted':
            commands = self._state_deleted(want, have)
        elif state == 'merged':
            commands = self._state_merged(want, have)
        elif state == 'replaced':
            commands = self._state_replaced(want, have)
        return commands
    def set_state(self, want, have):
        """ Select the appropriate function based on the state provided

        :param want: the desired configuration as a dictionary
        :param have: the current configuration as a dictionary
        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        state = self._module.params["state"]
        want = param_list_to_dict(want)
        have = param_list_to_dict(have)
        if state == "overridden":
            commands = self._state_overridden(want, have)
        elif state == "deleted":
            commands = self._state_deleted(want, have)
        elif state == "merged":
            commands = self._state_merged(want, have)
        elif state == "replaced":
            commands = self._state_replaced(want, have)
        return commands
Exemple #9
0
def generate_af_neighbor_commands(want, have):
    commands = []

    w_neighbors = param_list_to_dict(want, unique_key='neighbor')
    if have:
        h_neighbors = param_list_to_dict(have, unique_key='neighbor')
    else:
        h_neighbors = dict()

    diff = dict_diff(h_neighbors, w_neighbors)

    for w_neighbor, stats in iteritems(diff):
        if w_neighbor not in h_neighbors:
            commands.append('neighbor {} remote-as {}'.format(
                w_neighbor, stats['remote_as']))
        for key, value in iteritems(stats):
            if key == 'remote_as' or value is None:
                continue

            if key == 'maximum_prefix':
                commands.append('neighbor {} maximum-prefix {}'.format(
                    w_neighbor, value))
            elif key == 'prefix_list_in':
                commands.append('neighbor {} prefix-list {} in'.format(
                    w_neighbor, value))
            elif key == 'prefix_list_out':
                commands.append('neighbor {} prefix-list {} out'.format(
                    w_neighbor, value))
            else:
                if value and value != h_neighbors.get(w_neighbor, {}).get(
                        key, False):
                    commands.append('neighbor {} {}'.format(
                        w_neighbor, key.replace('_', '-'), value))
                elif not value and value != h_neighbors.get(
                        w_neighbor, {}).get(key, False):
                    commands.append('no neighbor {} {}'.format(
                        w_neighbor, key.replace('_', '-'), value))

    return commands
Exemple #10
0
def test_param_list_to_dict():
    params = [
        dict(name="interface1", mtu=1400),
        dict(name="interface2", speed="10G"),
        dict(name="interface3"),
    ]
    assert utils.param_list_to_dict(params) == {
        "interface1": dict(mtu=1400),
        "interface2": dict(speed="10G"),
        "interface3": dict(),
    }

    params = [
        dict(vlan_id=1, name="management"),
        dict(vlan_id=10, name="voice"),
        dict(vlan_id=99, name="guest"),
    ]
    assert utils.param_list_to_dict(
        params, unique_key="vlan_id", remove_key=False
    ) == {
        1: dict(vlan_id=1, name="management"),
        10: dict(vlan_id=10, name="voice"),
        99: dict(vlan_id=99, name="guest"),
    }
Exemple #11
0
def generate_redistribute_commands(want, have):
    commands = []

    h_protocols = param_list_to_dict(have, unique_key='protocol')

    for redistribute in want:

        w_protocol = redistribute['protocol']
        if w_protocol in h_protocols and h_protocols[w_protocol].get(
                'route_map') and not redistribute.get('route_map'):
            commands.append('no redistribute {}'.format(w_protocol))

        if remove_empties(redistribute) not in have:
            if redistribute.get('route_map'):
                commands.append('redistribute {} route-map {}'.format(
                    redistribute['protocol'], redistribute['route_map']))
            else:
                commands.append('redistribute {}'.format(
                    redistribute['protocol']))

    return commands