Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
39 changes: 39 additions & 0 deletions storage/transfer_service/get_transfer_job_with_retries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env

# Copyright 2021 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 storagetransfer_create_retry_handler]

"""Command-line sample that gets a transfer job using retries
"""

import googleapiclient.discovery


def get_transfer_job_with_retries(project_id, job_name, retries):
"""Check the latest transfer operation associated with a transfer job."""
storagetransfer = googleapiclient.discovery.build("storagetransfer", "v1")

transferJob = (
storagetransfer.transferJobs()
.get(projectId=project_id, jobName=job_name)
.execute(num_retries=retries)
)
print(
"Fetched transfer job: "
+ transferJob.get("name")
+ " using {} retries".format(retries)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - can you switch this to f-strings?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe f strings aren't compatible with python 2.7 which we do have kokoro tests for, am I mistaken?

)
# [END storagetransfer_create_retry_handler]
37 changes: 37 additions & 0 deletions storage/transfer_service/sts_snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import googleapiclient.discovery

import check_latest_transfer_operation
import get_transfer_job_with_retries


def test_latest_transfer_operation(capsys):
Expand Down Expand Up @@ -50,3 +51,39 @@ def test_latest_transfer_operation(capsys):
# The latest operation field can take a while to populate, so to avoid a
# flaky test we just check that the job exists and the field was checked
assert job_name in out


def test_get_transfer_Job_with_retries(capsys):
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]

transfer_job = {
"description": "Sample job",
"status": "ENABLED",
"projectId": project_id,
"schedule": {
"scheduleStartDate": {"day": "01", "month": "01", "year": "2000"},
"startTimeOfDay": {"hours": "00", "minutes": "00", "seconds": "00"},
},
"transferSpec": {
"gcsDataSource": {"bucketName": project_id + "-storagetransfer-source"},
"gcsDataSink": {"bucketName": project_id + "-storagetransfer-sink"},
"objectConditions": {
"minTimeElapsedSinceLastModification": "2592000s" # 30 days
},
"transferOptions": {"deleteObjectsFromSourceAfterTransfer": "true"},
},
}
storagetransfer = googleapiclient.discovery.build("storagetransfer", "v1")
result = storagetransfer.transferJobs().create(body=transfer_job).execute()

job_name = result.get("name")

retries = 3

get_transfer_job_with_retries.get_transfer_job_with_retries(
project_id, job_name, retries
)
out, _ = capsys.readouterr()
# This sample isn't really meant to do anything, just check that it ran without any issues
# when we populated num_retries
assert f"using {str(retries)} retries" in out