Skip to content

Commit

Permalink
Extract missing gcs_to_local example DAG from gcs example (#10767)
Browse files Browse the repository at this point in the history
Co-authored-by: Kamil Breguła <[email protected]>
  • Loading branch information
ephraimbuddy and Kamil Breguła committed Sep 8, 2020
1 parent 3c3342f commit 078bfaf
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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.

import os

from airflow import models
from airflow.providers.google.cloud.transfers.gcs_to_local import GCSToLocalFilesystemOperator
from airflow.utils.dates import days_ago

PROJECT_ID = os.environ.get("GCP_PROJECT_ID", "example-id")
BUCKET = os.environ.get("GCP_GCS_BUCKET", "test-gcs-example-bucket")

PATH_TO_REMOTE_FILE = os.environ.get("GCP_GCS_PATH_TO_UPLOAD_FILE", "test-gcs-example-remote.txt")
PATH_TO_LOCAL_FILE = os.environ.get("GCP_GCS_PATH_TO_SAVED_FILE", "test-gcs-example-local.txt")

with models.DAG(
"example_gcs_to_local", start_date=days_ago(1), schedule_interval=None, tags=['example'],
) as dag:
# [START howto_operator_gcs_download_file_task]
download_file = GCSToLocalFilesystemOperator(
task_id="download_file", object_name=PATH_TO_REMOTE_FILE, bucket=BUCKET, filename=PATH_TO_LOCAL_FILE,
)
# [END howto_operator_gcs_download_file_task]
2 changes: 1 addition & 1 deletion docs/howto/operator/google/transfer/gcs_to_local.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ data from GCS to local filesystem.

Below is an example of using this operator to upload a file to GCS.

.. exampleinclude:: /../airflow/providers/google/cloud/example_dags/example_gcs.py
.. exampleinclude:: /../airflow/providers/google/cloud/example_dags/example_gcs_to_local.py
:language: python
:dedent: 0
:start-after: [START howto_operator_gcs_download_file_task]
Expand Down
50 changes: 50 additions & 0 deletions tests/providers/google/cloud/transfers/test_gcs_to_local_system.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#
# 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.
import os

import pytest

from airflow.providers.google.cloud.example_dags.example_gcs_to_local import (
BUCKET,
PATH_TO_REMOTE_FILE,
PATH_TO_LOCAL_FILE,
)
from tests.providers.google.cloud.utils.gcp_authenticator import GCP_GCS_KEY
from tests.test_utils.gcp_system_helpers import CLOUD_DAG_FOLDER, GoogleSystemTest, provide_gcp_context


@pytest.mark.backend("mysql", "postgres")
@pytest.mark.credential_file(GCP_GCS_KEY)
class GoogleCloudStorageExampleDagsTest(GoogleSystemTest):
@provide_gcp_context(GCP_GCS_KEY)
def setUp(self):
super().setUp()
self.create_gcs_bucket(BUCKET)
self.upload_content_to_gcs(
lines=f"{os.urandom(1 * 1024 * 1024)}", bucket=BUCKET, filename=PATH_TO_REMOTE_FILE
)

@provide_gcp_context(GCP_GCS_KEY)
def tearDown(self):
self.delete_gcs_bucket(BUCKET)
os.remove(PATH_TO_LOCAL_FILE)
super().tearDown()

@provide_gcp_context(GCP_GCS_KEY)
def test_run_example_dag(self):
self.run_dag('example_gcs_to_local', CLOUD_DAG_FOLDER)
1 change: 0 additions & 1 deletion tests/test_project_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ class TestGoogleProviderProjectStructure(unittest.TestCase):
('cloud', 'cassandra_to_gcs'),
('cloud', 'mysql_to_gcs'),
('cloud', 'mssql_to_gcs'),
('cloud', 'gcs_to_local'),
('ads', 'ads_to_gcs'),
}

Expand Down

0 comments on commit 078bfaf

Please sign in to comment.