Google Cloud Stackdriver Operators

Prerequisite Tasks

To use these operators, you must do a few things:

StackdriverListAlertPoliciesOperator

Use the StackdriverListAlertPoliciesOperator to fetch all the Alert Policies identified by given filter.

Using the operator

You can use this operator with or without project id to fetch all the alert policies. If project id is missing it will be retrieved from Google Cloud connection used.

tests/system/providers/google/cloud/stackdriver/example_stackdriver.py[source]

list_alert_policies = StackdriverListAlertPoliciesOperator(
    task_id="list-alert-policies",
)

StackdriverEnableAlertPoliciesOperator

Use the StackdriverEnableAlertPoliciesOperator to enable Alert Policies identified by given filter.

Using the operator

You can use this operator with or without project id to fetch all the alert policies. If project id is missing it will be retrieved from Google Cloud connection used.

tests/system/providers/google/cloud/stackdriver/example_stackdriver.py[source]

enable_alert_policy = StackdriverEnableAlertPoliciesOperator(
    task_id="enable-alert-policies",
    filter_=f'(displayName="{ALERT_1_NAME}" OR displayName="{ALERT_2_NAME}")',
)

StackdriverDisableAlertPoliciesOperator

Use the StackdriverDisableAlertPoliciesOperator to disable Alert Policies identified by given filter.

Using the operator

You can use this operator with or without project id to fetch all the alert policies. If project id is missing it will be retrieved from Google Cloud connection used.

tests/system/providers/google/cloud/stackdriver/example_stackdriver.py[source]

disable_alert_policy = StackdriverDisableAlertPoliciesOperator(
    task_id="disable-alert-policies",
    filter_=f'displayName="{ALERT_1_NAME}"',
)

StackdriverUpsertAlertOperator

Use the StackdriverUpsertAlertOperator to upsert Alert Policies identified by given filter JSON string. If the alert with the give name already exists, then the operator updates the existing policy otherwise creates a new one.

Using the operator

You can use this operator with or without project id to fetch all the alert policies. If project id is missing it will be retrieved from Google Cloud connection used.

tests/system/providers/google/cloud/stackdriver/example_stackdriver.py[source]

create_alert_policy = StackdriverUpsertAlertOperator(
    task_id="create-alert-policies",
    alerts=json.dumps({"policies": [TEST_ALERT_POLICY_1, TEST_ALERT_POLICY_2]}),
)

StackdriverDeleteAlertOperator

Use the StackdriverDeleteAlertOperator to delete an Alert Policy identified by given name.

Using the operator

The name of the alert to be deleted should be given in the format projects/<PROJECT_NAME>/alertPolicies/<ALERT_NAME>

tests/system/providers/google/cloud/stackdriver/example_stackdriver.py[source]

delete_alert_policy = StackdriverDeleteAlertOperator(
    task_id="delete-alert-policy",
    name="{{ task_instance.xcom_pull('list-alert-policies')[0]['name'] }}",
)

StackdriverListNotificationChannelsOperator

Use the StackdriverListNotificationChannelsOperator to fetch all the Notification Channels identified by given filter.

Using the operator

You can use this operator with or without project id to fetch all the alert policies. If project id is missing it will be retrieved from Google Cloud connection used.

tests/system/providers/google/cloud/stackdriver/example_stackdriver.py[source]

list_notification_channel = StackdriverListNotificationChannelsOperator(
    task_id="list-notification-channel", filter_='type="pubsub"'
)

StackdriverEnableNotificationChannelsOperator

Use the StackdriverEnableNotificationChannelsOperator to enable Notification Channels identified by given filter.

Using the operator

You can use this operator with or without project id to fetch all the alert policies. If project id is missing it will be retrieved from Google Cloud connection used.

tests/system/providers/google/cloud/stackdriver/example_stackdriver.py[source]

enable_notification_channel = StackdriverEnableNotificationChannelsOperator(
    task_id="enable-notification-channel", filter_='type="pubsub"'
)

StackdriverDisableNotificationChannelsOperator

Use the StackdriverDisableNotificationChannelsOperator to disable Notification Channels identified by given filter.

Using the operator

You can use this operator with or without project id to fetch all the alert policies. If project id is missing it will be retrieved from Google Cloud connection used.

tests/system/providers/google/cloud/stackdriver/example_stackdriver.py[source]

disable_notification_channel = StackdriverDisableNotificationChannelsOperator(
    task_id="disable-notification-channel", filter_=f'displayName="{CHANNEL_1_NAME}"'
)

StackdriverUpsertNotificationChannelOperator

Use the StackdriverUpsertNotificationChannelOperator to upsert Notification Channels identified by given channel JSON string. If the channel with the give name already exists, then the operator updates the existing channel otherwise creates a new one.

Using the operator

You can use this operator with or without project id to fetch all the alert policies. If project id is missing it will be retrieved from Google Cloud connection used.

tests/system/providers/google/cloud/stackdriver/example_stackdriver.py[source]

disable_notification_channel = StackdriverDisableNotificationChannelsOperator(
    task_id="disable-notification-channel", filter_=f'displayName="{CHANNEL_1_NAME}"'
)

StackdriverDeleteNotificationChannelOperator

The name of the alert to be deleted should be given in the format projects/<PROJECT_NAME>/notificationChannels/<CHANNEL_NAME>

Using the operator

You can use this operator with or without project id to fetch all the alert policies. If project id is missing it will be retrieved from Google Cloud connection used.

tests/system/providers/google/cloud/stackdriver/example_stackdriver.py[source]

delete_notification_channel = StackdriverDeleteNotificationChannelOperator(
    task_id="delete-notification-channel",
    name="{{ task_instance.xcom_pull('list-notification-channel')[0]['name'] }}",
)

Was this entry helpful?