def _state_overridden(self, want, have): """ The command generator when state is overridden :rtype: A list :returns: the commands necessary to migrate the current configuration to the desired configuration """ commands = [] for each in have: for interface in want: if each['name'] == interface['name']: break 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']) commands.extend(self._clear_config(interface, each)) 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)) # Remove the duplicate interface call commands = remove_duplicate_interface(commands) return commands
def _state_replaced(self, want, have): """ The command generator when state is replaced :param want: the desired configuration as a dictionary :param have: the current configuration as a dictionary :param interface_type: interface type :rtype: A list :returns: the commands necessary to migrate the current configuration to the deisred configuration """ commands = [] for interface in want: for each in have: if each['name'] == interface['name']: break elif interface['name'] in each['name']: break else: continue have_dict = filter_dict_having_none_value(interface, each) want = dict() commands.extend(self._clear_config(want, have_dict)) commands.extend(self._set_config(interface, each)) # Remove the duplicate interface call commands = remove_duplicate_interface(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 = [] for interface in want: for each_interface in interface.get('members'): for each in have: if each.get('members'): for every in each.get('members'): match = False if every['member'] == each_interface['member']: match = True break else: commands.extend( self._clear_config(interface, each)) continue if match: 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)) elif each.get('name') == each_interface['member']: 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)) break # Remove the duplicate interface call commands = remove_duplicate_interface(commands) return commands
def _state_replaced(self, want, have): """ The command generator when state is replaced :param want: the desired configuration as a dictionary :param have: the current configuration as a dictionary :param interface_type: interface type :rtype: A list :returns: the commands necessary to migrate the current configuration to the deisred configuration """ commands = [] have_dict = filter_dict_having_none_value(want, have) commands.extend(self._clear_config(have_dict)) commands.extend(self._set_config(want, have)) return commands
def _state_overridden(self, want, have): """ The command generator when state is overridden :param want: the desired configuration as a dictionary :param obj_in_have: the current configuration as a dictionary :rtype: A list :returns: the commands necessary to migrate the current configuration to the desired configuration """ commands = [] for each in have: for interface in want: count = 0 if each['name'] == interface['name']: break elif interface['name'] in each['name']: break count += 1 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']) commands.extend(self._clear_config(interface, each)) 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)) # as the pre-existing interface are now configured by # above set_config call, deleting the respective # interface entry from the want list del want[count] # Iterating through want list which now only have new interfaces to be # configured for each in want: commands.extend(self._set_config(each, dict())) # Remove the duplicate interface call commands = remove_duplicate_interface(commands) return commands
def _state_replaced(self, want, have): """ The command generator when state is replaced :rtype: A list :returns: the commands necessary to migrate the current configuration to the desired configuration """ commands = [] # Drill each iteration of want n have and then based on dest and afi tyoe comparison take config call for w in want: for addr_want in w.get('address_families'): for route_want in addr_want.get('routes'): check = False for h in have: if h.get('address_families'): for addr_have in h.get('address_families'): for route_have in addr_have.get('routes'): if route_want.get('dest') == route_have.get('dest')\ and addr_want['afi'] == addr_have['afi']: check = True have_set = set() new_hops = [] for each in route_want.get( 'next_hops'): want_set = set() new_dict_to_set( each, [], want_set, 0) new_hops.append(want_set) new_dict_to_set( addr_have, [], have_set, 0) # Check if the have dict next_hops value is diff from want dict next_hops have_dict = filter_dict_having_none_value( route_want.get('next_hops')[0], route_have.get('next_hops')[0]) # update the have_dict with forward_router_address have_dict.update({ 'forward_router_address': route_have.get('next_hops')[0].get( 'forward_router_address') }) # updating the have_dict with next_hops val that's not None new_have_dict = {} for k, v in have_dict.items(): if v is not None: new_have_dict.update({k: v}) # Set the new config from the user provided want config cmd = self._set_config( w, h, addr_want, route_want, route_have, new_hops, have_set) if cmd: # since inplace update isn't allowed for static routes, preconfigured # static routes needs to be deleted before the new want static routes changes # are applied clear_route_have = copy.deepcopy( route_have) # inplace update is allowed in case of ipv6 static routes, so not deleting it # before applying the want changes if ':' not in route_want.get( 'dest'): commands.extend( self._clear_config( {}, h, {}, addr_have, {}, clear_route_have)) commands.extend(cmd) if check: break if check: break if not check: # For configuring any non-existing want config new_hops = [] for each in route_want.get('next_hops'): want_set = set() new_dict_to_set(each, [], want_set, 0) new_hops.append(want_set) commands.extend( self._set_config(w, {}, addr_want, route_want, {}, new_hops, set())) commands = [each for each in commands if 'no' in each] + \ [each for each in commands if 'no' not in each] return commands