FinalDivvyCloud

Custom Query Filters

Query filters identify target resources that meet a particular set of conditions. They can be used individually, or be linked together (like lego blocks). These filters can answer questions about your data in the form of an insight, or can instruct a bot as to what resources to take action on.

As of 18.7, there are over 600 query filters available out-of-the-box and developers can extend the system by writing their own.

When you create your own custom query filter, you can add it to your own plugin and drop it into the plugins/ subdirectory.

from DivvyResource.resource_types import ResourceType
from DivvyUtils.field_definition import MultiSelectionField, BooleanField
from DivvyUtils.field_definition import FieldOptions

from registry import QueryRegistry

@QueryRegistry.register(
    query_id='divvy.query.example_instance_image_id',
    name='Example Instance Image ID Filter',
    resource_types=[ResourceType.INSTANCE],
    description=(
        'Identify instances running a particular image ID'
    ),
    settings_config=[
        MultiSelectionField(
            choices=[],
            name='image_ids',
            display_name='Image IDs',
            description=(
                'Enter one or more image IDs to filter on'
            ),
            options=[FieldOptions.REQUIRED, FieldOptions.TAGS]
        ),
        BooleanField(
            name='not_in',
            display_name='Instances Not In',
            description=(
                'When enabled, this will identifty instances not running one '
                'of the supplied image ID values.'
            )
        )
    ]
)
def example_instance_image_id(query, db_cls, settings_config):
    if settings_config.get('not_in', False) is True:
        return query.filter(db_cls.image_id.notin_(settings_config['image_ids']))
    return query.filter(db_cls.image_id.in_(settings_config['image_ids']))