def remove_from_list(item_list, list_name, all_values): """ Remove items from list :param item_list: Item list to save :param list_name: Name of List :param all_values: All values :return: shallow copy of item_list. :return: """ return_list = [] loop = True while loop: action = [("Remove all", 'removeall'), ("Remove via pattern / regex", 'removere'), ("Go Back", 'back')] banner = "\nSelect Action:" line_fmt = "{0}: {1}" # just pull 2nd value selected_action = menus.quick_menu(banner, line_fmt, action)[1] if selected_action == 'removeall': print "\nRemoving all {1} WAN Network(s) from {0}.".format( list_name, len(item_list)) # shallow copy list. item_list = [] loop = False elif selected_action == 'removere': regex_pattern = menus.quick_str_input( "Enter regular expression pattern", '^.*$') try: temp_item_list = re_pick(all_values, regex_pattern) orig_size = len(item_list) print "\nAttempting to remove {0} items from {1} (if they exist).".format( len(temp_item_list), list_name) item_list = [x for x in item_list if x not in temp_item_list] removed_items = orig_size - len(item_list) if removed_items > 0: print "Actually removed {0} items.".format(removed_items) else: print "\nNo items matched, list unchanged." loop = False except re.error as e: print "\nERROR: Invalid regular expression / pattern: {0}.".format( e) elif selected_action == 'back': loop = False else: sys.exit() # return a shallow copy of site list return item_list[:]
def add_to_list(item_list, list_name, all_values): """ Add to list :param item_list: list of values :param list_name: name of list :param all_values: all possible list values in item_list :return: shallow copy of item_list. """ return_list = [] loop = True while loop: action = [("Add all", 'addall'), ("Add via pattern / regex", 'addre'), ("Go Back", 'back')] banner = "\nSelect Action:" line_fmt = "{0}: {1}" # just pull 2nd value selected_action = menus.quick_menu(banner, line_fmt, action)[1] if selected_action == 'addall': print "\nAdding all {1} site(s) to {0}.".format( list_name, len(all_values)) # shallow copy list. item_list = all_values[:] loop = False elif selected_action == 'addre': regex_pattern = menus.quick_str_input( "Enter regular expression pattern", '^.*$') try: temp_item_list = re_pick(all_values, regex_pattern) # shallow copy dict to item_list print "\nAdding {0} items to {1}.".format( len(temp_item_list), list_name) item_list += temp_item_list[:] loop = False except re.error as e: print "\nERROR: Invalid regular expression / pattern: {0}.".format( e) elif selected_action == 'back': loop = False else: sys.exit() # return a shallow copy of site list return item_list[:]
def edit_site_list(item_list, list_name, all_values, tenant_file_name): """ main site list edit menu :param item_list: list of values :param list_name: name of list :param all_values: all possible list values in item_list :param tenant_file_name: file compatable tenant name :return: shallow copy of item_list. """ loop = True while loop: action = [("View list", 'view'), ("Add to list", 'add'), ("Remove items from list", 'remove'), ("Load/Save list", 'file'), ("Go Back", 'back')] banner = "\nSelect Action:" line_fmt = "{0}: {1}" # just pull 2nd value selected_action = menus.quick_menu(banner, line_fmt, action)[1] if selected_action == 'view': print "\n{0} ({1} entries):".format(list_name, len(item_list)) for item in item_list: print "\t{0}".format(item) elif selected_action == 'add': item_list = add_to_list(item_list, list_name, all_values) elif selected_action == 'remove': item_list = remove_from_list(item_list, list_name, all_values) elif selected_action == 'file': item_list = load_save_list(item_list, list_name, all_values, tenant_file_name) elif selected_action == 'back': loop = False else: sys.exit() # return a shallow copy of site list return item_list[:]
def edit_wn_list(item_list, list_name, all_values, tenant_file_name): """ Edit WAN network list :param item_list: Item list to save :param list_name: Name of List :param all_values: All values :param tenant_file_name: File-system friendly tenant_name :return: shallow copy of item_list. """ loop = True while loop: action = [("View list", 'view'), ("Add to list", 'add'), ("Remove items from list", 'remove'), ("Load/Save list", 'file'), ("Go Back", 'back')] banner = "\nSelect Action:" line_fmt = "{0}: {1}" # just pull 2nd value selected_action = menus.quick_menu(banner, line_fmt, action)[1] if selected_action == 'view': print "\n{0} ({1} entries):".format(list_name, len(item_list)) for item in item_list: print "\t{0}".format(item) elif selected_action == 'add': item_list = add_to_list(item_list, list_name, all_values) elif selected_action == 'remove': item_list = remove_from_list(item_list, list_name, all_values) elif selected_action == 'file': item_list = load_save_list(item_list, list_name, all_values, tenant_file_name) elif selected_action == 'back': loop = False else: sys.exit() # return a shallow copy of site list return item_list[:]
def main_anynet_menu(new_anynets, current_anynets, site_swi_all_dict, swi_to_wn_dict, wn_to_swi_dict, id_wan_network_name_dict, wan_network_name_id_dict, swi_to_site_dict, id_sitename_dict, mesh_type, site_id_to_role_dict, sdk_vars, sdk_session): """ Main menu for anynet manipulation :param new_anynets: List of new anynet objects that can be created. :param current_anynets: List of current anynet objects :param site_swi_all_dict: Site-SWI dict for ALL sites selected. :param swi_to_wn_dict: xlation dict for SWI to Wan Network ID :param wn_to_swi_dict: xlation dict for WAN Network ID to SWI :param id_wan_network_name_dict: xlation dict for WAN Network ID to WAN Network Name :param wan_network_name_id_dict: xlation dict for WAN Network Name to WAN Network ID :param swi_to_site_dict: xlation dict for SWI to Site ID :param id_sitename_dict: xlation dict for site ID to site Name :param mesh_type: 'internet-stub' or 'priv-wan-stub' :param site_id_to_role_dict: site ID to Site Role text. :param tenantid: tenant ID :return: tuple with: action (string or true/false) site_a_action_dict (Site-SWI dict for list A) site_b_action_dict (Site-SWI dict for list B) """ tenantid = sdk_session.tenant_id current_anynet_text_list, new_anynet_text_list = calculate_anynet_links( current_anynets, new_anynets, id_sitename_dict, swi_to_wn_dict, id_wan_network_name_dict, site_id_to_role_dict) logger.debug("CURRENT_MESH ({0}): {1}".format( len(current_anynet_text_list), json.dumps(current_anynet_text_list, indent=4))) logger.debug("NEW_MESH ({0}): {1}".format( len(new_anynet_text_list), json.dumps(new_anynet_text_list, indent=4))) loop = True reload_main_menu = False links_modified = False while loop: # Print header if not links_modified: print "" print_selection_overview(current_anynet_text_list, "Matching Current Links") print "" print_selection_overview(new_anynet_text_list, "\"New\" links to finish Mesh") print "" else: print "" print "s" print "\tLinks have been modified. Please \"Refresh\" (option 7) to return to" print "\t previous menus and update stats on this page." print "" action = [ ("View Matching Current Links", 'view_c'), ("View New Links", 'view_n'), ("Admin Disable All Matching Current Links (4.4.1+)", 'disable_c'), ("Admin Enable All Matching Current Links (4.4.1+)", 'enable_c'), ("Delete All Matching Current Modifiable Links", 'delete_c'), ("Create All New Links", 'create_n'), ("Refresh Link Status (reload main menu)", 'reload'), ("Quit", 'quit') ] banner = "Select Action:" line_fmt = "{0}: {1}" # just pull 2nd value list_name, selected_action = menus.quick_menu(banner, line_fmt, action) if selected_action == 'view_c': print "\n{0} ({1} entries):".format("Matching Current Links:", len(current_anynet_text_list)) for item in current_anynet_text_list: print "\t{0}".format(item) elif selected_action == 'view_n': print "\n{0} ({1} entries):".format("New Links:", len(new_anynet_text_list)) for item in new_anynet_text_list: print "\t{0}".format(item) elif selected_action == 'disable_c': result = disable_anynets_menu(current_anynets, sdk_vars, sdk_session) # set links modified bit if operation was attempted. if result: links_modified = True elif selected_action == 'enable_c': result = enable_anynets_menu(current_anynets, sdk_vars, sdk_session) # set links modified bit if operation was attempted. if result: links_modified = True elif selected_action == 'delete_c': result = delete_anynets_menu(current_anynets, sdk_vars, sdk_session) # set links modified bit if operation was attempted. if result: links_modified = True elif selected_action == 'create_n': result = create_anynets_menu(new_anynets, sdk_vars, sdk_session) # set links modified bit if operation was attempted. if result: links_modified = True elif selected_action == 'reload': reload_main_menu = True loop = False else: sdk_session.interactive.logout() sys.exit() return reload_main_menu
def load_save_list(item_list, list_name, all_values, tenant_file_name): """ Load/save list JSON :param item_list: list of values :param list_name: name of list :param all_values: all possible list values in item_list :return: shallow copy of item_list. """ return_list = [] loop = True while loop: action = [("Load List", 'load'), ("Save List", 'save'), ("Go Back", 'back')] banner = "\nSelect Action:" line_fmt = "{0}: {1}" # just pull 2nd value selected_action = menus.quick_menu(banner, line_fmt, action)[1] default_filename = tenant_file_name + "_" + list_name.replace( " ", "_").lower() + ".json" cwd = os.getcwd() if selected_action == 'load': print "Current directory is {0}".format(cwd) filename = menus.quick_str_input("Enter file name to load", default_filename) try: with open(filename) as data_file: data = json.load(data_file) item_list = data[:] print "\n Successfully loaded {0} entries from {1}.".format( len(data), filename) loop = False except (ValueError, IOError) as e: print "ERROR, could not load {0}: {1}.".format(filename, e) elif selected_action == 'save': writefile = False print "Current directory is {0}".format(cwd) filename = menus.quick_str_input("Enter file name to save", default_filename) # check if exists. if os.path.exists(filename): if menus.quick_confirm("File exists, overwrite? ", "N") == 'y': writefile = True else: writefile = False else: writefile = True if writefile: try: with open(filename, 'w') as outfile: json.dump(item_list, outfile, indent=4) print "\n Successfully save {0} entries out to {1}.".format( len(item_list), filename) loop = False except (ValueError, IOError) as e: print "ERROR, could not save {0}: {1}.".format(filename, e) elif selected_action == 'back': loop = False else: sys.exit() # return a shallow copy of site list return item_list[:]
def main_vpn_menu(siteid_list_a, siteid_list_b, all_anynets, site_swi_all_dict, swi_to_wn_dict, wn_to_swi_dict, id_wan_network_name_dict, wan_network_name_id_dict, swi_to_site_dict, id_sitename_dict, mesh_type, site_id_to_role_dict, sdk_vars, sdk_session): """ Main menu for VPN manipulation :param siteid_list_a: List A of site IDs :param siteid_list_b: List B of site IDs :param current_anynets: List of current anynet objects :param site_swi_all_dict: Site-SWI dict for ALL sites selected. :param swi_to_wn_dict: xlation dict for SWI to Wan Network ID :param wn_to_swi_dict: xlation dict for WAN Network ID to SWI :param id_wan_network_name_dict: xlation dict for WAN Network ID to WAN Network Name :param wan_network_name_id_dict: xlation dict for WAN Network Name to WAN Network ID :param swi_to_site_dict: xlation dict for SWI to Site ID :param id_sitename_dict: xlation dict for site ID to site Name :param mesh_type: 'internet-stub' or 'priv-wan-stub' :param site_id_to_role_dict: site ID to Site Role text. :param sdk_vars: SDK global value dictionary :return: tuple with: action (string or true/false) site_a_action_dict (Site-SWI dict for list A) site_b_action_dict (Site-SWI dict for list B) """ tenantid = sdk_session.tenant_id action = False site_a_action_dict = {} site_b_action_dict = {} logger.debug("ID_WN: {0}".format( json.dumps(id_wan_network_name_dict, indent=4))) # Build {siteid: [swia, swib]} dict based on siteid lists. site_a_swi_dict, site_b_swi_dict = site_swi_dicts(siteid_list_a, siteid_list_b, site_swi_all_dict) # get a unique WAN Network name list for each site-swi dict. site_a_wan_networks = get_unique_wan_networks_from_swi_dict( site_a_swi_dict, swi_to_wn_dict, id_wan_network_name_dict) site_b_wan_networks = get_unique_wan_networks_from_swi_dict( site_b_swi_dict, swi_to_wn_dict, id_wan_network_name_dict) # discover new anynets needed to complete mesh, and calculate statistics. new_anynets, current_anynets, statistics = calculate_vpn_links( site_a_swi_dict, site_b_swi_dict, all_anynets, swi_to_site_dict, site_id_to_role_dict) # save original dicts/lists original_site_a_swi_dict = copy.deepcopy(site_a_swi_dict) original_site_b_swi_dict = copy.deepcopy(site_b_swi_dict) original_site_a_wan_networks = site_a_wan_networks[:] original_site_b_wan_networks = site_b_wan_networks[:] logger.debug("SITE A SWI ({0}): {1}".format( len(site_a_swi_dict), json.dumps(site_a_swi_dict, indent=4))) logger.debug("SITE B SWI ({0}): {1}".format( len(site_b_swi_dict), json.dumps(site_b_swi_dict, indent=4))) logger.debug("SITE A WN ({0}): {1}".format( len(site_a_wan_networks), json.dumps(site_a_wan_networks, indent=4))) logger.debug("SITE B WN ({0}): {1}".format( len(site_b_wan_networks), json.dumps(site_b_wan_networks, indent=4))) logger.debug("NEW AN ({0}): {1}".format(len(new_anynets), json.dumps(new_anynets, indent=4))) logger.debug("CURRENT AN ({0}): {1}".format( len(all_anynets), json.dumps(all_anynets, indent=4))) logger.debug("STATS ({0}): {1}".format(len(statistics), json.dumps(statistics, indent=4))) logger.debug("WN to SWI ({0}): {1}".format( len(wn_to_swi_dict.keys()), json.dumps(wn_to_swi_dict, indent=4))) if sdk_vars["reload_wn_list_a"]: # re-loop, or initial values - pull previous list out of sdk_vars dict. matching_wan_nets = [ x for x in site_a_wan_networks if x in sdk_vars["reload_wn_list_a"] ] # if any match filter - otherwise leave all. if matching_wan_nets: site_a_wan_networks = matching_wan_nets if sdk_vars["reload_wn_list_b"]: # re-loop, or initial values - pull previous list out of sdk_vars dict. matching_wan_nets = [ x for x in site_b_wan_networks if x in sdk_vars["reload_wn_list_b"] ] # if any match filter - otherwise leave all. if matching_wan_nets: site_b_wan_networks = matching_wan_nets loop = True while loop: # Print header print "" print_selection_overview(statistics, site_a_wan_networks, site_b_wan_networks) print "" action = [ ("Edit WAN Networks in List A", 'edit_wna'), ("Edit WAN Networks in List B", 'edit_wnb'), ("Save detailed mesh info to CSV", 'savecsv'), ("Save mesh create/delete info to TXT", 'savetxt'), ("View and Directly modify VPN Mesh Links that match the above WAN Networks", 'godoit') ] banner = "Select Action:" line_fmt = "{0}: {1}" # just pull 2nd value list_name, selected_action = menus.quick_menu(banner, line_fmt, action) if selected_action == 'edit_wna': # edit WAN network list site_a_wan_networks = edit_wn_list(site_a_wan_networks, "WAN Networks A", original_site_a_wan_networks, sdk_vars["tenant_str"]) # recalculate site-swi info based on WAN Network edits site_a_swi_dict, site_b_swi_dict = update_calculations( site_a_wan_networks, site_b_wan_networks, original_site_a_swi_dict, original_site_b_swi_dict, wn_to_swi_dict, wan_network_name_id_dict) # recalculate anynet topo changes. new_anynets, current_anynets, statistics = calculate_vpn_links( site_a_swi_dict, site_b_swi_dict, all_anynets, swi_to_site_dict, site_id_to_role_dict) elif selected_action == 'edit_wnb': # edit WAN network list site_b_wan_networks = edit_wn_list(site_b_wan_networks, "WAN Networks B", original_site_b_wan_networks, sdk_vars["tenant_str"]) # recalculate site-swi info based on WAN Network edits site_a_swi_dict, site_b_swi_dict = update_calculations( site_a_wan_networks, site_b_wan_networks, original_site_a_swi_dict, original_site_b_swi_dict, wn_to_swi_dict, wan_network_name_id_dict) # recalculate anynet topo changes. new_anynets, current_anynets, statistics = calculate_vpn_links( site_a_swi_dict, site_b_swi_dict, all_anynets, swi_to_site_dict, site_id_to_role_dict) elif selected_action == 'savecsv': save_to_csv(new_anynets, current_anynets, tenantid, id_sitename_dict, swi_to_wn_dict, id_wan_network_name_dict, site_id_to_role_dict, mesh_type) elif selected_action == 'savetxt': save_script_to_text(new_anynets, current_anynets, tenantid, mesh_type, id_sitename_dict, site_id_to_role_dict, id_wan_network_name_dict, swi_to_wn_dict) elif selected_action == "godoit": if (len(site_a_wan_networks) < 1) or (len(site_b_wan_networks) < 1): print "\nERROR, must select at least one WAN Network in each list." else: # Good to go, continue. loop = False else: sys.exit() # return the new anynet objects # save data for future loops sdk_vars["reload_wn_list_a"] = site_a_wan_networks sdk_vars["reload_wn_list_b"] = site_b_wan_networks return new_anynets, current_anynets