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 = [] want_copy = deepcopy(remove_empties(want)) have_copy = deepcopy(have) want_vifs = want_copy.pop("vifs", []) have_vifs = have_copy.pop("vifs", []) for key in dict_delete(have_copy, want_copy).keys(): if key == "enabled": continue commands.append( self._compute_commands(key=key, interface=want_copy["name"], remove=True)) if have_copy["enabled"] is False: commands.append( self._compute_commands(key="enabled", value=True, interface=want_copy["name"])) if have_vifs: for have_vif in have_vifs: want_vif = search_obj_in_list(have_vif["vlan_id"], want_vifs, key="vlan_id") if not want_vif: want_vif = { "vlan_id": have_vif["vlan_id"], "enabled": True, } for key in dict_delete(have_vif, want_vif).keys(): if key == "enabled": continue commands.append( self._compute_commands( key=key, interface=want_copy["name"], vif=want_vif["vlan_id"], remove=True, )) if have_vif["enabled"] is False: commands.append( self._compute_commands( key="enabled", value=True, interface=want_copy["name"], vif=want_vif["vlan_id"], )) return commands
def _update_blackhole(self, key, want, have): """ This function gets the difference for blackhole dict and form the commands to delete the attributes which are present in have but not in want. :param want: :param have: :return: list of commands :param key: :param want: :param have: :return: list of commands """ commands = [] want_copy = deepcopy(remove_empties(want)) have_copy = deepcopy(remove_empties(have)) want_blackhole = want_copy.get(key) or {} have_blackhole = have_copy.get(key) or {} updates = dict_delete(have_blackhole, want_blackhole) if updates: for attrib, value in iteritems(updates): if value: if attrib == 'distance': commands.append( self._compute_command(dest=want['dest'], key='blackhole', attrib=attrib, remove=True, value=str(value)) ) elif attrib == 'type' and 'distance' not in want_blackhole.keys(): commands.append( self._compute_command(dest=want['dest'], key='blackhole', remove=True) ) return commands
def _add_blackhole(self, key, want, have): """ This function gets the diff for blackhole config specific attributes and form the commands for attributes which are present in want but not in have. :param key: :param want: :param have: :return: list of commands """ commands = [] want_copy = deepcopy(remove_empties(want)) have_copy = deepcopy(remove_empties(have)) want_blackhole = want_copy.get(key) or {} have_blackhole = have_copy.get(key) or {} updates = dict_delete(want_blackhole, have_blackhole) if updates: for attrib, value in iteritems(updates): if value: if attrib == "distance": commands.append( self._compute_command( dest=want["dest"], key="blackhole", attrib=attrib, remove=False, value=str(value), )) elif attrib == "type": commands.append( self._compute_command(dest=want["dest"], key="blackhole")) return commands