Esempio n. 1
0
class StackdriverListAlertPoliciesOperator(BaseOperator):
    """
    Fetches all the Alert Policies identified by the filter passed as
    filter parameter. The desired return type can be specified by the
    format parameter, the supported formats are "dict", "json" and None
    which returns python dictionary, stringified JSON and protobuf
    respectively.

    .. seealso::
        For more information on how to use this operator, take a look at the guide:
        :ref:`howto/operator:StackdriverListAlertPoliciesOperator`

    :param format_: (Optional) Desired output format of the result. The
        supported formats are "dict", "json" and None which returns
        python dictionary, stringified JSON and protobuf respectively.
    :param filter_:  If provided, this field specifies the criteria that must be met by alert
        policies to be included in the response.
        For more details, see https://cloud.google.com/monitoring/api/v3/sorting-and-filtering.
    :param order_by: A comma-separated list of fields by which to sort the result.
        Supports the same set of field references as the ``filter`` field. Entries
        can be prefixed with a minus sign to sort by the field in descending order.
        For more details, see https://cloud.google.com/monitoring/api/v3/sorting-and-filtering.
    :param page_size: The maximum number of resources contained in the
        underlying API response. If page streaming is performed per-
        resource, this parameter does not affect the return value. If page
        streaming is performed per-page, this determines the maximum number
        of resources in a page.
    :param retry: A retry object used to retry requests. If ``None`` is
        specified, requests will be retried using a default configuration.
    :param timeout: The amount of time, in seconds, to wait
        for the request to complete. Note that if ``retry`` is
        specified, the timeout applies to each individual attempt.
    :param metadata: Additional metadata that is provided to the method.
    :param gcp_conn_id: (Optional) The connection ID used to connect to Google
        Cloud Platform.
    :param project_id: The project to fetch alerts from.
    :param delegate_to: The account to impersonate using domain-wide delegation of authority,
        if any. For this to work, the service account making the request must have
        domain-wide delegation enabled.
    :param impersonation_chain: Optional service account to impersonate using short-term
        credentials, or chained list of accounts required to get the access_token
        of the last account in the list, which will be impersonated in the request.
        If set as a string, the account must grant the originating account
        the Service Account Token Creator IAM role.
        If set as a sequence, the identities from the list must grant
        Service Account Token Creator IAM role to the directly preceding identity, with first
        account from the list granting this role to the originating account (templated).
    """

    template_fields: Sequence[str] = (
        'filter_',
        'impersonation_chain',
    )
    ui_color = "#e5ffcc"

    def __init__(
        self,
        *,
        format_: Optional[str] = None,
        filter_: Optional[str] = None,
        order_by: Optional[str] = None,
        page_size: Optional[int] = None,
        retry: Optional[str] = DEFAULT,
        timeout: Optional[float] = None,
        metadata: Sequence[Tuple[str, str]] = (),
        gcp_conn_id: str = 'google_cloud_default',
        project_id: Optional[str] = None,
        delegate_to: Optional[str] = None,
        impersonation_chain: Optional[Union[str, Sequence[str]]] = None,
        **kwargs,
    ) -> None:
        super().__init__(**kwargs)
        self.format_ = format_
        self.filter_ = filter_
        self.order_by = order_by
        self.page_size = page_size
        self.retry = retry
        self.timeout = timeout
        self.metadata = metadata
        self.gcp_conn_id = gcp_conn_id
        self.project_id = project_id
        self.delegate_to = delegate_to
        self.impersonation_chain = impersonation_chain
        self.hook: Optional[StackdriverHook] = None

    def execute(self, context: 'Context'):
        self.log.info(
            'List Alert Policies: Project id: %s Format: %s Filter: %s Order By: %s Page Size: %s',
            self.project_id,
            self.format_,
            self.filter_,
            self.order_by,
            self.page_size,
        )
        if self.hook is None:
            self.hook = StackdriverHook(
                gcp_conn_id=self.gcp_conn_id,
                delegate_to=self.delegate_to,
                impersonation_chain=self.impersonation_chain,
            )

        result = self.hook.list_alert_policies(
            project_id=self.project_id,
            format_=self.format_,
            filter_=self.filter_,
            order_by=self.order_by,
            page_size=self.page_size,
            retry=self.retry,
            timeout=self.timeout,
            metadata=self.metadata,
        )
        return [AlertPolicy.to_dict(policy) for policy in result]
Esempio n. 2
0
class StackdriverListAlertPoliciesOperator(BaseOperator):
    """
    Fetches all the Alert Policies identified by the filter passed as
    filter parameter. The desired return type can be specified by the
    format parameter, the supported formats are "dict", "json" and None
    which returns python dictionary, stringified JSON and protobuf
    respectively.

    .. seealso::
        For more information on how to use this operator, take a look at the guide:
        :ref:`howto/operator:StackdriverListAlertPoliciesOperator`

    :param format_: (Optional) Desired output format of the result. The
        supported formats are "dict", "json" and None which returns
        python dictionary, stringified JSON and protobuf respectively.
    :type format_: str
    :param filter_:  If provided, this field specifies the criteria that must be met by alert
        policies to be included in the response.
        For more details, see https://cloud.google.com/monitoring/api/v3/sorting-and-filtering.
    :type filter_: str
    :param order_by: A comma-separated list of fields by which to sort the result.
        Supports the same set of field references as the ``filter`` field. Entries
        can be prefixed with a minus sign to sort by the field in descending order.
        For more details, see https://cloud.google.com/monitoring/api/v3/sorting-and-filtering.
    :type order_by: str
    :param page_size: The maximum number of resources contained in the
        underlying API response. If page streaming is performed per-
        resource, this parameter does not affect the return value. If page
        streaming is performed per-page, this determines the maximum number
        of resources in a page.
    :type page_size: int
    :param retry: A retry object used to retry requests. If ``None`` is
        specified, requests will be retried using a default configuration.
    :type retry: str
    :param timeout: The amount of time, in seconds, to wait
        for the request to complete. Note that if ``retry`` is
        specified, the timeout applies to each individual attempt.
    :type timeout: float
    :param metadata: Additional metadata that is provided to the method.
    :type metadata: str
    :param gcp_conn_id: (Optional) The connection ID used to connect to Google
        Cloud Platform.
    :type gcp_conn_id: str
    :param project_id: The project to fetch alerts from.
    :type project_id: str
    :param delegate_to: (Optional) The account to impersonate, if any.
        For this to work, the service account making the request must have
        domain-wide delegation enabled.
    :type delegate_to: str
    """

    template_fields = ('filter_', )
    ui_color = "#e5ffcc"

    # pylint: disable=too-many-arguments
    @apply_defaults
    def __init__(self,
                 format_: Optional[str] = None,
                 filter_: Optional[str] = None,
                 order_by: Optional[str] = None,
                 page_size: Optional[int] = None,
                 retry: Optional[str] = DEFAULT,
                 timeout: Optional[float] = DEFAULT,
                 metadata: Optional[str] = None,
                 gcp_conn_id: Optional[str] = 'google_cloud_default',
                 project_id: Optional[str] = None,
                 delegate_to: Optional[str] = None,
                 **kwargs):
        super().__init__(**kwargs)
        self.format_ = format_
        self.filter_ = filter_
        self.order_by = order_by
        self.page_size = page_size
        self.retry = retry
        self.timeout = timeout
        self.metadata = metadata
        self.gcp_conn_id = gcp_conn_id
        self.project_id = project_id
        self.delegate_to = delegate_to
        self.hook = None

    def execute(self, context):
        self.log.info(
            'List Alert Policies: Project id: %s Format: %s Filter: %s Order By: %s Page Size: %d',
            self.project_id, self.format_, self.filter_, self.order_by,
            self.page_size)
        if self.hook is None:
            self.hook = StackdriverHook(gcp_conn_id=self.gcp_conn_id,
                                        delegate_to=self.delegate_to)

        return self.hook.list_alert_policies(project_id=self.project_id,
                                             format_=self.format_,
                                             filter_=self.filter_,
                                             order_by=self.order_by,
                                             page_size=self.page_size,
                                             retry=self.retry,
                                             timeout=self.timeout,
                                             metadata=self.metadata)