Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: add search_all_resources and search_all_iam_policies (via s…
…ynth) (#32)

* changes without context

        autosynth cannot find the source of changes triggered by earlier changes in this
        repository, or by version upgrades to tools such as linters.

* fix: consolidate logging setup (#541)

* fix: consolidate logging setup

When switching autosynth to use synthtool's git module, we transitively
load synthtool's logger setup which adds a log handler globally. This
causes logs to be emitted twice (with 2 different styles).

This consolidates the logging setup into a single helper and now both
autosynth's and synthtool's logger instance use the same configuration
(with different logger names).

* fix: add the level name when the log isn't colorize

Source-Author: Jeff Ching <chingor@google.com>
Source-Date: Tue May 12 15:53:43 2020 -0700
Source-Repo: googleapis/synthtool
Source-Sha: c585ac3b5eff5cd2097a5315ffd9cf4823cc1ed2
Source-Link: googleapis/synthtool@c585ac3

* cloudasset: add SearchAllResources and SearchAllIamPolicies RPCs

PiperOrigin-RevId: 311601348

Source-Author: Google APIs <noreply@google.com>
Source-Date: Thu May 14 14:03:19 2020 -0700
Source-Repo: googleapis/googleapis
Source-Sha: 2433bd50656264a2ef9f684bf646fb4d250d39ff
Source-Link: googleapis/googleapis@2433bd5

* feat!: additional client library annotations
feat: reload information in the Document resource
BREAKING CHANGE: additional long-running operation annotations
BREAKING CHANGE: some existing request fields are now explicitly required
BREAKING CHANGE: the order of the Session resource patterns has changed

PiperOrigin-RevId: 312345206

Source-Author: Google APIs <noreply@google.com>
Source-Date: Tue May 19 13:46:01 2020 -0700
Source-Repo: googleapis/googleapis
Source-Sha: 7a8875d4bcf51993f6f7aa181066cc5ec95539d5
Source-Link: googleapis/googleapis@7a8875d

* Upgrade protoc-docs-plugin version to 0.7.0 to fix unstable docstrings.

PiperOrigin-RevId: 312689208

Source-Author: Google APIs <noreply@google.com>
Source-Date: Thu May 21 10:00:47 2020 -0700
Source-Repo: googleapis/googleapis
Source-Sha: dec3204175104cef49bf21d685d5517caaf0058f
Source-Link: googleapis/googleapis@dec3204

* docs: Properly format literal strings.

PiperOrigin-RevId: 313202929

Source-Author: Google APIs <noreply@google.com>
Source-Date: Tue May 26 09:12:28 2020 -0700
Source-Repo: googleapis/googleapis
Source-Sha: f563b1dffee42be2f61c326c39af4974c1984278
Source-Link: googleapis/googleapis@f563b1d

* docs: fix wrapping

Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com>
Co-authored-by: Bu Sun Kim <busunkim@google.com>
  • Loading branch information
3 people committed Jun 10, 2020
1 parent 43f13ec commit 24a0827
Show file tree
Hide file tree
Showing 16 changed files with 1,843 additions and 250 deletions.
304 changes: 304 additions & 0 deletions google/cloud/asset_v1/gapic/asset_service_client.py
Expand Up @@ -16,6 +16,7 @@

"""Accesses the google.cloud.asset.v1 AssetService API."""

import functools
import pkg_resources
import warnings

Expand All @@ -28,6 +29,7 @@
import google.api_core.grpc_helpers
import google.api_core.operation
import google.api_core.operations_v1
import google.api_core.page_iterator
import google.api_core.path_template
import grpc

Expand Down Expand Up @@ -830,3 +832,305 @@ def update_feed(
return self._inner_api_calls["update_feed"](
request, retry=retry, timeout=timeout, metadata=metadata
)

def search_all_resources(
self,
scope,
query=None,
asset_types=None,
page_size=None,
order_by=None,
retry=google.api_core.gapic_v1.method.DEFAULT,
timeout=google.api_core.gapic_v1.method.DEFAULT,
metadata=None,
):
"""
Searches all the resources within the given accessible scope (e.g., a
project, a folder or an organization). Callers should have
cloud.assets.SearchAllResources permission upon the requested scope,
otherwise the request will be rejected.
Example:
>>> from google.cloud import asset_v1
>>>
>>> client = asset_v1.AssetServiceClient()
>>>
>>> # TODO: Initialize `scope`:
>>> scope = ''
>>>
>>> # Iterate over all results
>>> for element in client.search_all_resources(scope):
... # process element
... pass
>>>
>>>
>>> # Alternatively:
>>>
>>> # Iterate over results one page at a time
>>> for page in client.search_all_resources(scope).pages:
... for element in page:
... # process element
... pass
Args:
scope (str): Required. A scope can be a project, a folder or an organization. The
search is limited to the resources within the ``scope``.
The allowed values are:
- projects/{PROJECT_ID}
- projects/{PROJECT_NUMBER}
- folders/{FOLDER_NUMBER}
- organizations/{ORGANIZATION_NUMBER}
query (str): Optional. The query statement. An empty query can be specified to
search all the resources of certain ``asset_types`` within the given
``scope``.
Examples:
- ``name : "Important"`` to find Cloud resources whose name contains
"Important" as a word.
- ``displayName : "Impor*"`` to find Cloud resources whose display name
contains "Impor" as a word prefix.
- ``description : "*por*"`` to find Cloud resources whose description
contains "por" as a substring.
- ``location : "us-west*"`` to find Cloud resources whose location is
prefixed with "us-west".
- ``labels : "prod"`` to find Cloud resources whose labels contain
"prod" as a key or value.
- ``labels.env : "prod"`` to find Cloud resources which have a label
"env" and its value is "prod".
- ``labels.env : *`` to find Cloud resources which have a label "env".
- ``"Important"`` to find Cloud resources which contain "Important" as
a word in any of the searchable fields.
- ``"Impor*"`` to find Cloud resources which contain "Impor" as a word
prefix in any of the searchable fields.
- ``"*por*"`` to find Cloud resources which contain "por" as a
substring in any of the searchable fields.
- ``("Important" AND location : ("us-west1" OR "global"))`` to find
Cloud resources which contain "Important" as a word in any of the
searchable fields and are also located in the "us-west1" region or
the "global" location.
See `how to construct a
query <https://cloud.google.com/asset-inventory/docs/searching-resources#how_to_construct_a_query>`__
for more details.
asset_types (list[str]): Optional. A list of asset types that this request searches for. If
empty, it will search all the `searchable asset
types <https://cloud.google.com/asset-inventory/docs/supported-asset-types#searchable_asset_types>`__.
page_size (int): 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.
order_by (str): Optional. A comma separated list of fields specifying the sorting
order of the results. The default order is ascending. Add " DESC" after
the field name to indicate descending order. Redundant space characters
are ignored. Example: "location DESC, name". See `supported resource
metadata
fields <https://cloud.google.com/asset-inventory/docs/searching-resources#query_on_resource_metadata_fields>`__
for more details.
retry (Optional[google.api_core.retry.Retry]): A retry object used
to retry requests. If ``None`` is specified, requests will
be retried using a default configuration.
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.api_core.page_iterator.PageIterator` instance.
An iterable of :class:`~google.cloud.asset_v1.types.ResourceSearchResult` instances.
You can also iterate over the pages of the response
using its `pages` property.
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 "search_all_resources" not in self._inner_api_calls:
self._inner_api_calls[
"search_all_resources"
] = google.api_core.gapic_v1.method.wrap_method(
self.transport.search_all_resources,
default_retry=self._method_configs["SearchAllResources"].retry,
default_timeout=self._method_configs["SearchAllResources"].timeout,
client_info=self._client_info,
)

request = asset_service_pb2.SearchAllResourcesRequest(
scope=scope,
query=query,
asset_types=asset_types,
page_size=page_size,
order_by=order_by,
)
if metadata is None:
metadata = []
metadata = list(metadata)
try:
routing_header = [("scope", scope)]
except AttributeError:
pass
else:
routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
routing_header
)
metadata.append(routing_metadata)

iterator = google.api_core.page_iterator.GRPCIterator(
client=None,
method=functools.partial(
self._inner_api_calls["search_all_resources"],
retry=retry,
timeout=timeout,
metadata=metadata,
),
request=request,
items_field="results",
request_token_field="page_token",
response_token_field="next_page_token",
)
return iterator

def search_all_iam_policies(
self,
scope,
query=None,
page_size=None,
retry=google.api_core.gapic_v1.method.DEFAULT,
timeout=google.api_core.gapic_v1.method.DEFAULT,
metadata=None,
):
"""
Searches all the IAM policies within the given accessible scope (e.g., a
project, a folder or an organization). Callers should have
cloud.assets.SearchAllIamPolicies permission upon the requested scope,
otherwise the request will be rejected.
Example:
>>> from google.cloud import asset_v1
>>>
>>> client = asset_v1.AssetServiceClient()
>>>
>>> # TODO: Initialize `scope`:
>>> scope = ''
>>>
>>> # Iterate over all results
>>> for element in client.search_all_iam_policies(scope):
... # process element
... pass
>>>
>>>
>>> # Alternatively:
>>>
>>> # Iterate over results one page at a time
>>> for page in client.search_all_iam_policies(scope).pages:
... for element in page:
... # process element
... pass
Args:
scope (str): Required. A scope can be a project, a folder or an organization. The
search is limited to the IAM policies within the ``scope``.
The allowed values are:
- projects/{PROJECT_ID}
- projects/{PROJECT_NUMBER}
- folders/{FOLDER_NUMBER}
- organizations/{ORGANIZATION_NUMBER}
query (str): Optional. The query statement. An empty query can be specified to
search all the IAM policies within the given ``scope``.
Examples:
- ``policy : "amy@gmail.com"`` to find Cloud IAM policy bindings that
specify user "amy@gmail.com".
- ``policy : "roles/compute.admin"`` to find Cloud IAM policy bindings
that specify the Compute Admin role.
- ``policy.role.permissions : "storage.buckets.update"`` to find Cloud
IAM policy bindings that specify a role containing
"storage.buckets.update" permission.
- ``resource : "organizations/123"`` to find Cloud IAM policy bindings
that are set on "organizations/123".
- ``(resource : ("organizations/123" OR "folders/1234") AND policy : "amy")``
to find Cloud IAM policy bindings that are set on "organizations/123"
or "folders/1234", and also specify user "amy".
See `how to construct a
query <https://cloud.google.com/asset-inventory/docs/searching-iam-policies#how_to_construct_a_query>`__
for more details.
page_size (int): 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.
retry (Optional[google.api_core.retry.Retry]): A retry object used
to retry requests. If ``None`` is specified, requests will
be retried using a default configuration.
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.api_core.page_iterator.PageIterator` instance.
An iterable of :class:`~google.cloud.asset_v1.types.IamPolicySearchResult` instances.
You can also iterate over the pages of the response
using its `pages` property.
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 "search_all_iam_policies" not in self._inner_api_calls:
self._inner_api_calls[
"search_all_iam_policies"
] = google.api_core.gapic_v1.method.wrap_method(
self.transport.search_all_iam_policies,
default_retry=self._method_configs["SearchAllIamPolicies"].retry,
default_timeout=self._method_configs["SearchAllIamPolicies"].timeout,
client_info=self._client_info,
)

request = asset_service_pb2.SearchAllIamPoliciesRequest(
scope=scope, query=query, page_size=page_size
)
if metadata is None:
metadata = []
metadata = list(metadata)
try:
routing_header = [("scope", scope)]
except AttributeError:
pass
else:
routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
routing_header
)
metadata.append(routing_metadata)

iterator = google.api_core.page_iterator.GRPCIterator(
client=None,
method=functools.partial(
self._inner_api_calls["search_all_iam_policies"],
retry=retry,
timeout=timeout,
metadata=metadata,
),
request=request,
items_field="results",
request_token_field="page_token",
response_token_field="next_page_token",
)
return iterator
10 changes: 10 additions & 0 deletions google/cloud/asset_v1/gapic/asset_service_client_config.py
Expand Up @@ -52,6 +52,16 @@
"retry_codes_name": "non_idempotent",
"retry_params_name": "default",
},
"SearchAllResources": {
"timeout_millis": 60000,
"retry_codes_name": "idempotent",
"retry_params_name": "default",
},
"SearchAllIamPolicies": {
"timeout_millis": 60000,
"retry_codes_name": "idempotent",
"retry_params_name": "default",
},
},
}
}
Expand Down
Expand Up @@ -214,3 +214,35 @@ def update_feed(self):
deserialized response object.
"""
return self._stubs["asset_service_stub"].UpdateFeed

@property
def search_all_resources(self):
"""Return the gRPC stub for :meth:`AssetServiceClient.search_all_resources`.
Searches all the resources within the given accessible scope (e.g., a
project, a folder or an organization). Callers should have
cloud.assets.SearchAllResources permission upon the requested scope,
otherwise the request will be rejected.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["asset_service_stub"].SearchAllResources

@property
def search_all_iam_policies(self):
"""Return the gRPC stub for :meth:`AssetServiceClient.search_all_iam_policies`.
Searches all the IAM policies within the given accessible scope (e.g., a
project, a folder or an organization). Callers should have
cloud.assets.SearchAllIamPolicies permission upon the requested scope,
otherwise the request will be rejected.
Returns:
Callable: A callable which accepts the appropriate
deserialized request object and returns a
deserialized response object.
"""
return self._stubs["asset_service_stub"].SearchAllIamPolicies

0 comments on commit 24a0827

Please sign in to comment.