def load_store_serials(store_name, serials_list): settings["serial1"] = None settings["serial2"] = None settings["device-name1"] = None settings["device-name2"] = None settings["serial"] = None settings["device-name"] = None serial_count = 0 for item in serials_list: if store_name == item["Network name"]: if settings["serial1"]: settings["serial2"] = item["Serial"] settings["device-name2"] = item["Name"] serial_count += 1 else: settings["serial1"] = item["Serial"] settings["device-name1"] = item["Name"] serial_count += 1 if serial_count > 2: logger.error("store {} has too many serial".format(store_name)) runlogs_logger.error( "store {} has too many serial".format(store_name)) gv.fake_assert() return 0 return serial_count
def getCreatedNetworkId(networkName): fname = "network_{}".format(networkName) network = Json.reader(fname) if network is None: logger.error("unable to load rules from firewall_template") runlogs_logger.error("unable to load rules from firewall_template") gv.fake_assert() return network["id"]
def removedevice(networkid, serial): try: meraki.removedevfromnet(config.api_key, networkid, serial) except (meraki.EmailFormatError, meraki.OrgPermissionError, meraki.ListError) as err: logger.error("Meraki error: {}".format(err.default)) runlogs_logger.error("Meraki error: {}".format(err.default)) exit(-1)
def json_writer(fpath, data): str = make_pretty(data) try: with open(fpath, 'w') as f: f.write(str) except Exception as err: logger.error("failure") runlogs_logger.error("failure") gv.fake_assert()
def pmdb_init(): global settings load_cli_settings() settings["org-name"] = None settings["store-name"] = None settings["agent"] = None settings["org-id"] = None settings["folder-time-stamp"] = None settings["store-name"] = None settings["store-number"] = None settings["time-stamp"] = None settings["netid"] = None settings["device-name"] = None settings["vlans-add-list"] = None fname = settings["CLI"].get("vlans-add-list") if fname: aux = json_reader("{}/{}.json".format(TEMPLATES_DIR, fname)) settings["vlans-add-list"] = aux fname = settings["CLI"].get("vlans-delete-list") if settings["CLI"].get("vlans-delete-list"): aux = json_reader("{}/{}.json".format(TEMPLATES_DIR, fname)) settings["vlans-delete-list"] = aux fname = settings["CLI"].get("networks-serials") if fname: aux = json_reader("{}/{}.json".format(TEMPLATES_DIR, fname)) settings["networks-serials"] = aux else: runlogs_logger.error("networks-serials {} not found".format(fname)) config = json_reader("../../config/safeway-config.json") settings["CONFIG"] = dict() settings["CONFIG"]["network"] = config[0]["network"] firewall = config[0]["firewall"] settings["CONFIG"]["static-route-next-hop"] = firewall[ 'static_route_next_hop'] vlan = config[0]["vlan"] settings["CONFIG"]["funnel-file"] = vlan["funnel_file"] settings["CONFIG"]["netx-file"] = vlan['netx_file'] settings["CONFIG"]["device-prefix"] = vlan["device_prefix"] settings["CONFIG"]["device-postfix"] = vlan["device_postfix"] vpn = config[0]["vpn"] settings["CONFIG"]["hubnetworks"] = vpn["hubnetworks"] settings["CONFIG"]["defaultroute"] = vpn["defaultroute"] # Netx and Non-Netx settings["NON-NETX"] = dict() settings["NON-NETX"]["new_summary"] = None fname = "vlans-non-netx" aux = json_reader("{}/{}.json".format(TEMPLATES_DIR, fname)) settings["NON-NETX"] = aux
def json_reader(fpath): data = None cwd = os.getcwd() try: json_data = open(fpath).read() # json_data = json_data.replace("\n","") data = json.loads(json_data) except Exception as err: logger.error("cwd {} fpath {}".format(cwd, fpath)) runlogs_logger.error("cwd {} fpath {}".format(cwd, fpath)) gv.fake_assert() return data
def deln(self, networkid): success = False str = None try: success, str = meraki.delnetwork(config.api_key, networkid) logger.info("success {}".format(networkid)) if not success: logger.error("failed netid {}".format(networkid)) gv.fake_assert() except Exception as err: logger.error("exception failure netid {} {}".format( networkid, str)) runlogs_logger.error("exception failure netid {} {}".format( networkid, str)) gv.fake_assert() return success, str
def getdetail(self, networkid): success = False self.network = None try: success, self.network = meraki.getnetworkdetail( config.api_key, networkid) log.logger.debug(Json.make_pretty(self.network)) if not success: log.logger.error("failed: {}".format(self.network)) log.runlogs_logger.error("failed: {}".format(self.network)) gv.fake_assert() except Exception as err: logger.error("exception failure networkid: {} {}".format( networkid, self.network)) runlogs_logger.error("exception failure {}".format(self.network)) gv.fake_assert() return success, self.network
def add(self, orgid, name, nettype): success = False self.network = None try: clone_id = config.get_clone_id() success, self.network = meraki.addnetwork(config.api_key, orgid, name, nettype, tags=None, tz="US/Pacific", cloneid=clone_id, suppressprint=False) store_number = settings["store-number"] fname = "network_{}".format(store_number) data = dict() data["store-name"] = settings["store-name"] data["netid"] = settings["netid"] data["clone-source"] = settings["CLI"]["networks-clone-source"] json.writer(fname, data, path="data", header=None, logPath=False) if success: logger.info("success org-id {} {} {} {}".format( orgid, name, nettype, self.network)) else: logger.error("failed org-id {} {} {} {}".format( orgid, name, nettype, self.network)) runlogs_logger.error("failed org-id {} {} {}".format( orgid, name, nettype, self.network)) gv.fake_assert() logger.debug("cloned network, {} {} {}".format( name, nettype, self.network)) except Exception as err: logger.error("orgid {} name {} nettype {}".format( orgid, name, nettype)) runlogs_logger.error("orgid {} name {} nettype {}".format( orgid, name, nettype)) gv.fake_assert() assert (0) return success, self.network
def load_store(_orchestration_agent, store_name): org_name = settings["org-name"] load_org(_orchestration_agent, org_name) store_name = "{}".format(store_name) settings["store-name"] = store_name store_number = utils.obtain_store_number(store_name) settings["store-number"] = store_number settings["time-stamp"] = utils.create_store_data_dir(_orchestration_agent) settings["netid"] = utils.obtain_netid(store_number, store_name) if not settings["netid"]: logger.error("store '{}' not found in org: {}".format( settings["store-name"], settings["org-name"])) runlogs_logger.error("store '{}' not found in org: {}".format( settings["store-name"], settings["org-name"])) runlogs_logger.error("store '{}' not found in org: {}".format( settings["store-name,"], settings["org-name"])) gv.fake_assert() return False # men_and_mice.get_vlan_funnel() set_cli_selections() return True
def claim(self, serial, licensekey=None, licensemode=None, orderid=None): success = False str = None try: success, self.claim = meraki.claim(config.api_key, settings["org-id"], serial, licensekey, licensemode, orderid) if not success: runlogs_logger.error("orgid: {} serial:{} claim:{}".format( settings["org-id"], serial, self.claim)) logger.error("orgid: {} serial:{} claim:{}".format( settings["org-id"], serial, self.claim)) return False #json.writer("claim_{}".format(serial), self.claim) except (meraki.EmailFormatError, meraki.OrgPermissionError, meraki.ListError) as err: logger.error("orgid: {} Meraki error: {}".format( settings["org-id"], err.default)) runlogs_logger.error("orgid: {} Meraki error: {}".format( settings["org-id"], err.default)) exit(-1) except Exception as err: logger.error("serial:{}".format(serial)) runlogs_logger.error("serial:{}".format(serial)) gv.fake_assert() return success
def update_device(self, networkid=None, serial=None, name=None): success = False str = None try: success, str = meraki.updatedevice(config.api_key, networkid, serial, name, tags=None, lat=None, lng=None, address=None, move=None, suppressprint=False) if success: logger.info("success {}".format(networkid)) #json.writer("addtonet_{}".format(serial), str) else: runlogs_logger.error( "updatedevice failed networkid: {} serial:{}".format( networkid, serial)) logger.error( "updatedevice failed networkid: {} serial:{}".format( networkid, serial)) except (meraki.EmailFormatError, meraki.OrgPermissionError, meraki.ListError) as err: runlogs_logger.info("updatedevice name success") logger.info("updatedevice success networkid {} serial{}".format( networkid, serial)) exit(-1) except Exception as err: runlogs_logger.error( "updatedevice failed {} networkid: {} serial:{}".format( err, networkid, serial)) logger.error( "updatedevice failed {} networkid: {} serial:{}".format( err, networkid, serial)) gv.fake_assert() return success, str
def update(self, networkid, name): success = False self.network = None try: success, str = meraki.updatenetwork(config.api_key, networkid, name, tz="US/Pacific", tags=None) self.network = str logger.debug(Json.make_pretty(self.network)) if not success: logger.error("{}".format(str)) runlogs_logger.error("{}".format(str)) gv.fake_assert() except Exception as err: logger.error("exception failure netid: {} name: {}".format( networkid, name)) runlogs_logger.error("exception failure name: {}".format(name)) gv.fake_assert() return success, self.network
def addtonet(self, networkid, serial): success = False try: success, _str = meraki.adddevtonet(config.api_key, networkid, serial) if success: logger.debug("success {}".format(networkid)) json.writer("addtonet {}".format(serial), _str) else: logger.error("failed.") logger.error("{}".format(str)) except (meraki.EmailFormatError, meraki.OrgPermissionError, meraki.ListError) as err: logger.error("failed {}".format(err.default)) runlogs_logger.error("failed {}".format(err.default)) exit(-1) except Exception as err: runlogs_logger.error("failed {} serial:{}".format(err, serial)) logger.error("failed {} networkid: {} serial:{}".format( err, networkid, serial)) gv.fake_assert() return success, _str
def __returnhandler_custom(statuscode, _returntext, objtype, suppressprint): """ Args: statuscode: HTTP Status Code returntext: JSON String objtype: Type of object that operation was performed on (i.e. SSID, Network, Org, etc) suppressprint: Suppress any print output when function is called Returns: errmsg: If returntext JSON contains {'errors'} element returntext: If no error element, returns returntext """ validreturn = meraki.__isjson(_returntext) noerr = False errmesg = '' if validreturn: returntext = json.loads(_returntext) try: errmesg = returntext['errors'] except KeyError: noerr = True except TypeError: noerr = True try: __returntext = _returntext.replace(",", "\n") except: pass if str(statuscode) == '200' and validreturn: logger.info( '{0} Operation Successful - See returned data for results\n'. format(str(objtype))) logger.debug(_returntext) return (True, returntext) elif str(statuscode) == '200': _str = '{0} Operation Successful\n'.format(str(objtype)) logger.info(_str) logger.debug(_returntext) return (True, None) elif str(statuscode) == '201' and validreturn: _str = '{0} Added Successfully - See returned data for results\n'.format( str(objtype)) logger.info(_str) logger.debug(_returntext) return (True, returntext) elif str(statuscode) == '201': _str = '{0} Added Successfully\n'.format(str(objtype)) logger.info(_str) logger.debug(_returntext) return (True, returntext) elif str(statuscode) == '204' and validreturn: _str = '{0} Deleted Successfully - See returned data for results\n'.format( str(objtype)) logger.info(_str) logger.debug(_returntext) return (True, returntext) elif str(statuscode) == '204': _str = '{0} Deleted Successfully\n'.format(str(objtype)) logger.info(_str) logger.debug(_returntext) return (True, None) elif str(statuscode) == '400' and validreturn and noerr is False: runlogs_logger.error( 'Bad Request - See returned data for error details\n') runlogs_logger.error(__returntext) logger.error('Bad Request - See returned data for error details\n') logger.error(__returntext) return (False, __returntext) elif str(statuscode) == '400' and validreturn and noerr: runlogs_logger.error('Bad Request - See returned data for details\n') runlogs_logger.error('Returned data:\n{}'.format(__returntext)) logger.error('Bad Request - See returned data for details\n') logger.error('Returned data:\n{}'.format(__returntext)) return (False, __returntext) elif str(statuscode) == '400': runlogs_logger.error( 'Bad Request - No additional error data available\n') logger.error('Bad Request - No additional error data available\n') elif str(statuscode) == '401' and validreturn and noerr is False: runlogs_logger.error( 'Unauthorized Access - See returned data for error details\n') runlogs_logger.error('Returned data:\n{}'.format(__returntext)) logger.error( 'Unauthorized Access - See returned data for error details\n') logger.error('Returned data:\n{}'.format(__returntext)) return (False, __returntext) elif str(statuscode) == '401' and validreturn: runlogs_logger.error('Unauthorized Access') logger.error('Unauthorized Access') runlogs_logger.error('Returned data:\n{}'.format(__returntext)) logger.error('Returned data:\n{}'.format(__returntext)) return (False, __returntext) elif str(statuscode) == '404' and validreturn and noerr is False: runlogs_logger.error( 'Resource Not Found - See returned data for error details\n') runlogs_logger.error('Returned data:\n{}'.format(__returntext)) logger.error( 'Resource Not Found - See returned data for error details\n') logger.error('Returned data:\n{}'.format(__returntext)) return (False, errmesg) elif str(statuscode) == '404' and validreturn: runlogs_logger.error('Resource Not Found') runlogs_logger.error('Returned data:\n{}'.format(__returntext)) logger.error('Resource Not Found') logger.error('Returned data:\n{}'.format(__returntext)) return (False, __returntext) elif str(statuscode) == '500': runlogs_logger.error('HTTP 500 - Server Error') runlogs_logger.error('Returned data:\n{}'.format(__returntext)) logger.error('HTTP 500 - Server Error') logger.error('Returned data:\n{}'.format(__returntext)) return (False, __returntext) elif validreturn and noerr is False: runlogs_logger.error( 'HTTP Status Code: {0} - See returned data for error details\n'. format(str(statuscode))) runlogs_logger.error('Returned data:\n{}'.format(__returntext)) logger.error( 'HTTP Status Code: {0} - See returned data for error details\n'. format(str(statuscode))) logger.error('Returned data:\n{}'.format(__returntext)) return False, errmesg else: runlogs_logger.error( 'HTTP Status Code: {0} - No returned data (possibly BAD API_KEY)\n' .format(str(statuscode))) runlogs_logger.error('Returned data:\n{}'.format(__returntext)) logger.error( 'HTTP Status Code: {0} - No returned data (possible invalid API_KEY)\n' .format(str(statuscode))) logger.error('Returned data:\n{}'.format(__returntext)) return (False, _returntext)