|
16 | 16 |
|
17 | 17 | """Accesses the google.cloud.asset.v1 AssetService API.""" |
18 | 18 |
|
| 19 | +import functools |
19 | 20 | import pkg_resources |
20 | 21 | import warnings |
21 | 22 |
|
|
28 | 29 | import google.api_core.grpc_helpers |
29 | 30 | import google.api_core.operation |
30 | 31 | import google.api_core.operations_v1 |
| 32 | +import google.api_core.page_iterator |
31 | 33 | import google.api_core.path_template |
32 | 34 | import grpc |
33 | 35 |
|
@@ -830,3 +832,305 @@ def update_feed( |
830 | 832 | return self._inner_api_calls["update_feed"]( |
831 | 833 | request, retry=retry, timeout=timeout, metadata=metadata |
832 | 834 | ) |
| 835 | + |
| 836 | + def search_all_resources( |
| 837 | + self, |
| 838 | + scope, |
| 839 | + query=None, |
| 840 | + asset_types=None, |
| 841 | + page_size=None, |
| 842 | + order_by=None, |
| 843 | + retry=google.api_core.gapic_v1.method.DEFAULT, |
| 844 | + timeout=google.api_core.gapic_v1.method.DEFAULT, |
| 845 | + metadata=None, |
| 846 | + ): |
| 847 | + """ |
| 848 | + Searches all the resources within the given accessible scope (e.g., a |
| 849 | + project, a folder or an organization). Callers should have |
| 850 | + cloud.assets.SearchAllResources permission upon the requested scope, |
| 851 | + otherwise the request will be rejected. |
| 852 | +
|
| 853 | + Example: |
| 854 | + >>> from google.cloud import asset_v1 |
| 855 | + >>> |
| 856 | + >>> client = asset_v1.AssetServiceClient() |
| 857 | + >>> |
| 858 | + >>> # TODO: Initialize `scope`: |
| 859 | + >>> scope = '' |
| 860 | + >>> |
| 861 | + >>> # Iterate over all results |
| 862 | + >>> for element in client.search_all_resources(scope): |
| 863 | + ... # process element |
| 864 | + ... pass |
| 865 | + >>> |
| 866 | + >>> |
| 867 | + >>> # Alternatively: |
| 868 | + >>> |
| 869 | + >>> # Iterate over results one page at a time |
| 870 | + >>> for page in client.search_all_resources(scope).pages: |
| 871 | + ... for element in page: |
| 872 | + ... # process element |
| 873 | + ... pass |
| 874 | +
|
| 875 | + Args: |
| 876 | + scope (str): Required. A scope can be a project, a folder or an organization. The |
| 877 | + search is limited to the resources within the ``scope``. |
| 878 | +
|
| 879 | + The allowed values are: |
| 880 | +
|
| 881 | + - projects/{PROJECT_ID} |
| 882 | + - projects/{PROJECT_NUMBER} |
| 883 | + - folders/{FOLDER_NUMBER} |
| 884 | + - organizations/{ORGANIZATION_NUMBER} |
| 885 | + query (str): Optional. The query statement. An empty query can be specified to |
| 886 | + search all the resources of certain ``asset_types`` within the given |
| 887 | + ``scope``. |
| 888 | +
|
| 889 | + Examples: |
| 890 | +
|
| 891 | + - ``name : "Important"`` to find Cloud resources whose name contains |
| 892 | + "Important" as a word. |
| 893 | + - ``displayName : "Impor*"`` to find Cloud resources whose display name |
| 894 | + contains "Impor" as a word prefix. |
| 895 | + - ``description : "*por*"`` to find Cloud resources whose description |
| 896 | + contains "por" as a substring. |
| 897 | + - ``location : "us-west*"`` to find Cloud resources whose location is |
| 898 | + prefixed with "us-west". |
| 899 | + - ``labels : "prod"`` to find Cloud resources whose labels contain |
| 900 | + "prod" as a key or value. |
| 901 | + - ``labels.env : "prod"`` to find Cloud resources which have a label |
| 902 | + "env" and its value is "prod". |
| 903 | + - ``labels.env : *`` to find Cloud resources which have a label "env". |
| 904 | + - ``"Important"`` to find Cloud resources which contain "Important" as |
| 905 | + a word in any of the searchable fields. |
| 906 | + - ``"Impor*"`` to find Cloud resources which contain "Impor" as a word |
| 907 | + prefix in any of the searchable fields. |
| 908 | + - ``"*por*"`` to find Cloud resources which contain "por" as a |
| 909 | + substring in any of the searchable fields. |
| 910 | + - ``("Important" AND location : ("us-west1" OR "global"))`` to find |
| 911 | + Cloud resources which contain "Important" as a word in any of the |
| 912 | + searchable fields and are also located in the "us-west1" region or |
| 913 | + the "global" location. |
| 914 | +
|
| 915 | + See `how to construct a |
| 916 | + query <https://cloud.google.com/asset-inventory/docs/searching-resources#how_to_construct_a_query>`__ |
| 917 | + for more details. |
| 918 | + asset_types (list[str]): Optional. A list of asset types that this request searches for. If |
| 919 | + empty, it will search all the `searchable asset |
| 920 | + types <https://cloud.google.com/asset-inventory/docs/supported-asset-types#searchable_asset_types>`__. |
| 921 | + page_size (int): The maximum number of resources contained in the |
| 922 | + underlying API response. If page streaming is performed per- |
| 923 | + resource, this parameter does not affect the return value. If page |
| 924 | + streaming is performed per-page, this determines the maximum number |
| 925 | + of resources in a page. |
| 926 | + order_by (str): Optional. A comma separated list of fields specifying the sorting |
| 927 | + order of the results. The default order is ascending. Add " DESC" after |
| 928 | + the field name to indicate descending order. Redundant space characters |
| 929 | + are ignored. Example: "location DESC, name". See `supported resource |
| 930 | + metadata |
| 931 | + fields <https://cloud.google.com/asset-inventory/docs/searching-resources#query_on_resource_metadata_fields>`__ |
| 932 | + for more details. |
| 933 | + retry (Optional[google.api_core.retry.Retry]): A retry object used |
| 934 | + to retry requests. If ``None`` is specified, requests will |
| 935 | + be retried using a default configuration. |
| 936 | + timeout (Optional[float]): The amount of time, in seconds, to wait |
| 937 | + for the request to complete. Note that if ``retry`` is |
| 938 | + specified, the timeout applies to each individual attempt. |
| 939 | + metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata |
| 940 | + that is provided to the method. |
| 941 | +
|
| 942 | + Returns: |
| 943 | + A :class:`~google.api_core.page_iterator.PageIterator` instance. |
| 944 | + An iterable of :class:`~google.cloud.asset_v1.types.ResourceSearchResult` instances. |
| 945 | + You can also iterate over the pages of the response |
| 946 | + using its `pages` property. |
| 947 | +
|
| 948 | + Raises: |
| 949 | + google.api_core.exceptions.GoogleAPICallError: If the request |
| 950 | + failed for any reason. |
| 951 | + google.api_core.exceptions.RetryError: If the request failed due |
| 952 | + to a retryable error and retry attempts failed. |
| 953 | + ValueError: If the parameters are invalid. |
| 954 | + """ |
| 955 | + # Wrap the transport method to add retry and timeout logic. |
| 956 | + if "search_all_resources" not in self._inner_api_calls: |
| 957 | + self._inner_api_calls[ |
| 958 | + "search_all_resources" |
| 959 | + ] = google.api_core.gapic_v1.method.wrap_method( |
| 960 | + self.transport.search_all_resources, |
| 961 | + default_retry=self._method_configs["SearchAllResources"].retry, |
| 962 | + default_timeout=self._method_configs["SearchAllResources"].timeout, |
| 963 | + client_info=self._client_info, |
| 964 | + ) |
| 965 | + |
| 966 | + request = asset_service_pb2.SearchAllResourcesRequest( |
| 967 | + scope=scope, |
| 968 | + query=query, |
| 969 | + asset_types=asset_types, |
| 970 | + page_size=page_size, |
| 971 | + order_by=order_by, |
| 972 | + ) |
| 973 | + if metadata is None: |
| 974 | + metadata = [] |
| 975 | + metadata = list(metadata) |
| 976 | + try: |
| 977 | + routing_header = [("scope", scope)] |
| 978 | + except AttributeError: |
| 979 | + pass |
| 980 | + else: |
| 981 | + routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( |
| 982 | + routing_header |
| 983 | + ) |
| 984 | + metadata.append(routing_metadata) |
| 985 | + |
| 986 | + iterator = google.api_core.page_iterator.GRPCIterator( |
| 987 | + client=None, |
| 988 | + method=functools.partial( |
| 989 | + self._inner_api_calls["search_all_resources"], |
| 990 | + retry=retry, |
| 991 | + timeout=timeout, |
| 992 | + metadata=metadata, |
| 993 | + ), |
| 994 | + request=request, |
| 995 | + items_field="results", |
| 996 | + request_token_field="page_token", |
| 997 | + response_token_field="next_page_token", |
| 998 | + ) |
| 999 | + return iterator |
| 1000 | + |
| 1001 | + def search_all_iam_policies( |
| 1002 | + self, |
| 1003 | + scope, |
| 1004 | + query=None, |
| 1005 | + page_size=None, |
| 1006 | + retry=google.api_core.gapic_v1.method.DEFAULT, |
| 1007 | + timeout=google.api_core.gapic_v1.method.DEFAULT, |
| 1008 | + metadata=None, |
| 1009 | + ): |
| 1010 | + """ |
| 1011 | + Searches all the IAM policies within the given accessible scope (e.g., a |
| 1012 | + project, a folder or an organization). Callers should have |
| 1013 | + cloud.assets.SearchAllIamPolicies permission upon the requested scope, |
| 1014 | + otherwise the request will be rejected. |
| 1015 | +
|
| 1016 | + Example: |
| 1017 | + >>> from google.cloud import asset_v1 |
| 1018 | + >>> |
| 1019 | + >>> client = asset_v1.AssetServiceClient() |
| 1020 | + >>> |
| 1021 | + >>> # TODO: Initialize `scope`: |
| 1022 | + >>> scope = '' |
| 1023 | + >>> |
| 1024 | + >>> # Iterate over all results |
| 1025 | + >>> for element in client.search_all_iam_policies(scope): |
| 1026 | + ... # process element |
| 1027 | + ... pass |
| 1028 | + >>> |
| 1029 | + >>> |
| 1030 | + >>> # Alternatively: |
| 1031 | + >>> |
| 1032 | + >>> # Iterate over results one page at a time |
| 1033 | + >>> for page in client.search_all_iam_policies(scope).pages: |
| 1034 | + ... for element in page: |
| 1035 | + ... # process element |
| 1036 | + ... pass |
| 1037 | +
|
| 1038 | + Args: |
| 1039 | + scope (str): Required. A scope can be a project, a folder or an organization. The |
| 1040 | + search is limited to the IAM policies within the ``scope``. |
| 1041 | +
|
| 1042 | + The allowed values are: |
| 1043 | +
|
| 1044 | + - projects/{PROJECT_ID} |
| 1045 | + - projects/{PROJECT_NUMBER} |
| 1046 | + - folders/{FOLDER_NUMBER} |
| 1047 | + - organizations/{ORGANIZATION_NUMBER} |
| 1048 | + query (str): Optional. The query statement. An empty query can be specified to |
| 1049 | + search all the IAM policies within the given ``scope``. |
| 1050 | +
|
| 1051 | + Examples: |
| 1052 | +
|
| 1053 | + - ``policy : "amy@gmail.com"`` to find Cloud IAM policy bindings that |
| 1054 | + specify user "amy@gmail.com". |
| 1055 | + - ``policy : "roles/compute.admin"`` to find Cloud IAM policy bindings |
| 1056 | + that specify the Compute Admin role. |
| 1057 | + - ``policy.role.permissions : "storage.buckets.update"`` to find Cloud |
| 1058 | + IAM policy bindings that specify a role containing |
| 1059 | + "storage.buckets.update" permission. |
| 1060 | + - ``resource : "organizations/123"`` to find Cloud IAM policy bindings |
| 1061 | + that are set on "organizations/123". |
| 1062 | + - ``(resource : ("organizations/123" OR "folders/1234") AND policy : "amy")`` |
| 1063 | + to find Cloud IAM policy bindings that are set on "organizations/123" |
| 1064 | + or "folders/1234", and also specify user "amy". |
| 1065 | +
|
| 1066 | + See `how to construct a |
| 1067 | + query <https://cloud.google.com/asset-inventory/docs/searching-iam-policies#how_to_construct_a_query>`__ |
| 1068 | + for more details. |
| 1069 | + page_size (int): The maximum number of resources contained in the |
| 1070 | + underlying API response. If page streaming is performed per- |
| 1071 | + resource, this parameter does not affect the return value. If page |
| 1072 | + streaming is performed per-page, this determines the maximum number |
| 1073 | + of resources in a page. |
| 1074 | + retry (Optional[google.api_core.retry.Retry]): A retry object used |
| 1075 | + to retry requests. If ``None`` is specified, requests will |
| 1076 | + be retried using a default configuration. |
| 1077 | + timeout (Optional[float]): The amount of time, in seconds, to wait |
| 1078 | + for the request to complete. Note that if ``retry`` is |
| 1079 | + specified, the timeout applies to each individual attempt. |
| 1080 | + metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata |
| 1081 | + that is provided to the method. |
| 1082 | +
|
| 1083 | + Returns: |
| 1084 | + A :class:`~google.api_core.page_iterator.PageIterator` instance. |
| 1085 | + An iterable of :class:`~google.cloud.asset_v1.types.IamPolicySearchResult` instances. |
| 1086 | + You can also iterate over the pages of the response |
| 1087 | + using its `pages` property. |
| 1088 | +
|
| 1089 | + Raises: |
| 1090 | + google.api_core.exceptions.GoogleAPICallError: If the request |
| 1091 | + failed for any reason. |
| 1092 | + google.api_core.exceptions.RetryError: If the request failed due |
| 1093 | + to a retryable error and retry attempts failed. |
| 1094 | + ValueError: If the parameters are invalid. |
| 1095 | + """ |
| 1096 | + # Wrap the transport method to add retry and timeout logic. |
| 1097 | + if "search_all_iam_policies" not in self._inner_api_calls: |
| 1098 | + self._inner_api_calls[ |
| 1099 | + "search_all_iam_policies" |
| 1100 | + ] = google.api_core.gapic_v1.method.wrap_method( |
| 1101 | + self.transport.search_all_iam_policies, |
| 1102 | + default_retry=self._method_configs["SearchAllIamPolicies"].retry, |
| 1103 | + default_timeout=self._method_configs["SearchAllIamPolicies"].timeout, |
| 1104 | + client_info=self._client_info, |
| 1105 | + ) |
| 1106 | + |
| 1107 | + request = asset_service_pb2.SearchAllIamPoliciesRequest( |
| 1108 | + scope=scope, query=query, page_size=page_size |
| 1109 | + ) |
| 1110 | + if metadata is None: |
| 1111 | + metadata = [] |
| 1112 | + metadata = list(metadata) |
| 1113 | + try: |
| 1114 | + routing_header = [("scope", scope)] |
| 1115 | + except AttributeError: |
| 1116 | + pass |
| 1117 | + else: |
| 1118 | + routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( |
| 1119 | + routing_header |
| 1120 | + ) |
| 1121 | + metadata.append(routing_metadata) |
| 1122 | + |
| 1123 | + iterator = google.api_core.page_iterator.GRPCIterator( |
| 1124 | + client=None, |
| 1125 | + method=functools.partial( |
| 1126 | + self._inner_api_calls["search_all_iam_policies"], |
| 1127 | + retry=retry, |
| 1128 | + timeout=timeout, |
| 1129 | + metadata=metadata, |
| 1130 | + ), |
| 1131 | + request=request, |
| 1132 | + items_field="results", |
| 1133 | + request_token_field="page_token", |
| 1134 | + response_token_field="next_page_token", |
| 1135 | + ) |
| 1136 | + return iterator |
0 commit comments