def _state_overridden(self, want, have, diff_members, diff_portchannels): """ The command generator when state is overridden :rtype: A list :returns: the commands necessary to migrate the current configuration to the desired configuration """ requests = list() commands = list() delete_list = list() delete_list = get_diff(have, want, TEST_KEYS) delete_members, delete_portchannels = self.diff_list_for_member_creation(delete_list) replaced_list = list() for i in want: list_obj = search_obj_in_list(i['name'], delete_members, "name") if list_obj: replaced_list.append(list_obj) requests = self.get_delete_lag_interfaces_requests(replaced_list) commands.extend(update_states(replaced_list, "overridden")) delete_members = get_diff(delete_members, replaced_list, TEST_KEYS) commands_overridden, requests_overridden = self.template_for_lag_deletion(have, delete_members, delete_portchannels, "overridden") requests.extend(requests_overridden) commands.extend(commands_overridden) override_commands, override_requests = self.template_for_lag_creation(have, diff_members, diff_portchannels, "overridden") commands.extend(override_commands) requests.extend(override_requests) return commands, requests
def _state_deleted(self, want, have, diff): """ The command generator when state is deleted :param want: the objects from which the configuration should be removed :param have: the current configuration as a dictionary :rtype: A list :returns: the commands necessary to remove the current configuration of the provided objects """ is_delete_all = False if not want: is_delete_all = True if is_delete_all: commands = have new_have = have else: new_have = self.remove_default_entries(have) d_diff = get_diff(want, new_have, TEST_KEYS, is_skeleton=True) delete_diff = get_diff(want, d_diff, TEST_KEYS, is_skeleton=True) commands = delete_diff requests = self.get_delete_bgp_neighbor_requests( commands, new_have, is_delete_all) if commands and len(requests) > 0: commands = update_states(commands, "deleted") else: commands = [] return commands, requests
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 = [] requests = [] state = self._module.params['state'] for i in want: if i.get('members'): temp = [] for j in i['members']: temp.append(j.replace('\\\\', '\\')) i['members'] = temp diff = get_diff(want, have) for i in want: if i.get('members'): temp = [] for j in i['members']: temp.append(j.replace('\\', '\\\\')) i['members'] = temp if state == 'overridden': commands, requests = self._state_overridden(want, have, diff) elif state == 'deleted': commands, requests = self._state_deleted(want, have, diff) elif state == 'merged': commands, requests = self._state_merged(want, have, diff) elif state == 'replaced': commands, requests = self._state_replaced(want, have, diff) return commands, requests
def get_delete_advertise_list_request(self, vrf_name, conf_afi, conf_safi, conf_advt_list=None, mat_advt_list=None): requests = [] afi_safi = ("%s_%s" % (conf_afi, conf_safi)).upper() url = '%s=%s/%s' % (self.network_instance_path, vrf_name, self.protocol_bgp_path) url += '/%s=%s/%s/advertise-list' % (self.afi_safi_path, afi_safi, self.l2vpn_evpn_config_path) if conf_advt_list and mat_advt_list: del_adv_list = [] existing_list_len = len(mat_advt_list) for adv in conf_advt_list: diff = get_diff({'advertise_prefix': [adv]}, {'advertise_prefix': mat_advt_list}, [{'advertise_prefix': {'afi': '', 'safi': ''}}]) if not diff: del_adv_list.append(adv) del_adv_list_len = len(del_adv_list) if existing_list_len > 0 and existing_list_len == del_adv_list_len: requests.append({"path": url, "method": DELETE}) else: for del_adv in del_adv_list: del_afi_safi = ("=%s_%s" % (del_adv['afi'], del_adv['safi'])).upper() url += del_afi_safi requests.append({"path": url, "method": DELETE}) else: requests.append({"path": url, "method": DELETE}) return requests
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'] # diff method works on dict, so creating temp dict diff = get_diff(want, have) # removing the dict in case diff found if state == 'overridden': have = [ each_intf for each_intf in have if each_intf['name'].startswith('Ethernet') ] commands, requests = self._state_overridden(want, have, diff) elif state == 'deleted': commands, requests = self._state_deleted(want, have, diff) elif state == 'merged': commands, requests = self._state_merged(want, have, diff) elif state == 'replaced': commands, requests = self._state_replaced(want, have, diff) return commands, requests
def _state_overridden(self, want, have, diff): """ The command generator when state is overridden :rtype: A list :returns: the commands necessary to migrate the current configuration to the desired configuration """ requests = [] commands = [] commands_del = get_diff(have, want) requests_del = [] if commands_del: requests_del = self.get_delete_vxlan_request(commands_del, have) if requests_del: requests.extend(requests_del) commands_del = update_states(commands_del, "deleted") commands.extend(commands_del) commands_over = diff requests_over = [] if commands_over: requests_over = self.get_create_vxlans_request(commands_over, have) if requests_over: requests.extend(requests_over) commands_over = update_states(commands_over, "overridden") commands.extend(commands_over) return commands, requests
def _state_replaced(self, want, have, diff): """ The command generator when state is replaced :rtype: A list :returns: the commands necessary to migrate the current configuration to the desired configuration """ requests = [] commands = [] commands_del = get_diff(have, want, test_keys) requests_del = [] if commands_del: requests_del = self.get_delete_vxlan_request(commands_del, have) if requests_del: requests.extend(requests_del) commands_del = update_states(commands_del, "deleted") commands.extend(commands_del) commands_rep = diff requests_rep = [] if commands_rep: requests_rep = self.get_create_vxlans_request(commands_rep, have) if requests_rep: requests.extend(requests_rep) commands_rep = update_states(commands_rep, "replaced") commands.extend(commands_rep) return commands, requests
def _state_overridden(self, want, have, diff): """ The command generator when state is overridden :rtype: A list :returns: the commands necessary to migrate the current configuration to the desired configuration """ commands = [] requests = [] commands_del = get_diff(have, want, TEST_KEYS) requests_del = self.get_delete_all_switchport_requests(commands_del) if len(requests_del): requests.extend(requests_del) commands_del = update_states(commands_del, "deleted") commands.extend(commands_del) commands_over = diff requests_over = self.get_create_l2_interface_request(commands_over) if requests_over: requests.extend(requests_over) commands_over = update_states(commands_over, "overridden") commands.extend(commands_over) return commands, requests
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 = [] diff = get_diff(want, have, TEST_KEYS) if diff: diff_members, diff_portchannels = self.diff_list_for_member_creation(diff) else: diff_members = [] diff_portchannels = [] 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, requests = self._state_overridden(want, have, diff_members, diff_portchannels) elif state == 'deleted': commands, requests = self._state_deleted(want, have, diff) elif state == 'merged': commands, requests = self._state_merged(want, have, diff_members, diff_portchannels) elif state == 'replaced': commands, requests = self._state_replaced(want, have, diff_members, diff_portchannels) return commands, requests
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 = [] requests = [] state = self._module.params['state'] diff = get_diff(want, have) # with open('/root/ansible_log.log', 'a+') as fp: # fp.write('comm: want: ' + str(want) + '\n') # fp.write('comm: have: ' + str(have) + '\n') # fp.write('comm: diff: ' + str(diff) + '\n') if state == 'overridden': commands, requests = self._state_overridden(want, have, diff) elif state == 'deleted': commands, requests = self._state_deleted(want, have, diff) elif state == 'merged': commands, requests = self._state_merged(want, have, diff) elif state == 'replaced': commands, requests = self._state_replaced(want, have, diff) return commands, requests
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 = [] requests = [] state = self._module.params['state'] if not want: want = {} diff = get_diff(want, have, TEST_KEYS) if state == 'overridden': commands, requests = self._state_overridden(want, have, diff) elif state == 'deleted': commands, requests = self._state_deleted(want, have, diff) elif state == 'merged': commands, requests = self._state_merged(want, have, diff) elif state == 'replaced': commands, requests = self._state_replaced(want, have, diff) return commands, requests
def _state_replaced(self, want, have, diff): """ The command generator when state is replaced :rtype: A list :returns: the commands necessary to migrate the current configuration to the desired configuration """ ret_requests = list() commands = list() l3_interfaces_to_delete = get_diff(have, want, TEST_KEYS) obj = self.get_object(l3_interfaces_to_delete, want) diff = get_diff(obj, want, TEST_KEYS) if diff: delete_l3_interfaces_requests = self.get_delete_all_requests(want) ret_requests.extend(delete_l3_interfaces_requests) commands.extend(update_states(want, "deleted")) l3_interfaces_to_create_requests = self.get_create_l3_interfaces_requests(want, have, want) ret_requests.extend(l3_interfaces_to_create_requests) commands.extend(update_states(want, "merged")) return commands, ret_requests
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 = [] requests = [] if not want: if have: requests = self.get_delete_all_mclag_domain_request() if len(requests) > 0: commands = update_states(have, "deleted") else: new_have = self.remove_default_entries(have) d_diff = get_diff(want, new_have, TEST_KEYS, is_skeleton=True) diff_want = get_diff(want, d_diff, TEST_KEYS, is_skeleton=True) if diff_want: requests = self.get_delete_mclag_attribute_request(want, diff_want) if len(requests) > 0: commands = update_states(diff_want, "deleted") return commands, requests
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 = [] requests = [] state = self._module.params['state'] if not want: want = [] new_want = [{ 'name': conf['name'], 'role': conf['role'] } for conf in want] new_have = [{ 'name': conf['name'], 'role': conf['role'] } for conf in have] new_diff = get_diff(new_want, new_have) diff = [] for cfg in new_diff: match = next( (w_cfg for w_cfg in want if w_cfg['name'] == cfg['name']), None) if match: diff.append(match) for cfg in want: if cfg['password'] and cfg['update_password'] == 'always': d_match = next( (d_cfg for d_cfg in diff if d_cfg['name'] == cfg['name']), None) if d_match is None: diff.append(cfg) if state == 'overridden': commands, requests = self._state_overridden(want, have, diff) elif state == 'deleted': commands, requests = self._state_deleted(want, have, diff) elif state == 'merged': commands, requests = self._state_merged(want, have, diff) elif state == 'replaced': commands, requests = self._state_replaced(want, have, diff) return commands, requests
def read_and_compare(self, file_name): file_name = os.path.join(os.path.dirname(__file__), file_name) file_stream = open(file_name, "r") data = yaml.full_load(file_stream) file_stream.close() want = data.get('want', []) have = data.get('have', []) diff_exp = data.get('diff', []) test_keys = data.get('test_keys', None) is_skeleton = data.get('skeleton', None) diff_act = get_diff(want, have, test_keys, is_skeleton=is_skeleton) self.assertEqual(diff_exp, diff_act)
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 == 'deleted': commands = self._state_deleted(want, have) elif state == 'merged': diff = get_diff(want, have) commands = self._state_merged(want, have, diff) return commands
def _state_deleted(self, want, have, diff): """ The command generator when state is deleted :rtype: A list :returns: the commands necessary to remove the current configuration of the provided objects """ commands = list() # if want is none, then delete all the vlans if not want: commands = have else: # delete specific vlans commands = get_diff(want, diff, TEST_KEYS) requests = self.get_delete_vlans_requests(commands) commands = update_states(commands, "deleted") return commands, requests
def _state_overridden(self, want, have, diff): """ The command generator when state is overridden :rtype: A list :returns: the commands necessary to migrate the current configuration to the desired configuration """ ret_requests = list() commands = list() vlans_to_delete = get_diff(have, want, TEST_KEYS) if vlans_to_delete: delete_vlans_requests = self.get_delete_vlans_requests(vlans_to_delete) ret_requests.extend(delete_vlans_requests) commands.extend(update_states(vlans_to_delete, "deleted")) if diff: vlans_to_create_requests = self.get_create_vlans_requests(diff) ret_requests.extend(vlans_to_create_requests) commands.extend(update_states(diff, "merged")) return commands, ret_requests
def _state_deleted(self, want, have, diff): """ The command generator when state is deleted :rtype: A list :returns: the commands necessary to remove the current configuration of the provided objects """ commands = list() requests = list() portchannel_requests = list() # if want is none, then delete all the lag interfaces and all portchannels if not want: requests = self.get_delete_all_lag_interfaces_requests() portchannel_requests = self.get_delete_all_portchannel_requests() requests.extend(portchannel_requests) commands.extend(update_states(have, "Deleted")) else: # delete specific lag interfaces and specific portchannels commands = get_diff(want, diff, TEST_KEYS) commands = remove_empties_from_list(commands) want_members, want_portchannels = self.diff_list_for_member_creation(commands) commands, requests = self.template_for_lag_deletion(have, want_members, want_portchannels, "deleted") return commands, requests
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 = [] requests = [] state = self._module.params['state'] for i in want: if i.get('members'): temp = [] for j in i['members']: temp.append(j.replace('\\\\', '\\')) i['members'] = temp diff = get_diff(want, have) for i in want: if i.get('members'): temp = [] for j in i['members']: temp.append(j.replace('\\', '\\\\')) i['members'] = temp # with open('/root/ansible_log.log', 'a+') as fp: # fp.write('as_path_list: want: ' + str(want) + '\n') # fp.write('as_path_list: have: ' + str(have) + '\n') # fp.write('as_path_list: diff: ' + str(diff) + '\n') if state == 'overridden': commands, requests = self._state_overridden(want, have, diff) elif state == 'deleted': commands, requests = self._state_deleted(want, have, diff) elif state == 'merged': commands, requests = self._state_merged(want, have, diff) elif state == 'replaced': commands, requests = self._state_replaced(want, have, diff) return commands, requests
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'] # diff method works on dict, so creating temp dict diff = get_diff(want, have, TEST_KEYS) if state == 'overridden': commands, requests = self._state_overridden(want, have, diff) elif state == 'deleted': commands, requests = self._state_deleted(want, have, diff) elif state == 'merged': commands, requests = self._state_merged(want, have, diff) elif state == 'replaced': commands, requests = self._state_replaced(want, have, diff) ret_commands = remove_empties_from_list(commands) return ret_commands, requests