示例#1
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
        """
        commands = []

        if want:
            for interface in want:
                interface['name'] = normalize_interface(interface['name'])
                for each in have:
                    if each['name'] == interface['name']:
                        break
                    elif interface['name'] in each['name']:
                        break
                else:
                    continue
                interface = dict(name=interface['name'])
                commands.extend(self._clear_config(interface, each))
        else:
            for each in have:
                want = dict()
                commands.extend(self._clear_config(want, each))

        return commands
示例#2
0
    def render_config(self, spec, conf):
        """
        Render config as dictionary structure and delete keys from spec for null values
        :param spec: The facts tree, generated from the argspec
        :param conf: The configuration
        :rtype: dictionary
        :returns: The generated config
        """

        config = deepcopy(spec)
        match = re.search(r'^(\S+)', conf)
        if match.group(1).lower() == "preconfigure":
            match = re.search(r'^(\S+ \S+)', conf)
        intf = match.group(1)
        if get_interface_type(intf) == 'unknown':
            return {}
        # populate the facts from the configuration
        config['name'] = normalize_interface(intf)
        config['description'] = utils.parse_conf_arg(conf, 'description')
        if utils.parse_conf_arg(conf, 'speed'):
            config['speed'] = int(utils.parse_conf_arg(conf, 'speed'))
        if utils.parse_conf_arg(conf, 'mtu'):
            config['mtu'] = int(utils.parse_conf_arg(conf, 'mtu'))
        config['duplex'] = utils.parse_conf_arg(conf, 'duplex')
        enabled = utils.parse_conf_cmd_arg(conf, 'shutdown', False)
        config['enabled'] = enabled if enabled is not None else True

        return utils.remove_empties(config)
示例#3
0
    def set_config(self, existing_acl_interfaces_facts):
        """ Collect the configuration from the args passed to the module,
            collect the current configuration (as a dict from facts)

        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        want = self._module.params['config']
        if want:
            for item in want:
                item['name'] = normalize_interface(item['name'])
                if 'members' in want and want['members']:
                    for item in want['members']:
                        item.update({
                            'member':
                            normalize_interface(item['member']),
                            'mode':
                            item['mode']
                        })
        have = existing_acl_interfaces_facts
        resp = self.set_state(want, have)
        return to_list(resp)
示例#4
0
    def _state_merged(self, want, have, module):
        """ The command generator when state is merged
        :rtype: A list
        :returns: the commands necessary to merge the provided into
                  the current configuration
        """
        commands = []

        for interface in want:
            interface['name'] = normalize_interface(interface['name'])
            for each in have:
                if each['name'] == interface['name']:
                    break
            else:
                commands.extend(self._set_config(interface, dict(), module))
                continue
            commands.extend(self._set_config(interface, each, module))

        return commands
示例#5
0
    def _state_overridden(self, want, have, module):
        """ The command generator when state is overridden
        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        commands = []
        not_in_have = set()
        in_have = set()

        for each in have:
            for interface in want:
                interface['name'] = normalize_interface(interface['name'])
                if each['name'] == interface['name']:
                    in_have.add(interface['name'])
                    break
                elif interface['name'] != each['name']:
                    not_in_have.add(interface['name'])
            else:
                # We didn't find a matching desired state, which means we can
                # pretend we recieved an empty desired state.
                interface = dict(name=each['name'])
                kwargs = {'want': interface, 'have': each}
                commands.extend(self._clear_config(**kwargs))
                continue
            have_dict = filter_dict_having_none_value(interface, each)
            commands.extend(self._clear_config(dict(), have_dict))
            commands.extend(self._set_config(interface, each, module))
        # Add the want interface that's not already configured in have interface
        for each in (not_in_have - in_have):
            for every in want:
                interface = 'interface {0}'.format(every['name'])
                if each and interface not in commands:
                    commands.extend(self._set_config(every, {}, module))
        # Remove the duplicate interface call
        commands = remove_duplicate_interface(commands)

        return commands
示例#6
0
    def _state_replaced(self, want, have, module):
        """ The command generator when state is replaced
        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the desired configuration
        """
        commands = []

        for interface in want:
            interface['name'] = normalize_interface(interface['name'])
            for each in have:
                if each['name'] == interface['name']:
                    break
            else:
                commands.extend(self._set_config(interface, {}, module))
                continue
            have_dict = filter_dict_having_none_value(interface, each)
            commands.extend(self._clear_config(dict(), have_dict))
            commands.extend(self._set_config(interface, each, module))
        # Remove the duplicate interface call
        commands = remove_duplicate_interface(commands)

        return commands