16
16
#
17
17
18
18
from collections import OrderedDict
19
+ from distutils import util
19
20
import os
20
21
import re
21
- from typing import Callable , Dict , Sequence , Tuple , Type , Union
22
+ from typing import Callable , Dict , Optional , Sequence , Tuple , Type , Union
22
23
import pkg_resources
23
24
24
- import google .api_core . client_options as ClientOptions # type: ignore
25
+ from google .api_core import client_options as client_options_lib # type: ignore
25
26
from google .api_core import exceptions # type: ignore
26
27
from google .api_core import gapic_v1 # type: ignore
27
28
from google .api_core import retry as retries # type: ignore
28
29
from google .auth import credentials # type: ignore
29
30
from google .auth .transport import mtls # type: ignore
31
+ from google .auth .transport .grpc import SslCredentials # type: ignore
30
32
from google .auth .exceptions import MutualTLSChannelError # type: ignore
31
33
from google .oauth2 import service_account # type: ignore
32
34
@@ -134,6 +136,15 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):
134
136
135
137
from_service_account_json = from_service_account_file
136
138
139
+ @property
140
+ def transport (self ) -> DeviceManagerTransport :
141
+ """Return the transport used by the client instance.
142
+
143
+ Returns:
144
+ DeviceManagerTransport: The transport used by the client instance.
145
+ """
146
+ return self ._transport
147
+
137
148
@staticmethod
138
149
def device_path (project : str , location : str , registry : str , device : str ,) -> str :
139
150
"""Return a fully-qualified device string."""
@@ -166,12 +177,71 @@ def parse_registry_path(path: str) -> Dict[str, str]:
166
177
)
167
178
return m .groupdict () if m else {}
168
179
180
+ @staticmethod
181
+ def common_billing_account_path (billing_account : str ,) -> str :
182
+ """Return a fully-qualified billing_account string."""
183
+ return "billingAccounts/{billing_account}" .format (
184
+ billing_account = billing_account ,
185
+ )
186
+
187
+ @staticmethod
188
+ def parse_common_billing_account_path (path : str ) -> Dict [str , str ]:
189
+ """Parse a billing_account path into its component segments."""
190
+ m = re .match (r"^billingAccounts/(?P<billing_account>.+?)$" , path )
191
+ return m .groupdict () if m else {}
192
+
193
+ @staticmethod
194
+ def common_folder_path (folder : str ,) -> str :
195
+ """Return a fully-qualified folder string."""
196
+ return "folders/{folder}" .format (folder = folder ,)
197
+
198
+ @staticmethod
199
+ def parse_common_folder_path (path : str ) -> Dict [str , str ]:
200
+ """Parse a folder path into its component segments."""
201
+ m = re .match (r"^folders/(?P<folder>.+?)$" , path )
202
+ return m .groupdict () if m else {}
203
+
204
+ @staticmethod
205
+ def common_organization_path (organization : str ,) -> str :
206
+ """Return a fully-qualified organization string."""
207
+ return "organizations/{organization}" .format (organization = organization ,)
208
+
209
+ @staticmethod
210
+ def parse_common_organization_path (path : str ) -> Dict [str , str ]:
211
+ """Parse a organization path into its component segments."""
212
+ m = re .match (r"^organizations/(?P<organization>.+?)$" , path )
213
+ return m .groupdict () if m else {}
214
+
215
+ @staticmethod
216
+ def common_project_path (project : str ,) -> str :
217
+ """Return a fully-qualified project string."""
218
+ return "projects/{project}" .format (project = project ,)
219
+
220
+ @staticmethod
221
+ def parse_common_project_path (path : str ) -> Dict [str , str ]:
222
+ """Parse a project path into its component segments."""
223
+ m = re .match (r"^projects/(?P<project>.+?)$" , path )
224
+ return m .groupdict () if m else {}
225
+
226
+ @staticmethod
227
+ def common_location_path (project : str , location : str ,) -> str :
228
+ """Return a fully-qualified location string."""
229
+ return "projects/{project}/locations/{location}" .format (
230
+ project = project , location = location ,
231
+ )
232
+
233
+ @staticmethod
234
+ def parse_common_location_path (path : str ) -> Dict [str , str ]:
235
+ """Parse a location path into its component segments."""
236
+ m = re .match (r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)$" , path )
237
+ return m .groupdict () if m else {}
238
+
169
239
def __init__ (
170
240
self ,
171
241
* ,
172
- credentials : credentials .Credentials = None ,
173
- transport : Union [str , DeviceManagerTransport ] = None ,
174
- client_options : ClientOptions = None ,
242
+ credentials : Optional [ credentials .Credentials ] = None ,
243
+ transport : Union [str , DeviceManagerTransport , None ] = None ,
244
+ client_options : Optional [ client_options_lib . ClientOptions ] = None ,
175
245
client_info : gapic_v1 .client_info .ClientInfo = DEFAULT_CLIENT_INFO ,
176
246
) -> None :
177
247
"""Instantiate the device manager client.
@@ -185,53 +255,74 @@ def __init__(
185
255
transport (Union[str, ~.DeviceManagerTransport]): The
186
256
transport to use. If set to None, a transport is chosen
187
257
automatically.
188
- client_options (ClientOptions): Custom options for the client. It
189
- won't take effect if a ``transport`` instance is provided.
258
+ client_options (client_options_lib. ClientOptions): Custom options for the
259
+ client. It won't take effect if a ``transport`` instance is provided.
190
260
(1) The ``api_endpoint`` property can be used to override the
191
- default endpoint provided by the client. GOOGLE_API_USE_MTLS
261
+ default endpoint provided by the client. GOOGLE_API_USE_MTLS_ENDPOINT
192
262
environment variable can also be used to override the endpoint:
193
263
"always" (always use the default mTLS endpoint), "never" (always
194
- use the default regular endpoint, this is the default value for
195
- the environment variable) and "auto" (auto switch to the default
196
- mTLS endpoint if client SSL credentials is present). However,
197
- the ``api_endpoint`` property takes precedence if provided.
198
- (2) The ``client_cert_source`` property is used to provide client
199
- SSL credentials for mutual TLS transport. If not provided, the
200
- default SSL credentials will be used if present.
201
- client_info (google.api_core.gapic_v1.client_info.ClientInfo):
202
- The client info used to send a user-agent string along with
203
- API requests. If ``None``, then default info will be used.
204
- Generally, you only need to set this if you're developing
264
+ use the default regular endpoint) and "auto" (auto switch to the
265
+ default mTLS endpoint if client certificate is present, this is
266
+ the default value). However, the ``api_endpoint`` property takes
267
+ precedence if provided.
268
+ (2) If GOOGLE_API_USE_CLIENT_CERTIFICATE environment variable
269
+ is "true", then the ``client_cert_source`` property can be used
270
+ to provide client certificate for mutual TLS transport. If
271
+ not provided, the default SSL client certificate will be used if
272
+ present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not
273
+ set, no client certificate will be used.
274
+ client_info (google.api_core.gapic_v1.client_info.ClientInfo):
275
+ The client info used to send a user-agent string along with
276
+ API requests. If ``None``, then default info will be used.
277
+ Generally, you only need to set this if you're developing
205
278
your own client library.
206
279
207
280
Raises:
208
281
google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport
209
282
creation failed for any reason.
210
283
"""
211
284
if isinstance (client_options , dict ):
212
- client_options = ClientOptions .from_dict (client_options )
285
+ client_options = client_options_lib .from_dict (client_options )
213
286
if client_options is None :
214
- client_options = ClientOptions .ClientOptions ()
287
+ client_options = client_options_lib .ClientOptions ()
288
+
289
+ # Create SSL credentials for mutual TLS if needed.
290
+ use_client_cert = bool (
291
+ util .strtobool (os .getenv ("GOOGLE_API_USE_CLIENT_CERTIFICATE" , "false" ))
292
+ )
293
+
294
+ ssl_credentials = None
295
+ is_mtls = False
296
+ if use_client_cert :
297
+ if client_options .client_cert_source :
298
+ import grpc # type: ignore
215
299
216
- if client_options .api_endpoint is None :
217
- use_mtls_env = os .getenv ("GOOGLE_API_USE_MTLS" , "never" )
300
+ cert , key = client_options .client_cert_source ()
301
+ ssl_credentials = grpc .ssl_channel_credentials (
302
+ certificate_chain = cert , private_key = key
303
+ )
304
+ is_mtls = True
305
+ else :
306
+ creds = SslCredentials ()
307
+ is_mtls = creds .is_mtls
308
+ ssl_credentials = creds .ssl_credentials if is_mtls else None
309
+
310
+ # Figure out which api endpoint to use.
311
+ if client_options .api_endpoint is not None :
312
+ api_endpoint = client_options .api_endpoint
313
+ else :
314
+ use_mtls_env = os .getenv ("GOOGLE_API_USE_MTLS_ENDPOINT" , "auto" )
218
315
if use_mtls_env == "never" :
219
- client_options . api_endpoint = self .DEFAULT_ENDPOINT
316
+ api_endpoint = self .DEFAULT_ENDPOINT
220
317
elif use_mtls_env == "always" :
221
- client_options . api_endpoint = self .DEFAULT_MTLS_ENDPOINT
318
+ api_endpoint = self .DEFAULT_MTLS_ENDPOINT
222
319
elif use_mtls_env == "auto" :
223
- has_client_cert_source = (
224
- client_options .client_cert_source is not None
225
- or mtls .has_default_client_cert_source ()
226
- )
227
- client_options .api_endpoint = (
228
- self .DEFAULT_MTLS_ENDPOINT
229
- if has_client_cert_source
230
- else self .DEFAULT_ENDPOINT
320
+ api_endpoint = (
321
+ self .DEFAULT_MTLS_ENDPOINT if is_mtls else self .DEFAULT_ENDPOINT
231
322
)
232
323
else :
233
324
raise MutualTLSChannelError (
234
- "Unsupported GOOGLE_API_USE_MTLS value. Accepted values: never, auto, always"
325
+ "Unsupported GOOGLE_API_USE_MTLS_ENDPOINT value. Accepted values: never, auto, always"
235
326
)
236
327
237
328
# Save or instantiate the transport.
@@ -255,10 +346,9 @@ def __init__(
255
346
self ._transport = Transport (
256
347
credentials = credentials ,
257
348
credentials_file = client_options .credentials_file ,
258
- host = client_options . api_endpoint ,
349
+ host = api_endpoint ,
259
350
scopes = client_options .scopes ,
260
- api_mtls_endpoint = client_options .api_endpoint ,
261
- client_cert_source = client_options .client_cert_source ,
351
+ ssl_channel_credentials = ssl_credentials ,
262
352
quota_project_id = client_options .quota_project_id ,
263
353
client_info = client_info ,
264
354
)
@@ -1390,13 +1480,7 @@ def set_iam_policy(
1390
1480
request = iam_policy .SetIamPolicyRequest (** request )
1391
1481
1392
1482
elif not request :
1393
- request = iam_policy .SetIamPolicyRequest ()
1394
-
1395
- # If we have keyword arguments corresponding to fields on the
1396
- # request, apply these.
1397
-
1398
- if resource is not None :
1399
- request .resource = resource
1483
+ request = iam_policy .SetIamPolicyRequest (resource = resource ,)
1400
1484
1401
1485
# Wrap the RPC method; this adds retry and timeout information,
1402
1486
# and friendly error handling.
@@ -1531,13 +1615,7 @@ def get_iam_policy(
1531
1615
request = iam_policy .GetIamPolicyRequest (** request )
1532
1616
1533
1617
elif not request :
1534
- request = iam_policy .GetIamPolicyRequest ()
1535
-
1536
- # If we have keyword arguments corresponding to fields on the
1537
- # request, apply these.
1538
-
1539
- if resource is not None :
1540
- request .resource = resource
1618
+ request = iam_policy .GetIamPolicyRequest (resource = resource ,)
1541
1619
1542
1620
# Wrap the RPC method; this adds retry and timeout information,
1543
1621
# and friendly error handling.
@@ -1616,16 +1694,9 @@ def test_iam_permissions(
1616
1694
request = iam_policy .TestIamPermissionsRequest (** request )
1617
1695
1618
1696
elif not request :
1619
- request = iam_policy .TestIamPermissionsRequest ()
1620
-
1621
- # If we have keyword arguments corresponding to fields on the
1622
- # request, apply these.
1623
-
1624
- if resource is not None :
1625
- request .resource = resource
1626
-
1627
- if permissions :
1628
- request .permissions .extend (permissions )
1697
+ request = iam_policy .TestIamPermissionsRequest (
1698
+ resource = resource , permissions = permissions ,
1699
+ )
1629
1700
1630
1701
# Wrap the RPC method; this adds retry and timeout information,
1631
1702
# and friendly error handling.
0 commit comments