Custom Query FiltersΒΆ

The purpose of query filters is to identifty target resources which meet a particular set of conditions. They are a key piece of the DivvyCloud platform, and can be used to instruct a bot on what resources to take action on or report on resources. As of 17.03 there are over 175 query filters to take advantage of, and developers can extend the system and author their own.

As mentioned earlier, you can roll your own custom query filters. Doing so simply requires that you write 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']))