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
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"] or interface["name"] in each["name"] ): break else: commands.extend(self._set_config(interface, {}, module)) continue interface = remove_empties(interface) 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
def set_config(self, existing_lag_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_lag_interfaces_facts resp = self.set_state(want, have) return to_list(resp)
def set_config(self, existing_lag_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_lag_interfaces_facts resp = self.set_state(want, have) return to_list(resp)
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
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"]) interface = remove_empties(interface) for each in have: if (each["name"] == interface["name"] or interface["name"] in each["name"]): break else: commands.extend(self._set_config(interface, {}, module)) continue commands.extend(self._set_config(interface, each, module)) return commands
def _set_config(self, want, have): # Set the interface config based on the want and have config commands = [] want["name"] = normalize_interface(want["name"]) 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
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