1# -*- coding: utf-8 -*-
2# Copyright 2022 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import abc
17from typing import Awaitable, Callable, Dict, Optional, Sequence, Union
18
19import google.api_core
20from google.api_core import exceptions as core_exceptions
21from google.api_core import gapic_v1
22from google.api_core import retry as retries
23import google.auth # type: ignore
24from google.auth import credentials as ga_credentials # type: ignore
25from google.iam.v1 import iam_policy_pb2 # type: ignore
26from google.iam.v1 import policy_pb2 # type: ignore
27from google.longrunning import operations_pb2
28from google.oauth2 import service_account # type: ignore
29
30from google.cloud.resourcemanager_v3 import gapic_version as package_version
31from google.cloud.resourcemanager_v3.types import organizations
32
33DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
34 gapic_version=package_version.__version__
35)
36
37
38class OrganizationsTransport(abc.ABC):
39 """Abstract transport class for Organizations."""
40
41 AUTH_SCOPES = (
42 "https://www.googleapis.com/auth/cloud-platform",
43 "https://www.googleapis.com/auth/cloud-platform.read-only",
44 )
45
46 DEFAULT_HOST: str = "cloudresourcemanager.googleapis.com"
47
48 def __init__(
49 self,
50 *,
51 host: str = DEFAULT_HOST,
52 credentials: Optional[ga_credentials.Credentials] = None,
53 credentials_file: Optional[str] = None,
54 scopes: Optional[Sequence[str]] = None,
55 quota_project_id: Optional[str] = None,
56 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
57 always_use_jwt_access: Optional[bool] = False,
58 api_audience: Optional[str] = None,
59 **kwargs,
60 ) -> None:
61 """Instantiate the transport.
62
63 Args:
64 host (Optional[str]):
65 The hostname to connect to.
66 credentials (Optional[google.auth.credentials.Credentials]): The
67 authorization credentials to attach to requests. These
68 credentials identify the application to the service; if none
69 are specified, the client will attempt to ascertain the
70 credentials from the environment.
71 credentials_file (Optional[str]): A file with credentials that can
72 be loaded with :func:`google.auth.load_credentials_from_file`.
73 This argument is mutually exclusive with credentials.
74 scopes (Optional[Sequence[str]]): A list of scopes.
75 quota_project_id (Optional[str]): An optional project to use for billing
76 and quota.
77 client_info (google.api_core.gapic_v1.client_info.ClientInfo):
78 The client info used to send a user-agent string along with
79 API requests. If ``None``, then default info will be used.
80 Generally, you only need to set this if you're developing
81 your own client library.
82 always_use_jwt_access (Optional[bool]): Whether self signed JWT should
83 be used for service account credentials.
84 """
85
86 scopes_kwargs = {"scopes": scopes, "default_scopes": self.AUTH_SCOPES}
87
88 # Save the scopes.
89 self._scopes = scopes
90
91 # If no credentials are provided, then determine the appropriate
92 # defaults.
93 if credentials and credentials_file:
94 raise core_exceptions.DuplicateCredentialArgs(
95 "'credentials_file' and 'credentials' are mutually exclusive"
96 )
97
98 if credentials_file is not None:
99 credentials, _ = google.auth.load_credentials_from_file(
100 credentials_file, **scopes_kwargs, quota_project_id=quota_project_id
101 )
102 elif credentials is None:
103 credentials, _ = google.auth.default(
104 **scopes_kwargs, quota_project_id=quota_project_id
105 )
106 # Don't apply audience if the credentials file passed from user.
107 if hasattr(credentials, "with_gdch_audience"):
108 credentials = credentials.with_gdch_audience(
109 api_audience if api_audience else host
110 )
111
112 # If the credentials are service account credentials, then always try to use self signed JWT.
113 if (
114 always_use_jwt_access
115 and isinstance(credentials, service_account.Credentials)
116 and hasattr(service_account.Credentials, "with_always_use_jwt_access")
117 ):
118 credentials = credentials.with_always_use_jwt_access(True)
119
120 # Save the credentials.
121 self._credentials = credentials
122
123 # Save the hostname. Default to port 443 (HTTPS) if none is specified.
124 if ":" not in host:
125 host += ":443"
126 self._host = host
127
128 def _prep_wrapped_messages(self, client_info):
129 # Precompute the wrapped methods.
130 self._wrapped_methods = {
131 self.get_organization: gapic_v1.method.wrap_method(
132 self.get_organization,
133 default_retry=retries.Retry(
134 initial=0.1,
135 maximum=60.0,
136 multiplier=1.3,
137 predicate=retries.if_exception_type(
138 core_exceptions.ServiceUnavailable,
139 ),
140 deadline=60.0,
141 ),
142 default_timeout=60.0,
143 client_info=client_info,
144 ),
145 self.search_organizations: gapic_v1.method.wrap_method(
146 self.search_organizations,
147 default_timeout=60.0,
148 client_info=client_info,
149 ),
150 self.get_iam_policy: gapic_v1.method.wrap_method(
151 self.get_iam_policy,
152 default_retry=retries.Retry(
153 initial=0.1,
154 maximum=60.0,
155 multiplier=1.3,
156 predicate=retries.if_exception_type(
157 core_exceptions.ServiceUnavailable,
158 ),
159 deadline=60.0,
160 ),
161 default_timeout=60.0,
162 client_info=client_info,
163 ),
164 self.set_iam_policy: gapic_v1.method.wrap_method(
165 self.set_iam_policy,
166 default_timeout=60.0,
167 client_info=client_info,
168 ),
169 self.test_iam_permissions: gapic_v1.method.wrap_method(
170 self.test_iam_permissions,
171 default_timeout=None,
172 client_info=client_info,
173 ),
174 }
175
176 def close(self):
177 """Closes resources associated with the transport.
178
179 .. warning::
180 Only call this method if the transport is NOT shared
181 with other clients - this may cause errors in other clients!
182 """
183 raise NotImplementedError()
184
185 @property
186 def get_organization(
187 self,
188 ) -> Callable[
189 [organizations.GetOrganizationRequest],
190 Union[organizations.Organization, Awaitable[organizations.Organization]],
191 ]:
192 raise NotImplementedError()
193
194 @property
195 def search_organizations(
196 self,
197 ) -> Callable[
198 [organizations.SearchOrganizationsRequest],
199 Union[
200 organizations.SearchOrganizationsResponse,
201 Awaitable[organizations.SearchOrganizationsResponse],
202 ],
203 ]:
204 raise NotImplementedError()
205
206 @property
207 def get_iam_policy(
208 self,
209 ) -> Callable[
210 [iam_policy_pb2.GetIamPolicyRequest],
211 Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]],
212 ]:
213 raise NotImplementedError()
214
215 @property
216 def set_iam_policy(
217 self,
218 ) -> Callable[
219 [iam_policy_pb2.SetIamPolicyRequest],
220 Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]],
221 ]:
222 raise NotImplementedError()
223
224 @property
225 def test_iam_permissions(
226 self,
227 ) -> Callable[
228 [iam_policy_pb2.TestIamPermissionsRequest],
229 Union[
230 iam_policy_pb2.TestIamPermissionsResponse,
231 Awaitable[iam_policy_pb2.TestIamPermissionsResponse],
232 ],
233 ]:
234 raise NotImplementedError()
235
236 @property
237 def get_operation(
238 self,
239 ) -> Callable[
240 [operations_pb2.GetOperationRequest],
241 Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]],
242 ]:
243 raise NotImplementedError()
244
245 @property
246 def kind(self) -> str:
247 raise NotImplementedError()
248
249
250__all__ = ("OrganizationsTransport",)