|
| 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_get_latest_transfer_operation] |
| 18 | + |
| 19 | +"""Command-line sample that checks the latest operation of a transfer. |
| 20 | +This sample is used on this page: |
| 21 | + https://cloud.google.com/storage/transfer/create-transfer |
| 22 | +For more information, see README.md. |
| 23 | +""" |
| 24 | + |
| 25 | +import argparse |
| 26 | +import json |
| 27 | + |
| 28 | +import googleapiclient.discovery |
| 29 | + |
| 30 | + |
| 31 | +def check_latest_transfer_operation(project_id, job_name): |
| 32 | + """Check the latest transfer operation associated with a transfer job.""" |
| 33 | + storagetransfer = googleapiclient.discovery.build("storagetransfer", "v1") |
| 34 | + |
| 35 | + transferJob = ( |
| 36 | + storagetransfer.transferJobs() |
| 37 | + .get(projectId=project_id, jobName=job_name) |
| 38 | + .execute() |
| 39 | + ) |
| 40 | + latestOperationName = transferJob.get("latestOperationName") |
| 41 | + |
| 42 | + if latestOperationName: |
| 43 | + result = ( |
| 44 | + storagetransfer.transferOperations().get(name=latestOperationName).execute() |
| 45 | + ) |
| 46 | + print( |
| 47 | + "The latest operation for job" |
| 48 | + + job_name |
| 49 | + + " is: {}".format(json.dumps(result, indent=4, sort_keys=True)) |
| 50 | + ) |
| 51 | + |
| 52 | + else: |
| 53 | + print( |
| 54 | + "Transfer job " |
| 55 | + + job_name |
| 56 | + + " does not have an operation scheduled yet, " |
| 57 | + + "try again once the job starts running." |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +if __name__ == "__main__": |
| 62 | + parser = argparse.ArgumentParser( |
| 63 | + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter |
| 64 | + ) |
| 65 | + parser.add_argument("project_id", help="Your Google Cloud project ID.") |
| 66 | + parser.add_argument("job_name", help="Your job name.") |
| 67 | + |
| 68 | + args = parser.parse_args() |
| 69 | + |
| 70 | + check_latest_transfer_operation(args.project_id, args.job_name) |
| 71 | +# [END storagetransfer_get_latest_transfer_operation] |
0 commit comments