1313# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414# See the License for the specific language governing permissions and
1515# limitations under the License.
16- #
1716
18- import re
17+ from types import ModuleType
1918from typing import Any , Dict , Optional
2019from urllib import request
2120
2221from google .auth import credentials as auth_credentials
2322from google .auth import transport
2423from google .cloud import storage
24+ from google .cloud .aiplatform .constants import pipeline as pipeline_constants
2525
2626# Pattern for an Artifact Registry URL.
27- _VALID_AR_URL = re .compile (r"^https:\/\/([\w-]+)-kfp\.pkg\.dev\/.*" )
27+ _VALID_AR_URL = pipeline_constants ._VALID_AR_URL
28+
29+ # Pattern for any JSON or YAML file over HTTPS.
30+ _VALID_HTTPS_URL = pipeline_constants ._VALID_HTTPS_URL
2831
2932
3033def load_yaml (
@@ -36,8 +39,8 @@ def load_yaml(
3639
3740 Args:
3841 path (str):
39- Required. The path of the YAML document in Google Cloud Storage or
40- local .
42+ Required. The path of the YAML document. It can be a local path, a
43+ Google Cloud Storage URI, an Artifact Registry URI, or an HTTPS URI .
4144 project (str):
4245 Optional. Project to initiate the Storage client with.
4346 credentials (auth_credentials.Credentials):
@@ -48,12 +51,31 @@ def load_yaml(
4851 """
4952 if path .startswith ("gs://" ):
5053 return _load_yaml_from_gs_uri (path , project , credentials )
51- elif _VALID_AR_URL .match (path ):
52- return _load_yaml_from_ar_uri (path , credentials )
54+ elif path .startswith ("http://" ) or path .startswith ("https://" ):
55+ if _VALID_AR_URL .match (path ) or _VALID_HTTPS_URL .match (path ):
56+ return _load_yaml_from_https_uri (path , credentials )
57+ else :
58+ raise ValueError (
59+ "Invalid HTTPS URI. If not using Artifact Registry, please "
60+ "ensure the URI ends with .json, .yaml, or .yml."
61+ )
5362 else :
5463 return _load_yaml_from_local_file (path )
5564
5665
66+ def _maybe_import_yaml () -> ModuleType :
67+ """Tries to import the PyYAML module."""
68+ try :
69+ import yaml
70+ except ImportError :
71+ raise ImportError (
72+ "PyYAML is not installed and is required to parse PipelineJob or "
73+ 'PipelineSpec files. Please install the SDK using "pip install '
74+ 'google-cloud-aiplatform[pipelines]"'
75+ )
76+ return yaml
77+
78+
5779def _load_yaml_from_gs_uri (
5880 uri : str ,
5981 project : Optional [str ] = None ,
@@ -72,13 +94,7 @@ def _load_yaml_from_gs_uri(
7294 Returns:
7395 A Dict object representing the YAML document.
7496 """
75- try :
76- import yaml
77- except ImportError :
78- raise ImportError (
79- "pyyaml is not installed and is required to parse PipelineJob or PipelineSpec files. "
80- 'Please install the SDK using "pip install google-cloud-aiplatform[pipelines]"'
81- )
97+ yaml = _maybe_import_yaml ()
8298 storage_client = storage .Client (project = project , credentials = credentials )
8399 blob = storage .Blob .from_string (uri , storage_client )
84100 return yaml .safe_load (blob .download_as_bytes ())
@@ -94,39 +110,27 @@ def _load_yaml_from_local_file(file_path: str) -> Dict[str, Any]:
94110 Returns:
95111 A Dict object representing the YAML document.
96112 """
97- try :
98- import yaml
99- except ImportError :
100- raise ImportError (
101- "pyyaml is not installed and is required to parse PipelineJob or PipelineSpec files. "
102- 'Please install the SDK using "pip install google-cloud-aiplatform[pipelines]"'
103- )
113+ yaml = _maybe_import_yaml ()
104114 with open (file_path ) as f :
105115 return yaml .safe_load (f )
106116
107117
108- def _load_yaml_from_ar_uri (
118+ def _load_yaml_from_https_uri (
109119 uri : str ,
110120 credentials : Optional [auth_credentials .Credentials ] = None ,
111121) -> Dict [str , Any ]:
112122 """Loads data from a YAML document referenced by a Artifact Registry URI.
113123
114124 Args:
115- path (str):
125+ uri (str):
116126 Required. Artifact Registry URI for YAML document.
117127 credentials (auth_credentials.Credentials):
118128 Optional. Credentials to use with Artifact Registry.
119129
120130 Returns:
121131 A Dict object representing the YAML document.
122132 """
123- try :
124- import yaml
125- except ImportError :
126- raise ImportError (
127- "pyyaml is not installed and is required to parse PipelineJob or PipelineSpec files. "
128- 'Please install the SDK using "pip install google-cloud-aiplatform[pipelines]"'
129- )
133+ yaml = _maybe_import_yaml ()
130134 req = request .Request (uri )
131135
132136 if credentials :
0 commit comments