Skip to content

Commit 44935b9

Browse files
kosteevleahecole
andauthored
Update composer_storage_trigger.py script to support stable API (GoogleCloudPlatform#6632)
* Update composer_storage_trigger.py script to support stable API for Airflow 2. * Update composer/functions/composer_storage_trigger.py * Apply suggestions from code review Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>
1 parent 04787e4 commit 44935b9

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

composer/functions/composer_storage_trigger.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
IAM_SCOPE = 'https://www.googleapis.com/auth/iam'
2323
OAUTH_TOKEN_URI = 'https://www.googleapis.com/oauth2/v4/token'
24+
# If you are using the stable API, set this value to False
25+
# For more info about Airflow APIs see https://cloud.google.com/composer/docs/access-airflow-api
26+
USE_EXPERIMENTAL_API = True
2427

2528

2629
def trigger_dag(data, context=None):
@@ -46,16 +49,22 @@ def trigger_dag(data, context=None):
4649
webserver_id = 'YOUR-TENANT-PROJECT'
4750
# The name of the DAG you wish to trigger
4851
dag_name = 'composer_sample_trigger_response_dag'
52+
53+
if USE_EXPERIMENTAL_API:
54+
endpoint = f'api/experimental/dags/{dag_name}/dag_runs'
55+
json_data = {'conf': data, 'replace_microseconds': 'false'}
56+
else:
57+
endpoint = f'api/v1/dags/{dag_name}/dagRuns'
58+
json_data = {'conf': data}
4959
webserver_url = (
5060
'https://'
5161
+ webserver_id
52-
+ '.appspot.com/api/experimental/dags/'
53-
+ dag_name
54-
+ '/dag_runs'
62+
+ '.appspot.com/'
63+
+ endpoint
5564
)
5665
# Make a POST request to IAP which then Triggers the DAG
5766
make_iap_request(
58-
webserver_url, client_id, method='POST', json={"conf": data, "replace_microseconds": 'false'})
67+
webserver_url, client_id, method='POST', json=json_data)
5968

6069

6170
# This code is copied from

composer/functions/composer_storage_trigger_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,26 @@ def test_iap_response_error(make_iap_request_mock):
3535
trigger_event = {'file': 'some-gcs-file'}
3636
with pytest.raises(Exception):
3737
composer_storage_trigger.trigger_dag(trigger_event)
38+
39+
40+
@mock.patch('composer_storage_trigger.make_iap_request', autospec=True)
41+
def test_experimental_api_endpoint(make_iap_request_mock):
42+
composer_storage_trigger.trigger_dag({'test': 'a'})
43+
44+
make_iap_request_mock.assert_called_once_with(
45+
'https://YOUR-TENANT-PROJECT.appspot.com/api/experimental/dags/composer_sample_trigger_response_dag/dag_runs',
46+
'YOUR-CLIENT-ID', method='POST',
47+
json={'conf': {'test': 'a'}, 'replace_microseconds': 'false'},
48+
)
49+
50+
51+
@mock.patch('composer_storage_trigger.make_iap_request', autospec=True)
52+
@mock.patch('composer_storage_trigger.USE_EXPERIMENTAL_API', False)
53+
def test_stable_api_endpoint(make_iap_request_mock):
54+
composer_storage_trigger.trigger_dag({'test': 'a'})
55+
56+
make_iap_request_mock.assert_called_once_with(
57+
'https://YOUR-TENANT-PROJECT.appspot.com/api/v1/dags/composer_sample_trigger_response_dag/dagRuns',
58+
'YOUR-CLIENT-ID', method='POST',
59+
json={'conf': {'test': 'a'}},
60+
)

0 commit comments

Comments
 (0)