diff --git a/storage/transfer_service/get_transfer_job_with_retries.py b/storage/transfer_service/get_transfer_job_with_retries.py new file mode 100644 index 00000000000..1d162d0942c --- /dev/null +++ b/storage/transfer_service/get_transfer_job_with_retries.py @@ -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) + ) +# [END storagetransfer_create_retry_handler] diff --git a/storage/transfer_service/sts_snippets_test.py b/storage/transfer_service/sts_snippets_test.py index 4338e8ded26..e7ff2e90400 100644 --- a/storage/transfer_service/sts_snippets_test.py +++ b/storage/transfer_service/sts_snippets_test.py @@ -17,6 +17,7 @@ import googleapiclient.discovery import check_latest_transfer_operation +import get_transfer_job_with_retries def test_latest_transfer_operation(capsys): @@ -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