|
26 | 26 | from google.cloud.aiplatform.metadata import constants
|
27 | 27 |
|
28 | 28 |
|
29 |
| -class BaseArtifactSchema(metaclass=abc.ABCMeta): |
| 29 | +class BaseArtifactSchema(artifact.Artifact): |
30 | 30 | """Base class for Metadata Artifact types."""
|
31 | 31 |
|
32 | 32 | @property
|
@@ -81,13 +81,40 @@ def __init__(
|
81 | 81 | Pipelines), and the system does not prescribe or
|
82 | 82 | check the validity of state transitions.
|
83 | 83 | """
|
| 84 | + # resource_id is not stored in the proto. Create method uses the |
| 85 | + # resource_id along with project_id and location to construct an |
| 86 | + # resource_name which is stored in the proto message. |
84 | 87 | self.artifact_id = artifact_id
|
85 |
| - self.uri = uri |
86 |
| - self.display_name = display_name |
87 |
| - self.schema_version = schema_version or constants._DEFAULT_SCHEMA_VERSION |
88 |
| - self.description = description |
89 |
| - self.metadata = metadata |
90 |
| - self.state = state |
| 88 | + |
| 89 | + # Store all other attributes using the proto structure. |
| 90 | + self._gca_resource = gca_artifact.Artifact() |
| 91 | + self._gca_resource.uri = uri |
| 92 | + self._gca_resource.display_name = display_name |
| 93 | + self._gca_resource.schema_version = ( |
| 94 | + schema_version or constants._DEFAULT_SCHEMA_VERSION |
| 95 | + ) |
| 96 | + self._gca_resource.description = description |
| 97 | + |
| 98 | + # If metadata is None covert to {} |
| 99 | + metadata = metadata if metadata else {} |
| 100 | + self._nested_update_metadata(self._gca_resource, metadata) |
| 101 | + self._gca_resource.state = state |
| 102 | + |
| 103 | + # TODO() Switch to @singledispatchmethod constructor overload after py>=3.8 |
| 104 | + def _init_with_resource_name( |
| 105 | + self, |
| 106 | + *, |
| 107 | + artifact_name: str, |
| 108 | + ): |
| 109 | + |
| 110 | + """Initializes the Artifact instance using an existing resource. |
| 111 | +
|
| 112 | + Args: |
| 113 | + artifact_name (str): |
| 114 | + Artifact name with the following format, this is globally unique in a metadataStore: |
| 115 | + projects/123/locations/us-central1/metadataStores/<metadata_store_id>/artifacts/<resource_id>. |
| 116 | + """ |
| 117 | + super(BaseArtifactSchema, self).__init__(artifact_name=artifact_name) |
91 | 118 |
|
92 | 119 | def create(
|
93 | 120 | self,
|
@@ -117,10 +144,27 @@ def create(
|
117 | 144 | Returns:
|
118 | 145 | Artifact: Instantiated representation of the managed Metadata Artifact.
|
119 | 146 | """
|
120 |
| - return artifact.Artifact.create_from_base_artifact_schema( |
121 |
| - base_artifact_schema=self, |
| 147 | + |
| 148 | + # Check if metadata exists to avoid proto read error |
| 149 | + metadata = None |
| 150 | + if self._gca_resource.metadata: |
| 151 | + metadata = self.metadata |
| 152 | + |
| 153 | + new_artifact_instance = artifact.Artifact.create( |
| 154 | + resource_id=self.artifact_id, |
| 155 | + schema_title=self.schema_title, |
| 156 | + uri=self.uri, |
| 157 | + display_name=self.display_name, |
| 158 | + schema_version=self.schema_version, |
| 159 | + description=self.description, |
| 160 | + metadata=metadata, |
| 161 | + state=self.state, |
122 | 162 | metadata_store_id=metadata_store_id,
|
123 | 163 | project=project,
|
124 | 164 | location=location,
|
125 | 165 | credentials=credentials,
|
126 | 166 | )
|
| 167 | + |
| 168 | + # Reinstantiate this class using the newly created resource. |
| 169 | + self._init_with_resource_name(artifact_name=new_artifact_instance.resource_name) |
| 170 | + return self |
0 commit comments