Ejemplo n.º 1
0
    def _set_config(self, want, have):
        # Set the interface config based on the want and have config
        commands = []
        vlan = 'vlan {0}'.format(want.get('vlan_id'))

        # Get the diff b/w want n have
        want_dict = dict_to_set(want)
        have_dict = dict_to_set(have)
        diff = want_dict - have_dict

        if diff:
            name = dict(diff).get('name')
            state = dict(diff).get('state')
            shutdown = dict(diff).get('shutdown')
            mtu = dict(diff).get('mtu')
            remote_span = dict(diff).get('remote_span')
            if name:
                cmd = 'name {0}'.format(name)
                self.add_command_to_config_list(vlan, cmd, commands)
            if state:
                cmd = 'state {0}'.format(state)
                self.add_command_to_config_list(vlan, cmd, commands)
            if mtu:
                cmd = 'mtu {0}'.format(mtu)
                self.add_command_to_config_list(vlan, cmd, commands)
            if remote_span:
                self.add_command_to_config_list(vlan, 'remote-span', commands)
            if shutdown == 'enabled':
                self.add_command_to_config_list(vlan, 'shutdown', commands)
            elif shutdown == 'disabled':
                self.add_command_to_config_list(vlan, 'no shutdown', commands)

        return commands
    def _set_config(self, want, have):
        # Set the interface config based on the want and have config
        commands = []
        vlan = "vlan {0}".format(want.get("vlan_id"))

        # Get the diff b/w want n have
        want_dict = dict_to_set(want)
        have_dict = dict_to_set(have)
        diff = want_dict - have_dict

        if diff:
            name = dict(diff).get("name")
            state = dict(diff).get("state")
            shutdown = dict(diff).get("shutdown")
            mtu = dict(diff).get("mtu")
            remote_span = dict(diff).get("remote_span")
            if name:
                cmd = "name {0}".format(name)
                self.add_command_to_config_list(vlan, cmd, commands)
            if state:
                cmd = "state {0}".format(state)
                self.add_command_to_config_list(vlan, cmd, commands)
            if mtu:
                cmd = "mtu {0}".format(mtu)
                self.add_command_to_config_list(vlan, cmd, commands)
            if remote_span:
                self.add_command_to_config_list(vlan, "remote-span", commands)
            if shutdown == "enabled":
                self.add_command_to_config_list(vlan, "shutdown", commands)
            elif shutdown == "disabled":
                self.add_command_to_config_list(vlan, "no shutdown", commands)

        return commands
Ejemplo n.º 3
0
    def _set_config(self, want, have, module):
        # Set the interface config based on the want and have config
        commands = []
        interface = "interface " + want["name"]

        # Get the diff b/w want and have
        want_dict = dict_to_set(want)
        have_dict = dict_to_set(have)
        want_trunk = dict(want_dict).get("trunk")
        have_trunk = dict(have_dict).get("trunk")
        if want_trunk and have_trunk:
            diff = set(tuple(dict(want_dict).get("trunk"))) - set(
                tuple(dict(have_dict).get("trunk"))
            )
        else:
            diff = want_dict - have_dict

        if diff:
            diff = dict(diff)

            if diff.get("access"):
                cmd = "switchport access vlan {0}".format(
                    diff.get("access")[0][1]
                )
                add_command_to_config_list(interface, cmd, commands)

            if want_trunk:
                if diff.get("trunk"):
                    diff = dict(diff.get("trunk"))
                if diff.get("encapsulation"):
                    cmd = self.trunk_cmds["encapsulation"] + " {0}".format(
                        diff.get("encapsulation")
                    )
                    add_command_to_config_list(interface, cmd, commands)
                if diff.get("native_vlan"):
                    cmd = self.trunk_cmds["native_vlan"] + " {0}".format(
                        diff.get("native_vlan")
                    )
                    add_command_to_config_list(interface, cmd, commands)
                allowed_vlans = diff.get("allowed_vlans")
                pruning_vlans = diff.get("pruning_vlans")

                if allowed_vlans and self._check_for_correct_vlan_range(
                    allowed_vlans, module
                ):
                    allowed_vlans = ",".join(allowed_vlans)
                    cmd = self.trunk_cmds["allowed_vlans"] + " {0}".format(
                        allowed_vlans
                    )
                    add_command_to_config_list(interface, cmd, commands)
                if pruning_vlans and self._check_for_correct_vlan_range(
                    pruning_vlans, module
                ):
                    pruning_vlans = ",".join(pruning_vlans)
                    cmd = self.trunk_cmds["pruning_vlans"] + " {0}".format(
                        pruning_vlans
                    )
                    add_command_to_config_list(interface, cmd, commands)

        return commands
    def _set_config(self, want, have):
        # Set the interface config based on the want and have config
        commands = []

        want_dict = dict_to_set(want)
        have_dict = dict_to_set(have)
        diff = want_dict - have_dict

        if diff:
            cmd = "lacp system-priority {0}".format(
                want.get("system").get("priority"))
            self._add_command_to_config_list(cmd, commands)

        return commands
Ejemplo n.º 5
0
    def _set_config(self, want, have):
        # Set the interface config based on the want and have config
        commands = []

        interface = "interface " + want["name"]
        # Get the diff b/w want and have
        want_dict = dict_to_set(want)
        have_dict = dict_to_set(have)
        diff = want_dict - have_dict

        if diff:
            diff = dict(diff)
            receive = diff.get("receive")
            transmit = diff.get("transmit")
            med_tlv_select = diff.get("med_tlv_select")
            tlv_select = diff.get("tlv_select")
            if receive:
                cmd = "lldp receive"
                add_command_to_config_list(interface, cmd, commands)
            elif receive is False:
                cmd = "no lldp receive"
                add_command_to_config_list(interface, cmd, commands)
            if transmit:
                cmd = "lldp transmit"
                add_command_to_config_list(interface, cmd, commands)
            elif transmit is False:
                cmd = "no lldp transmit"
                add_command_to_config_list(interface, cmd, commands)

            if med_tlv_select:
                med_tlv_select = dict(med_tlv_select)
                if med_tlv_select.get("inventory_management"):
                    add_command_to_config_list(
                        interface,
                        "lldp med-tlv-select inventory-management",
                        commands,
                    )
            if tlv_select:
                tlv_select = dict(tlv_select)
                if tlv_select.get("power_management"):
                    add_command_to_config_list(
                        interface,
                        "lldp tlv-select power-management",
                        commands,
                    )

        return commands
    def _set_config(self, want, have, module):
        # Set the interface config based on the want and have config
        commands = []
        interface = 'interface ' + want['name']

        # Get the diff b/w want and have
        want_dict = dict_to_set(want)
        have_dict = dict_to_set(have)
        want_trunk = dict(want_dict).get('trunk')
        have_trunk = dict(have_dict).get('trunk')
        if want_trunk and have_trunk:
            diff = set(tuple(dict(want_dict).get('trunk'))) - set(tuple(dict(have_dict).get('trunk')))
        else:
            diff = want_dict - have_dict

        if diff:
            diff = dict(diff)

            if diff.get('access'):
                cmd = 'switchport access vlan {0}'.format(diff.get('access')[0][1])
                add_command_to_config_list(interface, cmd, commands)

            if want_trunk:
                if diff.get('trunk'):
                    diff = dict(diff.get('trunk'))
                if diff.get('encapsulation'):
                    cmd = self.trunk_cmds['encapsulation'] + ' {0}'.format(diff.get('encapsulation'))
                    add_command_to_config_list(interface, cmd, commands)
                if diff.get('native_vlan'):
                    cmd = self.trunk_cmds['native_vlan'] + ' {0}'.format(diff.get('native_vlan'))
                    add_command_to_config_list(interface, cmd, commands)
                allowed_vlans = diff.get('allowed_vlans')
                pruning_vlans = diff.get('pruning_vlans')

                if allowed_vlans and self._check_for_correct_vlan_range(allowed_vlans, module):
                    allowed_vlans = ','.join(allowed_vlans)
                    cmd = self.trunk_cmds['allowed_vlans'] + ' {0}'.format(allowed_vlans)
                    add_command_to_config_list(interface, cmd, commands)
                if pruning_vlans and self._check_for_correct_vlan_range(pruning_vlans, module):
                    pruning_vlans = ','.join(pruning_vlans)
                    cmd = self.trunk_cmds['pruning_vlans'] + ' {0}'.format(pruning_vlans)
                    add_command_to_config_list(interface, cmd, commands)

        return commands
    def _set_config(self, want, have):
        # Set the interface config based on the want and have config
        commands = []

        interface = 'interface ' + have['name']
        # Get the diff b/w want and have
        want_dict = dict_to_set(want)
        have_dict = dict_to_set(have)
        diff = want_dict - have_dict

        if diff:
            diff = dict(diff)
            receive = diff.get('receive')
            transmit = diff.get('transmit')
            med_tlv_select = diff.get('med_tlv_select')
            tlv_select = diff.get('tlv_select')
            if receive:
                cmd = 'lldp receive'
                add_command_to_config_list(interface, cmd, commands)
            elif receive is False:
                cmd = 'no lldp receive'
                add_command_to_config_list(interface, cmd, commands)
            if transmit:
                cmd = 'lldp transmit'
                add_command_to_config_list(interface, cmd, commands)
            elif transmit is False:
                cmd = 'no lldp transmit'
                add_command_to_config_list(interface, cmd, commands)

            if med_tlv_select:
                med_tlv_select = dict(med_tlv_select)
                if med_tlv_select.get('inventory_management'):
                    add_command_to_config_list(
                        interface, 'lldp med-tlv-select inventory-management',
                        commands)
            if tlv_select:
                tlv_select = dict(tlv_select)
                if tlv_select.get('power_management'):
                    add_command_to_config_list(
                        interface, 'lldp tlv-select power-management',
                        commands)

        return commands
    def _set_config(self, want, have, module):
        # Set the interface config based on the want and have config
        commands = []

        # To remove keys with None values from want dict
        want = utils.remove_empties(want)
        # Get the diff b/w want and have
        want_dict = dict_to_set(want)
        have_dict = dict_to_set(have)
        diff = want_dict - have_dict

        # To get the channel-id from lag port-channel name
        lag_config = dict(diff).get("members")
        channel_name = re.search(r"(\d+)", want.get("name"))
        if channel_name:
            channel_id = channel_name.group()
        else:
            module.fail_json(msg="Lag Interface Name is not correct!")
        if lag_config:
            for each in lag_config:
                each = dict(each)
                each_interface = "interface {0}".format(each.get("member"))
                if have.get("name") == want["members"][0][
                    "member"
                ] or have.get("name").lower().startswith("po"):
                    if each.get("mode"):
                        cmd = "channel-group {0} mode {1}".format(
                            channel_id, each.get("mode")
                        )
                        self.add_command_to_config_list(
                            each_interface, cmd, commands
                        )
                    elif each.get("link"):
                        cmd = "channel-group {0} link {1}".format(
                            channel_id, each.get("link")
                        )
                        self.add_command_to_config_list(
                            each_interface, cmd, commands
                        )

        return commands
Ejemplo n.º 9
0
    def _set_config(self, want, have):
        # Set the interface config based on the want and have config
        commands = []
        interface = "interface " + want["name"]

        # Get the diff b/w want and have
        want_dict = dict_to_set(want)
        have_dict = dict_to_set(have)
        diff = want_dict - have_dict

        if diff:
            diff = dict(diff)
            for item in self.params:
                if diff.get(item):
                    cmd = item + " " + str(want.get(item))
                    add_command_to_config_list(interface, cmd, commands)
            if diff.get("enabled"):
                add_command_to_config_list(interface, "no shutdown", commands)
            elif diff.get("enabled") is False:
                add_command_to_config_list(interface, "shutdown", commands)

        return commands
Ejemplo n.º 10
0
    def _set_config(self, want, have):
        # Set the interface config based on the want and have config
        commands = []
        interface = 'interface ' + want['name']

        # Get the diff b/w want and have
        want_dict = dict_to_set(want)
        have_dict = dict_to_set(have)
        diff = want_dict - have_dict

        if diff:
            diff = dict(diff)
            for item in self.params:
                if diff.get(item):
                    cmd = item + ' ' + str(want.get(item))
                    add_command_to_config_list(interface, cmd, commands)
            if diff.get('enabled'):
                add_command_to_config_list(interface, 'no shutdown', commands)
            elif diff.get('enabled') is False:
                add_command_to_config_list(interface, 'shutdown', commands)

        return commands
Ejemplo n.º 11
0
    def _set_config(self, want, have):
        # Set the lldp global config based on the want and have config
        commands = []

        # Get the diff b/w want and have
        want_dict = dict_to_set(want)
        have_dict = dict_to_set(have)
        diff = want_dict - have_dict

        if diff:
            diff = dict(diff)
            holdtime = diff.get("holdtime")
            enabled = diff.get("enabled")
            timer = diff.get("timer")
            reinit = diff.get("reinit")
            tlv_select = diff.get("tlv_select")

            if holdtime:
                cmd = "lldp holdtime {0}".format(holdtime)
                self.add_command_to_config_list(cmd, commands)
            if enabled:
                cmd = "lldp run"
                self.add_command_to_config_list(cmd, commands)
            if timer:
                cmd = "lldp timer {0}".format(timer)
                self.add_command_to_config_list(cmd, commands)
            if reinit:
                cmd = "lldp reinit {0}".format(reinit)
                self.add_command_to_config_list(cmd, commands)
            if tlv_select:
                tlv_selec_dict = dict(tlv_select)
                for k, v in iteritems(self.tlv_select_params):
                    if k in tlv_selec_dict and tlv_selec_dict[k]:
                        cmd = "lldp tlv-select {0}".format(v)
                        self.add_command_to_config_list(cmd, commands)

        return commands
    def _set_config(self, want, have):
        # Set the interface config based on the want and have config
        commands = []

        # Get the diff b/w want and have
        want_dict = dict_to_set(want)
        have_dict = dict_to_set(have)
        diff = want_dict - have_dict

        if diff:
            diff = dict(diff)
            holdtime = diff.get('holdtime')
            enabled = diff.get('enabled')
            timer = diff.get('timer')
            reinit = diff.get('reinit')
            tlv_select = diff.get('tlv_select')

            if holdtime:
                cmd = 'lldp holdtime {0}'.format(holdtime)
                self.add_command_to_config_list(cmd, commands)
            if enabled:
                cmd = 'lldp run'
                self.add_command_to_config_list(cmd, commands)
            if timer:
                cmd = 'lldp timer {0}'.format(timer)
                self.add_command_to_config_list(cmd, commands)
            if reinit:
                cmd = 'lldp reinit {0}'.format(reinit)
                self.add_command_to_config_list(cmd, commands)
            if tlv_select:
                tlv_selec_dict = dict(tlv_select)
                for k, v in iteritems(self.tlv_select_params):
                    if k in tlv_selec_dict and tlv_selec_dict[k]:
                        cmd = 'lldp tlv-select {0}'.format(v)
                        self.add_command_to_config_list(cmd, commands)

        return commands
Ejemplo n.º 13
0
    def _set_config(self, want, have):
        # Set the interface config based on the want and have config
        commands = []
        interface = "interface " + want["name"]

        want_dict = dict_to_set(want)
        have_dict = dict_to_set(have)
        diff = want_dict - have_dict

        if diff:
            port_priotity = dict(diff).get("port_priority")
            max_bundle = dict(diff).get("max_bundle")
            fast_switchover = dict(diff).get("fast_switchover")
            if port_priotity:
                cmd = "lacp port-priority {0}".format(port_priotity)
                add_command_to_config_list(interface, cmd, commands)
            if max_bundle:
                cmd = "lacp max-bundle {0}".format(max_bundle)
                add_command_to_config_list(interface, cmd, commands)
            if fast_switchover:
                cmd = "lacp fast-switchover"
                add_command_to_config_list(interface, cmd, commands)

        return commands
Ejemplo n.º 14
0
    def _set_config(self, want, have):
        # Set the interface config based on the want and have config
        commands = []
        interface = 'interface ' + have['name']

        want_dict = dict_to_set(want)
        have_dict = dict_to_set(have)
        diff = want_dict - have_dict

        if diff:
            port_priotity = dict(diff).get('port_priority')
            max_bundle = dict(diff).get('max_bundle')
            fast_switchover = dict(diff).get('fast_switchover')
            if port_priotity:
                cmd = 'lacp port-priority {0}'.format(port_priotity)
                add_command_to_config_list(interface, cmd, commands)
            if max_bundle:
                cmd = 'lacp max-bundle {0}'.format(max_bundle)
                add_command_to_config_list(interface, cmd, commands)
            if fast_switchover:
                cmd = 'lacp fast-switchover'
                add_command_to_config_list(interface, cmd, commands)

        return commands
Ejemplo n.º 15
0
    def _set_config(self, want, have, module):
        # Set the interface config based on the want and have config
        commands = []
        interface = "interface " + want["name"]

        # expand a have allowed and pruning vlan if any
        if have.get("trunk") and want.get("trunk"):
            for each in ["allowed_vlans", "pruning_vlans"]:
                if have["trunk"].get(each) and want["trunk"].get(each):
                    (check, want["trunk"][each]) = self._expand_vlan_range_if_any(
                        "want",
                        want["trunk"][each],
                    )
                    if not check:
                        (check, have["trunk"][each]) = self._expand_vlan_range_if_any(
                            "have",
                            have["trunk"][each],
                        )

        # Get the diff b/w want and have
        diff = set()
        want_dict = dict_to_set(want)
        have_dict = dict_to_set(have)
        want_trunk = dict(want_dict).get("trunk")
        have_trunk = dict(have_dict).get("trunk")
        if want_trunk and have_trunk:
            diff = set(tuple(dict(want_dict).get("trunk"))) - set(
                tuple(dict(have_dict).get("trunk")),
            )
            _want_dict = dict(want_dict)
            _have_dict = dict(have_dict)
            if _want_dict.get("trunk"):
                del _want_dict["trunk"]
            if _have_dict.get("trunk"):
                del _have_dict["trunk"]
            _want_dict = dict_to_set(_want_dict)
            _have_dict = dict_to_set(_have_dict)
            diff.update(_want_dict - _have_dict)
        else:
            diff = want_dict - have_dict

        if diff:
            diff = dict(diff)
            mode = diff.get("mode")
            access = diff.get("access")
            trunk = diff.get("trunk")

            if access:
                if access[0][0] == "vlan":
                    cmd = "switchport access vlan {0}".format(access[0][1])
                if access[0][0] == "vlan_name":
                    cmd = "switchport access vlan name {0}".format(access[0][1])
                add_command_to_config_list(interface, cmd, commands)

            if diff.get("voice"):
                if diff.get("voice")[0][0] == "vlan" or diff.get("voice")[0][0] == "vlan_tag":
                    cmd = "switchport voice vlan {0}".format(
                        diff.get("voice")[0][1],
                    )
                if diff.get("voice")[0][0] == "vlan_name":
                    cmd = "switchport voice vlan name {0}".format(
                        diff.get("voice")[0][1],
                    )
                add_command_to_config_list(interface, cmd, commands)

            if want_trunk:
                if trunk:
                    diff = dict(trunk)
                if diff.get("encapsulation"):
                    cmd = self.trunk_cmds["encapsulation"] + " {0}".format(
                        diff.get("encapsulation"),
                    )
                    add_command_to_config_list(interface, cmd, commands)
                if diff.get("native_vlan"):
                    cmd = self.trunk_cmds["native_vlan"] + " {0}".format(
                        diff.get("native_vlan"),
                    )
                    add_command_to_config_list(interface, cmd, commands)
                allowed_vlans = diff.get("allowed_vlans")
                pruning_vlans = diff.get("pruning_vlans")

                if allowed_vlans and self._check_for_correct_vlan_range(
                    allowed_vlans,
                    module,
                ):
                    diff = None
                    if self.state == "merged":
                        have_trunk = have.get("trunk")
                        if have_trunk and have_trunk.get("allowed_vlans"):
                            have_allowed_vlans = have_trunk.get(
                                "allowed_vlans",
                            )
                            allowed_vlans = list(allowed_vlans)
                            diff = set(allowed_vlans).difference(
                                set(have_allowed_vlans),
                            )
                            allowed_vlans = list(diff) if diff else tuple()
                    allowed_vlans = ",".join(allowed_vlans)
                    if allowed_vlans:
                        trunk_cmd = (
                            self.trunk_cmds["allowed_vlans_add"]
                            if self.state == "merged" and diff
                            else self.trunk_cmds["allowed_vlans"]
                        )
                        cmd = trunk_cmd + " {0}".format(allowed_vlans)
                        add_command_to_config_list(interface, cmd, commands)
                if pruning_vlans and self._check_for_correct_vlan_range(
                    pruning_vlans,
                    module,
                ):
                    diff = None
                    if self.state == "merged":
                        have_trunk = have.get("trunk")
                        if have_trunk and have_trunk.get("pruning_vlans"):
                            have_pruning_vlans = have_trunk.get(
                                "pruning_vlans",
                            )
                            pruning_vlans = list(pruning_vlans)
                            diff = set(pruning_vlans).difference(
                                set(have_pruning_vlans),
                            )
                            pruning_vlans = list(diff) if diff else tuple()
                    pruning_vlans = ",".join(pruning_vlans)
                    if pruning_vlans:
                        trunk_cmd = (
                            self.trunk_cmds["pruning_vlans_add"]
                            if self.state == "merged" and diff
                            else self.trunk_cmds["pruning_vlans"]
                        )
                        cmd = trunk_cmd + " {0}".format(pruning_vlans)
                        add_command_to_config_list(interface, cmd, commands)
            if mode:
                cmd = "switchport mode {0}".format(mode)
                add_command_to_config_list(interface, cmd, commands)

        return commands
Ejemplo n.º 16
0
    def _set_config(self, want, have, module):
        # Set the interface config based on the want and have config
        commands = []
        interface = "interface " + want["name"]

        # Get the diff b/w want and have
        want_dict = dict_to_set(want)
        have_dict = dict_to_set(have)
        want_trunk = dict(want_dict).get("trunk")
        have_trunk = dict(have_dict).get("trunk")
        if want_trunk and have_trunk:
            diff = set(tuple(dict(want_dict).get("trunk"))) - set(
                tuple(dict(have_dict).get("trunk"))
            )
        else:
            diff = want_dict - have_dict

        if diff:
            diff = dict(diff)
            mode = diff.get("mode")
            access = diff.get("access")
            trunk = diff.get("trunk")

            if access:
                cmd = "switchport access vlan {0}".format(access[0][1])
                add_command_to_config_list(interface, cmd, commands)

            if diff.get("voice"):
                cmd = "switchport voice vlan {0}".format(
                    diff.get("voice")[0][1]
                )
                add_command_to_config_list(interface, cmd, commands)

            if want_trunk:
                if trunk:
                    diff = dict(trunk)
                if diff.get("encapsulation"):
                    cmd = self.trunk_cmds["encapsulation"] + " {0}".format(
                        diff.get("encapsulation")
                    )
                    add_command_to_config_list(interface, cmd, commands)
                if diff.get("native_vlan"):
                    cmd = self.trunk_cmds["native_vlan"] + " {0}".format(
                        diff.get("native_vlan")
                    )
                    add_command_to_config_list(interface, cmd, commands)
                allowed_vlans = diff.get("allowed_vlans")
                pruning_vlans = diff.get("pruning_vlans")

                if allowed_vlans and self._check_for_correct_vlan_range(
                    allowed_vlans, module
                ):
                    if self.state == "merged":
                        have_trunk = have.get("trunk")
                        if have_trunk and have_trunk.get("allowed_vlans"):
                            have_allowed_vlans = have_trunk.get(
                                "allowed_vlans"
                            )
                            allowed_vlans = list(allowed_vlans)
                            if set(allowed_vlans).difference(
                                set(have_allowed_vlans)
                            ):
                                allowed_vlans.extend(list(have_allowed_vlans))
                            else:
                                allowed_vlans = tuple()
                    allowed_vlans = ",".join(allowed_vlans)
                    if allowed_vlans:
                        cmd = self.trunk_cmds["allowed_vlans"] + " {0}".format(
                            allowed_vlans
                        )
                        add_command_to_config_list(interface, cmd, commands)
                if pruning_vlans and self._check_for_correct_vlan_range(
                    pruning_vlans, module
                ):
                    if self.state == "merged":
                        have_trunk = have.get("trunk")
                        if have_trunk and have_trunk.get("pruning_vlans"):
                            have_pruning_vlans = have_trunk.get(
                                "pruning_vlans"
                            )
                            pruning_vlans = list(pruning_vlans)
                            if set(pruning_vlans).difference(
                                set(have_pruning_vlans)
                            ):
                                pruning_vlans.extend(list(have_pruning_vlans))
                            else:
                                pruning_vlans = tuple()
                    pruning_vlans = ",".join(pruning_vlans)
                    if pruning_vlans:
                        cmd = self.trunk_cmds["pruning_vlans"] + " {0}".format(
                            pruning_vlans
                        )
                        add_command_to_config_list(interface, cmd, commands)
            if mode:
                cmd = "switchport mode {0}".format(mode)
                add_command_to_config_list(interface, cmd, commands)

        return commands
    def _set_config(self, want, have, module):
        # Set the interface config based on the want and have config
        commands = []
        interface = "interface " + want["name"]

        # To handle L3 IPV4 configuration
        if want.get("ipv4"):
            for each in want.get("ipv4"):
                if each.get("address") != "dhcp":
                    ip_addr_want = validate_n_expand_ipv4(module, each)
                    each["address"] = ip_addr_want

        # Convert the want and have dict to set
        want_dict = dict_to_set(want)
        have_dict = dict_to_set(have)

        # To handle L3 IPV4 configuration
        if want.get("ipv4"):
            # Get the diff b/w want and have IPV4
            if have.get("ipv4"):
                ipv4 = tuple(
                    set(dict(want_dict).get("ipv4")) -
                    set(dict(have_dict).get("ipv4")))
                if ipv4:
                    ipv4 = (ipv4 if self.verify_diff_again(
                        dict(want_dict).get("ipv4"),
                        dict(have_dict).get("ipv4"),
                    ) else ())
            else:
                diff = want_dict - have_dict
                ipv4 = dict(diff).get("ipv4")
            if ipv4:
                for each in ipv4:
                    ipv4_dict = dict(each)
                    if ipv4_dict.get("address") != "dhcp":
                        cmd = "ip address {0}".format(ipv4_dict["address"])
                        if ipv4_dict.get("secondary"):
                            cmd += " secondary"
                    elif ipv4_dict.get("address") == "dhcp":
                        cmd = "ip address dhcp"
                        if ipv4_dict.get(
                                "dhcp_client") is not None and ipv4_dict.get(
                                    "dhcp_hostname"):
                            cmd = "ip address dhcp client-id GigabitEthernet 0/{0} hostname {1}".format(
                                ipv4_dict.get("dhcp_client"),
                                ipv4_dict.get("dhcp_hostname"),
                            )
                        elif ipv4_dict.get(
                                "dhcp_client"
                        ) and not ipv4_dict.get("dhcp_hostname"):
                            cmd = "ip address dhcp client-id GigabitEthernet 0/{0}".format(
                                ipv4_dict.get("dhcp_client"))
                        elif not ipv4_dict.get(
                                "dhcp_client") and ipv4_dict.get(
                                    "dhcp_hostname"):
                            cmd = "ip address dhcp hostname {0}".format(
                                ipv4_dict.get("dhcp_client"))

                    add_command_to_config_list(interface, cmd, commands)

        # To handle L3 IPV6 configuration
        if want.get("ipv6"):
            # Get the diff b/w want and have IPV6
            if have.get("ipv6"):
                ipv6 = tuple(
                    set(dict(want_dict).get("ipv6")) -
                    set(dict(have_dict).get("ipv6")))
            else:
                diff = want_dict - have_dict
                ipv6 = dict(diff).get("ipv6")
            if ipv6:
                for each in ipv6:
                    ipv6_dict = dict(each)
                    validate_ipv6(ipv6_dict.get("address"), module)
                    cmd = "ipv6 address {0}".format(ipv6_dict.get("address"))
                    add_command_to_config_list(interface, cmd, commands)

        return commands
Ejemplo n.º 18
0
    def _set_config(self, want, have):
        # Set the interface config based on the want and have config
        commands = []
        vlan = "vlan {0}".format(want.get("vlan_id"))

        def negate_have_config(want_diff, have_diff, vlan, commands):
            name = dict(have_diff).get("name")
            if name and not dict(want_diff).get("name"):
                self.remove_command_from_config_list(
                    vlan,
                    "name {0}".format(name),
                    commands,
                )
            state = dict(have_diff).get("state")
            if state and not dict(want_diff).get("state"):
                self.remove_command_from_config_list(
                    vlan,
                    "state {0}".format(state),
                    commands,
                )
            shutdown = dict(have_diff).get("shutdown")
            if shutdown and not dict(want_diff).get("shutdown"):
                self.remove_command_from_config_list(
                    vlan,
                    "shutdown",
                    commands,
                )
            mtu = dict(have_diff).get("mtu")
            if mtu and not dict(want_diff).get("mtu"):
                self.remove_command_from_config_list(
                    vlan,
                    "mtu {0}".format(mtu),
                    commands,
                )
            remote_span = dict(have_diff).get("remote_span")
            if remote_span and not dict(want_diff).get("remote_span"):
                self.remove_command_from_config_list(
                    vlan,
                    "remote-span",
                    commands,
                )

        # Get the diff b/w want n have
        want_dict = dict_to_set(want)
        have_dict = dict_to_set(have)
        diff = want_dict - have_dict
        have_diff = have_dict - want_dict

        if diff:
            if have_diff and (self.state == "replaced" or self.state == "overridden"):
                negate_have_config(diff, have_diff, vlan, commands)

            name = dict(diff).get("name")
            state = dict(diff).get("state")
            shutdown = dict(diff).get("shutdown")
            mtu = dict(diff).get("mtu")
            remote_span = dict(diff).get("remote_span")
            if name:
                self.add_command_to_config_list(
                    vlan,
                    "name {0}".format(name),
                    commands,
                )
            if state:
                self.add_command_to_config_list(
                    vlan,
                    "state {0}".format(state),
                    commands,
                )
            if mtu:
                self.add_command_to_config_list(
                    vlan,
                    "mtu {0}".format(mtu),
                    commands,
                )
            if remote_span:
                self.add_command_to_config_list(vlan, "remote-span", commands)
            if shutdown == "enabled":
                self.add_command_to_config_list(vlan, "shutdown", commands)
            elif shutdown == "disabled":
                self.add_command_to_config_list(vlan, "no shutdown", commands)
        elif have_diff and (self.state == "replaced" or self.state == "overridden"):
            negate_have_config(diff, have_diff, vlan, commands)

        return commands