Exemple #1
0
def main(token, customerid=None):
    """
    Gets contacts and prompts user for what contacts
    and alert times will be used for check
    """

    # All availavle schedules to user
    all_schedules = ["All the time"]

    # Collects existin schedules from user's account
    cust_schedules = schedules.get_schedule(token, customerid=customerid)

    # Gets the names of the schedules
    for key, _value in cust_schedules.items():
        all_schedules.append(key)

    single_contacts = contacts.get_all(token, customerid=customerid)
    contactgroups = group_contacts.get_all(token, customerid=customerid)
    single_contacts.update(contactgroups)

    single_contacts = format_contacts(single_contacts)

    contact_info = choose_contacts(single_contacts, all_schedules)

    return contact_info
Exemple #2
0
def test_get_all_groups():
    """
    """

    result = group_contacts.get_all(TOKEN, customerid=CUSTOMERID)

    assert "error" not in result.keys()
Exemple #3
0
def test_delete_group():
    """
    """

    contact_groups = group_contacts.get_all(TOKEN, customerid=CUSTOMERID)

    for i in contact_groups:
        if contact_groups[i]['name'] == REPLACE_NAME:
            result = group_contacts.delete_group(TOKEN,
                                                 i,
                                                 customerid=CUSTOMERID)

            assert "error" not in result.keys()
Exemple #4
0
def test_update_group():
    """
    """

    contact_groups = group_contacts.get_all(TOKEN, customerid=CUSTOMERID)

    for i in contact_groups:
        if contact_groups[i]['name'] == NAME:
            result = group_contacts.update_group(TOKEN,
                                                 i,
                                                 name=REPLACE_NAME,
                                                 customerid=CUSTOMERID)

            print(result)
            assert result['name'] == REPLACE_NAME
def convert_contacts(notification_contacts, token, customerid):
    """ Takes in a contact/group list and converts to the expected IDs
    """
    def _get_contact_schedule(contact_name, contact, account_contacts):
        """
        {'contact': '4RF81', 'notifydelay': 0, 'notifyschedule': 'testwindow'}
        """

        delay = contact['notifydelay']
        schedule = contact['notifyschedule']

        # NOTE: There could be many contacts with these names but with
        # different contact ID keys. That means there's potential for
        # duplicates existing in the account, and if there is a duplicate,
        # this will only grab the first match. That means it is suggested
        # a user supplies the contact ID that can be acquired by the API.
        for _, value in account_contacts.items():
            # Some contacts might not have a name field.
            # Time to guess and check
            try:
                name = value['name'].rstrip()
            except KeyError:
                name = None

            # If a contactname was provided,
            # look for addresses under that contact
            if name:
                if contact_name == name:
                    addresses = value['addresses']
                else:
                    continue

                # If the address matches, pair the contact ID with the
                # Delay and schedule
                for _id, info in addresses.items():
                    if info['address'] == contact['address']:
                        return {_id: {"delay": delay, "schedule": schedule}}
            else:
                addresses = value['addresses']

                for _id, info in addresses.items():
                    if info['address'] == contact_name:
                        return {_id: {"delay": delay, "schedule": schedule}}

                    continue

    def _get_key_schedule(contact_name, contact, queried_contacts):
        """ Evaluates Ansible input and returns proper contact groups
        to pass to NodePing API

        :param contact: The individual contact to be evaluated
        :type contact: dict
        :param account_groups: All contact group info queried from API
        :type account_groups: dict
        :return: A group contact combined with its delay and schedule
        :rtype: dict
        """

        delay = contact['notifydelay']
        schedule = contact['notifyschedule']
        _id = contact[contact_name]

        keyname = None

        if contact_name == 'group':
            if _id in queried_contacts.keys():
                keyname = _id
            else:
                for key, value in queried_contacts.items():
                    if _id == value['name']:
                        keyname = key

        elif contact_name == 'contact':
            for key, value in queried_contacts.items():
                if _id in value['addresses'].keys():
                    keyname = _id

        if keyname:
            return {keyname: {"delay": delay, "schedule": schedule}}

        return {"error": "No matching key on account %s" % contact_name}

    # End of _get_group_schedule function

    return_contacts = []

    # Retrieve full contacts list for account/subaccount
    account_contacts = contacts.get_all(token, customerid=customerid)
    groups = group_contacts.get_all(token, customerid=customerid)

    # Compare each contact name and its addresses to the contact name
    # and addresses provided in playbook.
    for contact in notification_contacts:

        # If the key is group, then it is a group contact ID
        if contact.get('group'):
            return_contacts.append(_get_key_schedule('group', contact, groups))
        # If the key is contact, it is a contact ID
        elif contact.get('contact'):
            return_contacts.append(
                _get_key_schedule('contact', contact, account_contacts))
        # If the key is name, it is a contact name and we have to find the ID
        elif contact.get('name'):
            return_contacts.append(
                _get_contact_schedule(contact['name'], contact,
                                      account_contacts))

    return return_contacts