def set_state(self, want, have): """ Select the appropriate function based on the state provided :param want: the desired configuration as a dictionary :param have: the current configuration as a dictionary :rtype: A list :returns: the commands necessary to migrate the current configuration to the desired configuration """ commands = [] state = self._module.params['state'] if state in ('overridden', 'merged', 'replaced') and not want: self._module.fail_json( msg='value of config parameter must not be empty for state {0}' .format(state)) if state == 'overridden': commands.extend(Lacp_interfaces._state_overridden(want, have)) elif state == 'deleted': if not want: for intf in have: commands.extend( Lacp_interfaces._state_deleted({'name': intf['name']}, intf)) else: for item in want: obj_in_have = search_obj_in_list(item['name'], have) commands.extend( Lacp_interfaces._state_deleted(item, obj_in_have)) else: for item in want: name = item['name'] obj_in_have = search_obj_in_list(name, have) if state == 'merged': commands.extend( Lacp_interfaces._state_merged(item, obj_in_have)) elif state == 'replaced': commands.extend( Lacp_interfaces._state_replaced(item, obj_in_have)) return commands
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 have_intf in have: intf_in_want = search_obj_in_list(have_intf['name'], want) if not intf_in_want: commands.extend(self._purge_attribs(have_intf)) for intf in want: intf_in_have = search_obj_in_list(intf['name'], have) commands.extend(self._state_replaced(intf, intf_in_have)) return commands
def set_commands(self, w, have): commands = [] obj_in_have = search_obj_in_list(w['name'], have, 'name') if not obj_in_have: commands = self.add_commands(w['members'], w['name']) else: diff = self.diff_list_of_dicts(w['members'], obj_in_have['members']) commands = self.add_commands(diff, w['name']) return commands
def del_intf_commands(self, w, have): commands = [] obj_in_have = search_obj_in_list(w['name'], have, 'name') if obj_in_have: lst_to_del = self.intersect_list_of_dicts(w['members'], obj_in_have['members']) if lst_to_del: for item in lst_to_del: commands.append('interface' + ' ' + item['member']) commands.append('no channel-group') return commands
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 h in have: obj_in_want = search_obj_in_list(h['name'], want, 'name') if h == obj_in_want: continue commands.extend(self.del_all_commands(h)) for w in want: commands.extend(self.set_commands(w, have)) return commands
def diff_list_of_dicts(self, want, have): if not want: want = [] if not have: have = [] diff = [] for w_item in want: h_item = search_obj_in_list(w_item['member'], have, key='member') or {} delta = dict_diff(h_item, w_item) if delta: if 'member' not in delta.keys(): delta['member'] = w_item['member'] diff.append(delta) return diff
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 w in want: obj_in_have = search_obj_in_list(w['name'], have, 'name') commands.extend(self.del_all_commands(obj_in_have)) else: if not have: return commands for h in have: commands.extend(self.del_all_commands(h)) return commands
def diff_list_of_dicts(w, h, key='member'): """ Returns a list containing diff between two list of dictionaries """ if not w: w = [] if not h: h = [] diff = [] for w_item in w: h_item = search_obj_in_list(w_item[key], h, key=key) or {} d = dict_diff(h_item, w_item) if d: if key not in d.keys(): d[key] = w_item[key] diff.append(d) return diff
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 not want: for item in have: commands.extend(self._purge_attribs(intf=item)) else: for item in want: name = item['name'] obj_in_have = search_obj_in_list(name, have) if not obj_in_have: self._module.fail_json( msg=('interface {0} does not exist'.format(name))) commands.extend(self._purge_attribs(intf=obj_in_have)) return commands
def set_state(self, want, have): """ Select the appropriate function based on the state provided :param want: the desired configuration as a dictionary :param have: the current configuration as a dictionary :rtype: A list :returns: the commands necessary to migrate the current configuration to the desired configuration """ state = self._module.params['state'] commands = [] if state in ('overridden', 'merged', 'replaced') and not want: self._module.fail_json( msg='value of config parameter must not be empty for state {0}' .format(state)) if state == 'overridden': commands.extend(self._state_overridden(want, have)) elif state == 'deleted': commands.extend(self._state_deleted(want, have)) else: # Instead of passing entire want and have # list of dictionaries to the respective # _state_* methods we are passing the want # and have dictionaries per interface for item in want: name = item['name'] obj_in_have = search_obj_in_list(name, have) if state == 'merged': commands.extend(self._state_merged(item, obj_in_have)) elif state == 'replaced': commands.extend(self._state_replaced(item, obj_in_have)) return commands