diff --git a/google/cloud/aiplatform/base.py b/google/cloud/aiplatform/base.py index 70b35329d1..f0629eaa19 100644 --- a/google/cloud/aiplatform/base.py +++ b/google/cloud/aiplatform/base.py @@ -320,6 +320,16 @@ def _get_gca_resource(self, resource_name: str) -> proto.Message: project=self.project, location=self.location, ) + ( + resource_project, + resource_location, + ) = utils.extract_project_location_from_resource_name( + resource_name=resource_name, resource_noun=self._resource_noun + ) + if resource_location != self.location: + self.api_client = self._instantiate_client( + resource_location, self.credentials + ) return getattr(self.api_client, self._getter_method)(name=resource_name) diff --git a/google/cloud/aiplatform/utils.py b/google/cloud/aiplatform/utils.py index ec39038942..58e135dcf1 100644 --- a/google/cloud/aiplatform/utils.py +++ b/google/cloud/aiplatform/utils.py @@ -128,6 +128,35 @@ def extract_fields_from_resource_name( return fields +def extract_project_location_from_resource_name( + resource_name: str, resource_noun: Optional[str] = None +) -> Optional[Tuple]: + """Returns extracted fields from a fully-qualified resource name. + Returns None if name is invalid. + + Args: + resource_name (str): + Required. A fully-qualified AI Platform (Unified) resource name + + resource_noun (str): + A plural resource noun to validate the resource name against. + For example, you would pass "datasets" to validate + "projects/123/locations/us-central1/datasets/456". + + Returns: + (Tuple): + A tuple containing 2 extracted fields from a resource name: + project, location. These fields can be used for + subsequent method calls in the SDK. + """ + fields = extract_fields_from_resource_name(resource_name, resource_noun) + + if not fields: + return None + + return fields.project, fields.location + + def full_resource_name( resource_name: str, resource_noun: str,