Skip to content

Commit a330bdc

Browse files
committed
fix(metadata): default execution credentials to none
1 parent 89d2b91 commit a330bdc

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

google/cloud/aiplatform/metadata/execution.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def create(
100100
metadata_store_id: str = "default",
101101
project: Optional[str] = None,
102102
location: Optional[str] = None,
103-
credentials=Optional[auth_credentials.Credentials],
103+
credentials: Optional[auth_credentials.Credentials] = None,
104104
) -> "Execution":
105105
"""
106106
Creates a new Metadata Execution.
@@ -149,6 +149,11 @@ def create(
149149
"aiplatform.metadata.execution.Execution.create"
150150
)
151151

152+
if metadata_store_id == "default":
153+
metadata_store._MetadataStore.ensure_default_metadata_store_exists(
154+
project=project, location=location, credentials=credentials
155+
)
156+
152157
return cls._create(
153158
resource_id=resource_id,
154159
schema_title=schema_title,
@@ -178,7 +183,7 @@ def _create(
178183
metadata_store_id: str = "default",
179184
project: Optional[str] = None,
180185
location: Optional[str] = None,
181-
credentials=Optional[auth_credentials.Credentials],
186+
credentials: Optional[auth_credentials.Credentials] = None,
182187
) -> "Execution":
183188
"""
184189
Creates a new Metadata Execution.

tests/unit/aiplatform/test_metadata_resources.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#
1717

1818
from importlib import reload
19+
from unittest import mock
1920
from unittest.mock import patch
2021

2122
import pytest
@@ -28,6 +29,7 @@
2829
from google.cloud.aiplatform.metadata import artifact
2930
from google.cloud.aiplatform.metadata import context
3031
from google.cloud.aiplatform.metadata import execution
32+
from google.cloud.aiplatform.metadata import metadata_store
3133
from google.cloud.aiplatform.metadata import utils as metadata_utils
3234
from google.cloud.aiplatform_v1 import (
3335
MetadataServiceClient,
@@ -49,6 +51,9 @@
4951
_TEST_METADATA_STORE = "test-metadata-store"
5052
_TEST_ALT_LOCATION = "europe-west4"
5153
_TEST_PARENT = f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}/metadataStores/{_TEST_METADATA_STORE}"
54+
_TEST_DEFAULT_PARENT = (
55+
f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}/metadataStores/default"
56+
)
5257

5358
# resource attributes
5459
_TEST_DISPLAY_NAME = "test-display-name"
@@ -749,6 +754,57 @@ def test_init_execution_with_id(self, get_execution_mock):
749754
name=_TEST_EXECUTION_NAME, retry=base._DEFAULT_RETRY
750755
)
751756

757+
def test_create_execution_uses_default_credentials_and_metadata_store(self):
758+
aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION)
759+
api_client = mock.Mock()
760+
api_client.create_execution.return_value = GapicExecution(
761+
name=f"{_TEST_DEFAULT_PARENT}/executions/{_TEST_EXECUTION_ID}",
762+
display_name=_TEST_DISPLAY_NAME,
763+
schema_title=_TEST_SCHEMA_TITLE,
764+
schema_version=_TEST_SCHEMA_VERSION,
765+
description=_TEST_DESCRIPTION,
766+
metadata=_TEST_METADATA,
767+
state=GapicExecution.State.RUNNING,
768+
)
769+
770+
with patch.object(
771+
metadata_store._MetadataStore, "ensure_default_metadata_store_exists"
772+
) as ensure_metadata_store_mock, patch.object(
773+
execution.Execution, "_instantiate_client", return_value=api_client
774+
) as instantiate_client_mock:
775+
my_execution = execution.Execution.create(
776+
resource_id=_TEST_EXECUTION_ID,
777+
schema_title=_TEST_SCHEMA_TITLE,
778+
display_name=_TEST_DISPLAY_NAME,
779+
schema_version=_TEST_SCHEMA_VERSION,
780+
description=_TEST_DESCRIPTION,
781+
metadata=_TEST_METADATA,
782+
)
783+
784+
ensure_metadata_store_mock.assert_called_once_with(
785+
project=None, location=None, credentials=None
786+
)
787+
assert instantiate_client_mock.call_args_list[0] == mock.call(
788+
location=None,
789+
credentials=None,
790+
appended_user_agent=[
791+
"sdk_command/aiplatform.metadata.execution.Execution.create"
792+
],
793+
)
794+
api_client.create_execution.assert_called_once_with(
795+
parent=_TEST_DEFAULT_PARENT,
796+
execution_id=_TEST_EXECUTION_ID,
797+
execution=GapicExecution(
798+
schema_title=_TEST_SCHEMA_TITLE,
799+
schema_version=_TEST_SCHEMA_VERSION,
800+
display_name=_TEST_DISPLAY_NAME,
801+
description=_TEST_DESCRIPTION,
802+
metadata=_TEST_METADATA,
803+
state=GapicExecution.State.RUNNING,
804+
),
805+
)
806+
assert my_execution._gca_resource == api_client.create_execution.return_value
807+
752808
def test_get_or_create_execution(
753809
self, get_execution_for_get_or_create_mock, create_execution_mock
754810
):

0 commit comments

Comments
 (0)