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, operations_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 # type: ignore
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 projects
32
33DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
34 gapic_version=package_version.__version__
35)
36
37
38class ProjectsTransport(abc.ABC):
39 """Abstract transport class for Projects."""
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_project: gapic_v1.method.wrap_method(
132 self.get_project,
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.list_projects: gapic_v1.method.wrap_method(
146 self.list_projects,
147 default_retry=retries.Retry(
148 initial=0.1,
149 maximum=60.0,
150 multiplier=1.3,
151 predicate=retries.if_exception_type(
152 core_exceptions.ServiceUnavailable,
153 ),
154 deadline=60.0,
155 ),
156 default_timeout=60.0,
157 client_info=client_info,
158 ),
159 self.search_projects: gapic_v1.method.wrap_method(
160 self.search_projects,
161 default_timeout=60.0,
162 client_info=client_info,
163 ),
164 self.create_project: gapic_v1.method.wrap_method(
165 self.create_project,
166 default_timeout=60.0,
167 client_info=client_info,
168 ),
169 self.update_project: gapic_v1.method.wrap_method(
170 self.update_project,
171 default_timeout=60.0,
172 client_info=client_info,
173 ),
174 self.move_project: gapic_v1.method.wrap_method(
175 self.move_project,
176 default_timeout=60.0,
177 client_info=client_info,
178 ),
179 self.delete_project: gapic_v1.method.wrap_method(
180 self.delete_project,
181 default_timeout=60.0,
182 client_info=client_info,
183 ),
184 self.undelete_project: gapic_v1.method.wrap_method(
185 self.undelete_project,
186 default_timeout=60.0,
187 client_info=client_info,
188 ),
189 self.get_iam_policy: gapic_v1.method.wrap_method(
190 self.get_iam_policy,
191 default_retry=retries.Retry(
192 initial=0.1,
193 maximum=60.0,
194 multiplier=1.3,
195 predicate=retries.if_exception_type(
196 core_exceptions.ServiceUnavailable,
197 ),
198 deadline=60.0,
199 ),
200 default_timeout=60.0,
201 client_info=client_info,
202 ),
203 self.set_iam_policy: gapic_v1.method.wrap_method(
204 self.set_iam_policy,
205 default_timeout=60.0,
206 client_info=client_info,
207 ),
208 self.test_iam_permissions: gapic_v1.method.wrap_method(
209 self.test_iam_permissions,
210 default_timeout=None,
211 client_info=client_info,
212 ),
213 }
214
215 def close(self):
216 """Closes resources associated with the transport.
217
218 .. warning::
219 Only call this method if the transport is NOT shared
220 with other clients - this may cause errors in other clients!
221 """
222 raise NotImplementedError()
223
224 @property
225 def operations_client(self):
226 """Return the client designed to process long-running operations."""
227 raise NotImplementedError()
228
229 @property
230 def get_project(
231 self,
232 ) -> Callable[
233 [projects.GetProjectRequest],
234 Union[projects.Project, Awaitable[projects.Project]],
235 ]:
236 raise NotImplementedError()
237
238 @property
239 def list_projects(
240 self,
241 ) -> Callable[
242 [projects.ListProjectsRequest],
243 Union[projects.ListProjectsResponse, Awaitable[projects.ListProjectsResponse]],
244 ]:
245 raise NotImplementedError()
246
247 @property
248 def search_projects(
249 self,
250 ) -> Callable[
251 [projects.SearchProjectsRequest],
252 Union[
253 projects.SearchProjectsResponse, Awaitable[projects.SearchProjectsResponse]
254 ],
255 ]:
256 raise NotImplementedError()
257
258 @property
259 def create_project(
260 self,
261 ) -> Callable[
262 [projects.CreateProjectRequest],
263 Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]],
264 ]:
265 raise NotImplementedError()
266
267 @property
268 def update_project(
269 self,
270 ) -> Callable[
271 [projects.UpdateProjectRequest],
272 Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]],
273 ]:
274 raise NotImplementedError()
275
276 @property
277 def move_project(
278 self,
279 ) -> Callable[
280 [projects.MoveProjectRequest],
281 Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]],
282 ]:
283 raise NotImplementedError()
284
285 @property
286 def delete_project(
287 self,
288 ) -> Callable[
289 [projects.DeleteProjectRequest],
290 Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]],
291 ]:
292 raise NotImplementedError()
293
294 @property
295 def undelete_project(
296 self,
297 ) -> Callable[
298 [projects.UndeleteProjectRequest],
299 Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]],
300 ]:
301 raise NotImplementedError()
302
303 @property
304 def get_iam_policy(
305 self,
306 ) -> Callable[
307 [iam_policy_pb2.GetIamPolicyRequest],
308 Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]],
309 ]:
310 raise NotImplementedError()
311
312 @property
313 def set_iam_policy(
314 self,
315 ) -> Callable[
316 [iam_policy_pb2.SetIamPolicyRequest],
317 Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]],
318 ]:
319 raise NotImplementedError()
320
321 @property
322 def test_iam_permissions(
323 self,
324 ) -> Callable[
325 [iam_policy_pb2.TestIamPermissionsRequest],
326 Union[
327 iam_policy_pb2.TestIamPermissionsResponse,
328 Awaitable[iam_policy_pb2.TestIamPermissionsResponse],
329 ],
330 ]:
331 raise NotImplementedError()
332
333 @property
334 def get_operation(
335 self,
336 ) -> Callable[
337 [operations_pb2.GetOperationRequest],
338 Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]],
339 ]:
340 raise NotImplementedError()
341
342 @property
343 def kind(self) -> str:
344 raise NotImplementedError()
345
346
347__all__ = ("ProjectsTransport",)