예제 #1
0
    def test_batch_get_assets_history(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = asset_service_pb2.BatchGetAssetsHistoryResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = asset_v1p2beta1.AssetServiceClient()

        # Setup Request
        parent = "parent-995424086"
        asset_names = []
        content_type = enums.ContentType.CONTENT_TYPE_UNSPECIFIED

        response = client.batch_get_assets_history(parent, asset_names,
                                                   content_type)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = asset_service_pb2.BatchGetAssetsHistoryRequest(
            parent=parent, asset_names=asset_names, content_type=content_type)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def batch_get_assets_history(
        self,
        parent,
        asset_names,
        content_type,
        read_time_window=None,
        retry=google.api_core.gapic_v1.method.DEFAULT,
        timeout=google.api_core.gapic_v1.method.DEFAULT,
        metadata=None,
    ):
        """
        Batch gets the update history of assets that overlap a time window. For
        RESOURCE content, this API outputs history with asset in both non-delete
        or deleted status. For IAM\_POLICY content, this API outputs history
        when the asset and its attached IAM POLICY both exist. This can create
        gaps in the output history.

        Example:
            >>> from google.cloud import asset_v1p2beta1
            >>> from google.cloud.asset_v1p2beta1 import enums
            >>>
            >>> client = asset_v1p2beta1.AssetServiceClient()
            >>>
            >>> # TODO: Initialize `parent`:
            >>> parent = ''
            >>>
            >>> # TODO: Initialize `asset_names`:
            >>> asset_names = []
            >>>
            >>> # TODO: Initialize `content_type`:
            >>> content_type = enums.ContentType.CONTENT_TYPE_UNSPECIFIED
            >>>
            >>> response = client.batch_get_assets_history(parent, asset_names, content_type)

        Args:
            parent (str): Required. The relative name of the root asset. It can only be an
                organization number (such as "organizations/123"), a project ID (such as
                "projects/my-project-id")", or a project number (such as "projects/12345").
            asset_names (list[str]): A list of the full names of the assets. For example:
                ``//compute.googleapis.com/projects/my_project_123/zones/zone1/instances/instance1``.
                See `Resource
                Names <https://cloud.google.com/apis/design/resource_names#full_resource_name>`__
                and `Resource Name
                Format <https://cloud.google.com/resource-manager/docs/cloud-asset-inventory/resource-name-format>`__
                for more info.

                The request becomes a no-op if the asset name list is empty, and the max
                size of the asset name list is 100 in one request.
            content_type (~google.cloud.asset_v1p2beta1.types.ContentType): Required. The content type.
            read_time_window (Union[dict, ~google.cloud.asset_v1p2beta1.types.TimeWindow]): Optional. The time window for the asset history. Both start\_time and
                end\_time are optional and if set, it must be after 2018-10-02 UTC. If
                end\_time is not set, it is default to current timestamp. If start\_time
                is not set, the snapshot of the assets at end\_time will be returned.
                The returned results contain all temporal assets whose time window
                overlap with read\_time\_window.

                If a dict is provided, it must be of the same form as the protobuf
                message :class:`~google.cloud.asset_v1p2beta1.types.TimeWindow`
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will not
                be retried.
            timeout (Optional[float]): 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.
            metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata
                that is provided to the method.

        Returns:
            A :class:`~google.cloud.asset_v1p2beta1.types.BatchGetAssetsHistoryResponse` instance.

        Raises:
            google.api_core.exceptions.GoogleAPICallError: If the request
                    failed for any reason.
            google.api_core.exceptions.RetryError: If the request failed due
                    to a retryable error and retry attempts failed.
            ValueError: If the parameters are invalid.
        """
        # Wrap the transport method to add retry and timeout logic.
        if "batch_get_assets_history" not in self._inner_api_calls:
            self._inner_api_calls[
                "batch_get_assets_history"] = google.api_core.gapic_v1.method.wrap_method(
                    self.transport.batch_get_assets_history,
                    default_retry=self.
                    _method_configs["BatchGetAssetsHistory"].retry,
                    default_timeout=self.
                    _method_configs["BatchGetAssetsHistory"].timeout,
                    client_info=self._client_info,
                )

        request = asset_service_pb2.BatchGetAssetsHistoryRequest(
            parent=parent,
            asset_names=asset_names,
            content_type=content_type,
            read_time_window=read_time_window,
        )
        if metadata is None:
            metadata = []
        metadata = list(metadata)
        try:
            routing_header = [("parent", parent)]
        except AttributeError:
            pass
        else:
            routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
                routing_header)
            metadata.append(routing_metadata)

        return self._inner_api_calls["batch_get_assets_history"](
            request, retry=retry, timeout=timeout, metadata=metadata)