def delete_anynets_menu(current_anynets, sdk_vars, sdk_session): modifiable_anynets = {} # only do the modifiable ones for key, value in current_anynets.iteritems(): # if modifiable, add to list sub_type = value.get('sub_type', 'other') if sub_type == 'on-demand': modifiable_anynets[key] = value num_anynets = len(modifiable_anynets.keys()) # quick confirm do_we_go = menus.quick_confirm("This command will DELETE {0} current MODIFIABLE VPN Mesh Links.\n" "Are you really really sure?"\ .format(num_anynets), 'N') if do_we_go in ['y']: print "Preparing to DISABLE {0} VPN Mesh Links..".format(num_anynets) counter = 1 pbar = ProgressBar(widgets=[Percentage(), Bar(), ETA()], max_value=num_anynets + 1).start() for anynet_uniqueid, anynet in modifiable_anynets.iteritems(): status = False rest_call_retry = 0 while not status: status, disable_result = delete_anynet_link( anynet['path_id'], sdk_vars=sdk_vars, sdk_session=sdk_session) if not status: print "API request to disable Mesh VPN Link {0} failed/timed out. Retrying."\ .format(anynet['path_id']) rest_call_retry += 1 # have we hit retry limit? # print rest_call_retry # print sdk_vars['rest_call_max_retry'] # print rest_call_retry >= sdk_vars['rest_call_max_retry'] if rest_call_retry >= sdk_vars['rest_call_max_retry']: # Bail out print "ERROR: Could not disable Mesh VPN Link {0}. Continuing.".format( anynet['path_id']) status = True disable_result = False else: # wait and keep going. time.sleep(1) counter += 1 pbar.update(counter) else: print "Canceling..." return do_we_go
def create_anynets_menu(new_anynets, sdk_vars, sdk_session): num_anynets = len(new_anynets.keys()) # quick confirm do_we_go = menus.quick_confirm("This command will create {0} new VPN Mesh Links. \nAre you sure? "\ .format(num_anynets), 'N') if do_we_go in ['y']: print "Preparing to deploy {0} VPN Mesh Links..".format(num_anynets) counter = 1 pbar = ProgressBar(widgets=[Percentage(), Bar(), ETA()], max_value=num_anynets + 1).start() for anynet_uniqueid, anynet in new_anynets.iteritems(): status = False rest_call_retry = 0 while not status: status, create_result = create_anynet_link( anynet['source_site_id'], anynet['source_wan_if_id'], anynet['target_site_id'], anynet['target_wan_if_id'], forced=True, admin_state=True, sdk_vars=sdk_vars, sdk_session=sdk_session) if not status: print "API request to create Mesh VPN Link {0}({1}) <-> {2}({3}) failed/timed out. Retrying."\ .format(anynet['source_site_id'], anynet['source_wan_if_id'], anynet['target_site_id'], anynet['target_wan_if_id']) rest_call_retry += 1 # have we hit retry limit? if rest_call_retry >= sdk_vars['rest_call_max_retry']: # Bail out print "ERROR: Could not create Mesh VPN Link {0}({1}) <-> {2}({3}). Continuing."\ .format(anynet['source_site_id'], anynet['source_wan_if_id'], anynet['target_site_id'], anynet['target_wan_if_id']) status = True create_result = False else: # wait and keep going. time.sleep(1) counter += 1 pbar.update(counter) else: print "Canceling..." return do_we_go
def disable_anynets_menu(current_anynets, sdk_vars, sdk_session): num_anynets = len(current_anynets.keys()) # quick confirm do_we_go = menus.quick_confirm("***THIS COMMAND CAN TAKE DOWN BRANCH->DC VPNS***\n" "This command will Admin Disable {0} current VPN Mesh Links.\n" "Are you really really sure? "\ .format(num_anynets), 'N') if do_we_go in ['y']: print "Preparing to DISABLE {0} VPN Mesh Links..".format(num_anynets) counter = 1 pbar = ProgressBar(widgets=[Percentage(), Bar(), ETA()], max_value=num_anynets + 1).start() for anynet_uniqueid, anynet in current_anynets.iteritems(): status = False rest_call_retry = 0 while not status: status, disable_result = update_anynet_link( anynet['path_id'], admin_state=False, sdk_vars=sdk_vars, sdk_session=sdk_session) if not status: print "API request to disable Mesh VPN Link {0} failed/timed out. Retrying."\ .format(anynet['path_id']) rest_call_retry += 1 # have we hit retry limit? # print rest_call_retry # print sdk_vars['rest_call_max_retry'] # print rest_call_retry >= sdk_vars['rest_call_max_retry'] if rest_call_retry >= sdk_vars['rest_call_max_retry']: # Bail out print "ERROR: Could not disable Mesh VPN Link {0}. Continuing.".format( anynet['path_id']) status = True disable_result = False else: # wait and keep going. time.sleep(1) counter += 1 pbar.update(counter) else: print "Canceling..." return do_we_go
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 save_to_csv(new_anynets, current_anynets, tenantid, id_sitename_dict, swi_to_wn_dict, id_wan_network_name_dict, site_id_to_role, mesh_type): """ Save mesh_map.csv file :param new_anynets: list of anynet objects that are not created :param current_anynets: list of anynet objects that are currently created. :param tenantid: Customer tenant-ID :param id_sitename_dict: xlation dict of site id to site name format { '<siteid>': 'Site Name' } :param swi_to_wn_dict: xlation dict of swi id to wan network id format { '<swi_id>': '<wn_id>' } :param id_wan_network_name_dict: xlation dict of wan net id to wan net name format { '<wn_id>': 'WAN Network Name' } :return: empty """ default_filename = 'mesh_info.csv' type_xlate = { 'always-on': 'Always On', # always-on is auto in 4.4.1+ 'auto': 'Always On', 'on-demand': 'Modifiable' } role_xlate = {'HUB': 'DC', 'SPOKE': 'Branch'} cwd = os.getcwd() 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: write_list = [ 'Mesh Type,Site 1,Site 1 Role,Site 1 WAN Network,Site 2,Site 2 Role,Site 2 WAN Network,Status,' 'Admin State,VPN Type,Site 1 ID,Site 1 WAN Interface ID,Site 1 WAN Network ID,Site 2 ID,' 'Site 2 WAN Interface ID,Site 2 WAN Network ID,Path ID' ] for key, value in current_anynets.iteritems(): adminstate_test = value.get('admin_up') if adminstate_test == None: admin_state = "N/A" elif adminstate_test == True: admin_state = "Enabled" else: admin_state = "Disabled" path_id = value.get('path_id') write_list.append( '"{0}","{1}","{2}","{3}","{4}","{5}","{6}","{7}","{8}"' ',"{9}",="{10}",="{11}",="{12}",="{13}",="{14}",="{15}",="{16}"' .format( mesh_name(mesh_type), id_sitename_dict.get(value['source_site_id'], 'UNKNOWN'), role_xlate.get( site_id_to_role.get(value['source_site_id'], 'Other'), 'Other'), id_wan_network_name_dict.get( swi_to_wn_dict.get(value['source_wan_if_id'], 'UNKNOWN'), 'UNKNOWN'), id_sitename_dict.get(value['target_site_id'], 'UNKNOWN'), role_xlate.get( site_id_to_role.get(value['target_site_id'], 'Other'), 'Other'), id_wan_network_name_dict.get( swi_to_wn_dict.get(value['target_wan_if_id'], 'UNKNOWN'), 'UNKNOWN'), value['status'], admin_state, type_xlate.get(value['sub_type'], 'other'), value['source_site_id'], value['source_wan_if_id'], swi_to_wn_dict.get(value['source_wan_if_id'], 'UNKNOWN'), value['target_site_id'], value['target_wan_if_id'], swi_to_wn_dict.get(value['target_wan_if_id'], 'UNKNOWN'), value['path_id'])) for key, value in new_anynets.iteritems(): admin_state = "N/A" path_id = "N/A" write_list.append( '"{0}","{1}","{2}","{3}","{4}","{5}","{6}","{7}","{8}"' ',"{9}",="{10}",="{11}",="{12}",="{13}",="{14}",="{15}",="{16}"' .format( mesh_name(mesh_type), id_sitename_dict.get(value['source_site_id'], 'UNKNOWN'), role_xlate.get( site_id_to_role.get(value['source_site_id'], 'Other'), 'Other'), id_wan_network_name_dict.get( swi_to_wn_dict.get(value['source_wan_if_id'], 'UNKNOWN'), 'UNKNOWN'), id_sitename_dict.get(value['target_site_id'], 'UNKNOWN'), role_xlate.get( site_id_to_role.get(value['target_site_id'], 'Other'), 'Other'), id_wan_network_name_dict.get( swi_to_wn_dict.get(value['target_wan_if_id'], 'UNKNOWN'), 'UNKNOWN'), value['status'], admin_state, type_xlate.get('on-demand', 'other'), value['source_site_id'], value['source_wan_if_id'], swi_to_wn_dict.get(value['source_wan_if_id'], 'UNKNOWN'), value['target_site_id'], value['target_wan_if_id'], swi_to_wn_dict.get(value['target_wan_if_id'], 'UNKNOWN'), path_id)) try: with open(filename, 'w') as outfile: outfile.write("\n".join(write_list)) print "\n Successfully save {0} entries out to {1}.".format( len(write_list), filename) except (ValueError, IOError) as e: print "ERROR, could not save {0}: {1}.".format(filename, e) return
def save_script_to_text(new_anynets, current_anynets, tenantid, mesh_type, id_sitename_dict, site_id_to_role, id_wan_network_name_dict, swi_to_wn_dict): """ Save mesh_map.txt file :param new_anynets: list of anynet objects that are not created :param current_anynets: list of anynet objects that are currently created. :param tenantid: Customer tenant-ID :return: empty """ role_xlate = {'HUB': 'DC', 'SPOKE': 'Branch'} type_xlate = { 'always-on': 'Always On', # always-on is auto in 4.4.1+ 'auto': 'Always On', 'on-demand': 'Modifiable' } default_filename = 'mesh_map.txt' cwd = os.getcwd() writefile = False print "This menu will write a .txt file with create/delete statements for new/existing Mesh links." 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: write_list = [ '# ' + mesh_name(mesh_type) + ' Create Commands (All new possible "MODIFIABLE" VPNs):' ] for key, value in new_anynets.iteritems(): anynet_text = "[{1}] {0} ({2}) <-> [{4}] {3} ({5}) - {6}" \ .format(id_sitename_dict.get(value['source_site_id'], 'UNKNOWN'), role_xlate.get(site_id_to_role.get(value['source_site_id'], 'Other'), 'Other'), id_wan_network_name_dict.get(swi_to_wn_dict.get(value['source_wan_if_id'], 'UNKNOWN'), 'UNKNOWN'), id_sitename_dict.get(value['target_site_id'], 'UNKNOWN'), role_xlate.get(site_id_to_role.get(value['target_site_id'], 'Other'), 'Other'), id_wan_network_name_dict.get(swi_to_wn_dict.get(value['target_wan_if_id'], 'UNKNOWN'), 'UNKNOWN'), 'New') write_list.append(' # ' + anynet_text) write_list.append( ' net create --tenant-id {0} --spoke-site1 {1} --wan-if1 {2} --spoke-site2 {3}' ' --wan-if2 {4} --force'.format(tenantid, value['source_site_id'], value['source_wan_if_id'], value['target_site_id'], value['target_wan_if_id'])) write_list.append('') write_list.append( '# ' + mesh_name(mesh_type) + ' Disable Commands (for all existing VPNs, if desired):') for key, value in current_anynets.iteritems(): anynet_text = "[{1}] {0} ({2}) <-> [{4}] {3} ({5}) - {6}" \ .format(id_sitename_dict.get(value['source_site_id'], 'UNKNOWN'), role_xlate.get(site_id_to_role.get(value['source_site_id'], 'Other'), 'Other'), id_wan_network_name_dict.get(swi_to_wn_dict.get(value['source_wan_if_id'], 'UNKNOWN'), 'UNKNOWN'), id_sitename_dict.get(value['target_site_id'], 'UNKNOWN'), role_xlate.get(site_id_to_role.get(value['target_site_id'], 'Other'), 'Other'), id_wan_network_name_dict.get(swi_to_wn_dict.get(value['target_wan_if_id'], 'UNKNOWN'), 'UNKNOWN'), type_xlate.get(value['sub_type'], 'other')) write_list.append(' # ' + anynet_text) write_list.append( ' net update --tenant-id {0} --anynet-id {1} --admin-state false' .format(tenantid, value['path_id'])) write_list.append('') write_list.append( '# ' + mesh_name(mesh_type) + ' Enable Commands (for all existing VPNs, if desired):') for key, value in current_anynets.iteritems(): anynet_text = "[{1}] {0} ({2}) <-> [{4}] {3} ({5}) - {6}" \ .format(id_sitename_dict.get(value['source_site_id'], 'UNKNOWN'), role_xlate.get(site_id_to_role.get(value['source_site_id'], 'Other'), 'Other'), id_wan_network_name_dict.get(swi_to_wn_dict.get(value['source_wan_if_id'], 'UNKNOWN'), 'UNKNOWN'), id_sitename_dict.get(value['target_site_id'], 'UNKNOWN'), role_xlate.get(site_id_to_role.get(value['target_site_id'], 'Other'), 'Other'), id_wan_network_name_dict.get(swi_to_wn_dict.get(value['target_wan_if_id'], 'UNKNOWN'), 'UNKNOWN'), type_xlate.get(value['sub_type'], 'other')) write_list.append(' # ' + anynet_text) write_list.append( ' net update --tenant-id {0} --anynet-id {1} --admin-state true' .format(tenantid, value['path_id'])) write_list.append('') write_list.append( '# ' + mesh_name(mesh_type) + ' Delete Commands (for existing "MODIFIABLE" VPNs, if desired):') for key, value in current_anynets.iteritems(): if value['sub_type'] not in ["always-on", "auto"]: anynet_text = "[{1}] {0} ({2}) <-> [{4}] {3} ({5}) - {6}" \ .format(id_sitename_dict.get(value['source_site_id'], 'UNKNOWN'), role_xlate.get(site_id_to_role.get(value['source_site_id'], 'Other'), 'Other'), id_wan_network_name_dict.get(swi_to_wn_dict.get(value['source_wan_if_id'], 'UNKNOWN'), 'UNKNOWN'), id_sitename_dict.get(value['target_site_id'], 'UNKNOWN'), role_xlate.get(site_id_to_role.get(value['target_site_id'], 'Other'), 'Other'), id_wan_network_name_dict.get(swi_to_wn_dict.get(value['target_wan_if_id'], 'UNKNOWN'), 'UNKNOWN'), type_xlate.get(value['sub_type'], 'other')) write_list.append(' # ' + anynet_text) write_list.append( ' net delete --tenant-id {0} --anynet-id {1}'.format( tenantid, value['path_id'])) try: with open(filename, 'w') as outfile: outfile.write("\n".join(write_list)) print "\n Successfully save {0} entries out to {1}.".format( len(write_list), filename) except (ValueError, IOError) as e: print "ERROR, could not save {0}: {1}.".format(filename, e) return