Skip to content

Commit 5397fa4

Browse files
Add retry sample to STS (GoogleCloudPlatform#5760)
The example [retry code ](https://cloud.google.com/storage-transfer/docs/create-client#python_1)on the STS docs page is both incorrect and in-line, this adds a proper sample that simply executes a request using retries to replace it
1 parent 01fe692 commit 5397fa4

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env
2+
3+
# Copyright 2021 Google LLC
4+
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# [START storagetransfer_create_retry_handler]
18+
19+
"""Command-line sample that gets a transfer job using retries
20+
"""
21+
22+
import googleapiclient.discovery
23+
24+
25+
def get_transfer_job_with_retries(project_id, job_name, retries):
26+
"""Check the latest transfer operation associated with a transfer job."""
27+
storagetransfer = googleapiclient.discovery.build("storagetransfer", "v1")
28+
29+
transferJob = (
30+
storagetransfer.transferJobs()
31+
.get(projectId=project_id, jobName=job_name)
32+
.execute(num_retries=retries)
33+
)
34+
print(
35+
"Fetched transfer job: "
36+
+ transferJob.get("name")
37+
+ " using {} retries".format(retries)
38+
)
39+
# [END storagetransfer_create_retry_handler]

storage/transfer_service/sts_snippets_test.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import googleapiclient.discovery
1818

1919
import check_latest_transfer_operation
20+
import get_transfer_job_with_retries
2021

2122

2223
def test_latest_transfer_operation(capsys):
@@ -50,3 +51,39 @@ def test_latest_transfer_operation(capsys):
5051
# The latest operation field can take a while to populate, so to avoid a
5152
# flaky test we just check that the job exists and the field was checked
5253
assert job_name in out
54+
55+
56+
def test_get_transfer_Job_with_retries(capsys):
57+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
58+
59+
transfer_job = {
60+
"description": "Sample job",
61+
"status": "ENABLED",
62+
"projectId": project_id,
63+
"schedule": {
64+
"scheduleStartDate": {"day": "01", "month": "01", "year": "2000"},
65+
"startTimeOfDay": {"hours": "00", "minutes": "00", "seconds": "00"},
66+
},
67+
"transferSpec": {
68+
"gcsDataSource": {"bucketName": project_id + "-storagetransfer-source"},
69+
"gcsDataSink": {"bucketName": project_id + "-storagetransfer-sink"},
70+
"objectConditions": {
71+
"minTimeElapsedSinceLastModification": "2592000s" # 30 days
72+
},
73+
"transferOptions": {"deleteObjectsFromSourceAfterTransfer": "true"},
74+
},
75+
}
76+
storagetransfer = googleapiclient.discovery.build("storagetransfer", "v1")
77+
result = storagetransfer.transferJobs().create(body=transfer_job).execute()
78+
79+
job_name = result.get("name")
80+
81+
retries = 3
82+
83+
get_transfer_job_with_retries.get_transfer_job_with_retries(
84+
project_id, job_name, retries
85+
)
86+
out, _ = capsys.readouterr()
87+
# This sample isn't really meant to do anything, just check that it ran without any issues
88+
# when we populated num_retries
89+
assert f"using {str(retries)} retries" in out

0 commit comments

Comments
 (0)