示例#1
0
    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]
示例#2
0
    def list_alert_policies(
            self,
            project_id: str = PROVIDE_PROJECT_ID,
            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]] = (),
    ) -> Any:
        """
        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.

        :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 project_id: The project to fetch alerts from.
        """
        client = self._get_policy_client()
        policies_ = client.list_alert_policies(
            request={
                'name': f'projects/{project_id}',
                'filter': filter_,
                'order_by': order_by,
                'page_size': page_size,
            },
            retry=retry,
            timeout=timeout,
            metadata=metadata,
        )
        if format_ == "dict":
            return [AlertPolicy.to_dict(policy) for policy in policies_]
        elif format_ == "json":
            return [AlertPolicy.to_jsoon(policy) for policy in policies_]
        else:
            return policies_