ephraimbuddy commented on code in PR #55919: URL: https://github.com/apache/airflow/pull/55919#discussion_r2417329319
########## providers/google/src/airflow/providers/google/cloud/bundles/gcs.py: ########## @@ -0,0 +1,163 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import os +from pathlib import Path + +import structlog +from google.api_core.exceptions import NotFound + +from airflow.dag_processing.bundles.base import BaseDagBundle +from airflow.exceptions import AirflowException +from airflow.providers.google.cloud.hooks.gcs import GCSHook +from airflow.providers.google.common.hooks.base_google import GoogleBaseHook + + +class GCSDagBundle(BaseDagBundle): + """ + GCS Dag bundle - exposes a directory in GCS as a Dag bundle. + + This allows Airflow to load Dags directly from a GCS bucket. + + :param gcp_conn_id: Airflow connection ID for GCS. Defaults to GoogleBaseHook.default_conn_name. + :param bucket_name: The name of the GCS bucket containing the Dag files. + :param prefix: Optional subdirectory within the GCS bucket where the Dags are stored. + If None, Dags are assumed to be at the root of the bucket (Optional). + """ + + supports_versioning = False + + def __init__( + self, + *, + gcp_conn_id: str = GoogleBaseHook.default_conn_name, + bucket_name: str, + prefix: str = "", + **kwargs, + ) -> None: + super().__init__(**kwargs) + self.gcp_conn_id = gcp_conn_id + self.bucket_name = bucket_name + self.prefix = prefix + # Local path where GCS Dags are downloaded + self.gcs_dags_dir: Path = self.base_dir + + log = structlog.get_logger(__name__) + self._log = log.bind( + bundle_name=self.name, + version=self.version, + bucket_name=self.bucket_name, + prefix=self.prefix, + gcp_conn_id=self.gcp_conn_id, + ) + self._gcs_hook: GCSHook | None = None + + def _initialize(self): + with self.lock(): + if not self.gcs_dags_dir.exists(): + self._log.info("Creating local Dags directory: %s", self.gcs_dags_dir) + os.makedirs(self.gcs_dags_dir) + + if not self.gcs_dags_dir.is_dir(): + raise NotADirectoryError(f"Local Dags path: {self.gcs_dags_dir} is not a directory.") + + try: + self.gcs_hook.get_bucket(bucket_name=self.bucket_name) + except NotFound: + raise FileNotFoundError(f"GCS bucket '{self.bucket_name}' does not exist.") Review Comment: Let's use RuntimeError for this ########## providers/google/src/airflow/providers/google/cloud/bundles/gcs.py: ########## @@ -0,0 +1,163 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import os +from pathlib import Path + +import structlog +from google.api_core.exceptions import NotFound + +from airflow.dag_processing.bundles.base import BaseDagBundle +from airflow.exceptions import AirflowException +from airflow.providers.google.cloud.hooks.gcs import GCSHook +from airflow.providers.google.common.hooks.base_google import GoogleBaseHook + + +class GCSDagBundle(BaseDagBundle): + """ + GCS Dag bundle - exposes a directory in GCS as a Dag bundle. + + This allows Airflow to load Dags directly from a GCS bucket. + + :param gcp_conn_id: Airflow connection ID for GCS. Defaults to GoogleBaseHook.default_conn_name. + :param bucket_name: The name of the GCS bucket containing the Dag files. + :param prefix: Optional subdirectory within the GCS bucket where the Dags are stored. + If None, Dags are assumed to be at the root of the bucket (Optional). + """ + + supports_versioning = False + + def __init__( + self, + *, + gcp_conn_id: str = GoogleBaseHook.default_conn_name, + bucket_name: str, + prefix: str = "", + **kwargs, + ) -> None: + super().__init__(**kwargs) + self.gcp_conn_id = gcp_conn_id + self.bucket_name = bucket_name + self.prefix = prefix + # Local path where GCS Dags are downloaded + self.gcs_dags_dir: Path = self.base_dir + + log = structlog.get_logger(__name__) + self._log = log.bind( + bundle_name=self.name, + version=self.version, + bucket_name=self.bucket_name, + prefix=self.prefix, + gcp_conn_id=self.gcp_conn_id, + ) + self._gcs_hook: GCSHook | None = None + + def _initialize(self): + with self.lock(): + if not self.gcs_dags_dir.exists(): + self._log.info("Creating local Dags directory: %s", self.gcs_dags_dir) + os.makedirs(self.gcs_dags_dir) + + if not self.gcs_dags_dir.is_dir(): + raise NotADirectoryError(f"Local Dags path: {self.gcs_dags_dir} is not a directory.") + + try: + self.gcs_hook.get_bucket(bucket_name=self.bucket_name) + except NotFound: + raise FileNotFoundError(f"GCS bucket '{self.bucket_name}' does not exist.") + + if self.prefix: + # don't check when prefix is "" + if not self.gcs_hook.list(bucket_name=self.bucket_name, prefix=self.prefix): + raise FileNotFoundError( Review Comment: RuntimeError here too -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
