Skip to content

Commit bb06112

Browse files
authored
Add blogs folder for blog post code (GoogleCloudPlatform#5360)
* Add `blogs` folder for blog post code * fix lint * add apache license to YAML files * Add list_buckets_result task, change project * refactor to use variables and xcoms * add fixture to initialize airflow db * replace project string with placeholder * fix lint
1 parent 8de71e0 commit bb06112

File tree

15 files changed

+181
-0
lines changed

15 files changed

+181
-0
lines changed

composer/blog/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Cloud Composer Blog Posts
2+
3+
Beginning in 2021, any code samples written by Cloud Composer DevRel and featured in blog posts are found in this repo.

composer/blog/__init__.py

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# GCP Tech Blog
2+
The code here is featured in posts found on the [Google Cloud Platform Tech Blog](https://cloud.google.com/blog/topics/developers-practitioners)

composer/blog/gcp-tech-blog/__init__.py

Whitespace-only changes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2021 Google LLC
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
FROM python:3.8
16+
17+
# Allow statements and log messages to immediately appear in the Cloud Run logs
18+
ENV PYTHONUNBUFFERED True
19+
20+
COPY requirements.txt ./
21+
COPY requirements-composer.txt ./
22+
COPY requirements-test.txt ./
23+
24+
RUN pip install --no-cache-dir -r requirements.txt
25+
RUN pip install --no-cache-dir -r requirements-test.txt
26+
RUN pip install --no-cache-dir -r requirements-composer.txt
27+
28+
#copy dag code to container image
29+
ENV DAGS /dags
30+
WORKDIR $DAGS
31+
COPY . ./
32+
CMD ["pytest", "-s", "dags/example_dag_test.py"]

composer/blog/gcp-tech-blog/unit-test-dags-cloud-build/__init__.py

Whitespace-only changes.

composer/blog/gcp-tech-blog/unit-test-dags-cloud-build/dags/__init__.py

Whitespace-only changes.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2021 Google LLC
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import uuid
16+
17+
from airflow import models
18+
19+
from airflow.operators.bash_operator import BashOperator
20+
from airflow.operators.python_operator import PythonOperator
21+
from airflow.providers.google.cloud.operators.gcs import GCSCreateBucketOperator
22+
from airflow.providers.google.cloud.operators.gcs import GCSDeleteBucketOperator
23+
from airflow.providers.google.cloud.operators.gcs import GCSListObjectsOperator
24+
25+
from airflow.utils.dates import days_ago
26+
from airflow.utils.state import State
27+
28+
# Assumes existence of Airflow Variable set to name of GCP Project
29+
PROJECT_ID = models.Variable.get("gcp_project")
30+
31+
32+
with models.DAG(
33+
"example_gcs",
34+
start_date=days_ago(1),
35+
schedule_interval=None,
36+
) as dag:
37+
generate_uuid = PythonOperator(
38+
task_id='generate_uuid',
39+
python_callable=lambda: str(uuid.uuid4())
40+
)
41+
create_bucket = GCSCreateBucketOperator(task_id="create_bucket", bucket_name="{{ task_instance.xcom_pull('generate_uuid') }}", project_id=PROJECT_ID)
42+
list_objects = GCSListObjectsOperator(task_id="list_objects", bucket="{{ task_instance.xcom_pull('generate_uuid') }}")
43+
list_buckets_result = BashOperator(
44+
task_id="list_buckets_result",
45+
bash_command="echo \"{{ task_instance.xcom_pull('list_objects') }}\"",
46+
)
47+
delete_bucket = GCSDeleteBucketOperator(task_id="delete_bucket", bucket_name="{{ task_instance.xcom_pull('generate_uuid') }}")
48+
49+
generate_uuid >> create_bucket >> list_objects >> list_buckets_result >> delete_bucket
50+
51+
52+
if __name__ == "__main__":
53+
dag.clear(dag_run_state=State.NONE)
54+
dag.run()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2021 Google LLC
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from airflow import models
16+
import airflow.utils.db
17+
import internal_unit_testing
18+
import pytest
19+
20+
# user should substitute their project ID
21+
PROJECT_ID = 'your-project-id'
22+
23+
24+
@pytest.fixture(autouse=True, scope="module")
25+
def initalize_airflow_database():
26+
airflow.utils.db.initdb()
27+
28+
29+
def test_dag_import():
30+
models.Variable.set('gcp_project', PROJECT_ID)
31+
from . import example_dag
32+
internal_unit_testing.assert_has_valid_dag(example_dag)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": [
3+
"config:base"
4+
],
5+
"baseBranches": ["main"],
6+
"masterIssue": true,
7+
"pip_requirements": {
8+
"fileMatch": ["requirements-composer.txt"]
9+
}
10+
}

0 commit comments

Comments
 (0)