From 13c35ef7d0791b6ffbc3248e3a0c958588fb0af7 Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Tue, 6 Dec 2022 15:53:28 -0800 Subject: [PATCH] chore(functions): remove samples unused in docs (#8790) --- functions/concepts/README.md | 11 -- functions/concepts/main.py | 123 ------------ functions/concepts/main_test.py | 63 ------ functions/concepts/requirements-test.txt | 6 - functions/concepts/requirements.txt | 0 functions/tips/README.md | 13 -- functions/tips/main.py | 238 ----------------------- functions/tips/main_test.py | 96 --------- functions/tips/requirements-test.txt | 5 - functions/tips/requirements.txt | 5 - 10 files changed, 560 deletions(-) delete mode 100644 functions/concepts/README.md delete mode 100644 functions/concepts/main.py delete mode 100644 functions/concepts/main_test.py delete mode 100644 functions/concepts/requirements-test.txt delete mode 100644 functions/concepts/requirements.txt delete mode 100644 functions/tips/README.md delete mode 100644 functions/tips/main.py delete mode 100644 functions/tips/main_test.py delete mode 100644 functions/tips/requirements-test.txt delete mode 100644 functions/tips/requirements.txt diff --git a/functions/concepts/README.md b/functions/concepts/README.md deleted file mode 100644 index a889048b17a1..000000000000 --- a/functions/concepts/README.md +++ /dev/null @@ -1,11 +0,0 @@ -Google Cloud Platform logo - -# Google Cloud Functions - Concepts sample - -See: - -* [Cloud Functions Concepts tutorial][tutorial] -* [Cloud Functions Concepts sample source code][code] - -[tutorial]: https://cloud.google.com/functions/docs/concepts/exec -[code]: main.py diff --git a/functions/concepts/main.py b/functions/concepts/main.py deleted file mode 100644 index f6cf5a080532..000000000000 --- a/functions/concepts/main.py +++ /dev/null @@ -1,123 +0,0 @@ -# Copyright 2018 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 time - - -# [START functions_concepts_stateless] -# Global variable, modified within the function by using the global keyword. -count = 0 - - -def statelessness(request): - """ - HTTP Cloud Function that counts how many times it is executed - within a specific instance. - Args: - request (flask.Request): The request object. - - Returns: - The response text, or any set of values that can be turned into a - Response object using `make_response` - . - """ - global count - count += 1 - - # Note: the total function invocation count across - # all instances may not be equal to this value! - return 'Instance execution count: {}'.format(count) -# [END functions_concepts_stateless] - - -# Placeholder -def heavy_computation(): - return time.time() - - -# Placeholder -def light_computation(): - return time.time() - - -# [START functions_tips_scopes] -# Global (instance-wide) scope -# This computation runs at instance cold-start -instance_var = heavy_computation() - - -def scope_demo(request): - """ - HTTP Cloud Function that declares a variable. - Args: - request (flask.Request): The request object. - - Returns: - The response text, or any set of values that can be turned into a - Response object using `make_response` - . - """ - - # Per-function scope - # This computation runs every time this function is called - function_var = light_computation() - return 'Instance: {}; function: {}'.format(instance_var, function_var) -# [END functions_tips_scopes] - - -# [START functions_concepts_requests] -def make_request(request): - """ - HTTP Cloud Function that makes another HTTP request. - Args: - request (flask.Request): The request object. - - Returns: - The response text, or any set of values that can be turned into a - Response object using `make_response` - . - """ - import requests - - # The URL to send the request to - url = 'http://example.com' - - # Process the request - response = requests.get(url) - response.raise_for_status() - return 'Success!' -# [END functions_concepts_requests] - - -# [START functions_concepts_after_timeout] -def timeout(request): - print('Function running...') - time.sleep(120) - - # May not execute if function's timeout is <2 minutes - print('Function completed!') - return 'Function completed!' -# [END functions_concepts_after_timeout] - - -# [START functions_concepts_filesystem] -def list_files(request): - import os - from os import path - - root = path.dirname(path.abspath(__file__)) - children = os.listdir(root) - files = [c for c in children if path.isfile(path.join(root, c))] - return 'Files: {}'.format(files) -# [END functions_concepts_filesystem] diff --git a/functions/concepts/main_test.py b/functions/concepts/main_test.py deleted file mode 100644 index 71e07b50587d..000000000000 --- a/functions/concepts/main_test.py +++ /dev/null @@ -1,63 +0,0 @@ -# Copyright 2018 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 flask -import pytest -import requests -import responses - -import main - - -# Create a fake "app" for generating test request contexts. -@pytest.fixture(scope="module") -def app(): - return flask.Flask(__name__) - - -def test_statelessness(app): - with app.test_request_context(): - res = main.statelessness(flask.request) - assert res == 'Instance execution count: 1' - res = main.statelessness(flask.request) - assert res == 'Instance execution count: 2' - - -def test_scope_demo(app): - with app.test_request_context(): - main.scope_demo(flask.request) - - -@responses.activate -def test_make_request_200(app): - responses.add(responses.GET, 'http://example.com', - json={'status': 'OK'}, status=200) - with app.test_request_context(): - main.make_request(flask.request) - - -@responses.activate -def test_make_request_404(app): - responses.add(responses.GET, 'http://example.com', - json={'error': 'not found'}, status=404) - with app.test_request_context(): - with pytest.raises(requests.exceptions.HTTPError): - main.make_request(flask.request) - - -def test_list_files(app): - with app.test_request_context(): - res = main.list_files(flask.request) - assert 'main.py' in res diff --git a/functions/concepts/requirements-test.txt b/functions/concepts/requirements-test.txt deleted file mode 100644 index ae03507854ba..000000000000 --- a/functions/concepts/requirements-test.txt +++ /dev/null @@ -1,6 +0,0 @@ -flask==2.1.0 -mock==4.0.3 -pytest==7.0.1 -requests==2.27.1 -responses==0.17.0; python_version < '3.7' -responses==0.20.0; python_version > '3.6' diff --git a/functions/concepts/requirements.txt b/functions/concepts/requirements.txt deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/functions/tips/README.md b/functions/tips/README.md deleted file mode 100644 index 42e64d14c8fb..000000000000 --- a/functions/tips/README.md +++ /dev/null @@ -1,13 +0,0 @@ -Google Cloud Platform logo - -# Google Cloud Functions - Tips sample - -## Running locally -Set the `GCP_PROJECT` variable to the ID of the GCP project to use. - -## More info -See: - -* [Cloud Functions Tips sample source code][code] - -[code]: main.py diff --git a/functions/tips/main.py b/functions/tips/main.py deleted file mode 100644 index 3d67c8bccb31..000000000000 --- a/functions/tips/main.py +++ /dev/null @@ -1,238 +0,0 @@ -# Copyright 2018 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 functions_tips_infinite_retries] -from datetime import datetime, timezone -# [END functions_tips_infinite_retries] - -# [START functions_tips_gcp_apis] -import os -# [END functions_tips_gcp_apis] - -# [START functions_tips_infinite_retries] -# The 'python-dateutil' package must be included in requirements.txt. -from dateutil import parser - -# [END functions_tips_infinite_retries] - -# [START functions_tips_retry] -from google.cloud import error_reporting -# [END functions_tips_retry] - -# [START functions_tips_gcp_apis] -from google.cloud import pubsub_v1 -# [END functions_tips_gcp_apis] - -# [START functions_tips_connection_pooling] -import requests - -# [END functions_tips_connection_pooling] - -from functools import reduce # noqa I100 - - -# Placeholder -def file_wide_computation(): - return 1 - - -# Placeholder -def function_specific_computation(): - return 1 - - -def light_computation(): - numbers = list(range(1, 10)) - return reduce(lambda x, t: t + x, numbers) - - -def heavy_computation(): - # Multiplication is more computationally expensive than addition - numbers = list(range(1, 10)) - return reduce(lambda x, t: t * x, numbers) - - -# [START cloudrun_tips_global_scope] -# [START run_tips_global_scope] -# Global (instance-wide) scope -# This computation runs at instance cold-start -instance_var = heavy_computation() - - -def scope_demo(request): - """ - HTTP Cloud Function that declares a variable. - Args: - request (flask.Request): The request object. - - Returns: - The response text, or any set of values that can be turned into a - Response object using `make_response` - . - """ - function_var = light_computation() - return 'Per instance: {}, per function: {}'.format( - instance_var, function_var) -# [END run_tips_global_scope] -# [END cloudrun_tips_global_scope] - - -# [START functions_tips_lazy_globals] -# [START cloudrun_tips_global_lazy] -# [START run_tips_global_lazy] -# Always initialized (at cold-start) -non_lazy_global = file_wide_computation() - -# Declared at cold-start, but only initialized if/when the function executes -lazy_global = None - - -def lazy_globals(request): - """ - HTTP Cloud Function that uses lazily-initialized globals. - Args: - request (flask.Request): The request object. - - Returns: - The response text, or any set of values that can be turned into a - Response object using `make_response` - . - """ - global lazy_global, non_lazy_global - - # This value is initialized only if (and when) the function is called - if not lazy_global: - lazy_global = function_specific_computation() - - return 'Lazy: {}, non-lazy: {}.'.format(lazy_global, non_lazy_global) -# [END run_tips_global_lazy] -# [END cloudrun_tips_global_lazy] -# [END functions_tips_lazy_globals] - - -# [START functions_tips_connection_pooling] -# Create a global HTTP session (which provides connection pooling) -session = requests.Session() - - -def connection_pooling(request): - """ - HTTP Cloud Function that uses a connection pool to make HTTP requests. - Args: - request (flask.Request): The request object. - - Returns: - The response text, or any set of values that can be turned into a - Response object using `make_response` - . - """ - - # The URL to send the request to - url = 'http://example.com' - - # Process the request - response = session.get(url) - response.raise_for_status() - return 'Success!' -# [END functions_tips_connection_pooling] - - -# [START functions_tips_gcp_apis] - -# Create a global Pub/Sub client to avoid unneeded network activity -pubsub = pubsub_v1.PublisherClient() - - -def gcp_api_call(request): - """ - HTTP Cloud Function that uses a cached client library instance to - reduce the number of connections required per function invocation. - Args: - request (flask.Request): The request object. - Returns: - The response text, or any set of values that can be turned into a - Response object using `make_response` - . - """ - - project = os.getenv('GCP_PROJECT') - request_json = request.get_json() - - topic_name = request_json['topic'] - topic_path = pubsub.topic_path(project, topic_name) - - # Process the request - data = 'Test message'.encode('utf-8') - pubsub.publish(topic_path, data=data) - - return '1 message published' -# [END functions_tips_gcp_apis] - - -# [START functions_tips_infinite_retries] -def avoid_infinite_retries(data, context): - """Background Cloud Function that only executes within a certain - time period after the triggering event. - - Args: - data (dict): The event payload. - context (google.cloud.functions.Context): The event metadata. - Returns: - None; output is written to Stackdriver Logging - """ - - timestamp = context.timestamp - - event_time = parser.parse(timestamp) - event_age = (datetime.now(timezone.utc) - event_time).total_seconds() - event_age_ms = event_age * 1000 - - # Ignore events that are too old - max_age_ms = 10000 - if event_age_ms > max_age_ms: - print('Dropped {} (age {}ms)'.format(context.event_id, event_age_ms)) - return 'Timeout' - - # Do what the function is supposed to do - print('Processed {} (age {}ms)'.format(context.event_id, event_age_ms)) - return # To retry the execution, raise an exception here -# [END functions_tips_infinite_retries] - - -# [START functions_tips_retry] -error_client = error_reporting.Client() - - -def retry_or_not(data, context): - """Background Cloud Function that demonstrates how to toggle retries. - - Args: - data (dict): The event payload. - context (google.cloud.functions.Context): The event metadata. - Returns: - None; output is written to Stackdriver Logging - """ - - # Retry based on a user-defined parameter - try_again = data.data.get('retry') is not None - - try: - raise RuntimeError('I failed you') - except RuntimeError: - error_client.report_exception() - if try_again: - raise # Raise the exception and try again - else: - pass # Swallow the exception and don't retry -# [END functions_tips_retry] diff --git a/functions/tips/main_test.py b/functions/tips/main_test.py deleted file mode 100644 index e4fb31ac7537..000000000000 --- a/functions/tips/main_test.py +++ /dev/null @@ -1,96 +0,0 @@ -# Copyright 2018 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. - -from collections import UserDict -from datetime import datetime, timedelta, timezone -from unittest.mock import MagicMock, Mock, patch - -import flask -import pytest -import requests -import responses - -import main - - -# Create a fake "app" for generating test request contexts. -@pytest.fixture(scope="module") -def app(): - return flask.Flask(__name__) - - -def test_lazy_globals(app): - with app.test_request_context(): - main.lazy_globals(flask.request) - - -def test_scope_demo(app): - with app.test_request_context(): - res = main.scope_demo(flask.request) - assert res == 'Per instance: 362880, per function: 45' - - -@responses.activate -def test_connection_pooling_200(app): - responses.add(responses.GET, 'http://example.com', - json={'status': 'OK'}, status=200) - with app.test_request_context(): - main.connection_pooling(flask.request) - - -@responses.activate -def test_connection_pooling_404(app): - responses.add(responses.GET, 'http://example.com', - json={'error': 'not found'}, status=404) - with app.test_request_context(): - with pytest.raises(requests.exceptions.HTTPError): - main.connection_pooling(flask.request) - - -def test_avoid_infinite_retries(capsys): - now = datetime.now(timezone.utc) - - with patch('main.datetime', wraps=datetime) as datetime_mock: - datetime_mock.now = Mock(return_value=now) - - old_context = UserDict() - old_context.timestamp = (now - timedelta(seconds=15)).isoformat() - old_context.event_id = 'old_event_id' - - young_context = UserDict() - young_context.timestamp = (now - timedelta(seconds=5)).isoformat() - young_context.event_id = 'young_event_id' - - main.avoid_infinite_retries(None, old_context) - out, _ = capsys.readouterr() - assert f"Dropped {old_context.event_id} (age 15000.0ms)" in out - - main.avoid_infinite_retries(None, young_context) - out, _ = capsys.readouterr() - assert f"Processed {young_context.event_id} (age 5000.0ms)" in out - - -def test_retry_or_not(): - with patch('main.error_client') as error_client_mock: - error_client_mock.report_exception = MagicMock() - - event = Mock(data={}) - main.retry_or_not(event, None) - assert error_client_mock.report_exception.call_count == 1 - - event.data = {'retry': True} - with pytest.raises(RuntimeError): - main.retry_or_not(event, None) - - assert error_client_mock.report_exception.call_count == 2 diff --git a/functions/tips/requirements-test.txt b/functions/tips/requirements-test.txt deleted file mode 100644 index 0ac7cb14511d..000000000000 --- a/functions/tips/requirements-test.txt +++ /dev/null @@ -1,5 +0,0 @@ -flask==2.1.0 -pytest==7.0.1 -requests==2.27.1 -responses==0.17.0; python_version < '3.7' -responses==0.20.0; python_version > '3.6' diff --git a/functions/tips/requirements.txt b/functions/tips/requirements.txt deleted file mode 100644 index 536828666273..000000000000 --- a/functions/tips/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -google-cloud-error-reporting==1.6.0 -google-cloud-pubsub==2.9.0 -python-dateutil==2.8.2 -requests==2.27.1 -xmltodict==0.13.0