def update_cisco_eox_database(api_query): """ Synchronizes the local EoX data from the Cisco EoX API using the specified queries or the queries specified in the configuration when api_query is set to None :param api_query: single query that is send to the Cisco EoX API :return: """ # load application settings and check, that the API is enabled app_settings = AppSettings() app_settings.read_file() if not app_settings.is_cisco_api_enabled(): msg = "Cisco API access not enabled" logger.warn(msg) raise CiscoApiCallFailed(msg) blacklist = app_settings.get_product_blacklist_regex().split(";") create_missing = app_settings.is_auto_create_new_products() # start with Cisco EoX API queries logger.info("Query EoX database: %s" % api_query) query_results = query_cisco_eox_api(api_query, blacklist, create_missing) # filter empty result strings if len(query_results) == 0: query_results = [{ "PID": None, "blacklist": False, "updated": False, "created": False, "message": "No product update required" }] return query_results
def get_raw_api_data(api_query): """ returns all EoX records for a specific query (from all pages) :param api_query: single query that is send to the Cisco EoX API :raises CiscoApiCallFailed: exception raised if Cisco EoX API call failed :return: list that contains all EoX records from the Cisco EoX API """ if type(api_query) is not str: raise ValueError("api_query must be a string value") # load application settings and check, that the API is enabled app_settings = AppSettings() if not app_settings.is_cisco_api_enabled(): msg = "Cisco API access not enabled" logger.warning(msg) raise CiscoApiCallFailed(msg) # start Cisco EoX API query logger.info("send query to Cisco EoX database: %s" % api_query) eoxapi = CiscoEoxApi() eoxapi.load_client_credentials() results = [] try: current_page = 1 result_pages = 999 while current_page <= result_pages: if current_page == 1: logger.info("Executing API query '%s' on first page" % api_query) else: logger.info("Executing API query '%s' on page '%d" % (api_query, current_page)) # will raise a CiscoApiCallFailed exception on error eoxapi.query_product(product_id=api_query, page=current_page) result_pages = eoxapi.amount_of_pages() if eoxapi.get_page_record_count() > 0: results.extend(eoxapi.get_eox_records()) current_page += 1 except ConnectionFailedException: logger.error("Query failed, server not reachable: %s" % api_query, exc_info=True) raise except CiscoApiCallFailed: logger.fatal("Query failed: %s" % api_query, exc_info=True) raise return results
def get_raw_api_data(api_query=None, year=None): """ returns all EoX records for a specific query (from all pages) :param api_query: single query that is send to the Cisco EoX API :param year: get all EoX data that are announced in a specific year :raises CiscoApiCallFailed: exception raised if Cisco EoX API call failed :return: list that contains all EoX records from the Cisco EoX API """ if api_query is None and year is None: raise ValueError("either year or the api_query must be provided") if api_query: if type(api_query) is not str: raise ValueError("api_query must be a string value") if year: if type(year) is not int: raise ValueError("year must be an integer value") # load application settings and check, that the API is enabled app_settings = AppSettings() if not app_settings.is_cisco_api_enabled(): msg = "Cisco API access not enabled" logger.warning(msg) raise CiscoApiCallFailed(msg) # start Cisco EoX API query logger.info("send query to Cisco EoX database: %s" % api_query) eoxapi = CiscoEoxApi() eoxapi.load_client_credentials() results = [] try: current_page = 1 result_pages = 999 while current_page <= result_pages: logger.info("Executing API query %s on page '%d" % ('%s' % api_query if api_query else "for year %d" % year, current_page)) # will raise a CiscoApiCallFailed exception on error if year: eoxapi.query_year(year_to_query=year, page=current_page) else: eoxapi.query_product(product_id=api_query, page=current_page) result_pages = eoxapi.amount_of_pages() if eoxapi.get_page_record_count() > 0: results.extend(eoxapi.get_eox_records()) current_page += 1 except ConnectionFailedException: logger.error("Query failed, server not reachable: %s" % api_query, exc_info=True) raise except CiscoApiCallFailed: logger.fatal("Query failed: %s" % api_query, exc_info=True) raise logger.debug("found %d records for year %s" % (len(results), year)) return results
def raise_ciscoapicallfailed(): raise CiscoApiCallFailed("Cisco API call failed message")
def get(self, *args, **kwargs): raise CiscoApiCallFailed("The API is broken")
def mock_response(): raise CiscoApiCallFailed("The API is broken")
def mock_api_call_failed(): raise CiscoApiCallFailed()
def update_cisco_eox_database(api_query): """ synchronizes the local database with the Cisco EoX API using the specified query :param api_query: single query that is send to the Cisco EoX API :raises CiscoApiCallFailed: exception raised if Cisco EoX API call failed :return: list of dictionary that describe the updates to the database """ if type(api_query) is not str: raise ValueError("api_query must be a string value") # load application settings and check, that the API is enabled app_settings = AppSettings() if not app_settings.is_cisco_api_enabled(): msg = "Cisco API access not enabled" logger.warn(msg) raise CiscoApiCallFailed(msg) blacklist_raw_string = app_settings.get_product_blacklist_regex() create_missing = app_settings.is_auto_create_new_products() # clean blacklist string and remove empty statements # (split lines, if any and split the string by semicolon) blacklist = [] for e in [e.split(";") for e in blacklist_raw_string.splitlines()]: blacklist += e blacklist = [e for e in blacklist if e != ""] # start Cisco EoX API query logger.info("Query EoX database: %s" % api_query) eoxapi = CiscoEoxApi() eoxapi.load_client_credentials() results = [] try: max_pages = 999 current_page = 1 result_pages = 0 while current_page <= max_pages: logger.info("Executing API query '%s' on page '%d" % (api_query, current_page)) # will raise a CiscoApiCallFailed exception on error eoxapi.query_product(product_id=api_query, page=current_page) if current_page == 1: result_pages = eoxapi.amount_of_pages() logger.info("Initial query returns %d page(s)" % result_pages) records = eoxapi.get_eox_records() # check that the query has valid results if eoxapi.get_page_record_count() > 0: # processing records for record in records: result_record = {} pid = record['EOLProductID'] result_record["PID"] = pid result_record["created"] = False result_record["updated"] = False result_record["message"] = None logger.info("processing product '%s'..." % pid) pid_in_database = product_id_in_database(pid) # check if the product ID is blacklisted by a regular expression pid_blacklisted = False for regex in blacklist: try: if re.search(regex, pid, re.I): pid_blacklisted = True break except: # silently ignore the issue, invalid regular expressions are handled by the settings form logger.info("invalid regular expression: %s" % regex) # ignore if the product id is not in the database if pid_blacklisted and not pid_in_database: logger.info("Product '%s' blacklisted... no further processing" % pid) result_record.update({ "blacklist": True }) else: res = {} try: res = update_local_db_based_on_record(record, create_missing) except ValidationError: logger.error("invalid data received from Cisco API, cannot save data object for '%s'" % pid, exc_info=True) result_record["message"] = "invalid data received from Cisco EoX API, import incomplete" finally: res["blacklist"] = False result_record.update(res) results.append(result_record) if current_page == result_pages: break else: current_page += 1 # filter empty result strings if len(results) == 0: results = [ { "PID": None, "blacklist": False, "updated": False, "created": False, "message": "No product update required" } ] except ConnectionFailedException: logger.error("Query failed, server not reachable: %s" % api_query, exc_info=True) raise except CiscoApiCallFailed: logger.fatal("Query failed: %s" % api_query, exc_info=True) raise return results