예제 #1
0
    def _state_merged(self, want, have):
        """ The xml configuration generator when state is merged
         :rtype: A list
         :returns: the xml configuration necessary to merge the provided into
                   the current configuration
         """
        intf_xml = []

        for config in want:
            lacp_intf_name = config["name"]
            lacp_intf_root = build_root_xml_node("interface")

            build_child_xml_node(lacp_intf_root, "name", lacp_intf_name)
            if lacp_intf_name.startswith("ae"):
                element = build_subtree(
                    lacp_intf_root, "aggregated-ether-options/lacp"
                )
                if config["period"]:
                    build_child_xml_node(element, "periodic", config["period"])
                if config["sync_reset"]:
                    build_child_xml_node(
                        element, "sync-reset", config["sync_reset"]
                    )

                system = config["system"]
                if system:
                    mac = system.get("mac")
                    if mac:
                        if mac.get("address"):
                            build_child_xml_node(
                                element, "system-id", mac["address"]
                            )
                    if system.get("priority"):
                        build_child_xml_node(
                            element, "system-priority", system["priority"]
                        )
                intf_xml.append(lacp_intf_root)
            elif config["port_priority"] or config["force_up"] is not None:
                element = build_subtree(
                    lacp_intf_root, "ether-options/ieee-802.3ad/lacp"
                )
                if config["port_priority"] is not None:
                    build_child_xml_node(
                        element, "port-priority", config["port_priority"]
                    )
                else:
                    build_child_xml_node(
                        element, "port-priority", None, {"delete": "delete"}
                    )
                if config["force_up"]:
                    build_child_xml_node(element, "force-up")
                else:
                    build_child_xml_node(
                        element, "force-up", None, {"delete": "delete"}
                    )
                intf_xml.append(lacp_intf_root)

        return intf_xml
    def _state_deleted(self, want, have):
        """The xml configuration generator when state is deleted
        :rtype: A list
        :returns: the xml configuration necessary to remove the current configuration
                  of the provided objects
        """
        intf_xml = []
        intf_obj = want

        if not intf_obj:
            # delete lag interfaces attribute for all the interface
            intf_obj = have

        for config in intf_obj:
            lag_name = config["name"]
            lag_intf_root = build_root_xml_node("interface")
            build_child_xml_node(lag_intf_root, "name", lag_name)

            lag_node = build_subtree(lag_intf_root, "aggregated-ether-options")
            build_child_xml_node(
                lag_node, "link-protection", None, {"delete": "delete"}
            )

            lacp_node = build_child_xml_node(lag_node, "lacp")
            build_child_xml_node(
                lacp_node, "active", None, {"delete": "delete"}
            )
            build_child_xml_node(
                lacp_node, "passive", None, {"delete": "delete"}
            )

            intf_xml.append(lag_intf_root)

            # delete lag configuration from member interfaces
            for interface_obj in have:
                if lag_name == interface_obj["name"]:
                    for member in interface_obj.get("members", []):
                        lag_member_intf_root = build_root_xml_node("interface")
                        build_child_xml_node(
                            lag_member_intf_root, "name", member["member"]
                        )
                        lag_node = build_subtree(
                            lag_member_intf_root, "ether-options/ieee-802.3ad"
                        )
                        build_child_xml_node(
                            lag_node, "bundle", None, {"delete": "delete"}
                        )
                        build_child_xml_node(
                            lag_node, "primary", None, {"delete": "delete"}
                        )
                        build_child_xml_node(
                            lag_node, "backup", None, {"delete": "delete"}
                        )
                        intf_xml.append(lag_member_intf_root)

        return intf_xml
    def _state_deleted(self, want, have):
        """ The xml configuration generator when state is deleted
        :rtype: A list
        :returns: the xml configuration necessary to remove the current configuration
                  of the provided objects
        """
        intf_xml = []
        intf_obj = want

        if not intf_obj:
            # delete lag interfaces attribute for all the interface
            intf_obj = have

        for config in intf_obj:
            lag_name = config['name']
            lag_intf_root = build_root_xml_node('interface')
            build_child_xml_node(lag_intf_root, 'name', lag_name)

            lag_node = build_subtree(lag_intf_root, 'aggregated-ether-options')
            build_child_xml_node(lag_node, 'link-protection', None,
                                 {'delete': 'delete'})

            lacp_node = build_child_xml_node(lag_node, 'lacp')
            build_child_xml_node(lacp_node, 'active', None,
                                 {'delete': 'delete'})
            build_child_xml_node(lacp_node, 'passive', None,
                                 {'delete': 'delete'})

            intf_xml.append(lag_intf_root)

            # delete lag configuration from member interfaces
            for interface_obj in have:
                if lag_name == interface_obj['name']:
                    for member in interface_obj.get('members', []):
                        lag_member_intf_root = build_root_xml_node('interface')
                        build_child_xml_node(lag_member_intf_root, 'name',
                                             member['member'])
                        lag_node = build_subtree(lag_member_intf_root,
                                                 'ether-options/ieee-802.3ad')
                        build_child_xml_node(lag_node, 'bundle', None,
                                             {'delete': 'delete'})
                        build_child_xml_node(lag_node, 'primary', None,
                                             {'delete': 'delete'})
                        build_child_xml_node(lag_node, 'backup', None,
                                             {'delete': 'delete'})
                        intf_xml.append(lag_member_intf_root)

        return intf_xml
예제 #4
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
        """
        root = build_root_xml_node("protocols")
        lldp_intf_ele = build_subtree(root, "lldp")

        state = self._module.params["state"]
        if state == "overridden":
            config_xmls = self._state_overridden(want, have)
        elif state == "deleted":
            config_xmls = self._state_deleted(want, have)
        elif state == "merged":
            config_xmls = self._state_merged(want, have)
        elif state == "replaced":
            config_xmls = self._state_replaced(want, have)

        for xml in config_xmls:
            lldp_intf_ele.append(xml)

        return tostring(root)
예제 #5
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
        """
        root = build_root_xml_node("protocols")
        lldp_intf_ele = build_subtree(root, "lldp")

        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))
        if state == "overridden":
            config_xmls = self._state_overridden(want, have)
        elif state == "deleted":
            config_xmls = self._state_deleted(want, have)
        elif state in ("merged", "rendered"):
            config_xmls = self._state_merged(want, have)
        elif state == "replaced":
            config_xmls = self._state_replaced(want, have)

        for xml in config_xmls:
            lldp_intf_ele.append(xml)

        return tostring(root)
    def _state_deleted(self, want, have):
        """ The xml configuration generator when state is deleted
        :rtype: A list
        :returns: the xml configuration necessary to remove the current configuration
                  of the provided objects
        """
        intf_xml = []
        intf_obj = want

        if not intf_obj:
            # delete lag interfaces attribute for all the interface
            intf_obj = have

        for config in intf_obj:
            lacp_intf_name = config["name"]
            lacp_intf_root = build_root_xml_node("interface")
            build_child_xml_node(lacp_intf_root, "name", lacp_intf_name)
            if lacp_intf_name.startswith("ae"):
                element = build_subtree(
                    lacp_intf_root, "aggregated-ether-options/lacp"
                )
                build_child_xml_node(
                    element, "periodic", None, {"delete": "delete"}
                )
                build_child_xml_node(
                    element, "sync-reset", None, {"delete": "delete"}
                )
                build_child_xml_node(
                    element, "system-id", None, {"delete": "delete"}
                )
                build_child_xml_node(
                    element, "system-priority", None, {"delete": "delete"}
                )
            else:
                element = build_subtree(
                    lacp_intf_root, "ether-options/ieee-802.3ad/lacp"
                )
                build_child_xml_node(
                    element, "port-priority", None, {"delete": "delete"}
                )
                build_child_xml_node(
                    element, "force-up", None, {"delete": "delete"}
                )

            intf_xml.append(lacp_intf_root)

        return intf_xml
예제 #7
0
    def _state_deleted(self, want, have):
        """ The xml configuration generator when state is deleted

        :rtype: A list
        :returns: the xml configuration necessary to remove the current configuration
                  of the provided objects
        """
        l2_intf_xml = []
        l2_intf_obj = want

        config_filter = """
            <configuration>
                <interfaces/>
            </configuration>
            """
        data = get_resource_config(self._connection,
                                   config_filter=config_filter)

        if not l2_intf_obj:
            # delete l2 interfaces attribute from all the existing interface having l2 config
            l2_intf_obj = have

        for config in l2_intf_obj:
            name = config["name"]
            enhanced_layer = True
            l2_mode = data.xpath(
                "configuration/interfaces/interface[name='%s']/unit/family/ethernet-switching/interface-mode"
                % name)

            if not len(l2_mode):
                l2_mode = data.xpath(
                    "configuration/interfaces/interface[name='%s']/unit/family/ethernet-switching/port-mode"
                    % name)
                enhanced_layer = False

            if len(l2_mode):
                mode = "interface-mode" if enhanced_layer else "port-mode"

                intf = build_root_xml_node("interface")
                build_child_xml_node(intf, "name", name)

                unit_node = build_child_xml_node(intf, "unit")
                unit = config["unit"] if config["unit"] else "0"
                build_child_xml_node(unit_node, "name", unit)

                eth_node = build_subtree(unit_node,
                                         "family/ethernet-switching")
                build_child_xml_node(eth_node, mode, None,
                                     {"delete": "delete"})
                build_child_xml_node(eth_node, "vlan", None,
                                     {"delete": "delete"})
                build_child_xml_node(intf, "native-vlan-id", None,
                                     {"delete": "delete"})

                l2_intf_xml.append(intf)

        return l2_intf_xml
예제 #8
0
    def _state_merged(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 list xml configuration necessary to migrate the current configuration
                  to the desired configuration
        """
        lacp_xml = []

        lacp_root = build_root_xml_node('lacp')
        build_child_xml_node(lacp_root, 'system-priority', want.get('system_priority'))
        if want.get('link_protection') == 'non-revertive':
            build_subtree(lacp_root, 'link-protection/non-revertive')
        elif want.get('link_protection') == 'revertive':
            link_root = build_child_xml_node(lacp_root, 'link-protection')
            build_child_xml_node(link_root, 'non-revertive', None, {'delete': 'delete'})
        lacp_xml.append(lacp_root)
        return lacp_xml
예제 #9
0
    def _state_merged(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 list xml configuration necessary to migrate the current configuration
                  to the desired configuration
        """
        lacp_xml = []

        lacp_root = build_root_xml_node("lacp")
        build_child_xml_node(lacp_root, "system-priority",
                             want.get("system_priority"))
        if want.get("link_protection") == "non-revertive":
            build_subtree(lacp_root, "link-protection/non-revertive")
        elif want.get("link_protection") == "revertive":
            link_root = build_child_xml_node(lacp_root, "link-protection")
            build_child_xml_node(link_root, "non-revertive", None,
                                 {"delete": "delete"})
        lacp_xml.append(lacp_root)
        return lacp_xml
예제 #10
0
    def _state_merged(self, want, have):
        """ The xml configuration generator when state is merged

        :rtype: A list
        :returns: the xml configuration necessary to merge the provided into
                  the current configuration
        """
        intf_xml = []
        for config in want:
            enhanced_layer = True
            if config.get("enhanced_layer") is False:
                enhanced_layer = False

            mode = "interface-mode" if enhanced_layer else "port-mode"
            intf = build_root_xml_node("interface")
            build_child_xml_node(intf, "name", config["name"])
            unit_node = build_child_xml_node(intf, "unit")
            unit = config["unit"] if config["unit"] else "0"
            build_child_xml_node(unit_node, "name", unit)

            eth_node = build_subtree(unit_node, "family/ethernet-switching")
            if config.get("access"):
                vlan = config["access"].get("vlan")
                if vlan:
                    build_child_xml_node(eth_node, mode, "access")
                    vlan_node = build_child_xml_node(eth_node, "vlan")
                    build_child_xml_node(vlan_node, "members", vlan)
                    intf_xml.append(intf)
            elif config.get("trunk"):
                allowed_vlans = config["trunk"].get("allowed_vlans")
                native_vlan = config["trunk"].get("native_vlan")
                if allowed_vlans:
                    build_child_xml_node(eth_node, mode, "trunk")
                    vlan_node = build_child_xml_node(eth_node, "vlan")
                    for vlan in allowed_vlans:
                        build_child_xml_node(vlan_node, "members", vlan)
                if native_vlan:
                    build_child_xml_node(intf, "native-vlan-id", native_vlan)

                if allowed_vlans or native_vlan:
                    intf_xml.append(intf)

        return intf_xml
    def _state_merged(self, want, have):
        """ The xml configuration generator when state is merged

        :rtype: A list
        :returns: the xml configuration necessary to merge the provided into
                  the current configuration
        """
        intf_xml = []
        for config in want:
            enhanced_layer = True
            if config.get('enhanced_layer') is False:
                enhanced_layer = False

            mode = 'interface-mode' if enhanced_layer else 'port-mode'
            intf = build_root_xml_node('interface')
            build_child_xml_node(intf, 'name', config['name'])
            unit_node = build_child_xml_node(intf, 'unit')
            unit = config['unit'] if config['unit'] else '0'
            build_child_xml_node(unit_node, 'name', unit)

            eth_node = build_subtree(unit_node, 'family/ethernet-switching')
            if config.get('access'):
                vlan = config['access'].get('vlan')
                if vlan:
                    build_child_xml_node(eth_node, mode, 'access')
                    vlan_node = build_child_xml_node(eth_node, 'vlan')
                    build_child_xml_node(vlan_node, 'members', vlan)
                    intf_xml.append(intf)
            elif config.get('trunk'):
                allowed_vlans = config['trunk'].get('allowed_vlans')
                native_vlan = config['trunk'].get('native_vlan')
                if allowed_vlans:
                    build_child_xml_node(eth_node, mode, 'trunk')
                    vlan_node = build_child_xml_node(eth_node, 'vlan')
                    for vlan in allowed_vlans:
                        build_child_xml_node(vlan_node, 'members', vlan)
                if native_vlan:
                    build_child_xml_node(intf, 'native-vlan-id', native_vlan)

                if allowed_vlans or native_vlan:
                    intf_xml.append(intf)

        return intf_xml
예제 #12
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 list xml configuration necessary to migrate the current configuration
                  to the desired configuration
        """
        root = build_root_xml_node('chassis')
        ethernet_ele = build_subtree(root, 'aggregated-devices/ethernet')
        state = self._module.params['state']
        if state == 'overridden':
            config_xmls = self._state_overridden(want, have)
        elif state == 'deleted':
            config_xmls = self._state_deleted(want, have)
        elif state == 'merged':
            config_xmls = self._state_merged(want, have)
        elif state == 'replaced':
            config_xmls = self._state_replaced(want, have)

        for xml in config_xmls:
            ethernet_ele.append(xml)
        return tostring(root)
    def _state_merged(self, want, have):
        """ The xml configuration generator when state is merged
         :rtype: A list
         :returns: the xml configuration necessary to merge the provided into
                   the current configuration
         """
        intf_xml = []
        config_filter = """
            <configuration>
                <interfaces/>
            </configuration>
            """
        data = get_resource_config(self._connection,
                                   config_filter=config_filter)

        for config in want:
            lag_name = config['name']

            # if lag interface not already configured fail module.
            if not data.xpath("configuration/interfaces/interface[name='%s']" %
                              lag_name):
                self._module.fail_json(
                    msg="lag interface %s not configured, configure interface"
                    " %s before assigning members to lag" %
                    (lag_name, lag_name))

            lag_intf_root = build_root_xml_node('interface')
            build_child_xml_node(lag_intf_root, 'name', lag_name)
            ether_options_node = build_subtree(lag_intf_root,
                                               'aggregated-ether-options')
            if config['mode']:

                lacp_node = build_child_xml_node(ether_options_node, 'lacp')
                build_child_xml_node(lacp_node, config['mode'])

            link_protection = config['link_protection']
            if link_protection:
                build_child_xml_node(ether_options_node, 'link-protection')
            elif link_protection is False:
                build_child_xml_node(ether_options_node, 'link-protection',
                                     None, {'delete': 'delete'})

            intf_xml.append(lag_intf_root)

            members = config['members']
            for member in members:
                lag_member_intf_root = build_root_xml_node('interface')
                build_child_xml_node(lag_member_intf_root, 'name',
                                     member['member'])
                lag_node = build_subtree(lag_member_intf_root,
                                         'ether-options/ieee-802.3ad')
                build_child_xml_node(lag_node, 'bundle', config['name'])

                link_type = member.get('link_type')
                if link_type == "primary":
                    build_child_xml_node(lag_node, 'primary')
                elif link_type == "backup":
                    build_child_xml_node(lag_node, 'backup')

                intf_xml.append(lag_member_intf_root)

        return intf_xml
    def _state_merged(self, want, have):
        """ The xml configuration generator when state is merged
         :rtype: A list
         :returns: the xml configuration necessary to merge the provided into
                   the current configuration
         """
        intf_xml = []
        config_filter = """
            <configuration>
                <interfaces/>
            </configuration>
            """
        if self._module.params["state"] != "rendered":
            data = get_resource_config(self._connection,
                                       config_filter=config_filter)

        for config in want:
            lag_name = config["name"]

            # if lag interface not already configured fail module.
            if self._module.params["state"] != "rendered":
                if not data.xpath(
                        "configuration/interfaces/interface[name='%s']" %
                        lag_name):
                    self._module.fail_json(
                        msg=
                        "lag interface %s not configured, configure interface"
                        " %s before assigning members to lag" %
                        (lag_name, lag_name))

            lag_intf_root = build_root_xml_node("interface")
            build_child_xml_node(lag_intf_root, "name", lag_name)
            ether_options_node = build_subtree(lag_intf_root,
                                               "aggregated-ether-options")
            if config["mode"]:

                lacp_node = build_child_xml_node(ether_options_node, "lacp")
                build_child_xml_node(lacp_node, config["mode"])

            link_protection = config["link_protection"]
            if link_protection:
                build_child_xml_node(ether_options_node, "link-protection")
            elif link_protection is False:
                build_child_xml_node(
                    ether_options_node,
                    "link-protection",
                    None,
                    {"delete": "delete"},
                )

            intf_xml.append(lag_intf_root)

            members = config["members"]
            for member in members:
                lag_member_intf_root = build_root_xml_node("interface")
                build_child_xml_node(lag_member_intf_root, "name",
                                     member["member"])
                lag_node = build_subtree(lag_member_intf_root,
                                         "ether-options/ieee-802.3ad")
                build_child_xml_node(lag_node, "bundle", config["name"])

                link_type = member.get("link_type")
                if link_type == "primary":
                    build_child_xml_node(lag_node, "primary")
                elif link_type == "backup":
                    build_child_xml_node(lag_node, "backup")

                intf_xml.append(lag_member_intf_root)

        return intf_xml