def test_get_by_id(): """ """ # Need to get a check ID to test getting a single check query = get_checks.GetChecks(TOKEN, customerid=CUSTOMERID) all_account_checks = query.all_checks() single = next(iter(all_account_checks)) query = get_checks.GetChecks(TOKEN, checkid=single, customerid=CUSTOMERID) result = query.get_by_id() assert result['_id'] == single
def test_update_many_checks(): """ """ checks_to_update = {} check_types = parameters.CHECK_TYPES query = get_checks.GetChecks(TOKEN, customerid=CUSTOMERID) my_checks = query.all_checks() for check in check_types: label = "PYTEST_{0}_check".format(check) fields = {"public": False, "interval": 15} for _, value in my_checks.items(): if value['label'] == label: checks_to_update.update({value['_id']: value['type']}) # result = update_checks.update( # TOKEN, check_id, check, fields, customerid=CUSTOMERID) # assert "error" not in result result = update_checks.update_many( TOKEN, checks_to_update, fields, customerid=CUSTOMERID) assert "error" not in result
def test_get_all_checks(): """ """ query = get_checks.GetChecks(TOKEN, customerid=CUSTOMERID) result = query.all_checks() assert "error" not in result.keys()
def test_get_disabled_checks(): """ """ query = get_checks.GetChecks(TOKEN, customerid=CUSTOMERID) result = query.disabled_checks() for i in result: assert result[i]['type'] == 'disabled'
def test_get_failing_checks(): """ """ query = get_checks.GetChecks(TOKEN, customerid=CUSTOMERID) result = query.failing_checks() for i in result: assert result[i]['state'] == 0
def test_get_uptime(): """ """ query = get_checks.GetChecks(TOKEN, customerid=CUSTOMERID) acc_checks = query.all_checks() check_id = next(iter(acc_checks)) returned = results.get_uptime(TOKEN, check_id, customerid=CUSTOMERID) assert "error" not in returned
def _fetch_checks(token, customerid=None): """ Fetches all NodePing checks of type PUSH Fetches all NodePing checks for the account and returns only the checks that are of type PUSH """ query_nodeping = get_checks.GetChecks(token, customerid=customerid) checks = query_nodeping.all_checks() return { key: value for key, value in checks.items() if value['type'] == "PUSH" }
def test_delete_checks(): """ Delete all checks made in the create check test """ check_types = parameters.CHECK_TYPES query = get_checks.GetChecks(TOKEN, customerid=CUSTOMERID) my_checks = query.all_checks() for check in check_types: label = "PYTEST_{0}_check".format(check) for key, value in my_checks.items(): if value['label'] == label: result = delete_checks.remove(TOKEN, key, customerid=CUSTOMERID) assert "error" not in result.keys()
def get_nodeping_check(parameters): """ Gets the user defined check by its checkid """ token = parameters['token'] customerid = parameters['customerid'] checkid = parameters['checkid'] try: label = parameters['label'] except KeyError: label = None # Setting checkid do none gets all checks if checkid: if checkid.lower() == "all": checkid = None query = get_checks.GetChecks(token, checkid=checkid, customerid=customerid) if checkid: result = query.get_by_id() else: result = query.all_checks() if label: for _key, value in result.items(): if value.get('label') == label: result = value continue try: result.update({"changed": True}) except KeyError: result.update({"changed": False}) return (False, checkid, result) else: return (True, checkid, result)
def update_nodeping_check(parameters): """ Updates an existing check based on parameters passed in via Ansible """ update_fields = {} changed = False token = parameters['token'] check_id = parameters['checkid'] label = parameters['label'] customerid = parameters['customerid'] query = get_checks.GetChecks(token, checkid=check_id, customerid=customerid) check_info = query.get_by_id() checktype = check_info['type'] # Removes all values that are not provided by the user stripped_params = { key: value for (key, value) in parameters.items() if value } for key, value in check_info.items(): try: # The new value provided by the user to compare to # The value the check currently has compare = stripped_params[key] except KeyError: if not isinstance(value, dict): continue # Compare nested dictionaries for differences # (eg ipv6, sens, target, threshold) if isinstance(value, dict): for subkey, subvalue in value.items(): try: compare = stripped_params[subkey] except KeyError: continue if subvalue != compare: changed = True update_fields.update({subkey: compare}) continue # Replace the old notifications with the newly provided ones if key == "notifications": update_fields.update({ 'notifications': convert_contacts(parameters['notifications'], token, customerid) }) continue # Always pass the provided dependency because not passing it will # remove the dependency from the existing check if key == "dep": update_fields.update({'dep': compare}) continue # If the value is different, add the change if value != compare: changed = True update_fields.update({key: compare}) # Update the check result = update_checks.update(token, check_id, checktype, update_fields, customerid=customerid) result.update({"changed": changed}) try: result['error'] except KeyError: return (True, "%s changed" % label, result) else: return (False, check_id, result)