Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate code from googleapis/python-tasks #8478

Merged
merged 69 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
d792f63
docs: add samples from python-docs-samples/tasks (#36)
arithmetic1728 Aug 31, 2020
29c7b48
feat!: migrate to use microgen (#38)
arithmetic1728 Sep 2, 2020
a944737
chore(deps): update dependency google-cloud-tasks to v2 (#41)
renovate-bot Sep 16, 2020
ef58b96
feat: add common resource helpers; expose client transport; add sheba…
yoshi-automation Dec 7, 2020
6c3908a
fix: Update sample comments (#58)
averikitsch Dec 22, 2020
924dab7
chore(deps): update dependency google-cloud-tasks to v2.1.0 (#63)
renovate-bot Jan 13, 2021
4bed162
chore: upgrade gapic-generator-python to 0.39.1 (#64)
yoshi-automation Feb 24, 2021
ba19d22
chore(deps): update dependency google-cloud-tasks to v2.2.0 (#71)
renovate-bot Feb 24, 2021
9ed9d0c
chore: add constraints file check for python samples (#87)
yoshi-automation Apr 12, 2021
8971aaa
chore(deps): update dependency pytest to v6.2.3 (#88)
renovate-bot Apr 13, 2021
594e2b3
chore(deps): update dependency pytest to v6.2.4 (#101)
renovate-bot May 14, 2021
46877b0
chore: new owl bot post processor docker image (#110)
gcf-owl-bot[bot] May 22, 2021
6379083
chore(deps): update dependency google-cloud-tasks to v2.3.0 (#114)
renovate-bot Jun 3, 2021
7774b3c
fix: update sample for task name (#120)
averikitsch Jun 21, 2021
17b7530
chore(deps): update dependency google-cloud-tasks to v2.4.0 (#127)
renovate-bot Jul 3, 2021
4ef90da
feat: Set `audience` field in authenticated HTTP task example (#138)
ehsan-karamad Jul 21, 2021
e280d08
feat: add Samples section to CONTRIBUTING.rst (#135)
gcf-owl-bot[bot] Jul 22, 2021
2f7f798
chore(deps): update dependency google-cloud-tasks to v2.5.0 (#140)
renovate-bot Jul 22, 2021
b1e0306
chore(deps): update dependency google-cloud-tasks to v2.5.1 (#146)
renovate-bot Jul 29, 2021
516f833
chore: fix INSTALL_LIBRARY_FROM_SOURCE in noxfile.py (#150)
gcf-owl-bot[bot] Aug 11, 2021
1e5bd37
chore: drop mention of Python 2.7 from templates (#152)
gcf-owl-bot[bot] Aug 13, 2021
f9e2cb1
chore(deps): update dependency pytest to v6.2.5 (#155)
renovate-bot Aug 31, 2021
b79a142
chore: blacken samples noxfile template (#162)
gcf-owl-bot[bot] Sep 17, 2021
8a353ba
chore(deps): update dependency google-cloud-tasks to v2.5.2 (#167)
renovate-bot Sep 24, 2021
f6c8b1b
chore: fail samples nox session if python version is missing (#170)
gcf-owl-bot[bot] Sep 30, 2021
28f909a
chore(deps): update dependency google-cloud-tasks to v2.6.0 (#175)
renovate-bot Oct 7, 2021
cc50e86
chore(python): Add kokoro configs for python 3.10 samples testing (#179)
gcf-owl-bot[bot] Oct 8, 2021
053acd6
chore(deps): update dependency google-cloud-tasks to v2.7.0 (#185)
renovate-bot Oct 25, 2021
6f90bd1
chore(deps): update dependency google-cloud-tasks to v2.7.1 (#189)
renovate-bot Nov 2, 2021
dad3ee6
chore: delete owlbot.py (#184)
parthea Nov 22, 2021
76cf41c
chore(samples): Add check for tests in directory (#204)
gcf-owl-bot[bot] Jan 11, 2022
4b4614b
chore(deps): update dependency google-cloud-tasks to v2.7.2 (#206)
renovate-bot Jan 13, 2022
1e56320
chore: update samples (#207)
averikitsch Jan 13, 2022
4665108
chore(python): Noxfile recognizes that tests can live in a folder (#210)
gcf-owl-bot[bot] Jan 19, 2022
423a313
chore(deps): update dependency pytest to v7 (#219)
renovate-bot Feb 7, 2022
f4c8e64
chore(deps): update dependency pytest to v7.0.1 (#221)
renovate-bot Feb 14, 2022
63577c8
chore(deps): update dependency google-cloud-tasks to v2.8.0 (#222)
renovate-bot Feb 24, 2022
54e73a6
chore: Adding support for pytest-xdist and pytest-parallel (#233)
gcf-owl-bot[bot] Mar 4, 2022
95f8f52
chore(deps): update dependency google-cloud-tasks to v2.8.1 (#236)
renovate-bot Mar 7, 2022
d7278bd
chore(deps): update dependency pytest to v7.1.0 (#238)
renovate-bot Mar 13, 2022
f94cb21
chore(deps): update dependency pytest to v7.1.1 (#239)
renovate-bot Mar 19, 2022
ea78cc0
chore(python): use black==22.3.0 (#243)
gcf-owl-bot[bot] Mar 29, 2022
f5c55ef
chore(python): add nox session to sort python imports (#254)
gcf-owl-bot[bot] Apr 21, 2022
353a8b6
chore(deps): update dependency pytest to v7.1.2 (#257)
renovate-bot Apr 25, 2022
f71f1a2
chore(deps): update dependency google-cloud-tasks to v2.9.0 (#260)
renovate-bot May 7, 2022
c75fc59
fix: require python 3.7+ (#275)
gcf-owl-bot[bot] Jul 9, 2022
b6523f9
chore(deps): update all dependencies (#267)
renovate-bot Jul 16, 2022
9a993ef
chore(deps): update all dependencies (#281)
renovate-bot Aug 2, 2022
4a3afe8
docs(sample): update protobuf in create_http_task.py (#283)
averikitsch Aug 9, 2022
750ff9c
chore(deps): update all dependencies (#286)
renovate-bot Aug 9, 2022
c8655e0
chore(deps): update dependency google-cloud-tasks to v2.10.2 (#291)
renovate-bot Aug 16, 2022
6529c49
chore(deps): update dependency pytest to v7.1.3 (#299)
renovate-bot Sep 6, 2022
be9c48e
chore: detect samples tests in nested directories (#304)
gcf-owl-bot[bot] Sep 13, 2022
58066c9
chore(deps): update dependency google-cloud-tasks to v2.10.3 (#308)
renovate-bot Oct 4, 2022
d30253e
chore(deps): update dependency google-cloud-tasks to v2.10.4 (#311)
renovate-bot Oct 18, 2022
6327b4c
chore(deps): update dependency pytest to v7.2.0 (#312)
renovate-bot Oct 26, 2022
b24db83
Merge remote-tracking branch 'migration/main' into python-tasks-migra…
msampathkumar Nov 9, 2022
f23a03d
Update folder name to use underscore.
msampathkumar Nov 9, 2022
d5ed303
Update code owners for cloud_tasks
msampathkumar Nov 9, 2022
21d39b4
Add shebang & update licence header!
msampathkumar Nov 9, 2022
94f2fe1
Remove readme files
msampathkumar Nov 9, 2022
11a7745
Merge branch 'main' into python-tasks-migration
m-strzelczyk Nov 10, 2022
5b66782
Merge branch 'main' into python-tasks-migration
msampathkumar Nov 11, 2022
d87b26a
Merge branch 'main' into python-tasks-migration
msampathkumar Nov 11, 2022
2ac7e14
Merge branch 'main' into python-tasks-migration
msampathkumar Nov 14, 2022
974b90f
Merge branch 'main' into python-tasks-migration
msampathkumar Nov 15, 2022
5603ae2
Update blunderbuss.yml
msampathkumar Nov 15, 2022
71dcebe
Update blunderbuss.yml
msampathkumar Nov 15, 2022
98a5464
Merge branch 'main' into python-tasks-migration
msampathkumar Nov 15, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
/cdn/**/* @mpwarres @GoogleCloudPlatform/python-samples-reviewers
/cloudbuild/**/* @GoogleCloudPlatform/torus-dpe @GoogleCloudPlatform/python-samples-reviewers
/cloud-sql/**/* @GoogleCloudPlatform/infra-db-dpes @GoogleCloudPlatform/python-samples-reviewers
/cloud_tasks/**/* @GoogleCloudPlatform/infra-db-dpes @GoogleCloudPlatform/python-samples-reviewers
/codelabs/**/* @GoogleCloudPlatform/python-samples-reviewers
/composer/**/* @leahecole @rachael-ds @rafalbiegacz @GoogleCloudPlatform/python-samples-reviewers
/compute/**/* @m-strzelczyk @GoogleCloudPlatform/dee-infra @GoogleCloudPlatform/python-samples-reviewers
Expand Down
4 changes: 4 additions & 0 deletions .github/blunderbuss.yml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ assign_prs_by:
- 'api: talent'
to:
- GoogleCloudPlatform/python-samples-reviewers
- labels:
- 'api: cloudtasks'
to:
- GoogleCloudPlatform/infra-db-dpes

assign_issues:
- GoogleCloudPlatform/python-samples-owners
Expand Down
157 changes: 157 additions & 0 deletions cloud_tasks/snippets/create_http_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#!/usr/bin/env python
#
# Copyright 2022 Google, Inc.
#
# Licensed 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 print_function

import argparse


def create_http_task(
project,
queue,
location,
url,
payload=None,
in_seconds=None,
task_name=None,
deadline=None,
):
# [START cloud_tasks_create_http_task]
"""Create a task for a given queue with an arbitrary payload."""

import datetime
import json

from google.cloud import tasks_v2
from google.protobuf import duration_pb2, timestamp_pb2

# Create a client.
client = tasks_v2.CloudTasksClient()

# TODO(developer): Uncomment these lines and replace with your values.
# project = 'my-project-id'
# queue = 'my-queue'
# location = 'us-central1'
# url = 'https://example.com/task_handler'
# payload = 'hello' or {'param': 'value'} for application/json
# in_seconds = 180
# task_name = 'my-unique-task'
# deadline = 900

# Construct the fully qualified queue name.
parent = client.queue_path(project, location, queue)

# Construct the request body.
task = {
"http_request": { # Specify the type of request.
"http_method": tasks_v2.HttpMethod.POST,
"url": url, # The full url path that the task will be sent to.
}
}
if payload is not None:
if isinstance(payload, dict):
# Convert dict to JSON string
payload = json.dumps(payload)
# specify http content-type to application/json
task["http_request"]["headers"] = {"Content-type": "application/json"}

# The API expects a payload of type bytes.
converted_payload = payload.encode()

# Add the payload to the request.
task["http_request"]["body"] = converted_payload

if in_seconds is not None:
# Convert "seconds from now" into an rfc3339 datetime string.
d = datetime.datetime.utcnow() + datetime.timedelta(seconds=in_seconds)

# Create Timestamp protobuf.
timestamp = timestamp_pb2.Timestamp()
timestamp.FromDatetime(d)

# Add the timestamp to the tasks.
task["schedule_time"] = timestamp

if task_name is not None:
# Add the name to tasks.
task["name"] = client.task_path(project, location, queue, task_name)

if deadline is not None:
# Add dispatch deadline for requests sent to the worker.
duration = duration_pb2.Duration()
duration.FromSeconds(deadline)
task["dispatch_deadline"] = duration

# Use the client to build and send the task.
response = client.create_task(request={"parent": parent, "task": task})

print("Created task {}".format(response.name))
# [END cloud_tasks_create_http_task]
return response


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=create_http_task.__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)

parser.add_argument(
"--project",
help="Project of the queue to add the task to.",
required=True,
)

parser.add_argument(
"--queue",
help="ID (short name) of the queue to add the task to.",
required=True,
)

parser.add_argument(
"--location",
help="Location of the queue to add the task to.",
required=True,
)

parser.add_argument(
"--url",
help="The full url path that the request will be sent to.",
required=True,
)

parser.add_argument(
"--payload", help="Optional payload to attach to the push queue."
)

parser.add_argument(
"--in_seconds",
type=int,
help="The number of seconds from now to schedule task attempt.",
)

parser.add_argument("--task_name", help="Task name of the task to create")
args = parser.parse_args()

create_http_task(
args.project,
args.queue,
args.location,
args.url,
args.payload,
args.in_seconds,
args.task_name,
)
62 changes: 62 additions & 0 deletions cloud_tasks/snippets/create_http_task_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python
#
# Copyright 2022 Google, Inc.
#
# Licensed 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 uuid

from google.cloud import tasks_v2
import pytest

import create_http_task

TEST_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
TEST_LOCATION = os.getenv("TEST_QUEUE_LOCATION", "us-central1")
TEST_QUEUE_NAME = f"my-queue-{uuid.uuid4().hex}"


@pytest.fixture()
def test_queue():
client = tasks_v2.CloudTasksClient()
parent = f"projects/{TEST_PROJECT_ID}/locations/{TEST_LOCATION}"
queue = {
# The fully qualified path to the queue
"name": client.queue_path(TEST_PROJECT_ID, TEST_LOCATION, TEST_QUEUE_NAME),
}
q = client.create_queue(request={"parent": parent, "queue": queue})

yield q

client.delete_queue(request={"name": q.name})


def test_create_http_task(test_queue):
url = "https://example.com/task_handler"
result = create_http_task.create_http_task(
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION, url
)
assert TEST_QUEUE_NAME in result.name

result = create_http_task.create_http_task(
TEST_PROJECT_ID,
TEST_QUEUE_NAME,
TEST_LOCATION,
url,
payload="hello",
in_seconds=180,
task_name=uuid.uuid4().hex,
deadline=900,
)
assert TEST_QUEUE_NAME in result.name
75 changes: 75 additions & 0 deletions cloud_tasks/snippets/create_http_task_with_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env python
#
# Copyright 2022 Google, Inc.
#
# Licensed 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 print_function


def create_http_task(
project,
queue,
location,
url,
service_account_email,
audience=None,
payload=None,
):
# [START cloud_tasks_create_http_task_with_token]
"""Create a task for a given queue with an arbitrary payload."""

from google.cloud import tasks_v2

# Create a client.
client = tasks_v2.CloudTasksClient()

# TODO(developer): Uncomment these lines and replace with your values.
# project = 'my-project-id'
# queue = 'my-queue'
# location = 'us-central1'
# url = 'https://example.com/task_handler?param=value'
# audience = 'https://example.com/task_handler'
# service_account_email = 'service-account@my-project-id.iam.gserviceaccount.com';
# payload = 'hello'

# Construct the fully qualified queue name.
parent = client.queue_path(project, location, queue)

# Construct the request body.
task = {
"http_request": { # Specify the type of request.
"http_method": tasks_v2.HttpMethod.POST,
"url": url, # The full url path that the task will be sent to.
"oidc_token": {
"service_account_email": service_account_email,
"audience": audience,
},
}
}

if payload is not None:
# The API expects a payload of type bytes.
converted_payload = payload.encode()

# Add the payload to the request.
task["http_request"]["body"] = converted_payload

# Use the client to build and send the task.
response = client.create_task(request={"parent": parent, "task": task})

print("Created task {}".format(response.name))
return response


# [END cloud_tasks_create_http_task_with_token]
53 changes: 53 additions & 0 deletions cloud_tasks/snippets/create_http_task_with_token_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python
#
# Copyright 2022 Google, Inc.
#
# Licensed 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 uuid

from google.cloud import tasks_v2
import pytest

import create_http_task_with_token

TEST_PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
TEST_LOCATION = os.getenv("TEST_QUEUE_LOCATION", "us-central1")
TEST_QUEUE_NAME = f"my-queue-{uuid.uuid4().hex}"
TEST_SERVICE_ACCOUNT = (
"test-run-invoker@python-docs-samples-tests.iam.gserviceaccount.com"
)


@pytest.fixture()
def test_queue():
client = tasks_v2.CloudTasksClient()
parent = f"projects/{TEST_PROJECT_ID}/locations/{TEST_LOCATION}"
queue = {
# The fully qualified path to the queue
"name": client.queue_path(TEST_PROJECT_ID, TEST_LOCATION, TEST_QUEUE_NAME),
}
q = client.create_queue(request={"parent": parent, "queue": queue})

yield q

client.delete_queue(request={"name": q.name})


def test_create_http_task_with_token(test_queue):
url = "https://example.com/task_handler"
result = create_http_task_with_token.create_http_task(
TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION, url, TEST_SERVICE_ACCOUNT
)
assert TEST_QUEUE_NAME in result.name
1 change: 1 addition & 0 deletions cloud_tasks/snippets/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest==7.2.0
1 change: 1 addition & 0 deletions cloud_tasks/snippets/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-cloud-tasks==2.10.4
3 changes: 0 additions & 3 deletions tasks/README.md

This file was deleted.