Google Cloud Stackdriver Operators¶
Prerequisite Tasks¶
To use these operators, you must do a few things:
Select or create a Cloud Platform project using the Cloud Console.
Enable billing for your project, as described in the Google Cloud documentation.
Enable the API, as described in the Cloud Console documentation.
Install API libraries via pip.
pip install 'apache-airflow[google]'Detailed information is available for Installation.
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.
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.
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.
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.
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>
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.
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.
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.
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.
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.
delete_notification_channel = StackdriverDeleteNotificationChannelOperator(
task_id="delete-notification-channel",
name="{{ task_instance.xcom_pull('list-notification-channel')[0]['name'] }}",
)