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: h = flatten_dict(h) obj_in_want = flatten_dict( search_obj_in_list(h['name'], want, 'name')) if h == obj_in_want: continue for w in want: w = flatten_dict(w) if h['name'] == w['name']: wkeys = w.keys() hkeys = h.keys() for k in wkeys: if k in self.exclude_params and k in hkeys: del h[k] commands.extend(self.del_attribs(h)) for w in want: commands.extend(self.set_commands(flatten_dict(w), have, True)) 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'] if state in ('overridden', 'merged', 'replaced') and not want: self._module.fail_json( msg='config is required for state {0}'.format(state)) commands = list() if state == 'overridden': commands.extend(self._state_overridden(want, have)) elif state == 'deleted': commands.extend(self._state_deleted(want, have)) else: for w in want: if state == 'merged': commands.extend(self._state_merged(flatten_dict(w), have)) elif state == 'replaced': commands.extend(self._state_replaced( flatten_dict(w), 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 = [] want_intfs = [w['name'] for w in want] for h in have: h = flatten_dict(h) delete_dict = {} if h['name'] in want_intfs: for w in want: if w['name'] == h['name']: delete_keys = list(set(h) - set(flatten_dict(w))) for k in delete_keys: delete_dict.update({k: h[k]}) delete_dict.update({'name': h['name']}) break else: delete_dict.update(h) commands.extend(self.del_commands(delete_dict)) for w in want: commands.extend(self.set_commands(flatten_dict(w), 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 """ commands = [] state = self._module.params['state'] if state == 'overridden': commands = self._state_overridden(want, have) elif state == 'deleted': commands = self._state_deleted(want, have) elif state == 'rendered': commands = self._state_rendered(want) elif state == 'parsed': want = self._module.params['running_config'] commands = self._state_parsed(want) else: for w in want: if state == 'merged': commands.extend(self._state_merged(flatten_dict(w), have)) elif state == 'replaced': commands.extend(self._state_replaced( flatten_dict(w), have)) return commands
def set_commands(self, want, have): commands = [] obj_in_have = flatten_dict( search_obj_in_list(want['name'], have, 'name')) if not obj_in_have: commands = self.add_commands(flatten_dict(want)) else: diff = dict_diff(obj_in_have, want) if diff: diff.update({'name': want['name']}) commands = self.add_commands(diff) return commands
def set_commands(self, w, have, replace=False): commands = [] obj_in_have = flatten_dict(search_obj_in_list(w['name'], have, 'name')) if not obj_in_have: commands = self.add_commands(w) else: diff = self.diff_of_dicts(w, obj_in_have) if diff and not replace: if 'mode' in diff.keys() and diff['mode']: commands = self.add_commands(diff) if "allowed_vlans" in diff.keys() and diff["allowed_vlans"]: vlan_tobe_added = diff["allowed_vlans"].split(',') vlan_list = vlan_tobe_added[:] if obj_in_have.get("allowed_vlans"): have_vlans = obj_in_have["allowed_vlans"].split(',') else: have_vlans = [] for w_vlans in vlan_list: if w_vlans in have_vlans: vlan_tobe_added.pop(vlan_tobe_added.index(w_vlans)) if vlan_tobe_added: diff.update( {"allowed_vlans": ','.join(vlan_tobe_added)}) if have_vlans: commands = self.add_commands(diff, True) else: commands = self.add_commands(diff) return commands commands = self.add_commands(diff) return commands
def _state_replaced(self, w, 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 = [] obj_in_have = flatten_dict(search_obj_in_list(w['name'], have, 'name')) if obj_in_have: diff = dict_diff(w, obj_in_have) else: diff = w merged_commands = self.set_commands(w, have, True) if 'name' not in diff: diff['name'] = w['name'] dkeys = diff.keys() for k in w.copy(): if k in self.exclude_params and k in dkeys: del diff[k] replaced_commands = self.del_attribs(diff) if merged_commands: cmds = set(replaced_commands).intersection(set(merged_commands)) for cmd in cmds: merged_commands.remove(cmd) commands.extend(replaced_commands) commands.extend(merged_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 = [] del_commands = [] delete_dict = {} obj_in_have = flatten_dict( search_obj_in_list(want['name'], have, 'name')) for k1 in obj_in_have.keys(): if k1 not in want.keys(): delete_dict.update({k1: obj_in_have[k1]}) if delete_dict: delete_dict.update({'name': want['name']}) del_commands = self.del_commands(delete_dict) merged_commands = self.set_commands(want, have) if merged_commands: cmds = set(del_commands).intersection(set(merged_commands)) for cmd in cmds: merged_commands.remove(cmd) commands.extend(del_commands) commands.extend(merged_commands) return commands
def set_commands(self, w, have): commands = [] obj_in_have = flatten_dict(search_obj_in_list(w['name'], have, 'name')) if not obj_in_have: commands = self.add_commands(w) else: diff = self.diff_of_dicts(w, obj_in_have) commands = self.add_commands(diff) return commands
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 = flatten_dict(search_obj_in_list(w['name'], have, 'name')) commands.extend(self.del_attribs(obj_in_have)) else: if not have: return commands for h in have: commands.extend(self.del_attribs(flatten_dict(h))) 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 """ cmds = [] for h in have: # Check existing states, set to default if not in want or different than want h = flatten_dict(h) obj_in_want = search_obj_in_list(h['name'], want, 'name') if obj_in_want: # Let the 'want' loop handle all vals for this interface continue cmds.extend(self.del_attribs(h)) for w in want: # Update any want attrs if needed. The overridden state considers # the play as the source of truth for the entire device, therefore # set any unspecified attrs to their default state. w = self.set_none_vals_to_defaults(flatten_dict(w)) cmds.extend(self.set_commands(w, have)) return cmds