2020import functools
2121import inspect
2222import threading
23- from typing import Any , Callable , Dict , Optional , Sequence , Type , Union
23+ from typing import Any , Callable , Dict , Optional , Sequence , Tuple , Type , Union
2424
2525import proto
2626
@@ -266,6 +266,7 @@ def __init__(
266266 project : Optional [str ] = None ,
267267 location : Optional [str ] = None ,
268268 credentials : Optional [auth_credentials .Credentials ] = None ,
269+ resource_name : Optional [str ] = None ,
269270 ):
270271 """Initializes class with project, location, and api_client.
271272
@@ -274,8 +275,14 @@ def __init__(
274275 location(str): The location of the resource noun.
275276 credentials(google.auth.crendentials.Crendentials): Optional custom
276277 credentials to use when accessing interacting with resource noun.
278+ resource_name(str): A fully-qualified resource name or ID.
277279 """
278280
281+ if resource_name :
282+ project , location = self ._get_and_validate_project_location (
283+ resource_name = resource_name , project = project , location = location
284+ )
285+
279286 self .project = project or initializer .global_config .project
280287 self .location = location or initializer .global_config .location
281288 self .credentials = credentials or initializer .global_config .credentials
@@ -306,6 +313,41 @@ def _instantiate_client(
306313 prediction_client = cls ._is_client_prediction_client ,
307314 )
308315
316+ def _get_and_validate_project_location (
317+ self ,
318+ resource_name : str ,
319+ project : Optional [str ] = None ,
320+ location : Optional [str ] = None ,
321+ ) -> Tuple :
322+
323+ """Validate the project and location for the resource.
324+
325+ Args:
326+ resource_name(str): Required. A fully-qualified resource name or ID.
327+ project(str): Project of the resource noun.
328+ location(str): The location of the resource noun.
329+
330+ Raises:
331+ RuntimeError if location is different from resource location
332+ """
333+
334+ if not project and not location :
335+ return project , location
336+
337+ fields = utils .extract_fields_from_resource_name (
338+ resource_name , self ._resource_noun
339+ )
340+ if not fields :
341+ return project , location
342+
343+ if location and fields .location != location :
344+ raise RuntimeError (
345+ f"location { location } is provided, but different from "
346+ f"the resource location { fields .location } "
347+ )
348+
349+ return fields .project , fields .location
350+
309351 def _get_gca_resource (self , resource_name : str ) -> proto .Message :
310352 """Returns GAPIC service representation of client class resource."""
311353 """
@@ -493,6 +535,7 @@ def __init__(
493535 project : Optional [str ] = None ,
494536 location : Optional [str ] = None ,
495537 credentials : Optional [auth_credentials .Credentials ] = None ,
538+ resource_name : Optional [str ] = None ,
496539 ):
497540 """Initializes class with project, location, and api_client.
498541
@@ -502,9 +545,14 @@ def __init__(
502545 credentials(google.auth.crendentials.Crendentials):
503546 Optional. custom credentials to use when accessing interacting with
504547 resource noun.
548+ resource_name(str): A fully-qualified resource name or ID.
505549 """
506550 AiPlatformResourceNoun .__init__ (
507- self , project = project , location = location , credentials = credentials
551+ self ,
552+ project = project ,
553+ location = location ,
554+ credentials = credentials ,
555+ resource_name = resource_name ,
508556 )
509557 FutureManager .__init__ (self )
510558
@@ -514,6 +562,7 @@ def _empty_constructor(
514562 project : Optional [str ] = None ,
515563 location : Optional [str ] = None ,
516564 credentials : Optional [auth_credentials .Credentials ] = None ,
565+ resource_name : Optional [str ] = None ,
517566 ) -> "AiPlatformResourceNounWithFutureManager" :
518567 """Initializes with all attributes set to None.
519568
@@ -526,11 +575,18 @@ def _empty_constructor(
526575 credentials(google.auth.crendentials.Crendentials):
527576 Optional. custom credentials to use when accessing interacting with
528577 resource noun.
578+ resource_name(str): A fully-qualified resource name or ID.
529579 Returns:
530580 An instance of this class with attributes set to None.
531581 """
532582 self = cls .__new__ (cls )
533- AiPlatformResourceNoun .__init__ (self , project , location , credentials )
583+ AiPlatformResourceNoun .__init__ (
584+ self ,
585+ project = project ,
586+ location = location ,
587+ credentials = credentials ,
588+ resource_name = resource_name ,
589+ )
534590 FutureManager .__init__ (self )
535591 self ._gca_resource = None
536592 return self
0 commit comments