Beispiel #1
0
async def add_contact_to_active_monitor(dbcon: DBConnection, contact_id: int,
                                        monitor_id: int) -> None:
    """Connect a contact and an active monitor."""
    if not await active_monitor_exists(dbcon, monitor_id):
        raise errors.InvalidArguments('monitor does not exist')
    if not await contact_exists(dbcon, contact_id):
        raise errors.InvalidArguments('contact does not exist')
    q = """replace into active_monitor_contacts (active_monitor_id, contact_id) values (%s, %s)"""
    q_args = (monitor_id, contact_id)
    await dbcon.operation(q, q_args)
Beispiel #2
0
async def delete_active_monitor_from_monitor_group(dbcon: DBConnection,
                                                   monitor_group_id: int,
                                                   monitor_id: int) -> None:
    """Remove an active monitor from a monitor group."""
    if not await active_monitor_exists(dbcon, monitor_id):
        raise errors.InvalidArguments('monitor does not exist')
    if not await monitor_group_exists(dbcon, monitor_group_id):
        raise errors.InvalidArguments('monitor_group does not exist')
    q = """delete from monitor_group_active_monitors where monitor_group_id=%s and active_monitor_id=%s"""
    q_args = (monitor_group_id, monitor_id)
    await dbcon.operation(q, q_args)
Beispiel #3
0
async def add_active_monitor_to_monitor_group(dbcon: DBConnection,
                                              monitor_group_id: int,
                                              monitor_id: int) -> None:
    """Connect a monitor_group and an active monitor."""
    if not await active_monitor_exists(dbcon, monitor_id):
        raise errors.InvalidArguments('monitor does not exist')
    if not await monitor_group_exists(dbcon, monitor_group_id):
        raise errors.InvalidArguments('monitor_group does not exist')
    q = """replace into monitor_group_active_monitors (monitor_group_id, active_monitor_id) values (%s, %s)"""
    q_args = (monitor_group_id, monitor_id)
    await dbcon.operation(q, q_args)
Beispiel #4
0
async def delete_contact_group_from_monitor_group(
        dbcon: DBConnection, monitor_group_id: int,
        contact_group_id: int) -> None:
    """Remove a contact group from a monitor group."""
    if not await contact_group_exists(dbcon, contact_group_id):
        raise errors.InvalidArguments('contact does not exist')
    if not await monitor_group_exists(dbcon, monitor_group_id):
        raise errors.InvalidArguments('monitor_group does not exist')
    q = """delete from monitor_group_contact_groups where monitor_group_id=%s and contact_group_id=%s"""
    q_args = (monitor_group_id, contact_group_id)
    await dbcon.operation(q, q_args)
Beispiel #5
0
async def add_contact_to_contact_group(dbcon: DBConnection,
                                       contact_group_id: int,
                                       contact_id: int) -> None:
    """Connect a contact and a contact group."""
    if not await contact_group_exists(dbcon, contact_group_id):
        raise errors.InvalidArguments('contact group does not exist')
    if not await contact_exists(dbcon, contact_id):
        raise errors.InvalidArguments('contact does not exist')
    q = """replace into contact_group_contacts (contact_group_id, contact_id) values (%s, %s)"""
    q_args = (contact_group_id, contact_id)
    await dbcon.operation(q, q_args)
Beispiel #6
0
 def validate_monitor_args(self,
                           monitor_args: Dict[str, str],
                           permit_missing: bool = False) -> bool:
     if not permit_missing:
         for arg in self.arg_spec:
             if arg.required and arg.name not in monitor_args:
                 raise errors.InvalidArguments('missing argument %s' %
                                               arg.name)
     arg_name_set = {a.name for a in self.arg_spec}
     for key, value in monitor_args.items():
         if key not in arg_name_set:
             raise errors.InvalidArguments('invalid argument %s' % key)
     return True
Beispiel #7
0
 async def _run(cur: Cursor) -> None:
     for key, value in data.items():
         if key not in ['parent_id', 'name']:
             raise errors.IrisettError('invalid monitor_group key %s' % key)
         if key == 'parent_id' and value:
             if monitor_group_id == int(value):
                 raise errors.InvalidArguments(
                     'monitor group can\'t be its own parent')
             if not await monitor_group_exists(dbcon, value):
                 raise errors.InvalidArguments(
                     'parent monitor group does not exist')
         q = """update monitor_groups set %s=%%s where id=%%s""" % key
         q_args = (value, monitor_group_id)
         await cur.execute(q, q_args)
Beispiel #8
0
async def create_monitor_group(dbcon: DBConnection, parent_id: Optional[int],
                               name: str):
    """Add a monitor group to the database."""
    if not name:
        raise errors.InvalidArguments('missing monitor group name')
    if parent_id:
        if not await monitor_group_exists(dbcon, parent_id):
            raise errors.InvalidArguments(
                'parent monitor group does not exist')
        q = """insert into monitor_groups (parent_id, name) values (%s, %s)"""
        q_args = (parent_id, name)  # type: Any
    else:
        q = """insert into monitor_groups (name) values (%s)"""
        q_args = (name, )
    group_id = await dbcon.operation(q, q_args)
    return group_id
Beispiel #9
0
async def delete_contact_group(dbcon: DBConnection,
                               contact_group_id: int) -> None:
    """Remove a contact group from the database."""
    if not await contact_group_exists(dbcon, contact_group_id):
        raise errors.InvalidArguments('contact group does not exist')
    q = """delete from contact_groups where id=%s"""
    await dbcon.operation(q, (contact_group_id, ))
Beispiel #10
0
async def update_contact_group(dbcon: DBConnection, contact_group_id: int,
                               data: Dict[str, str]) -> None:
    """Update a contact groups information in the database.

    Data is a dict with name/active values that will be updated.
    """
    async def _run(cur: Cursor) -> None:
        for key, value in data.items():
            if key not in ['name', 'active']:
                raise errors.IrisettError('invalid contact key %s' % key)
            q = """update contact_groups set %s=%%s where id=%%s""" % key
            q_args = (value, contact_group_id)
            await cur.execute(q, q_args)

    if not await contact_group_exists(dbcon, contact_group_id):
        raise errors.InvalidArguments('contact group does not exist')
    await dbcon.transact(_run)
Beispiel #11
0
async def set_contact_group_contacts(dbcon: DBConnection,
                                     contact_group_id: int,
                                     contact_ids: Iterable[int]) -> None:
    """(Re-)set contacts for a contact group.

    Delete existing contacts for a contact group and set the given new
    contacts.
    """
    async def _run(cur: Cursor) -> None:
        q = """delete from contact_group_contacts where contact_group_id=%s"""
        await cur.execute(q, (contact_group_id, ))
        for contact_id in contact_ids:
            q = """insert into contact_group_contacts (contact_group_id, contact_id) values (%s, %s)"""
            q_args = (contact_group_id, contact_id)
            await cur.execute(q, q_args)

    if not await contact_group_exists(dbcon, contact_group_id):
        raise errors.InvalidArguments('contact group does not exist')
    await dbcon.transact(_run)
Beispiel #12
0
async def set_active_monitor_contact_groups(dbcon: DBConnection,
                                            contact_group_ids: Iterable[int],
                                            monitor_id: int) -> None:
    """(Re-)set contact_groups for an active monitor.

    Delete existing contact groups for an active monitor and set the given new
    contact groups.
    """
    async def _run(cur: Cursor) -> None:
        q = """delete from active_monitor_contact_groups where active_monitor_id=%s"""
        await cur.execute(q, (monitor_id, ))
        for contact_group_id in contact_group_ids:
            q = """insert into active_monitor_contact_groups (active_monitor_id, contact_group_id) values (%s, %s)"""
            q_args = (monitor_id, contact_group_id)
            await cur.execute(q, q_args)

    if not await active_monitor_exists(dbcon, monitor_id):
        raise errors.InvalidArguments('monitor does not exist')
    await dbcon.transact(_run)