예제 #1
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']
        root = build_root_xml_node('configuration')
        routing_options = build_child_xml_node(root, 'routing-options')
        routing_instances = build_child_xml_node(root, 'routing-instances')
        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:
            if xml['root_type'] == 'routing-options':
                routing_options.append(xml['static_route_xml'])
            elif xml['root_type'] == 'routing-instances':
                routing_instances.append(xml['static_route_xml'])

        return [tostring(xml) for xml in root.getchildren()]
예제 #2
0
    def _state_merged(self, want, have):
        """ The xml generator when state is merged

        :rtype: A list
        :returns: the xml necessary to merge the provided into
                  the current configuration
        """
        intf_xml = []
        for config in want:
            root_node, unit_node = self._get_common_xml_node(config["name"])
            build_child_xml_node(unit_node, "name", str(config["unit"]))
            if config.get("ipv4"):
                self.build_ipaddr_et(config, unit_node)
            if config.get("ipv6"):
                self.build_ipaddr_et(config, unit_node, protocol="ipv6")
            intf_xml.append(root_node)
        return intf_xml
예제 #3
0
    def _state_deleted(self, want, have):
        """ The command generator when state is deleted

        :rtype: A list
        :returns: the commands necessary to remove the current configuration
                  of the provided objects
        """
        lacp_xml = []

        lacp_root = build_root_xml_node('lacp')
        build_child_xml_node(lacp_root, 'system-priority', None,
                             {'delete': 'delete'})
        element = build_child_xml_node(lacp_root, 'link-protection', None,
                                       {'delete': 'delete'})
        build_child_xml_node(element, 'non-revertive', None,
                             {'delete': 'delete'})

        lacp_xml.append(lacp_root)
        return lacp_xml
예제 #4
0
    def _state_deleted(self, want, have):
        """ The command generator when state is deleted

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

        if not want:
            want = have

        for config in want:
            vlan_name = config["name"]
            vlan_root = build_root_xml_node("vlan")
            vlan_root.attrib.update({"delete": "delete"})
            build_child_xml_node(vlan_root, "name", vlan_name)
            intf_xml.append(vlan_root)
        return intf_xml
예제 #5
0
    def _state_deleted(self, want, have):
        """ The command generator when state is deleted
        :rtype: A list
        :returns: the commands necessary to remove the current configuration
                  of the provided objects
        """
        lldp_xml = []

        lldp_root = build_root_xml_node("lldp")
        build_child_xml_node(lldp_root, "management-address", None,
                             {"delete": "delete"})
        build_child_xml_node(lldp_root, "advertisement-interval", None,
                             {"delete": "delete"})
        build_child_xml_node(lldp_root, "transmit-delay", None,
                             {"delete": "delete"})
        build_child_xml_node(lldp_root, "hold-multiplier", None,
                             {"delete": "delete"})
        build_child_xml_node(lldp_root, "disable", None, {"delete": "delete"})
        lldp_xml.append(lldp_root)
        return lldp_xml
예제 #6
0
    def _state_deleted(self, want, have):
        """ The command generator when state is deleted
        :rtype: A list
        :returns: the commands necessary to remove the current configuration
                  of the provided objects
        """
        lldp_xml = []

        lldp_root = build_root_xml_node('lldp')
        build_child_xml_node(lldp_root, 'management-address', None,
                             {'delete': 'delete'})
        build_child_xml_node(lldp_root, 'advertisement-interval', None,
                             {'delete': 'delete'})
        build_child_xml_node(lldp_root, 'transmit-delay', None,
                             {'delete': 'delete'})
        build_child_xml_node(lldp_root, 'hold-multiplier', None,
                             {'delete': 'delete'})
        build_child_xml_node(lldp_root, 'disable', None, {'delete': 'delete'})
        lldp_xml.append(lldp_root)
        return lldp_xml
    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
예제 #8
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
        """
        lldp_intf_xml = []
        intf_obj = want

        if not intf_obj:
            # delete lldp interfaces attribute from all the existing interface
            intf_obj = have

        for config in intf_obj:
            lldp_intf_root = build_root_xml_node('interface')
            lldp_intf_root.attrib.update({'delete': 'delete'})
            build_child_xml_node(lldp_intf_root, 'name', config['name'])

            lldp_intf_xml.append(lldp_intf_root)

        return lldp_intf_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 build_ipaddr_et(self, config, unit_node, protocol='ipv4',
                     delete=False):
     family = build_child_xml_node(unit_node, 'family')
     inet = 'inet'
     if protocol == 'ipv6':
         inet = 'inet6'
     ip_protocol = build_child_xml_node(family, inet)
     for ip_addr in config[protocol]:
         if ip_addr['address'] == 'dhcp' and protocol == 'ipv4':
             build_child_xml_node(ip_protocol, 'dhcp')
         else:
             ip_addresses = build_child_xml_node(
                 ip_protocol, 'address')
             build_child_xml_node(
                 ip_addresses, 'name', ip_addr['address'])
예제 #11
0
 def build_ipaddr_et(self,
                     config,
                     unit_node,
                     protocol="ipv4",
                     delete=False):
     family = build_child_xml_node(unit_node, "family")
     inet = "inet"
     if protocol == "ipv6":
         inet = "inet6"
     ip_protocol = build_child_xml_node(family, inet)
     for ip_addr in config[protocol]:
         if ip_addr["address"] == "dhcp" and protocol == "ipv4":
             build_child_xml_node(ip_protocol, "dhcp")
         else:
             ip_addresses = build_child_xml_node(ip_protocol, "address")
             build_child_xml_node(ip_addresses, "name", ip_addr["address"])
예제 #12
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
         """
        lldp_intf_xml = []
        for config in want:
            lldp_intf_root = build_root_xml_node("interface")

            if config.get("name"):
                build_child_xml_node(lldp_intf_root, "name", config["name"])

            if config.get("enable") is not None:
                if config["enable"] is False:
                    build_child_xml_node(lldp_intf_root, "disable")
                else:
                    build_child_xml_node(lldp_intf_root, "disable", None,
                                         {"delete": "delete"})
            else:
                build_child_xml_node(lldp_intf_root, "disable", None,
                                     {"delete": "delete"})
            lldp_intf_xml.append(lldp_intf_root)
        return lldp_intf_xml
예제 #13
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
         """
        lldp_intf_xml = []
        for config in want:
            lldp_intf_root = build_root_xml_node('interface')

            if config.get('name'):
                build_child_xml_node(lldp_intf_root, 'name', config['name'])

            if config.get('enable') is not None:
                if config['enable'] is False:
                    build_child_xml_node(lldp_intf_root, 'disable')
                else:
                    build_child_xml_node(lldp_intf_root, 'disable', None,
                                         {'delete': 'delete'})
            else:
                build_child_xml_node(lldp_intf_root, 'disable', None,
                                     {'delete': 'delete'})
            lldp_intf_xml.append(lldp_intf_root)
        return lldp_intf_xml
예제 #14
0
    def _state_merged(self, want, have):
        """ The command generator when state is merged

        :rtype: A list
        :returns: the xml necessary to merge the provided into
                  the current configuration
        """
        intf_xml = []

        for config in want:
            vlan_name = str(config["name"])
            vlan_id = str(config["vlan_id"])
            vlan_description = config.get("description")
            vlan_root = build_root_xml_node("vlan")
            build_child_xml_node(vlan_root, "name", vlan_name)
            build_child_xml_node(vlan_root, "vlan-id", vlan_id)
            if vlan_description:
                build_child_xml_node(vlan_root, "description",
                                     vlan_description)
            intf_xml.append(vlan_root)
        return intf_xml
예제 #15
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:
            intf = build_root_xml_node('interface')
            build_child_xml_node(intf, 'name', config['name'])

            intf_fields = ['description', 'speed']
            if not config['name'].startswith('fxp'):
                intf_fields.append('mtu')
            for field in intf_fields:
                if config.get(field):
                    build_child_xml_node(intf, field, config[field])

            if config.get('duplex'):
                build_child_xml_node(intf, 'link-mode', config['duplex'])

            if config.get('enabled') is False:
                build_child_xml_node(intf, 'disable')

            holdtime = config.get('hold_time')
            if holdtime:
                holdtime_ele = build_child_xml_node(intf, 'hold-time')

                for holdtime_field in ['up', 'down']:
                    build_child_xml_node(holdtime_ele, holdtime_field,
                                         holdtime.get(holdtime_field, ''))
            intf_xml.append(intf)

        return intf_xml
예제 #16
0
 def _get_common_xml_node(self, name):
     root_node = build_root_xml_node("interface")
     build_child_xml_node(root_node, "name", name)
     intf_unit_node = build_child_xml_node(root_node, "unit")
     return root_node, intf_unit_node
    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 = self._connection.get_configuration(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
예제 #18
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
        """
        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
예제 #19
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')
                build_child_xml_node(element, 'port-priority', config['port_priority'])
                if config['force_up'] is False:
                    build_child_xml_node(element, 'force-up', None, {'delete': 'delete'})
                else:
                    build_child_xml_node(element, 'force-up')
                intf_xml.append(lacp_intf_root)

        return intf_xml
예제 #20
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
        """
        intf_xml = []
        existing_l3_intfs = [l3_intf['name'] for l3_intf in have]

        if not want:
            want = have

        for config in want:
            if config['name'] not in existing_l3_intfs:
                continue
            else:
                root_node, unit_node = self._get_common_xml_node(
                    config['name'])
                build_child_xml_node(unit_node, 'name',
                                                str(config['unit']))
                family = build_child_xml_node(unit_node, 'family')
                ipv4 = build_child_xml_node(family, 'inet')
                intf = next(
                    (intf for intf in have if intf['name'] == config['name']),
                    None)
                if 'ipv4' in intf:
                    if 'dhcp' in [x['address'] for x in intf.get('ipv4') if intf.get('ipv4') is not None]:
                        build_child_xml_node(ipv4, 'dhcp', None, {'delete': 'delete'})
                    else:
                        build_child_xml_node(
                            ipv4, 'address', None, {'delete': 'delete'})
                ipv6 = build_child_xml_node(family, 'inet6')
                build_child_xml_node(ipv6, 'address', None, {'delete': 'delete'})
            intf_xml.append(root_node)
        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
예제 #22
0
    def _state_merged(self, want, have, delete=None):
        """ The command generator when state is merged

        :rtype: A list
        :returns: the xml necessary to migrate the current configuration
                  to the desired configuration
        """
        static_route_xml = []
        root_type = ""
        vrf_name = None
        for config in want:
            if config.get('vrf'):
                vrf_name = config['vrf']
            if vrf_name:
                root_type = 'routing-instances'
                instance = build_root_xml_node('instance')
                build_child_xml_node(instance, 'name', vrf_name)
                routing_options = build_child_xml_node(instance,
                                                       'routing-options')
            else:
                root_type = 'routing-options'

            for afi in config['address_families']:
                protocol = afi['afi']
                if protocol == 'ipv6':
                    if vrf_name:
                        rib_route_root = build_child_xml_node(
                            routing_options, 'rib')
                        build_child_xml_node(rib_route_root, 'name',
                                             vrf_name + '.inet6.0')
                    else:
                        rib_route_root = build_root_xml_node('rib')
                        build_child_xml_node(rib_route_root, 'name', 'inet6.0')
                    static_route_root = build_child_xml_node(
                        rib_route_root, 'static')
                elif protocol == 'ipv4':
                    if vrf_name:
                        static_route_root = build_child_xml_node(
                            routing_options, 'static')
                    else:
                        static_route_root = build_root_xml_node('static')

                if afi.get('routes'):
                    for route in afi['routes']:
                        route_node = build_child_xml_node(
                            static_route_root, 'route')
                        if delete:
                            route_node.attrib.update(delete)
                        if route.get('dest'):
                            build_child_xml_node(route_node, 'name',
                                                 route['dest'])
                        if not delete:
                            if route.get('metric'):
                                build_child_xml_node(route_node, 'metric',
                                                     route['metric'])
                            if route.get('next_hop'):
                                for hop in route['next_hop']:
                                    build_child_xml_node(
                                        route_node, 'next-hop',
                                        hop['forward_router_address'])
                elif delete:
                    if vrf_name:
                        instance.attrib.update(delete)
                    static_route_root.attrib.update(delete)

                if vrf_name:
                    static_route_xml.append({
                        'root_type': root_type,
                        'static_route_xml': instance
                    })
                else:
                    if protocol == 'ipv6':
                        static_route_xml.append({
                            'root_type':
                            root_type,
                            'static_route_xml':
                            rib_route_root
                        })
                    else:
                        static_route_xml.append({
                            'root_type':
                            root_type,
                            'static_route_xml':
                            static_route_root
                        })
        return static_route_xml
예제 #23
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
예제 #24
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
        """
        intf_xml = []
        intf_obj = want

        if not intf_obj:
            # delete base interfaces attribute from all the existing interface
            intf_obj = have

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

            intf_fields = ["description"]
            if not config["name"].startswith("lo"):
                intf_fields.append("speed")

            if not any(
                [
                    config["name"].startswith("fxp"),
                    config["name"].startswith("lo"),
                ]
            ):
                intf_fields.append("mtu")

            for field in intf_fields:
                build_child_xml_node(intf, field, None, {"delete": "delete"})

            if not config["name"].startswith("lo"):
                build_child_xml_node(
                    intf, "link-mode", None, {"delete": "delete"}
                )

            build_child_xml_node(intf, "disable", None, {"delete": "delete"})

            holdtime_ele = build_child_xml_node(intf, "hold-time")
            for holdtime_field in ["up", "down"]:
                build_child_xml_node(
                    holdtime_ele, holdtime_field, None, {"delete": "delete"}
                )
            intf_xml.append(intf)

        return intf_xml
예제 #25
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
        """
        intf_xml = []
        existing_l3_intfs = [l3_intf["name"] for l3_intf in have]

        if not want:
            want = have

        for config in want:
            if config["name"] not in existing_l3_intfs:
                continue
            else:
                root_node, unit_node = self._get_common_xml_node(
                    config["name"])
                build_child_xml_node(unit_node, "name", str(config["unit"]))
                family = build_child_xml_node(unit_node, "family")
                ipv4 = build_child_xml_node(family, "inet")
                intf = next(
                    (intf for intf in have if intf["name"] == config["name"]),
                    None,
                )
                if "ipv4" in intf:
                    if "dhcp" in [
                            x["address"] for x in intf.get("ipv4")
                            if intf.get("ipv4") is not None
                    ]:
                        build_child_xml_node(ipv4, "dhcp", None,
                                             {"delete": "delete"})
                    else:
                        build_child_xml_node(ipv4, "address", None,
                                             {"delete": "delete"})
                ipv6 = build_child_xml_node(family, "inet6")
                build_child_xml_node(ipv6, "address", None,
                                     {"delete": "delete"})
            intf_xml.append(root_node)
        return intf_xml
예제 #26
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 = []
        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
예제 #27
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
        """
        intf_xml = []
        intf_obj = want

        if not intf_obj:
            # delete base interfaces attribute from all the existing interface
            intf_obj = have

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

            intf_fields = ['description']
            if not config['name'].startswith('lo'):
                intf_fields.append('speed')

            if not any([
                    config['name'].startswith('fxp'),
                    config['name'].startswith('lo')
            ]):
                intf_fields.append('mtu')

            for field in intf_fields:
                build_child_xml_node(intf, field, None, {'delete': 'delete'})

            if not config['name'].startswith('lo'):
                build_child_xml_node(intf, 'link-mode', None,
                                     {'delete': 'delete'})

            build_child_xml_node(intf, 'disable', None, {'delete': 'delete'})

            holdtime_ele = build_child_xml_node(intf, 'hold-time')
            for holdtime_field in ['up', 'down']:
                build_child_xml_node(holdtime_ele, holdtime_field, None,
                                     {'delete': 'delete'})
            intf_xml.append(intf)

        return intf_xml
예제 #28
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
        """
        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
예제 #29
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
예제 #30
0
 def _get_common_xml_node(self, name):
     root_node = build_root_xml_node('interface')
     build_child_xml_node(root_node, 'name', name)
     intf_unit_node = build_child_xml_node(root_node, 'unit')
     return root_node, intf_unit_node