Skip to content

Commit 524956c

Browse files
(storage) add sample for check latest transfer operation in transfer … (GoogleCloudPlatform#5278)
* (storage) add sample for check latest transfer operation in transfer service * Fix copyright headers * 2021 copyright headers * remove noxfile Co-authored-by: Frank Natividad <[email protected]>
1 parent ca63361 commit 524956c

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
17+
import googleapiclient.discovery
18+
19+
import check_latest_transfer_operation
20+
21+
22+
def test_latest_transfer_operation(capsys):
23+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
24+
25+
transfer_job = {
26+
"description": "Sample job",
27+
"status": "ENABLED",
28+
"projectId": project_id,
29+
"schedule": {
30+
"scheduleStartDate": {"day": "01", "month": "01", "year": "2000"},
31+
"startTimeOfDay": {"hours": "00", "minutes": "00", "seconds": "00"},
32+
},
33+
"transferSpec": {
34+
"gcsDataSource": {"bucketName": project_id + "-storagetransfer-source"},
35+
"gcsDataSink": {"bucketName": project_id + "-storagetransfer-sink"},
36+
"objectConditions": {
37+
"minTimeElapsedSinceLastModification": "2592000s" # 30 days
38+
},
39+
"transferOptions": {"deleteObjectsFromSourceAfterTransfer": "true"},
40+
},
41+
}
42+
storagetransfer = googleapiclient.discovery.build("storagetransfer", "v1")
43+
result = storagetransfer.transferJobs().create(body=transfer_job).execute()
44+
45+
job_name = result.get("name")
46+
check_latest_transfer_operation.check_latest_transfer_operation(
47+
project_id, job_name
48+
)
49+
out, _ = capsys.readouterr()
50+
# The latest operation field can take a while to populate, so to avoid a
51+
# flaky test we just check that the job exists and the field was checked
52+
assert job_name in out

0 commit comments

Comments
 (0)