8000 Merge branch 'main' into cicd_tutorial · riteshverma/python-docs-samples@00d8976 · GitHub
[go: up one dir, main page]

Skip to content
10000

Commit 00d8976

Browse files
authored
Merge branch 'main' into cicd_tutorial
2 parents 7414dc6 + 3341286 commit 00d8976

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1509
-166
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
/spanner/**/*.py @larkee @GoogleCloudPlatform/python-samples-owners
7272
/speech/**/*.py @telpirion @GoogleCloudPlatform/python-samples-owners
7373
/storage/**/*.py @GoogleCloudPlatform/cloud-storage-dpes @GoogleCloudPlatform/python-samples-owners
74+
/storagetransfer/**/*.py @GoogleCloudPlatform/cloud-storage-dpes @GoogleCloudPlatform/python-samples-owners
7475
/tables/automl/**/*.py @telpirion @GoogleCloudPlatform/python-samples-owners
7576
/tasks/**/*.py @averikitsch @GoogleCloudPlatform/python-samples-owners
7677
/texttospeech/**/*.py @telpirion @GoogleCloudPlatform/python-samples-owners

.kokoro/tests/run_tests.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ fi
9696
source ./testing/test-env.sh
9797
export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/testing/service-account.json
9898

99+
# Import secrets for AWS integration testing. This can be used for products
100+
# such as Storage Transfer Service.
101+
if [[ -f "${KOKORO_GFILE_DIR}/aws-secrets.sh" ]]; then
102+
source "${KOKORO_GFILE_DIR}/aws-secrets.sh"
103+
fi
104+
99105
# For cloud-run session, we activate the service account for gcloud sdk.
100106
gcloud auth activate-service-account \
101107
--key-file "${GOOGLE_APPLICATION_CREDENTIALS}"

appengine/standard_python3/cloud_debugger/noxfile_config.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222

2323
TEST_CONFIG_OVERRIDE = {
2424
# You can opt out from the test for specific Python versions.
25-
# google-python-cloud-debugger doesn't support Python 3.9.
26-
# https://github.com/GoogleCloudPlatform/cloud-debug-python/issues/22
27-
"ignored_versions": ["2.7", "3.9"],
25+
"ignored_versions": ["2.7", "3.10"],
2826
# Old samples are opted out of enforcing Python type hints
2927
# All new samples should feature them
3028
"enforce_type_hints": False,

cloud-sql/mysql/client-side-encryption/noxfile_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
TEST_CONFIG_OVERRIDE = {
2424
# You can opt out from the test for specific Python versions.
25-
"ignored_versions": ["2.7", "3.6"],
25+
"ignored_versions": ["2.7", "3.6", "3.10"],
2626
# Old samples are opted out of enforcing Python type hints
2727
# All new samples should feature them
2828
"enforce_type_hints": True,

cloud-sql/postgres/client-side-encryption/noxfile_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
TEST_CONFIG_OVERRIDE = {
2424
# You can opt out from the test for specific Python versions.
25-
"ignored_versions": ["2.7", "3.6"],
25+
"ignored_versions": ["2.7", "3.6", "3.10"],
2626
# Old samples are opted out of enforcing Python type hints
2727
# All new samples should feature them
2828
"enforce_type_hints": True,

cloud-sql/sql-server/client-side-encryption/noxfile_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222

2323
TEST_CONFIG_OVERRIDE = {
2424
# You can opt out from the test for specific Python versions.
25-
"ignored_versions": ["2.7", "3.6"],
25+
"ignored_versions": ["2.7", "3.6", "3.10"],
2626
# Old samples are opted out of enforcing Python type hints
2727
# All new samples should feature them
28-
"enforce_type_hints": True,
28+
"enforce_type_hints": False,
2929
# An envvar key for determining the project id to use. Change it
3030
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
3131
# build specific Cloud project. You can also use your own string
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Flask==2.0.2
22
gunicorn==20.0.4; python_version > '3.0'
33
gunicorn==19.10.0; python_version < '3.0'
4-
google-cloud-vision==2.6.2
4+
google-cloud-vision==2.6.3
55
google-cloud-storage==1.43.0
66
google-cloud-datastore==2.4.0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
google-api-python-client==2.33.0
22
google-cloud-storage==1.43.0
3-
pytest-xdist==2.4.0
3+
pytest-xdist==2.5.0
44
pytest==6.2.4
55
pyyaml==6.0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
google-cloud-vision==2.6.2
1+
google-cloud-vision==2.6.3
22
google-cloud-storage==1.43.0
33
Wand==0.6.7

functions/ocr/app/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
google-cloud-pubsub==2.9.0
22
google-cloud-storage==1.43.0
33
google-cloud-translate==3.6.1
4-
google-cloud-vision==2.6.2
4+
google-cloud-vision==2.6.3
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Copyright 2021 Google LLC
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+
16+
# [START functions_label_gce_instance]
17+
import re
18+
19+
from google.api_core.exceptions import GoogleAPIError
20+
from google.cloud import compute_v1
21+
from google.cloud.compute_v1.types import compute
22+
23+
instances_client = compute_v1.InstancesClient()
24+
25+
26+
# CloudEvent function that labels newly-created GCE instances
27+
# with the entity (user or service account) that created them.
28+
#
29+
# @param {object} cloudevent A CloudEvent containing the Cloud Audit Log entry.
30+
# @param {object} cloudevent.data.protoPayload The Cloud Audit Log entry.
31+
def label_gce_instance(cloudevent):
32+
# Extract parameters from the CloudEvent + Cloud Audit Log data
33+
payload = cloudevent.data.get('protoPayload', dict())
34+
auth_info = payload.get('authenticationInfo', dict())
35+
creator = auth_info.get('principalEmail')
36+
37+
# Get relevant VM instance details from the cloudevent's `subject` property
38+
# Example value:
39+
# compute.googleapis.com/projects/<PROJECT_ID>/zones/<ZONE_ID>/instances/<INSTANCE_NAME>
40+
instance_params = cloudevent['subject'].split('/')
41+
42+
# Validate data
43+
if not creator or not instance_params or len(instance_params) != 7:
44+
# This is not something retries will fix, so don't throw an Exception
45+
# (Thrown exceptions trigger retries *if* you enable retries in GCF.)
46+
print('ERROR: Invalid `principalEmail` and/or CloudEvent `subject`.')
47+
return
48+
49+
instance_project = instance_params[2]
50+
instance_zone = instance_params[4]
51+
instance_name = instance_params[6]
52+
53+
# Format the 'creator' parameter to match GCE label validation requirements
54+
creator = re.sub('\\W', '_', creator.lower())
55+
56+
# Get the newly-created VM instance's label fingerprint
57+
# This is required by the Compute Engine API to prevent duplicate labels
58+
instance = instances_client.get(
59+
project=instance_project,
60+
zone=instance_zone,
61+
instance=instance_name
62+
)
63+
64+
# Construct API call to label the VM instance with its creator
65+
request_init = {
66+
'project': instance_project,
67+
'zone': instance_zone,
68+
'instance': instance_name
69+
}
70+
request_init['instances_set_labels_request_resource'] = \
71+
compute.InstancesSetLabelsRequest(
72+
label_fingerprint=instance.label_fingerprint,
73+
labels={'creator': creator}
74+
)
75+
request = compute.SetLabelsInstanceRequest(request_init)
76+
77+
# Perform instance-labeling API call
78+
try:
79+
instances_client.set_labels(request)
80+
print(f'Labelled VM instance {instance_name} with creator: {creator}')
81+
except GoogleAPIError as e:
82+
# Swallowing the exception means failed invocations WON'T be retried
83+
print('Label operation failed', e)
84+
85+
# Uncomment the line below to retry failed invocations.
86+
# (You'll also have to enable retries in Cloud Functions itself.)
87+
# raise e
88+
89+
return
90+
# [END functions_label_gce_instance]
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Copyright 2021 Google LLC
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+
import re
17+
import uuid
18+
19+
import backoff
20+
21+
from cloudevents.http import CloudEvent
22+
23+
from google.cloud import compute_v1
24+
25+
import main
26+
27+
28+
INSTANCE_PROJECT = os.getenv('GCLOUD_PROJECT')
29+
INSTANCE_ZONE = os.getenv('FUNCTIONS_COMPUTE_ZONE')
30+
INSTANCE_NAME = os.getenv('FUNCTIONS_COMPUTE_INSTANCE')
31+
32+
instances_client = compute_v1.InstancesClient()
33+
34+
# Avoid race conditions from parallel multi-Python-version CI builds
35+
PYTHON_VERSION = os.getenv('RUN_TESTS_SESSION', '').replace('.', '')
36+
if PYTHON_VERSION:
37+
INSTANCE_NAME += f'-{PYTHON_VERSION}'
38+
39+
40+
def test_functions_label_gce_instance_should_set_label():
41+
example_subject = 'compute.googleapis.com/'
42+
43+
example_subject += f'projects/{INSTANCE_PROJECT}/'
44+
example_subject += f'zones/{INSTANCE_ZONE}/'
45+
example_subject += f'instances/{INSTANCE_NAME}'
46+
47+
example_email = f'{uuid.uuid4().hex}@example.com'
48+
49+
attributes = {
50+
'type': '',
51+
'source': '',
52+
'subject': example_subject
53+
}
54+
55+
data = {
56+
'protoPayload': {
57+
'authenticationInfo': {
58+
'principalEmail': example_email
59+
},
60+
}
61+
}
62+
63+
event = CloudEvent(attributes, data)
64+
65+
# Call function
66+
main.label_gce_instance(event)
67+
68+
# Run assertions, with exponential backoff
69+
@backoff.on_exception(backoff.expo, AssertionError, max_time=120)
70+
def run_assertions():
71+
# Get labeled instance
72+
expected_label = re.sub('\\W', '_', example_email)
73+
labels = instances_client.get(
74+
project=INSTANCE_PROJECT,
75+
zone=INSTANCE_ZONE,
76+
instance=INSTANCE_NAME
77+
).labels
78+
79+
# Check that instance was labelled
80+
assert 'creator' in labels
81+
assert labels.get('creator') == expected_label
82+
83+
run_assertions()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pytest==6.2.4
2+
backoff==1.10.0
3+
cloudevents==1.2.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-compute==0.8.0

iam/api-client/requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
google-api-python-client==2.33.0
22
google-auth==2.3.3
33
google-auth-httplib2==0.1.0
4-
boto3==1.20.22
5-
botocore==1.23.22
4+
boto3==1.20.24
5+
botocore==1.23.24
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
google-cloud-storage==1.43.0
2-
pytest-xdist==2.4.0
2+
pytest-xdist==2.5.0
33
pytest==6.2.4
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
google-api-python-client==2.33.0
2-
pytest-xdist==2.4.0
2+
pytest-xdist==2.5.0
33
pytest==6.2.4

people-and-planet-ai/timeseries-classification/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ Flask==2.0.2
22
apache-beam[gcp]==2.33.0
33
google-cloud-aiplatform==1.8.0
44
gunicorn==20.1.0
5-
pandas==1.3.4
5+
pandas==1.3.5
66
tensorflow==2.7.0

run/image-processing/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ Flask==2.0.2
22
pytest==5.3.0; python_version > "3.0"
33
pytest==4.6.6; python_version < "3.0"
44
gunicorn==20.1.0
5-
google-cloud-vision==2.6.2
5+
google-cloud-vision==2.6.3
66
google-cloud-storage==1.43.0
77
Wand==0.6.7

storage/s3-sdk/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
boto3==1.20.22
1+
boto3==1.20.24

storage/transfer_service/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
These samples have been moved.
2+
3+
https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/storagetransfer

storage/transfer_service/README.rst.in

Lines changed: 0 additions & 30 deletions
This file was deleted.

storage/transfer_service/requirements-test.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)
0