Skip to content
This repository was archived by the owner on Feb 1, 2024. It is now read-only.

Commit 775acfe

Browse files
authored
[Cloud Tasks] Add task with authentication sample [(#2113)](GoogleCloudPlatform/python-docs-samples#2113)
* Add task with authentication sample * Fix linting * Fix linting * Fix spacing * Update tests with service account * Move samples and update READMEs * Update version and linting
1 parent adf9218 commit 775acfe

File tree

4 files changed

+119
-30
lines changed

4 files changed

+119
-30
lines changed

samples/snippets/README.md

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ App Engine queues push tasks to an App Engine HTTP target. This directory
1212
contains both the App Engine app to deploy, as well as the snippets to run
1313
locally to push tasks to it, which could also be called on App Engine.
1414

15-
`create_app_engine_queue_task.py` is a simple command-line program to create
16-
tasks to be pushed to the App Engine app.
15+
`create_http_task.py` is a simple command-line program to create
16+
tasks to be pushed to an URL endpoint.
17+
18+
`create_http_task_with_token.py` is a simple command-line program to create
19+
tasks to be pushed to an URL endpoint with authorization header.
1720

1821
`main.py` is the main App Engine app. This app serves as an endpoint to receive
1922
App Engine task attempts.
@@ -41,33 +44,6 @@ gcloud beta tasks queues create-app-engine-queue my-appengine-queue
4144
Note: A newly created queue will route to the default App Engine service and
4245
version unless configured to do otherwise.
4346

44-
## Deploying the App Engine App
45-
46-
Deploy the App Engine app with gcloud:
47-
48-
* To deploy to the Standard environment:
49-
```
50-
gcloud app deploy app.yaml
51-
```
52-
* To deploy to the Flexible environment:
53-
```
54-
gcloud app deploy app.flexible.yaml
55-
```
56-
57-
Verify the index page is serving:
58-
59-
```
60-
gcloud app browse
61-
```
62-
63-
The App Engine app serves as a target for the push requests. It has an
64-
endpoint `/example_task_handler` that reads the payload (i.e., the request body)
65-
of the HTTP POST request and logs it. The log output can be viewed with:
66-
67-
```
68-
gcloud app logs read
69-
```
70-
7147
## Run the Sample Using the Command Line
7248

7349
Set environment variables:
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright 2019 Google LLC All Rights Reserved.
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+
# http://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 __future__ import print_function
16+
17+
import datetime
18+
19+
20+
def create_http_task(project,
21+
queue,
22+
location,
23+
url,
24+
service_account_email,
25+
payload=None,
26+
in_seconds=None):
27+
# [START cloud_tasks_create_http_task_with_token]
28+
"""Create a task for a given queue with an arbitrary payload."""
29+
30+
from google.cloud import tasks_v2beta3
31+
from google.protobuf import timestamp_pb2
32+
33+
# Create a client.
34+
client = tasks_v2beta3.CloudTasksClient()
35+
36+
# TODO(developer): Uncomment these lines and replace with your values.
37+
# project = 'my-project-id'
38+
# queue = 'my-appengine-queue'
39+
# location = 'us-central1'
40+
# url = 'https://example.com/example_task_handler'
41+
# payload = 'hello'
42+
43+
# Construct the fully qualified queue name.
44+
parent = client.queue_path(project, location, queue)
45+
46+
# Construct the request body.
47+
task = {
48+
'http_request': { # Specify the type of request.
49+
'http_method': 'POST',
50+
'url': url, # The full url path that the task will be sent to.
51+
'oidc_token': {
52+
'service_account_email': service_account_email
53+
}
54+
}
55+
}
56+
57+
if payload is not None:
58+
# The API expects a payload of type bytes.
59+
converted_payload = payload.encode()
60+
61+
# Add the payload to the request.
62+
task['http_request']['body'] = converted_payload
63+
64+
if in_seconds is not None:
65+
# Convert "seconds from now" into an rfc3339 datetime string.
66+
d = datetime.datetime.utcnow() + datetime.timedelta(seconds=in_seconds)
67+
68+
# Create Timestamp protobuf.
69+
timestamp = timestamp_pb2.Timestamp()
70+
timestamp.FromDatetime(d)
71+
72+
# Add the timestamp to the tasks.
73+
task['schedule_time'] = timestamp
74+
75+
# Use the client to build and send the task.
76+
response = client.create_task(parent, task)
77+
78+
print('Created task {}'.format(response.name))
79+
return response
80+
# [END cloud_tasks_create_http_task_with_token]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2019 Google LLC All Rights Reserved.
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+
# http://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 os
16+
17+
import create_http_task_with_token
18+
19+
TEST_PROJECT_ID = os.getenv('GCLOUD_PROJECT')
20+
TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1')
21+
TEST_QUEUE_NAME = os.getenv('TEST_QUEUE_NAME', 'my-appengine-queue')
22+
TEST_SERVICE_ACCOUNT = (
23+
'test-run-invoker@python-docs-samples-tests.iam.gserviceaccount.com')
24+
25+
26+
def test_create_http_task_with_token():
27+
url = 'https://example.com/example_task_handler'
28+
result = create_http_task_with_token.create_http_task(TEST_PROJECT_ID,
29+
TEST_QUEUE_NAME,
30+
TEST_LOCATION,
31+
url,
32+
TEST_SERVICE_ACCOUNT)
33+
assert TEST_QUEUE_NAME in result.name

samples/snippets/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Flask==1.0.2
22
gunicorn==19.9.0
3-
google-cloud-tasks==0.6.0
3+
google-cloud-tasks==0.7.0

0 commit comments

Comments
 (0)