From da38d9239fe3c0b8af253045330c3f26d284941e Mon Sep 17 00:00:00 2001 From: arithmetic1728 Date: Wed, 2 Sep 2020 23:21:20 -0700 Subject: [PATCH] Remove tasks samples --- tasks/README.md | 88 +------------- tasks/create_http_task.py | 138 ---------------------- tasks/create_http_task_test.py | 48 -------- tasks/create_http_task_with_token.py | 86 -------------- tasks/create_http_task_with_token_test.py | 53 --------- tasks/create_queue.py | 35 ------ tasks/create_queue_test.py | 40 ------- tasks/delete_queue.py | 30 ----- tasks/delete_queue_test.py | 56 --------- tasks/list_queues.py | 36 ------ tasks/list_queues_test.py | 55 --------- tasks/requirements-test.txt | 1 - tasks/requirements.txt | 1 - 13 files changed, 2 insertions(+), 665 deletions(-) delete mode 100644 tasks/create_http_task.py delete mode 100644 tasks/create_http_task_test.py delete mode 100644 tasks/create_http_task_with_token.py delete mode 100644 tasks/create_http_task_with_token_test.py delete mode 100644 tasks/create_queue.py delete mode 100644 tasks/create_queue_test.py delete mode 100644 tasks/delete_queue.py delete mode 100644 tasks/delete_queue_test.py delete mode 100644 tasks/list_queues.py delete mode 100644 tasks/list_queues_test.py delete mode 100644 tasks/requirements-test.txt delete mode 100644 tasks/requirements.txt diff --git a/tasks/README.md b/tasks/README.md index b83eae655a8..f472ecff2c8 100644 --- a/tasks/README.md +++ b/tasks/README.md @@ -1,87 +1,3 @@ -# Google Cloud Tasks Samples +These samples have been moved. -[![Open in Cloud Shell][shell_img]][shell_link] - -[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png -[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=tasks/README.md - -This sample demonstrates how to use the -[Cloud Tasks](https://cloud.google.com/tasks/docs/) client library. - -`create_http_task.py` is a simple command-line program to create -tasks to be pushed to an URL endpoint. - -`create_http_task_with_token.py` is a simple command-line program to create -tasks to be pushed to an URL endpoint with authorization header. - -## Prerequisites to run locally: - -Please refer to [Setting Up a Python Development Environment](https://cloud.google.com/python/setup). - -## Authentication - -To set up authentication, please refer to our -[authentication getting started guide](https://cloud.google.com/docs/authentication/getting-started). - -## Install Dependencies - -To install the dependencies for this sample, use the following command: - -``` -pip install -r requirements.txt -``` - -This sample uses the common protos in the [googleapis](https://github.com/googleapis/googleapis) -repository. For more info, see -[Protocol Buffer Basics](https://developers.google.com/protocol-buffers/docs/pythontutorial). - -## Creating a queue - -To create a queue (named `my-queue`) using the Cloud SDK, use the following -gcloud command: - -``` -gcloud tasks queues create my-queue -``` - -## Run the Sample Using the Command Line - -Set environment variables: - -First, your project ID: - -``` -export PROJECT_ID=my-project-id -``` - -Then the queue ID, as specified at queue creation time. Queue IDs already -created can be listed with `gcloud tasks queues list`. - -``` -export QUEUE_ID=my-queue -``` - -And finally the location ID, which can be discovered with -`gcloud tasks queues describe my-queue`, with the location embedded in -the "name" value (for instance, if the name is -"projects/my-project/locations/us-central1/queues/my-queue", then the -location is "us-central1"). - -``` -export LOCATION_ID=us-central1 -``` - -### Creating Tasks with HTTP Targets - -Set an environment variable for the endpoint to your task handler. This is an -example url: -``` -export URL=https://example.com/task_handler -``` - -Running the sample will create a task and send the task to the specific URL -endpoint, with a payload specified: - -``` -python create_http_task.py --project=$PROJECT_ID --queue=$QUEUE_ID --location=$LOCATION_ID --url=$URL --payload=hello -``` +https://github.com/googleapis/python-tasks/tree/master/samples diff --git a/tasks/create_http_task.py b/tasks/create_http_task.py deleted file mode 100644 index 23896209a10..00000000000 --- a/tasks/create_http_task.py +++ /dev/null @@ -1,138 +0,0 @@ -# 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): - # [START cloud_tasks_create_http_task] - """Create a task for a given queue with an arbitrary payload.""" - - from google.cloud import tasks_v2 - from google.protobuf import timestamp_pb2 - import datetime - import json - - # 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 - - # 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': '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'] = task_name - - # Use the client to build and send the task. - response = client.create_task(parent, 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) diff --git a/tasks/create_http_task_test.py b/tasks/create_http_task_test.py deleted file mode 100644 index b0fb3ed758b..00000000000 --- a/tasks/create_http_task_test.py +++ /dev/null @@ -1,48 +0,0 @@ -# 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 = client.location_path(TEST_PROJECT_ID, 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(parent, queue) - - yield q - - client.delete_queue(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 diff --git a/tasks/create_http_task_with_token.py b/tasks/create_http_task_with_token.py deleted file mode 100644 index 7320ede3a99..00000000000 --- a/tasks/create_http_task_with_token.py +++ /dev/null @@ -1,86 +0,0 @@ -# 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 datetime - - -def create_http_task(project, - queue, - location, - url, - service_account_email, - payload=None, - in_seconds=None, - task_name=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 - from google.protobuf import 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' - # 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': 'POST', - 'url': url, # The full url path that the task will be sent to. - 'oidc_token': { - 'service_account_email': service_account_email - } - } - } - - 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 - - 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'] = task_name - - # Use the client to build and send the task. - response = client.create_task(parent, task) - - print('Created task {}'.format(response.name)) - return response -# [END cloud_tasks_create_http_task_with_token] diff --git a/tasks/create_http_task_with_token_test.py b/tasks/create_http_task_with_token_test.py deleted file mode 100644 index dd90d9199b1..00000000000 --- a/tasks/create_http_task_with_token_test.py +++ /dev/null @@ -1,53 +0,0 @@ -# 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 = client.location_path(TEST_PROJECT_ID, 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(parent, queue) - - yield q - - client.delete_queue(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 diff --git a/tasks/create_queue.py b/tasks/create_queue.py deleted file mode 100644 index d8d4dfaea26..00000000000 --- a/tasks/create_queue.py +++ /dev/null @@ -1,35 +0,0 @@ -# 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 = client.location_path(project, 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(parent, queue) - - print('Created queue {}'.format(response.name)) - return response -# [END cloud_tasks_create_queue] diff --git a/tasks/create_queue_test.py b/tasks/create_queue_test.py deleted file mode 100644 index 1f623a2aa6c..00000000000 --- a/tasks/create_queue_test.py +++ /dev/null @@ -1,40 +0,0 @@ -# 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. - -import os -import uuid - -from google.cloud import tasks_v2 -import pytest - -import create_queue - -TEST_PROJECT_ID = os.environ['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() - q = create_queue.create_queue(TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION) - - yield q - - client.delete_queue(q.name) - - -def test_create_queue(capsys, test_queue): - out, _ = capsys.readouterr() - assert 'Created queue' in out diff --git a/tasks/delete_queue.py b/tasks/delete_queue.py deleted file mode 100644 index d69514249a1..00000000000 --- a/tasks/delete_queue.py +++ /dev/null @@ -1,30 +0,0 @@ -# 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_delete_queue] -def delete_queue(project, queue_name, location): - """Delete a task queue.""" - - from google.cloud import tasks_v2 - - # Create a client. - client = tasks_v2.CloudTasksClient() - - # Get the fully qualified path to queue. - queue = client.queue_path(project, location, queue_name) - - # Use the client to delete the queue. - client.delete_queue(queue) - print('Deleted queue') -# [END cloud_tasks_delete_queue] diff --git a/tasks/delete_queue_test.py b/tasks/delete_queue_test.py deleted file mode 100644 index 4e1acf403da..00000000000 --- a/tasks/delete_queue_test.py +++ /dev/null @@ -1,56 +0,0 @@ -# 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. - -import os -import uuid - -from google.api_core import exceptions -from google.cloud import tasks_v2 -import pytest - -import delete_queue - - -TEST_PROJECT_ID = os.environ['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 = client.location_path(TEST_PROJECT_ID, 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(parent, queue) - - yield q - - try: - # Attempt to delete the queue in case the sample failed. - client.delete_queue(q.name) - except exceptions.NotFound: - # The queue was already successfully deleted. - print('Queue already deleted successfully') - - -def test_delete_queue(capsys, test_queue): - delete_queue.delete_queue( - TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION - ) - out, _ = capsys.readouterr() - assert 'Deleted queue' in out diff --git a/tasks/list_queues.py b/tasks/list_queues.py deleted file mode 100644 index fa48907fad1..00000000000 --- a/tasks/list_queues.py +++ /dev/null @@ -1,36 +0,0 @@ -# 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_list_queues] -def list_queues(project, location): - """List all task queues.""" - - from google.cloud import tasks_v2 - - # Create a client. - client = tasks_v2.CloudTasksClient() - - # Construct the fully qualified location path. - parent = client.location_path(project, location) - - # Use the client to obtain the queues. - response = client.list_queues(parent) - - # Print the results. - for queue in response: - print(queue.name) - - if response.num_results == 0: - print('No queues found!') -# [END cloud_tasks_list_queues] diff --git a/tasks/list_queues_test.py b/tasks/list_queues_test.py deleted file mode 100644 index df11d0bd4f9..00000000000 --- a/tasks/list_queues_test.py +++ /dev/null @@ -1,55 +0,0 @@ -# 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. - -import os -import uuid - -from google.cloud import tasks_v2 -import pytest - -import list_queues - -TEST_PROJECT_ID = os.environ['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 = client.location_path(TEST_PROJECT_ID, 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(parent, queue) - - yield q - - client.delete_queue(q.name) - - -def test_list_queues_not_present(capsys): - list_queues.list_queues(TEST_PROJECT_ID, TEST_LOCATION) - out, _ = capsys.readouterr() - - assert(TEST_QUEUE_NAME not in out) - - -def test_list_queues_present(capsys, test_queue): - list_queues.list_queues(TEST_PROJECT_ID, TEST_LOCATION) - out, _ = capsys.readouterr() - - assert(TEST_QUEUE_NAME in out) diff --git a/tasks/requirements-test.txt b/tasks/requirements-test.txt deleted file mode 100644 index 7e460c8c866..00000000000 --- a/tasks/requirements-test.txt +++ /dev/null @@ -1 +0,0 @@ -pytest==6.0.1 diff --git a/tasks/requirements.txt b/tasks/requirements.txt deleted file mode 100644 index b7bc58a3b74..00000000000 --- a/tasks/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -google-cloud-tasks==1.5.0