def zone_name_members_func(session, inputs, usage, func): # comment zone helper to execute & commit # name, and member based operations if "name" not in inputs: print("*** missing input: name") pyfos_auth.logout(session) brcd_util.full_usage(usage) sys.exit() name = inputs["name"] if "members" not in inputs: print("*** missing input: members") pyfos_auth.logout(session) brcd_util.full_usage(usage) sys.exit() members = inputs["members"] current_effective = pyfos_zone.effective_configuration.get(session) print("executing", func.__name__) result = func(session, name, members) if pyfos_util.is_failed_resp(result): pyfos_util.response_print(result) print(func.__name__, "failed. Aborting transaction.") result = cfgabort.cfgabort(session) pyfos_util.response_print(result) else: pyfos_util.response_print(result) print(func.__name__, "succeeded. Saving transaction.") result = cfgsave.cfgsave(session, current_effective.peek_checksum()) if pyfos_util.is_failed_resp(result): pyfos_util.response_print(result) print(func.__name__, "failed. Aborting transaction.") result = cfgabort.cfgabort(session) pyfos_util.response_print(result)
def dumptofile(self, session, dumpfile, fmtfile=None, fmtobj=None): """Dump the switch configuration to a persistent file.""" print("Dump configuration Start") self.log(1, "Dump configuration Start") if fmtfile is None: fmtfile = self.fmtfile if fmtobj is None: fmtobj = self.fmtobj ret = blade.get(session) ret = fibrechannel_logical_switch.get(session) if utils.is_failed_resp(ret): # confirm ls is not supported for platform like AG and Awing fid = "None" self.dumpformatfid(session, fid, dumpfile, fmtfile, fmtobj) if fid not in self.dumpfids: self.dumpfids.append(fid) else: if not isinstance(ret, list): ret = [ret] lslist = ret for i in range(len(lslist)): fid = str(lslist[i].peek_fabric_id()) self.dumpformatfid(session, fid, dumpfile, fmtfile, fmtobj) if fid not in self.dumpfids: self.dumpfids.append(fid) print("Dump configuration Completed") self.log(1, "Dump configuration Completed")
def wait_diag_completion(session, name): count = 0 print("wait for the test to complete") time.sleep(5) diag_info = pyfos_diag.fibrechannel_diagnostics.get(session, name) if pyfos_util.is_failed_resp(diag_info): print("Cannot validate test result") pyfos_util.response_print(diag_info) return diag_state = diag_info.peek_state() if diag_info.peek_state() == "PASSED": print("D-Port test completed") pyfos_util.response_print(diag_info) return while ("IN PROGRESS" in diag_state or "NOT STARTED" in diag_state or "RESPONDER" in diag_state or "STOPPED" in diag_state): print(".") count += 1 time.sleep(5) diag_info = pyfos_diag.fibrechannel_diagnostics.get(session, name) if diag_info.peek_state() == "PASSED": print("D-Port test completed") pyfos_util.response_print(diag_info) return elif count > 12: break print("D-Port test failed") pyfos_util.response_print(diag_info)
def dumptofidclasses(self, wb, session, fmtobj): """ Dump the switch configuration object in .xlsx file based on format. """ containerlist = [] for i in range(len(self.clslist)): cls = self.clslist[i] if not clsmanager.isConfigClass(cls, session): # print("skipping non config class", self.clslist[i]) continue ret = self.clslist[i].get(session) if utils.is_failed_resp(ret): # print(ret) continue if not isinstance(ret, list): ret = [ret] container = clsmanager.getContainerfromCls(cls) containerlist += [container] sheetdict = clsmanager.getmultiplesheetdict(container) if sheetdict is None or fmtobj == 'json': ws = wb.create_sheet(container) self.dumplist = ret listlen = len(self.dumplist) if fmtobj == 'json': chunk = self.chunkcount cols = self.columncount loopcount = int(listlen / chunk) + 1 if chunk > 1 and int(listlen % chunk) > 0: loopcount += 1 endindex = 0 for j in range(loopcount): startindex = endindex endindex = startindex + chunk r = int(2 + (j / cols)) c = int(2 + (j % cols)) cell = ws.cell(row=r, column=c) if loopcount >= j + 1: cell.value = str(self.dumplist[startindex:endindex]) else: cell.value = str(self.dumplist[startindex:]) else: baserow = 5 addcount = 0 for j in range(len(self.dumplist)): obj = self.dumplist[j] if sheetdict is None: if j == 0: obj.dumpheaders(ws, baserow - 1) count = obj.dumpdata(ws, baserow + addcount) addcount += count else: for k, v in sheetdict.items(): # print(k, v) ws = wb.create_sheet(container + "." + k) if j == 0: obj.dumpheaders(ws, baserow - 1, v) count = obj.dumpdata(ws, baserow + addcount, v) # cell = ws.cell(row=1, column=1) # cell.value = listlen self.dumpcontainers = containerlist
def main(argv): inputs = brcd_util.generic_input(argv, usage) session = pyfos_auth.login(inputs["login"], inputs["password"], inputs["ipaddr"], isHttps) if pyfos_auth.is_failed_login(session): print("login failed because", session.get(pyfos_auth.CREDENTIAL_KEY) [pyfos_auth.LOGIN_ERROR_KEY]) usage() sys.exit() brcd_util.exit_register(session) vfid = None if 'vfid' in inputs: vfid = inputs['vfid'] if vfid is not None: pyfos_auth.vfid_set(session, vfid) current_effective = pyfos_zone.effective_configuration.get(session) result = cfgclear(session) if pyfos_util.is_failed_resp(result): pyfos_util.response_print(result) else: pyfos_util.response_print(result) result = cfgsave.cfgsave(session, current_effective.peek_checksum()) pyfos_util.response_print(result) pyfos_auth.logout(session)
def loadcurrent(self, session, container=None): """Load the current switch configuration.""" ret = blade.get(session) self.currentslots = clsmanager.getbladeslots(ret) ret = fibrechannel_logical_switch.get(session) if utils.is_failed_resp(ret): # confirm ls is not supported for platform like AG and Awing fid = "None" if fid not in self.currentfids: self.currentfids.append(fid) self.loadcurrentfid(session, fid, container) else: if not isinstance(ret, list): ret = [ret] lslist = ret oldfid = session['vfid'] for i in range(len(lslist)): fid = str(lslist[i].peek_fabric_id()) # print (fid, type(fid)) if fid != "None": session['vfid'] = fid self.loadcurrentfid(session, fid, container) if fid not in self.currentfids: self.currentfids.append(fid) session['vfid'] = oldfid # print(self.currentdict) self.log(1, self.currentdict)
def zone_name_members_pmembers_func(session, inputs, usage, func): # comment zone helper to execute & commit # name, member, and principal member based operations if "name" not in inputs: pyfos_auth.logout(session) usage() sys.exit() name = inputs["name"] if "members" not in inputs: members = None else: members = inputs["members"] if "pmembers" not in inputs: pmembers = None else: pmembers = inputs["pmembers"] if members is None and pmembers is None: pyfos_auth.logout(session) usage() sys.exit() current_effective = pyfos_zone.effective_configuration.get(session) result = func(session, name, members, pmembers) if pyfos_util.is_failed_resp(result): pyfos_util.response_print(result) else: pyfos_util.response_print(result) result = cfgsave.cfgsave(session, current_effective.peek_checksum()) pyfos_util.response_print(result)
def loadcurrentfid(self, session, fid, container): """Load the current switch configuration per fid.""" print("Loading Switch configuration Start[", fid, "].") self.log(1, "Loading Switch configuration Start[", fid, "].") tmpdict = dict() for i in range(len(self.clslist)): if not clsmanager.isConfigClass(self.clslist[i], session): continue if container is not None and \ container != self.clslist[i]().getcontainer(): continue if any(self.configlist) and\ self.clslist[i]().getcontainer() not in self.configlist: continue ret = self.clslist[i].get(session) if utils.is_failed_resp(ret): # print(ret) tmpdict.update(dict({self.clslist[i]().getcontainer(): list()})) continue if not isinstance(ret, list): ret = [ret] tmpdict.update(dict({self.clslist[i]().getcontainer(): ret})) self.currentdict.update(dict({fid: tmpdict})) print("Loading Switch configuration Complete.") self.log(1, "Loading Switch configuration Complete.")
def _get_firmwaredownload_status(session, ss_req_obj, ss_rsp_obj): status = show_status(ss_rsp_obj) ss_state = status.peek_status() if ss_state == "error": pyfos_util.response_print(ss_rsp_obj) return sleep(5) print("\r\n") while ("in-progress" in ss_state or "queued" in ss_state or "done" in ss_state): resp = ss_req_obj.post(session) if pyfos_util.is_failed_resp(resp): ss_state = "error" else: status = show_status(resp) ss_state = status.peek_status() percentage = status.peek_percentage_complete() print("Firmwawredownload status: " + ss_state + " Completion percentage: " + str(percentage) + " ", end='\r', flush=True) sleep(5) print("\r\n") if ss_state == "error": pyfos_util.response_print(resp)
def main(argv): filters = ["zone_object"] inputs = brcd_util.parse(argv, zone_operation_parameters, filters, validate) session = brcd_util.getsession(inputs) # Store the checksum before editing the zoneDB to use for the # cfgsave at the end current_effective = pyfos_zone.effective_configuration.get(session) result = do_zone_object_expunge(session, inputs['utilobject']) pyfos_util.response_print(result) # If the expunge operation succeeds, perform an implicit cfgsave # If the expunge operation fails, it should get automatically aborted # in the FOS Zone module, so no need to abort the transaction. if pyfos_util.is_success_resp(result): result = cfgsave.cfgsave(session, current_effective.peek_checksum()) # If the save operation fails, check to see if there is an open # transaction. If there is, then abort the transaction if pyfos_util.is_failed_resp(result): print("Expunge succeeded, but the cfgSave failed.") current_effective = pyfos_zone.effective_configuration.get(session) if current_effective.peek_transaction_token() != 0: pyfos_util.response_print(result) print("\nAborting the transaction.") result = cfgabort.cfgabort(session) pyfos_util.response_print(result) pyfos_auth.logout(session)
def get_uptime_from_fabric(fabric_switch_ips): # Define a dictionary fabric_uptime = {} # Get uptime from all the fabric switches for ip in fabric_switch_ips: session = get_session(ip) if pyfos_auth.is_failed_login(session): print( "Login failed due to", session.get( pyfos_auth.CREDENTIAL_KEY)[pyfos_auth.LOGIN_ERROR_KEY]) continue switch = fibrechannel_switch.get(session) if pyfos_util.is_failed_resp(switch): pyfos_util.response_print(switch) continue # Peek uptime from fabric switch if isinstance(switch, list): for entry in switch: uptime = entry.peek_up_time() else: uptime = switch.peek_up_time() # Update dictionary fabric_uptime[ip] = uptime pyfos_auth.logout(session) return fabric_uptime
def _get_supportsave_status(session, ss_req_obj, ss_rsp_obj): status = show_status(ss_rsp_obj) ss_state = status.peek_status() sleep(5) print("\r\n") while ("in-progress" in ss_state or "queued" in ss_state or "done" in ss_state): resp = _post_supportsave(session, ss_req_obj) if pyfos_util.is_failed_resp(resp): ss_state = "failed" else: status = show_status(resp) ss_state = status.peek_status() percentage = status.peek_percentage_complete() print("Supportsave status: " + ss_state + " Completion percentage: " + str(percentage) + " ", end='\r', flush=True) sleep(5) print("\r\n") if ss_state == "failed": pyfos_util.response_print(resp)
def main(argv): valid_options = ["name"] inputs = brcd_util.generic_input(argv, usage, valid_options) session = brcd_util.getsession(inputs) vfid = None if 'vfid' in inputs: vfid = inputs['vfid'] if vfid is not None: pyfos_auth.vfid_set(session, vfid) if "name" in inputs: name = inputs["name"] result = pyfos_switchfcport.fibrechannel.get(session, name) else: result = pyfos_switchfcport.fibrechannel.get(session) if not pyfos_util.is_failed_resp(result): available_ports = [] if isinstance(result, list): for port in result: id_avail_port(port, available_ports) else: id_avail_port(result, available_ports) pyfos_util.response_print(available_ports) else: if "name" in inputs: print("failed to get information on", inputs["name"]) else: print("failed to get information on ports") pyfos_auth.logout(session)
def main(argv): valid_options = ["allaccess"] inputs = brcd_util.generic_input(argv, usage, valid_options) session = pyfos_auth.login(inputs["login"], inputs["password"], inputs["ipaddr"], inputs["secured"], verbose=inputs["verbose"]) if pyfos_auth.is_failed_login(session): print( "login failed because", session.get(pyfos_auth.CREDENTIAL_KEY)[pyfos_auth.LOGIN_ERROR_KEY]) brcd_util.full_usage(usage) sys.exit() brcd_util.exit_register(session) vfid = None if 'vfid' in inputs: vfid = inputs['vfid'] if vfid is not None: pyfos_auth.vfid_set(session, vfid) if "allaccess" not in inputs: pyfos_auth.logout(session) usage() sys.exit() allaccess = inputs["allaccess"] current_effective = pyfos_zone.effective_configuration.get(session) result = defzone(session, allaccess) if pyfos_util.is_failed_resp(result): pyfos_util.response_print(result) else: pyfos_util.response_print(result) result = cfgsave.cfgsave(session, current_effective.peek_checksum()) pyfos_util.response_print(result) if pyfos_util.is_failed_resp(result): print("failed. Aborting transaction.") result = cfgabort.cfgabort(session) pyfos_util.response_print(result) pyfos_auth.logout(session)
def setclsoptions(cls, tmpcls, session): """ Set the OPTIONS for a pyfos class. """ if len(optionsdict[tmpcls]['OPTIONS']) == 0: ret = tmpcls().options(session) if not utils.is_failed_resp(ret): optionsdict[tmpcls]['OPTIONS'] = ret
def set_ag_mode(session, ag_mode): sw_obj = fibrechannel_switch() current_sw = fibrechannel_switch.get(session) if pyfos_util.is_failed_resp(current_sw): return current_sw sw_obj.set_name(current_sw.peek_name()) sw_obj.set_ag_mode(ag_mode) # print(settings_obj) return sw_obj.patch(session)
def fd_show_status(session, fd_dict): print(fd_dict) status_obj = {"show-status": {"message-id": fd_dict["show-status"] ["message-id"]}} ss_req_obj = show_status(status_obj) ss_rsp_obj = ss_req_obj.post(session) if pyfos_util.is_failed_resp(ss_rsp_obj): print("Firmwaredownload operation failed.\n") pyfos_util.response_print(ss_rsp_obj) return _get_firmwaredownload_status(session, ss_req_obj, ss_rsp_obj) return
def ss_show_status(session, ssObj): ss_obj = show_status(ssObj) ss_id = ss_obj.peek_message_id() status_obj = {"show-status": {"message-id": ss_id}} ss_req_obj = show_status(status_obj) ss_rsp_obj = _post_supportsave(session, ss_req_obj) if pyfos_util.is_failed_resp(ss_rsp_obj): print("Supportsave operation failed.\n") pyfos_util.response_print(ss_rsp_obj) return _get_supportsave_status(session, ss_req_obj, ss_rsp_obj) return
def get(session, name=None): if name is None: return pyfos_rest_util.get_all(session, fibrechannel_switch) else: obj = fibrechannel_switch() obj.set_name(name) showobj = obj.get_uri(session) retobj = fibrechannel_switch() if pyfos_util.is_failed_resp(showobj): return retobj else: retobj.load(showobj) return retobj
def getbladeslots(cls, ret): """ Get the blade slots as per blade response """ tmpslots = [] if not utils.is_failed_resp(ret): if isinstance(ret, blade): ret = [ret] for i in range(len(ret)): rret, slot = cls.getSlotforObject(ret[i]) if rret is False and slot != '0': print(ret[i]) tmpslots.append(slot) return tmpslots
def main(argv): valid_options = ["name"] inputs = brcd_util.generic_input(argv, usage, valid_options) session = pyfos_auth.login(inputs["login"], inputs["password"], inputs["ipaddr"], inputs["secured"], verbose=inputs["verbose"]) if pyfos_auth.is_failed_login(session): print( "login failed because", session.get(pyfos_auth.CREDENTIAL_KEY)[pyfos_auth.LOGIN_ERROR_KEY]) usage() sys.exit() brcd_util.exit_register(session) vfid = None if 'vfid' in inputs: vfid = inputs['vfid'] if vfid is not None: pyfos_auth.vfid_set(session, vfid) if "name" in inputs: name = inputs["name"] result = pyfos_switchfcport.fibrechannel.get(session, name) else: result = pyfos_switchfcport.fibrechannel.get(session) if not pyfos_util.is_failed_resp(result): available_ports = [] if isinstance(result, list): for port in result: id_avail_port(port, available_ports) else: id_avail_port(result, available_ports) pyfos_util.response_print(available_ports) else: if "name" in inputs: print("failed to get information on", inputs["name"]) else: print("failed to get information on ports") pyfos_auth.logout(session)
def main(): # Get fabric from seed switch fabric = get_fabric_from_seedswitch() # If response is success, get ipaddress list if pyfos_util.is_failed_resp(fabric): pyfos_util.response_print(fabric) sys.exit() else: fabric_switch_ips = get_fabric_ipaddr_list(fabric) # Get uptime list from fabric switches uptime_list = get_uptime_from_fabric(fabric_switch_ips) if not uptime_list: print("No switch available in the fabric.") else: print("Fabric uptime: " + str(uptime_list))
def main(argv): filters = [] inputs = brcd_util.parse(argv, license, filters) session = brcd_util.getsession(inputs) result = get_license_info(inputs['session']) pyfos_util.response_print(result) chassis_obj = chassis() result = chassis_obj.get(inputs['session']) if pyfos_util.is_failed_resp(result): pyfos_util.response_print(result) else: print("License ID:", result.peek_license_id()) pyfos_auth.logout(session)
def zone_name_func(session, inputs, usage, func): # comment zone helper to execute & commit # name based operations if "name" not in inputs: pyfos_auth.logout(session) usage() sys.exit() name = inputs["name"] current_effective = pyfos_zone.effective_configuration.get(session) result = func(session, name) if pyfos_util.is_failed_resp(result): pyfos_util.response_print(result) else: pyfos_util.response_print(result) result = cfgsave.cfgsave(session, current_effective.peek_checksum()) pyfos_util.response_print(result)
def main(argv): filters = ['host', 'user_name', 'password', 'remote_directory', 'protocol'] inputs = brcd_util.parse(argv, supportsave, filters, ss_validate) ss_obj = inputs['utilobject'] session = brcd_util.getsession(inputs) ss_rsp_obj = _post_supportsave(session, ss_obj) if ("info-message" in ss_rsp_obj and ss_rsp_obj["info-message"] == "Switch version is lower than the object"): pyfos_util.response_print(ss_rsp_obj) pyfos_auth.logout(session) sys.exit() if pyfos_util.is_failed_resp(ss_rsp_obj): print("Supportsave operation failed.\n") pyfos_util.response_print(ss_rsp_obj) else: ss_show_status(session, ss_rsp_obj) pyfos_auth.logout(session)
def main(argv): valid_options = ["name"] inputs = brcd_util.generic_input(argv, usage, valid_options) session = pyfos_auth.login(inputs["login"], inputs["password"], inputs["ipaddr"], inputs["secured"], verbose=inputs["verbose"]) if pyfos_auth.is_failed_login(session): print( "login failed because", session.get(pyfos_auth.CREDENTIAL_KEY)[pyfos_auth.LOGIN_ERROR_KEY]) brcd_util.full_usage(usage, valid_options) sys.exit() brcd_util.exit_register(session) if "FOS-" in inputs["name"]: err_str = "Invalid license format. " print(err_str + "Please specify only key based license.") brcd_util.full_usage(usage, valid_options) sys.exit() if "name" not in inputs: print("License key is required") brcd_util.full_usage(usage, valid_options) sys.exit() l_obj = license_parameters() l_obj.set_action("remove") l_obj.set_name(inputs["name"]) l_rsp_obj = l_obj.post(session) if ("info-message" in l_rsp_obj and l_rsp_obj["info-message"] == "Switch version is lower than the object"): pyfos_util.response_print(l_rsp_obj) pyfos_auth.logout(session) sys.exit() if pyfos_util.is_failed_resp(l_rsp_obj): pyfos_util.response_print(l_rsp_obj) else: pyfos_util.response_print(l_rsp_obj) pyfos_auth.logout(session)
def _get_firmwaredownload_status(session, ss_req_obj, ss_rsp_obj): status = show_status(ss_rsp_obj) ss_state = status.peek_status() if ss_state == "error": pyfos_util.response_print(ss_rsp_obj) return sleep(10) print("\r\n") try: while ("in-progress" in ss_state or "queued" in ss_state or "done" in ss_state): resp = ss_req_obj.post(session) if pyfos_util.is_failed_resp(resp): ss_state = "error" print("\r\n") pyfos_util.response_print(resp) break else: status = show_status(resp) eula_text = status.peek_eula_text() if eula_text: print("EULA: \n", eula_text) break else: pyfos_util.response_print(status) ss_state = status.peek_status() if ss_state == "done": # stage = true, reponse displayed above now return break sleep(10) except socket_error as serr: if serr.errno == errno.ECONNREFUSED: print("Switch is rebooting as part of firmwaredownload. Use showstatus \ script with message-id as 20000 to track the firmwaredownload \ status after switch reboots.\n")
def print_connected_location(session, device_wwn): connected_ports = [] ports = pyfos_switchfcport.fibrechannel.get(session) if not pyfos_util.is_failed_resp(ports): # check the list of ports if isinstance(ports, list): for port in ports: neighbor_list = port.peek_neighbor_wwn() if len(neighbor_list) > 0: for device in neighbor_list: if device_wwn == device: connected_ports.append(port.peek_name()) # otherwise, just one port returned else: neighbor_list = ports.peek_neighbor_wwn() if len(neighbor_list) > 0: for device in neighbor_list: if device_wwn == device: connected_ports.append(ports.peek_name()) return connected_ports
def fdmishow(session, inputs): # Get HBA ID, if provided if "hbaid" not in inputs: hba = None else: hba = inputs["hbaid"] # Get HBA attributes hba_attributes = pyfos_fdmi.hba.get(session, hba) pyfos_util.response_print(hba_attributes) # Sort port attributes if HBA ID specified if hba is not None: # Only print ports if HBA is valid if not pyfos_util.is_failed_resp(hba_attributes): # Create list of ports associated with specified HBA hba_ports = [] # Get port attributes port_attributes = pyfos_fdmi.port.get(session, None) # Check if multiple port entries if isinstance(port_attributes, list): # Iterate through port entries for port_entry in port_attributes: # Check if same HBA ID if hba == port_entry.peek_hba_id(): # Add port entry to list hba_ports.append(port_entry) else: if hba == port_attributes.peek_hba_id(): # Add port entry to list hba_ports.append(port_attributes) # Print HBA port attributes pyfos_util.response_print(hba_ports) else: # Print all HBA port attributes port_attributes = pyfos_fdmi.port.get(session, None) pyfos_util.response_print(port_attributes)
def dumptofiddefault(self, wb, session, fmtobj): """ Dump the switch configuration object in .xlsx file in JSON format. """ self.dumplist = [] if fmtobj != 'json': self.log(1, "Defaulting to json for the supported fmtfile") print("Defaulting to json for the supported fmtfile") for i in range(len(self.clslist)): cls = self.clslist[i] if any(self.configlist) and\ cls().getcontainer() not in self.configlist: continue if not clsmanager.isConfigClass(cls, session): # print("skipping non config class", self.clslist[i]) continue ret = self.clslist[i].get(session) if utils.is_failed_resp(ret): # print(ret) continue if not isinstance(ret, list): ret = [ret] self.dumplist += ret wb.write(str(self.dumplist))