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
    def populate_facts(self, connection, ansible_facts, data=None):
        """ Populate the facts for interfaces
        :param connection: the device connection
        :param data: previously collected configuration as lxml ElementTree root instance
                     or valid xml sting
        :rtype: dictionary
        :returns: facts
        """
        if not HAS_LXML:
            self._module.fail_json(msg='lxml is not installed.')

        if not data:
            config_filter = """
                <configuration>
                    <chassis>
                        <aggregated-devices>
                            <ethernet>
                                <lacp>
                                </lacp>
                            </ethernet>
                        </aggregated-devices>
                    </chassis>
                </configuration>
                """
            data = get_resource_config(connection, config_filter=config_filter)

        if isinstance(data, string_types):
            data = etree.fromstring(
                to_bytes(data, errors='surrogate_then_replace'))

        facts = {}
        config = deepcopy(self.generated_spec)
        resources = data.xpath(
            'configuration/chassis/aggregated-devices/ethernet/lacp')
        if resources:

            lacp_root = resources[0]
            config['system_priority'] = utils.get_xml_conf_arg(
                lacp_root, 'system-priority')

            if utils.get_xml_conf_arg(lacp_root,
                                      'link-protection/non-revertive',
                                      data='tag'):
                config['link_protection'] = "non-revertive"

            elif utils.get_xml_conf_arg(lacp_root, 'link-protection'):
                config['link_protection'] = "revertive"

        params = utils.validate_config(
            self.argument_spec, {'config': utils.remove_empties(config)})
        facts['lacp'] = {}
        facts['lacp'].update(utils.remove_empties(params['config']))

        ansible_facts['ansible_network_resources'].update(facts)

        return ansible_facts
Beispiel #3
0
    def populate_facts(self, connection, ansible_facts, data=None):
        """ Populate the facts for interfaces
        :param connection: the device connection
        :param data: previously collected configuration as lxml ElementTree root instance
                     or valid xml sting
        :rtype: dictionary
        :returns: facts
        """
        if not HAS_LXML:
            self._module.fail_json(msg='lxml is not installed.')

        if not data:
            config_filter = """
                <configuration>
                    <protocols>
                        <lldp>
                            <interface>
                            </interface>
                        </lldp>
                    </protocols>
                </configuration>
                """
            data = get_resource_config(connection, config_filter=config_filter)

        if isinstance(data, string_types):
            data = etree.fromstring(
                to_bytes(data, errors='surrogate_then_replace'))

        self._resources = data.xpath('configuration/protocols/lldp/interface')

        objs = []
        for resource in self._resources:
            if resource is not None:
                obj = self.render_config(self.generated_spec, resource)
                if obj:
                    objs.append(obj)
        facts = {}
        if objs:
            facts['lldp_interfaces'] = []
            params = utils.validate_config(self.argument_spec,
                                           {'config': objs})
            for cfg in params['config']:
                facts['lldp_interfaces'].append(utils.remove_empties(cfg))
        ansible_facts['ansible_network_resources'].update(facts)
        return ansible_facts
    def populate_facts(self, connection, ansible_facts, data=None):
        """ Populate the facts for interfaces
        :param connection: the device connection
        :param data: previously collected configuration as lxml ElementTree root instance
                     or valid xml sting
        :rtype: dictionary
        :returns: facts
        """
        if not HAS_LXML:
            self._module.fail_json(msg='lxml is not installed.')

        if not data:
            config_filter = """
                <configuration>
                    <protocols>
                        <lldp>
                        </lldp>
                    </protocols>
                </configuration>
                """
            data = get_resource_config(connection, config_filter=config_filter)

        if isinstance(data, string_types):
            data = etree.fromstring(to_bytes(data, errors='surrogate_then_replace'))

        facts = {}
        config = deepcopy(self.generated_spec)
        resources = data.xpath('configuration/protocols/lldp')
        if resources:
            lldp_root = resources[0]
            config['address'] = utils.get_xml_conf_arg(lldp_root, 'management-address')
            config['interval'] = utils.get_xml_conf_arg(lldp_root, 'advertisement-interval')
            config['transmit_delay'] = utils.get_xml_conf_arg(lldp_root, 'transmit-delay')
            config['hold_multiplier'] = utils.get_xml_conf_arg(lldp_root, 'hold-multiplier')
            if utils.get_xml_conf_arg(lldp_root, 'disable', data='tag'):
                config['enable'] = False

        params = utils.validate_config(self.argument_spec, {'config': utils.remove_empties(config)})

        facts['lldp_global'] = utils.remove_empties(params['config'])
        ansible_facts['ansible_network_resources'].update(facts)

        return ansible_facts
    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