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 #8477

Closed
wants to merge 57 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
900a8c2
docs: add samples from python-docs-samples/tasks (#36)
arithmetic1728 Aug 31, 2020
0beb1fe
feat!: migrate to use microgen (#38)
arithmetic1728 Sep 2, 2020
720f6dd
chore(deps): update dependency google-cloud-tasks to v2 (#41)
renovate-bot Sep 16, 2020
90b04e2
feat: add common resource helpers; expose client transport; add sheba…
yoshi-automation Dec 7, 2020
73148f4
fix: Update sample comments (#58)
averikitsch Dec 22, 2020
79812ad
chore(deps): update dependency google-cloud-tasks to v2.1.0 (#63)
renovate-bot Jan 13, 2021
e9d8d0f
chore: upgrade gapic-generator-python to 0.39.1 (#64)
yoshi-automation Feb 24, 2021
095f75a
chore(deps): update dependency google-cloud-tasks to v2.2.0 (#71)
renovate-bot Feb 24, 2021
eca1fe1
chore: add constraints file check for python samples (#87)
yoshi-automation Apr 12, 2021
e20e3b0
chore(deps): update dependency pytest to v6.2.3 (#88)
renovate-bot Apr 13, 2021
34ec416
chore(deps): update dependency pytest to v6.2.4 (#101)
renovate-bot May 14, 2021
2fe11b6
chore: new owl bot post processor docker image (#110)
gcf-owl-bot[bot] May 22, 2021
fb8a24b
chore(deps): update dependency google-cloud-tasks to v2.3.0 (#114)
renovate-bot Jun 3, 2021
eeb11b4
fix: update sample for task name (#120)
averikitsch Jun 21, 2021
df5bde3
chore(deps): update dependency google-cloud-tasks to v2.4.0 (#127)
renovate-bot Jul 3, 2021
c0a590d
feat: Set `audience` field in authenticated HTTP task example (#138)
ehsan-karamad Jul 21, 2021
f6cd458
feat: add Samples section to CONTRIBUTING.rst (#135)
gcf-owl-bot[bot] Jul 22, 2021
627c3a8
chore(deps): update dependency google-cloud-tasks to v2.5.0 (#140)
renovate-bot Jul 22, 2021
dcbcbc1
chore(deps): update dependency google-cloud-tasks to v2.5.1 (#146)
renovate-bot Jul 29, 2021
d11063d
chore: fix INSTALL_LIBRARY_FROM_SOURCE in noxfile.py (#150)
gcf-owl-bot[bot] Aug 11, 2021
781b57e
chore: drop mention of Python 2.7 from templates (#152)
gcf-owl-bot[bot] Aug 13, 2021
a416142
chore(deps): update dependency pytest to v6.2.5 (#155)
renovate-bot Aug 31, 2021
36093a5
chore: blacken samples noxfile template (#162)
gcf-owl-bot[bot] Sep 17, 2021
8d90f11
chore(deps): update dependency google-cloud-tasks to v2.5.2 (#167)
renovate-bot Sep 24, 2021
9adfab3
chore: fail samples nox session if python version is missing (#170)
gcf-owl-bot[bot] Sep 30, 2021
81ef423
chore(deps): update dependency google-cloud-tasks to v2.6.0 (#175)
renovate-bot Oct 7, 2021
8aace26
chore(python): Add kokoro configs for python 3.10 samples testing (#179)
gcf-owl-bot[bot] Oct 8, 2021
017f859
chore(deps): update dependency google-cloud-tasks to v2.7.0 (#185)
renovate-bot Oct 25, 2021
5def823
chore(deps): update dependency google-cloud-tasks to v2.7.1 (#189)
renovate-bot Nov 2, 2021
83f3ab6
chore: delete owlbot.py (#184)
parthea Nov 22, 2021
11b028b
chore(samples): Add check for tests in directory (#204)
gcf-owl-bot[bot] Jan 11, 2022
a59a520
chore(deps): update dependency google-cloud-tasks to v2.7.2 (#206)
renovate-bot Jan 13, 2022
e1fdd00
chore: update samples (#207)
averikitsch Jan 13, 2022
c4d714d
chore(python): Noxfile recognizes that tests can live in a folder (#210)
gcf-owl-bot[bot] Jan 19, 2022
a1846ba
chore(deps): update dependency pytest to v7 (#219)
renovate-bot Feb 7, 2022
a05a931
chore(deps): update dependency pytest to v7.0.1 (#221)
renovate-bot Feb 14, 2022
41eca65
chore(deps): update dependency google-cloud-tasks to v2.8.0 (#222)
renovate-bot Feb 24, 2022
b70c98d
chore: Adding support for pytest-xdist and pytest-parallel (#233)
gcf-owl-bot[bot] Mar 4, 2022
535c5a9
chore(deps): update dependency google-cloud-tasks to v2.8.1 (#236)
renovate-bot Mar 7, 2022
0dac9f6
chore(deps): update dependency pytest to v7.1.0 (#238)
renovate-bot Mar 13, 2022
403c5b3
chore(deps): update dependency pytest to v7.1.1 (#239)
renovate-bot Mar 19, 2022
8f790ca
chore(python): use black==22.3.0 (#243)
gcf-owl-bot[bot] Mar 29, 2022
2e41690
chore(python): add nox session to sort python imports (#254)
gcf-owl-bot[bot] Apr 21, 2022
728ac7a
chore(deps): update dependency pytest to v7.1.2 (#257)
renovate-bot Apr 25, 2022
d6c532d
chore(deps): update dependency google-cloud-tasks to v2.9.0 (#260)
renovate-bot May 7, 2022
11f4488
fix: require python 3.7+ (#275)
gcf-owl-bot[bot] Jul 9, 2022
f22c0ba
chore(deps): update all dependencies (#267)
renovate-bot Jul 16, 2022
764c633
chore(deps): update all dependencies (#281)
renovate-bot Aug 2, 2022
7507435
docs(sample): update protobuf in create_http_task.py (#283)
averikitsch Aug 9, 2022
6927f5e
chore(deps): update all dependencies (#286)
renovate-bot Aug 9, 2022
2704c41
chore(deps): update dependency google-cloud-tasks to v2.10.2 (#291)
renovate-bot Aug 16, 2022
7e20ac6
chore(deps): update dependency pytest to v7.1.3 (#299)
renovate-bot Sep 6, 2022
8b4f125
chore: detect samples tests in nested directories (#304)
gcf-owl-bot[bot] Sep 13, 2022
2c7e389
chore(deps): update dependency google-cloud-tasks to v2.10.3 (#308)
renovate-bot Oct 4, 2022
8484d0d
chore(deps): update dependency google-cloud-tasks to v2.10.4 (#311)
renovate-bot Oct 18, 2022
afe893e
chore(deps): update dependency pytest to v7.2.0 (#312)
renovate-bot Oct 26, 2022
114902e
Merge remote-tracking branch 'migration/main' into python-tasks-migra…
msampathkumar Nov 9, 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
155 changes: 155 additions & 0 deletions cloud-tasks/snippets/create_http_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# Copyright 2019 Google LLC All Rights Reserved.
#
# 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,
)
60 changes: 60 additions & 0 deletions cloud-tasks/snippets/create_http_task_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2019 Google LLC All Rights Reserved.
#
# 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
73 changes: 73 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,73 @@
# Copyright 2019 Google LLC All Rights Reserved.
#
# 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 = '[email protected]';
# 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]
51 changes: 51 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,51 @@
# Copyright 2019 Google LLC All Rights Reserved.
#
# 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
37 changes: 37 additions & 0 deletions cloud-tasks/snippets/create_queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2020 Google LLC
#
# 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.

# [START cloud_tasks_create_queue]
def create_queue(project, queue_name, location):
"""Create a task queue."""

from google.cloud import tasks_v2

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

# Construct the fully qualified location path.
parent = f"projects/{project}/locations/{location}"

# Construct the create queue request.
queue = {"name": client.queue_path(project, location, queue_name)}

# Use the client to create the queue.
response = client.create_queue(request={"parent": parent, "queue": queue})

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


# [END cloud_tasks_create_queue]
Loading