Skip to content

Commit

Permalink
Add Stackdriver assets and migrate system tests to AIP-47 (#23320)
Browse files Browse the repository at this point in the history
Change-Id: I6f751e6576f57a89a5145aeb05f506da8a22b379

Co-authored-by: Bartlomiej Hirsz <[email protected]>
  • Loading branch information
bhirsz and Bartlomiej Hirsz committed May 4, 2022
1 parent 7f40fa0 commit 627b569
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 71 deletions.
69 changes: 69 additions & 0 deletions airflow/providers/google/cloud/links/stackdriver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""This module contains Google Stackdriver links."""
from typing import TYPE_CHECKING, Optional

from airflow.models import BaseOperator
from airflow.providers.google.cloud.links.base import BaseGoogleLink

if TYPE_CHECKING:
from airflow.utils.context import Context

STACKDRIVER_BASE_LINK = "https://pantheon.corp.google.com/monitoring/alerting"
STACKDRIVER_NOTIFICATIONS_LINK = STACKDRIVER_BASE_LINK + "/notifications?project={project_id}"
STACKDRIVER_POLICIES_LINK = STACKDRIVER_BASE_LINK + "/policies?project={project_id}"


class StackdriverNotificationsLink(BaseGoogleLink):
"""Helper class for constructing Stackdriver Notifications Link"""

name = "Cloud Monitoring Notifications"
key = "stackdriver_notifications"
format_str = STACKDRIVER_NOTIFICATIONS_LINK

@staticmethod
def persist(
operator_instance: BaseOperator,
context: "Context",
project_id: Optional[str],
):
operator_instance.xcom_push(
context,
key=StackdriverNotificationsLink.key,
value={"project_id": project_id},
)


class StackdriverPoliciesLink(BaseGoogleLink):
"""Helper class for constructing Stackdriver Policies Link"""

name = "Cloud Monitoring Policies"
key = "stackdriver_policies"
format_str = STACKDRIVER_POLICIES_LINK

@staticmethod
def persist(
operator_instance: BaseOperator,
context: "Context",
project_id: Optional[str],
):
operator_instance.xcom_push(
context,
key=StackdriverPoliciesLink.key,
value={"project_id": project_id},
)
55 changes: 53 additions & 2 deletions airflow/providers/google/cloud/operators/stackdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

from airflow.models import BaseOperator
from airflow.providers.google.cloud.hooks.stackdriver import StackdriverHook
from airflow.providers.google.cloud.links.stackdriver import (
StackdriverNotificationsLink,
StackdriverPoliciesLink,
)

if TYPE_CHECKING:
from airflow.utils.context import Context
Expand Down Expand Up @@ -82,6 +86,7 @@ class StackdriverListAlertPoliciesOperator(BaseOperator):
'filter_',
'impersonation_chain',
)
operator_extra_links = (StackdriverPoliciesLink(),)
ui_color = "#e5ffcc"

def __init__(
Expand Down Expand Up @@ -140,6 +145,11 @@ def execute(self, context: 'Context'):
timeout=self.timeout,
metadata=self.metadata,
)
StackdriverPoliciesLink.persist(
context=context,
operator_instance=self,
project_id=self.project_id or self.hook.project_id,
)
return [AlertPolicy.to_dict(policy) for policy in result]


Expand Down Expand Up @@ -182,6 +192,7 @@ class StackdriverEnableAlertPoliciesOperator(BaseOperator):
'filter_',
'impersonation_chain',
)
operator_extra_links = (StackdriverPoliciesLink(),)

def __init__(
self,
Expand Down Expand Up @@ -222,6 +233,11 @@ def execute(self, context: 'Context'):
timeout=self.timeout,
metadata=self.metadata,
)
StackdriverPoliciesLink.persist(
context=context,
operator_instance=self,
project_id=self.project_id or self.hook.project_id,
)


# Disable Alert Operator
Expand Down Expand Up @@ -264,6 +280,7 @@ class StackdriverDisableAlertPoliciesOperator(BaseOperator):
'filter_',
'impersonation_chain',
)
operator_extra_links = (StackdriverPoliciesLink(),)

def __init__(
self,
Expand Down Expand Up @@ -304,6 +321,11 @@ def execute(self, context: 'Context'):
timeout=self.timeout,
metadata=self.metadata,
)
StackdriverPoliciesLink.persist(
context=context,
operator_instance=self,
project_id=self.project_id or self.hook.project_id,
)


class StackdriverUpsertAlertOperator(BaseOperator):
Expand Down Expand Up @@ -346,6 +368,7 @@ class StackdriverUpsertAlertOperator(BaseOperator):
'impersonation_chain',
)
template_ext: Sequence[str] = ('.json',)
operator_extra_links = (StackdriverPoliciesLink(),)

ui_color = "#e5ffcc"

Expand Down Expand Up @@ -388,6 +411,11 @@ def execute(self, context: 'Context'):
timeout=self.timeout,
metadata=self.metadata,
)
StackdriverPoliciesLink.persist(
context=context,
operator_instance=self,
project_id=self.project_id or self.hook.project_id,
)


class StackdriverDeleteAlertOperator(BaseOperator):
Expand Down Expand Up @@ -522,6 +550,7 @@ class StackdriverListNotificationChannelsOperator(BaseOperator):
'filter_',
'impersonation_chain',
)
operator_extra_links = (StackdriverNotificationsLink(),)

ui_color = "#e5ffcc"

Expand Down Expand Up @@ -580,8 +609,12 @@ def execute(self, context: 'Context'):
timeout=self.timeout,
metadata=self.metadata,
)
result = [NotificationChannel.to_dict(channel) for channel in channels]
return result
StackdriverNotificationsLink.persist(
context=context,
operator_instance=self,
project_id=self.project_id or self.hook.project_id,
)
return [NotificationChannel.to_dict(channel) for channel in channels]


class StackdriverEnableNotificationChannelsOperator(BaseOperator):
Expand Down Expand Up @@ -622,6 +655,7 @@ class StackdriverEnableNotificationChannelsOperator(BaseOperator):
'filter_',
'impersonation_chain',
)
operator_extra_links = (StackdriverNotificationsLink(),)

ui_color = "#e5ffcc"

Expand Down Expand Up @@ -666,6 +700,11 @@ def execute(self, context: 'Context'):
timeout=self.timeout,
metadata=self.metadata,
)
StackdriverNotificationsLink.persist(
context=context,
operator_instance=self,
project_id=self.project_id or self.hook.project_id,
)


class StackdriverDisableNotificationChannelsOperator(BaseOperator):
Expand Down Expand Up @@ -706,6 +745,7 @@ class StackdriverDisableNotificationChannelsOperator(BaseOperator):
'filter_',
'impersonation_chain',
)
operator_extra_links = (StackdriverNotificationsLink(),)

ui_color = "#e5ffcc"

Expand Down Expand Up @@ -750,6 +790,11 @@ def execute(self, context: 'Context'):
timeout=self.timeout,
metadata=self.metadata,
)
StackdriverNotificationsLink.persist(
context=context,
operator_instance=self,
project_id=self.project_id or self.hook.project_id,
)


class StackdriverUpsertNotificationChannelOperator(BaseOperator):
Expand Down Expand Up @@ -792,6 +837,7 @@ class StackdriverUpsertNotificationChannelOperator(BaseOperator):
'impersonation_chain',
)
template_ext: Sequence[str] = ('.json',)
operator_extra_links = (StackdriverNotificationsLink(),)

ui_color = "#e5ffcc"

Expand Down Expand Up @@ -836,6 +882,11 @@ def execute(self, context: 'Context'):
timeout=self.timeout,
metadata=self.metadata,
)
StackdriverNotificationsLink.persist(
context=context,
operator_instance=self,
project_id=self.project_id or self.hook.project_id,
)


class StackdriverDeleteNotificationChannelOperator(BaseOperator):
Expand Down
2 changes: 2 additions & 0 deletions airflow/providers/google/provider.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,8 @@ extra-links:
- airflow.providers.google.cloud.links.bigtable.BigtableInstanceLink
- airflow.providers.google.cloud.links.bigtable.BigtableClusterLink
- airflow.providers.google.cloud.links.bigtable.BigtableTablesLink
- airflow.providers.google.cloud.links.stackdriver.StackdriverNotificationsLink
- airflow.providers.google.cloud.links.stackdriver.StackdriverPoliciesLink
- airflow.providers.google.common.links.storage.StorageLink
- airflow.providers.google.common.links.storage.FileDetailsLink

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ 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.

.. exampleinclude:: /../../airflow/providers/google/cloud/example_dags/example_stackdriver.py
.. exampleinclude:: /../../tests/system/providers/google/stackdriver/example_stackdriver.py
:language: python
:dedent: 4
:start-after: [START howto_operator_gcp_stackdriver_list_alert_policy]
Expand All @@ -60,7 +60,7 @@ 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.

.. exampleinclude:: /../../airflow/providers/google/cloud/example_dags/example_stackdriver.py
.. exampleinclude:: /../../tests/system/providers/google/stackdriver/example_stackdriver.py
:language: python
:dedent: 4
:start-after: [START howto_operator_gcp_stackdriver_enable_alert_policy]
Expand All @@ -80,7 +80,7 @@ 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.

.. exampleinclude:: /../../airflow/providers/google/cloud/example_dags/example_stackdriver.py
.. exampleinclude:: /../../tests/system/providers/google/stackdriver/example_stackdriver.py
:language: python
:dedent: 4
:start-after: [START howto_operator_gcp_stackdriver_disable_alert_policy]
Expand All @@ -101,7 +101,7 @@ 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.

.. exampleinclude:: /../../airflow/providers/google/cloud/example_dags/example_stackdriver.py
.. exampleinclude:: /../../tests/system/providers/google/stackdriver/example_stackdriver.py
:language: python
:dedent: 4
:start-after: [START howto_operator_gcp_stackdriver_upsert_alert_policy]
Expand All @@ -120,7 +120,7 @@ Using the operator

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

.. exampleinclude:: /../../airflow/providers/google/cloud/example_dags/example_stackdriver.py
.. exampleinclude:: /../../tests/system/providers/google/stackdriver/example_stackdriver.py
:language: python
:dedent: 4
:start-after: [START howto_operator_gcp_stackdriver_delete_alert_policy]
Expand All @@ -140,7 +140,7 @@ 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.

.. exampleinclude:: /../../airflow/providers/google/cloud/example_dags/example_stackdriver.py
.. exampleinclude:: /../../tests/system/providers/google/stackdriver/example_stackdriver.py
:language: python
:dedent: 4
:start-after: [START howto_operator_gcp_stackdriver_list_notification_channel]
Expand All @@ -160,7 +160,7 @@ 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.

.. exampleinclude:: /../../airflow/providers/google/cloud/example_dags/example_stackdriver.py
.. exampleinclude:: /../../tests/system/providers/google/stackdriver/example_stackdriver.py
:language: python
:dedent: 4
:start-after: [START howto_operator_gcp_stackdriver_enable_notification_channel]
Expand All @@ -180,7 +180,7 @@ 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.

.. exampleinclude:: /../../airflow/providers/google/cloud/example_dags/example_stackdriver.py
.. exampleinclude:: /../../tests/system/providers/google/stackdriver/example_stackdriver.py
:language: python
:dedent: 4
:start-after: [START howto_operator_gcp_stackdriver_disable_notification_channel]
Expand All @@ -201,7 +201,7 @@ 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.

.. exampleinclude:: /../../airflow/providers/google/cloud/example_dags/example_stackdriver.py
.. exampleinclude:: /../../tests/system/providers/google/stackdriver/example_stackdriver.py
:language: python
:dedent: 4
:start-after: [START howto_operator_gcp_stackdriver_disable_notification_channel]
Expand All @@ -220,7 +220,7 @@ 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.

.. exampleinclude:: /../../airflow/providers/google/cloud/example_dags/example_stackdriver.py
.. exampleinclude:: /../../tests/system/providers/google/stackdriver/example_stackdriver.py
:language: python
:dedent: 4
:start-after: [START howto_operator_gcp_stackdriver_delete_notification_channel]
Expand Down

0 comments on commit 627b569

Please sign in to comment.