Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples: add cloud client samples from python-docs-samples #626

Merged
merged 201 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
201 commits
Select commit Hold shift + click to select a range
d0e44f4
Renaming storage gcloud samples folder. (#418)
Jul 13, 2016
9e4c4e8
Add gcloud-based storage usage samples. (#419)
Jul 14, 2016
625e4da
Refactor cloud client storage samples. (#421)
Jul 15, 2016
677d2cf
Add more storage samples for the cloud client libraries. (#432)
Jul 29, 2016
667c262
Auto-update dependencies. (#456)
dpebot Aug 16, 2016
a9d1ebc
Fix import order lint errors
Aug 19, 2016
1460861
Add storage acl samples
Aug 31, 2016
430c4bb
Address review comments
Aug 31, 2016
79aa1f5
Fix lint issue
Sep 1, 2016
93b7b07
Auto-update dependencies. (#540)
dpebot Sep 23, 2016
2a0b60a
Auto-update dependencies. (#542)
dpebot Sep 26, 2016
deff097
Move to google-cloud (#544)
Sep 30, 2016
197a0d5
Add new "quickstart" samples (#547)
jmdobry Oct 5, 2016
a6edaff
Quickstart tests (#569)
Oct 12, 2016
73df899
Add basic readme generator (#580)
Oct 20, 2016
2e3f667
Generate readmes for most service samples (#599)
Oct 24, 2016
8141e0a
Update samples to support latest Google Cloud Python (#656)
Nov 15, 2016
3510b2d
Edited upload/download to perform encryption properly (#667)
ryanmats Nov 17, 2016
3f9071f
Storage Encryption Key Rotation Sample using Veneer + Tests (#672)
ryanmats Nov 22, 2016
0f5037b
Auto-update dependencies. (#715)
dpebot Dec 13, 2016
003648d
Adds storage Pub/Sub notification polling tutorial (#875)
BrandonY Mar 27, 2017
b17b759
Remove cloud config fixture (#887)
Apr 4, 2017
21f7db6
Auto-update dependencies. (#914)
dpebot Apr 24, 2017
fba8c2d
Re-generate all readmes
Apr 27, 2017
afd5ed6
Add bucket-level IAM samples (#919)
Apr 28, 2017
be5c929
Auto-update dependencies. (#927)
dpebot May 2, 2017
e7b7ccd
Fix README rst links (#962)
waprin May 24, 2017
299847e
Auto-update dependencies. (#1004)
dpebot Jun 27, 2017
8e71bd5
Add bucket label samples (#1045)
Aug 1, 2017
2dc3106
Auto-update dependencies. (#1055)
dpebot Aug 7, 2017
453206e
Auto-update dependencies. (#1057)
dpebot Aug 8, 2017
dc7f8c7
Auto-update dependencies. (#1073)
dpebot Aug 16, 2017
201867d
Auto-update dependencies. (#1093)
dpebot Aug 29, 2017
5f81798
Auto-update dependencies. (#1097)
dpebot Sep 12, 2017
782de87
Update all generated readme auth instructions (#1121)
Sep 18, 2017
4b7bc6f
Fix TypeError when running Storage notification polling exmaple. (#1135)
BrandonY Sep 22, 2017
077282f
Add support for overwrite attributes (#1142)
BrandonY Sep 27, 2017
e4c0cda
Handle case where attribute not set (#1143)
BrandonY Sep 27, 2017
bb637d6
Added Link to Python Setup Guide (#1158)
Oct 12, 2017
b40d2fc
Auto-update dependencies. (#1138)
dpebot Oct 23, 2017
9f1c1fa
storage requester pays samples (#1122)
ryanmats Oct 30, 2017
42fa87b
Auto-update dependencies. (#1186)
dpebot Nov 1, 2017
cb0f1cd
Auto-update dependencies. (#1234)
dpebot Nov 28, 2017
992b4f6
Auto-update dependencies. (#1239)
dpebot Nov 30, 2017
ebe578b
Added "Open in Cloud Shell" buttons to README files (#1254)
Dec 7, 2017
55cf1e2
Auto-update dependencies. (#1263)
dpebot Dec 11, 2017
132c57a
Auto-update dependencies. (#1272)
dpebot Dec 12, 2017
2dc734b
Auto-update dependencies. (#1282)
dpebot Jan 2, 2018
a9d3af4
Auto-update dependencies. (#1320)
dpebot Feb 2, 2018
e2b368c
Auto-update dependencies. (#1359)
dpebot Feb 26, 2018
d20daa6
Auto-update dependencies. (#1377)
dpebot Mar 5, 2018
221ef4c
Auto-update dependencies. (#1389)
dpebot Mar 8, 2018
0e217fb
Auto-update dependencies.
dpebot Apr 2, 2018
c875b4c
Regenerate the README files and fix the Open in Cloud Shell link for …
Apr 7, 2018
32a3dd4
Update READMEs to fix numbering and add git clone (#1464)
frankyn Apr 26, 2018
ac3b25f
Fix typo. (#1509)
SurferJeffAtGoogle May 30, 2018
1884a24
Storage: add KMS samples (#1510)
broady Jun 11, 2018
3e89f19
[Storage] Update kms samples (#1517)
frankyn Jun 11, 2018
7f1943b
Resolved conflicts.
michaelawyu Jun 29, 2018
123fbc9
Remove leftover merge conflict. (#1657)
kurtisvg Aug 27, 2018
6db621b
Add region tag to upload_blob snippet (#1671)
alixhami Aug 29, 2018
9dfc16e
Bucket lock samples (#1588)
billyjacobson Oct 5, 2018
d22155b
[Storage] Add spacing in sample code. (#1735)
frankyn Oct 5, 2018
f8218b9
Auto-update dependencies. (#1846)
dpebot Nov 20, 2018
9abddd9
Update requirements.txt (#1944)
engelke Dec 19, 2018
f1c494d
Auto-update dependencies. (#1980)
dpebot Feb 6, 2019
00f34a9
storage: bucket policy only samples (#1976)
frankyn Feb 6, 2019
9ea1806
Add sleep to avoid bucket rate limit (#2136)
engelke May 1, 2019
8f15a51
feat(storage): Add snippets for v4 signed URLs (#2142)
jkwlui May 10, 2019
36df2ac
storage: add list buckets (#2149)
frankyn Jul 23, 2019
b752829
Drop xfail for passing test_remove_bucket_label (#2173)
jwhitlock Jul 24, 2019
70ea399
Update string reported in snippet and update test
gguuss Jul 24, 2019
b760eb4
Update list blobs to use new client.list_blobs() method. (#2296)
frankyn Jul 24, 2019
1106886
[Storage] Add comment to clarify which package version is necessary (…
frankyn Aug 6, 2019
bbf79fa
Storage: HMAC key samples (#2372)
tritone Sep 3, 2019
dea500b
Remove required argument from list buckets sample (#2394)
JesseLovelace Sep 13, 2019
81690d3
Fixup sample for list_hmac_keys (#2404)
tritone Sep 17, 2019
d25fbb6
Bucket metadata sample (#2414)
JesseLovelace Sep 24, 2019
e195db7
Adds updates for samples profiler ... vision (#2439)
gguuss Oct 7, 2019
fd92a5d
fix: add bucket-name as required arg to v4 snippets (#2502)
jkwlui Oct 28, 2019
f955a69
[Storage] Support rename of BPO to UniformBucketLevelAccess (#2335)
frankyn Nov 6, 2019
eb1b646
Fix region tag (#2515)
frankyn Nov 6, 2019
56583f9
Update documentation for prefix, delimiter search (#2537)
frankyn Nov 15, 2019
ed3cff2
[Storage] Split samples (#2602)
frankyn Dec 13, 2019
f28ca5b
Auto-update dependencies. (#2005)
dpebot Dec 21, 2019
a128459
samples(storage): IAM conditions samples (#2730)
jkwlui Jan 21, 2020
6f1483a
fix: Use unique resources for storage snippets. (#3029)
crwilcox Mar 5, 2020
a6d4783
fix: Reuse HMAC key as we have a limit of 5 (#3037)
crwilcox Mar 5, 2020
3e3c55a
fix: harden storage test fixtures (#3039)
crwilcox Mar 5, 2020
e14b9cd
storage: Fix docs for signed URL generation (#3008)
jakestambaugh Mar 6, 2020
a39b28f
chore(deps): update dependency google-cloud-storage to v1.26.0 (#3046)
renovate-bot Mar 9, 2020
ac7d9d2
feat: add remove conditional binding sample (#3107)
frankyn Mar 19, 2020
a49107e
Simplify noxfile setup. (#2806)
kurtisvg Apr 2, 2020
806536a
[storage] feat: add post policy sample (#3231)
frankyn Apr 3, 2020
886117a
Update dependency google-cloud-pubsub to v1.4.2 in Storage and Pub/Su…
busunkim96 Apr 24, 2020
de19d53
Remove name attribute from the input (#3569)
matarrese Apr 29, 2020
b2f7edd
[storage] fix: use unique blob name (#3568)
Apr 29, 2020
f4df95c
chore(deps): update dependency google-cloud-storage to v1.28.0 (#3260)
renovate-bot Apr 29, 2020
d9014d1
[storage] fix: use a different bucket for requester_pays_test (#3655)
Apr 30, 2020
fff3b99
docs(storage): add samples for lifer cycle and versioning (#3578)
HemangChothani May 4, 2020
2d338b5
chore: some lint fixes (#3750)
May 13, 2020
ef9c89d
chore(deps): update dependency google-cloud-pubsub to v1.4.3 (#3725)
renovate-bot May 13, 2020
80d2a25
docs(storage): add samples (#3687)
HemangChothani May 15, 2020
6a65819
chore(deps): update dependency google-cloud-storage to v1.28.1 (#3785)
renovate-bot May 19, 2020
faf59e8
docs(storage): add samples for file archive generation and cors conf…
HemangChothani May 20, 2020
1b1ef86
chore(deps): update dependency google-cloud-pubsub to v1.5.0 (#3781)
renovate-bot May 21, 2020
be94be0
Replace GCLOUD_PROJECT with GOOGLE_CLOUD_PROJECT. (#4022)
kurtisvg Jun 9, 2020
3e00322
[storage] testing: use multiple projects (#4048)
Jun 11, 2020
d927604
chore(deps): update dependency google-cloud-storage to v1.29.0 (#4040)
renovate-bot Jun 19, 2020
01bf3f6
Update dependency google-cloud-pubsub to v1.6.0 (#4039)
renovate-bot Jun 20, 2020
00702c3
chore(deps): update dependency google-cloud-pubsub to v1.6.1 (#4242)
renovate-bot Jul 6, 2020
6bcb58d
chore(deps): update dependency pytest to v5.4.3 (#4279)
renovate-bot Jul 12, 2020
c7edd26
chore(deps): update dependency mock to v4 (#4287)
renovate-bot Jul 13, 2020
f83c9b6
chore(deps): update dependency google-cloud-pubsub to v1.7.0 (#4290)
renovate-bot Jul 14, 2020
a8a789a
Fix mismatched storage region tags (#4194)
Jul 20, 2020
5d85bc4
Update dependency google-cloud-storage to v1.30.0
renovate-bot Jul 29, 2020
6f4edb3
Update dependency pytest to v6 (#4390)
renovate-bot Aug 1, 2020
e0d18e5
Resolved merge conflicts in requirements.txt files
engelke Aug 10, 2020
f6b9dcc
chore(deps): update dependency google-cloud-storage to v1.31.0 (#4564)
renovate-bot Aug 27, 2020
a5c72fc
chore: fix some more unmatched region tags (#4585)
Aug 28, 2020
948cd73
Update storage_get_metadata.py (#4615)
frankyn Sep 2, 2020
d2fa080
chore(deps): update dependency google-cloud-storage to v1.31.1 (#4714)
renovate-bot Sep 23, 2020
e1f90e7
chore(deps): update dependency google-cloud-storage to v1.31.2 (#4750)
renovate-bot Sep 24, 2020
19ebef4
chore(deps): update dependency pytest to v6.1.1 (#4761)
renovate-bot Oct 14, 2020
30dbe36
chore(deps): update dependency google-cloud-storage to v1.32.0 (#4871)
renovate-bot Oct 21, 2020
c2d76c4
chore(deps): update dependency pytest to v6.1.2 (#4921)
renovate-bot Oct 29, 2020
400fccf
change pprint to print. (#4856)
shivajid Oct 29, 2020
1deb741
chore(deps): update dependency google-cloud-storage to v1.33.0 (#4990)
renovate-bot Nov 19, 2020
9c44c38
Add patch call (#5013)
sdspikes Dec 1, 2020
147937e
fix: add a comment to draw attention to using get_blob, not blob (#5052)
crwilcox Dec 9, 2020
92d9955
chore(deps): update dependency mock to v4.0.3 (#5062)
renovate-bot Dec 10, 2020
3916356
fix(storage): Update comment, prefix should include delimiter (#5064)
crwilcox Dec 10, 2020
5df5392
chore(deps): update dependency google-cloud-storage to v1.35.0 (#5074)
renovate-bot Dec 15, 2020
56ef6f6
chore(deps): update dependency pytest to v6.2.1 (#5076)
renovate-bot Dec 21, 2020
8191ef8
chore(deps): update dependency google-cloud-pubsub to v2.2.0 (#4673)
renovate-bot Jan 5, 2021
acadcf7
chore(Dockerfile): add Python 3.9 (#4968)
Jan 16, 2021
63ca127
fix(storage): list all versions (#5325)
daquinoaldo Feb 4, 2021
5e3776a
docs: address sample feedback issues (#5329)
frankyn Feb 5, 2021
30b4c0c
chore(deps): update dependency google-cloud-pubsub to v2.3.0 (#5347)
renovate-bot Feb 9, 2021
e6cf484
chore(deps): update dependency google-cloud-storage to v1.35.1 (#5321)
renovate-bot Feb 9, 2021
173579b
chore(deps): update dependency google-cloud-pubsub to v2.4.0 (#5399)
renovate-bot Feb 22, 2021
a75684d
chore(deps): update dependency google-cloud-storage to v1.36.1 (#5353)
renovate-bot Feb 24, 2021
179c9f5
chore(deps): update dependency google-cloud-storage to v1.36.2 (#5520)
renovate-bot Mar 16, 2021
2283865
chore(deps): update dependency google-cloud-storage to v1.37.0 (#5580)
renovate-bot Mar 26, 2021
ada6fca
chore(deps): update dependency google-cloud-pubsub to v2.4.1 (#5610)
renovate-bot Apr 2, 2021
510cf40
docs: update description of parameters in storage_upload_file (#5707)
BenWhitehead Apr 16, 2021
3ff9770
samples(storage): update storage_set_bucket_public_iam to explicitly …
BenWhitehead Apr 16, 2021
1715fa6
chore: fix typo on noxfile (#5739)
dandhlee Apr 26, 2021
5eaf0e8
fix(storage): retry flaky test (#5744)
cojenco Apr 26, 2021
bd2f638
chore(deps): update dependency google-api-python-client to v2.3.0 (#5…
renovate-bot Apr 28, 2021
37efbc8
Update storage_list_files_with_prefix.py (#5747)
frankyn Apr 28, 2021
396bfe9
chore(deps): update dependency google-cloud-storage to v1.38.0 (#5640)
renovate-bot Apr 28, 2021
dad104c
chore(deps): update dependency pytest to v6.2.4 (#5787)
renovate-bot May 8, 2021
618b574
chore(deps): update dependency google-cloud-pubsub to v2.4.2 (#5810)
renovate-bot May 12, 2021
676d926
chore(deps): update dependency google-cloud-pubsub to v2.5.0 (#5845)
renovate-bot May 18, 2021
735dfad
chore(deps): update dependency google-api-python-client to v2.4.0 (#5…
renovate-bot May 19, 2021
8a0558a
chore(deps): update dependency google-api-python-client to v2.5.0 (#5…
renovate-bot May 21, 2021
36da4d0
chore(deps): update dependency google-api-python-client to v2.6.0 (#5…
renovate-bot May 27, 2021
0307fc0
chore(deps): update dependency google-api-python-client to v2.7.0 (#6…
renovate-bot Jun 2, 2021
79c76c3
chore(deps): update dependency google-cloud-pubsub to v2.6.0 (#6233)
renovate-bot Jun 25, 2021
4e210ca
public access prevention samples & tests (#4971)
shaffeeullah Jun 30, 2021
742d0ec
chore(deps): update dependency google-api-python-client to v2.11.0 (#…
renovate-bot Jul 1, 2021
d925aaa
chore(deps): update dependency google-cloud-pubsub to v2.6.1 (#6284)
renovate-bot Jul 12, 2021
ce6c7b4
chore(deps): update dependency backoff to v1.11.0 (#6285)
renovate-bot Jul 15, 2021
421cd6a
chore(deps): update dependency google-cloud-storage to v1.41.0 (#6197)
renovate-bot Jul 20, 2021
c6c1069
chore(deps): update dependency google-api-python-client to v2.12.0 (#…
renovate-bot Jul 20, 2021
458cc61
chore(deps): update dependency google-cloud-pubsub to v2.7.0 (#6486)
renovate-bot Jul 29, 2021
e070c90
fix(storage): update service account email for acl tests (#6529)
cojenco Aug 5, 2021
8762b13
docs(storage): update description in storage_download_file (#6553)
cojenco Aug 12, 2021
bd89e1d
Add storage move_blob sample and fix confusion with rename (#6554)
Aug 13, 2021
9f3fe3d
chore(deps): update dependency backoff to v1.11.1 (#6571)
renovate-bot Aug 16, 2021
ef138d6
chore(deps): update dependency google-api-python-client to v2.15.0 (#…
renovate-bot Aug 17, 2021
1a01d0c
chore(deps): update dependency google-cloud-storage to v1.42.0 (#6576)
renovate-bot Aug 17, 2021
f546502
chore(deps): update dependency google-api-python-client to v2.17.0 (#…
renovate-bot Aug 17, 2021
9d1326c
chore: Review the language fixes. (#6591)
pallabiwrites Aug 18, 2021
9ffb02c
chore(deps): update dependency google-cloud-pubsub to v2.7.1 (#6598)
renovate-bot Aug 20, 2021
0bd3c15
chore(deps): update dependency google-api-python-client to v2.18.0 (#…
renovate-bot Aug 24, 2021
5417de9
chore(deps): update dependency google-api-python-client to v2.19.0 (#…
renovate-bot Aug 31, 2021
0685d49
chore(deps): update dependency google-cloud-pubsub to v2.8.0 (#6664)
renovate-bot Sep 6, 2021
40fa6dc
chore(deps): update dependency google-api-python-client to v2.19.1 (#…
renovate-bot Sep 6, 2021
20a8096
chore(deps): update dependency google-cloud-storage to v1.42.1 (#6677)
renovate-bot Sep 9, 2021
8303373
chore(deps): update dependency google-api-python-client to v2.20.0 (#…
renovate-bot Sep 13, 2021
556555c
chore(deps): update dependency google-cloud-storage to v1.42.2 (#6700)
renovate-bot Sep 16, 2021
4d6b185
chore(deps): update dependency google-api-python-client to v2.21.0 (#…
renovate-bot Sep 20, 2021
2882b2a
chore(deps): update dependency google-api-python-client to v2.22.0 (#…
renovate-bot Sep 21, 2021
7c13d0f
fix(storage): skip test temporarily before pap changes release (#6750)
cojenco Sep 27, 2021
a844fca
fix(storage): add time for bucket patch changes to propagate (#6752)
cojenco Sep 28, 2021
2b2e9d9
fix(storage): add backoff to retry flaky test (#6745)
cojenco Sep 28, 2021
fd9d96f
chore(deps): update dependency google-api-python-client to v2.23.0 (#…
renovate-bot Sep 29, 2021
cbb361b
fix: update samples for pap, unspecified -> inherited (#6757)
Oct 4, 2021
12952ec
chore(deps): update dependency google-api-python-client to v2.24.0 (#…
renovate-bot Oct 5, 2021
cbe5676
fix(storage): add py-3.10 configs to noxfile config (#6903)
cojenco Oct 12, 2021
12d7382
chore(deps): update dependency google-api-python-client to v2.25.0 (#…
renovate-bot Oct 13, 2021
6982834
feat(storage): add retry configurations sample and test (#6900)
cojenco Oct 15, 2021
121fa16
samples: add cloud client samples from python-docs-samples
cojenco Oct 18, 2021
3800e64
Merge branch 'samples18' of /Users/cathyo/Dev/migration/sam-fork/pyth…
cojenco Oct 18, 2021
0a5c42b
🦉 Updates from OwlBot
gcf-owl-bot[bot] Oct 18, 2021
f609a81
add noxfile
cojenco Oct 18, 2021
c447719
noxfile
cojenco Oct 18, 2021
ab7ec09
🦉 Updates from OwlBot
gcf-owl-bot[bot] Oct 18, 2021
4f80ae0
remove invalid unused region tags
cojenco Oct 18, 2021
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
10 changes: 10 additions & 0 deletions samples/snippets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

For requester_pays_test.py, we need to use a different Storage bucket.

The test looks for an environment variable `REQUESTER_PAYS_TEST_BUCKET`.

Also, the service account for the test needs to have `Billing Project
Manager` role in order to make changes on buckets with requester pays
enabled.

We added that role to the test service account.
172 changes: 172 additions & 0 deletions samples/snippets/acl_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# Copyright 2016 Google, Inc.
#
# 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.

import os
import uuid

import backoff
from google.cloud import storage
from googleapiclient.errors import HttpError
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like a fossil from samples that used googleapiclient. I'm pretty sure that doing backoff / retries around this error is pointless -- nothing else in google-cloud-storage ever raises it (or imports / depends on googleapiclient).

Copy link
Contributor

Choose a reason for hiding this comment

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

The correct error class is probably google.api_core.exceptions.GoogleAPIError.

import pytest

import storage_add_bucket_default_owner
import storage_add_bucket_owner
import storage_add_file_owner
import storage_print_bucket_acl
import storage_print_bucket_acl_for_user
import storage_print_file_acl
import storage_print_file_acl_for_user
import storage_remove_bucket_default_owner
import storage_remove_bucket_owner
import storage_remove_file_owner

# Typically we'd use a @example.com address, but GCS requires a real Google
# account. Retrieve a service account email with storage admin permissions.
TEST_EMAIL = (
"py38-storage-test"
"@python-docs-samples-tests.iam.gserviceaccount.com"
)


@pytest.fixture(scope="module")
def test_bucket():
"""Yields a bucket that is deleted after the test completes."""

# The new projects have uniform bucket-level access and our tests don't
# pass with those buckets. We need to use the old main project for now.
original_value = os.environ['GOOGLE_CLOUD_PROJECT']
os.environ['GOOGLE_CLOUD_PROJECT'] = os.environ['MAIN_GOOGLE_CLOUD_PROJECT']
bucket = None
while bucket is None or bucket.exists():
bucket_name = "acl-test-{}".format(uuid.uuid4())
bucket = storage.Client().bucket(bucket_name)
bucket.create()
yield bucket
bucket.delete(force=True)
# Set the value back.
os.environ['GOOGLE_CLOUD_PROJECT'] = original_value


@pytest.fixture
def test_blob(test_bucket):
"""Yields a blob that is deleted after the test completes."""
bucket = test_bucket
blob = bucket.blob("storage_acl_test_sigil-{}".format(uuid.uuid4()))
blob.upload_from_string("Hello, is it me you're looking for?")
yield blob


def test_print_bucket_acl(test_bucket, capsys):
storage_print_bucket_acl.print_bucket_acl(test_bucket.name)
out, _ = capsys.readouterr()
assert out


def test_print_bucket_acl_for_user(test_bucket, capsys):
test_bucket.acl.user(TEST_EMAIL).grant_owner()
test_bucket.acl.save()

storage_print_bucket_acl_for_user.print_bucket_acl_for_user(
test_bucket.name, TEST_EMAIL
)

out, _ = capsys.readouterr()
assert "OWNER" in out


@backoff.on_exception(backoff.expo, HttpError, max_time=60)
def test_add_bucket_owner(test_bucket):
storage_add_bucket_owner.add_bucket_owner(test_bucket.name, TEST_EMAIL)

test_bucket.acl.reload()
assert "OWNER" in test_bucket.acl.user(TEST_EMAIL).get_roles()


@backoff.on_exception(backoff.expo, HttpError, max_time=60)
def test_remove_bucket_owner(test_bucket):
test_bucket.acl.user(TEST_EMAIL).grant_owner()
test_bucket.acl.save()

storage_remove_bucket_owner.remove_bucket_owner(
test_bucket.name, TEST_EMAIL)

test_bucket.acl.reload()
assert "OWNER" not in test_bucket.acl.user(TEST_EMAIL).get_roles()


@backoff.on_exception(backoff.expo, HttpError, max_time=60)
def test_add_bucket_default_owner(test_bucket):
storage_add_bucket_default_owner.add_bucket_default_owner(
test_bucket.name, TEST_EMAIL
)

test_bucket.default_object_acl.reload()
roles = test_bucket.default_object_acl.user(TEST_EMAIL).get_roles()
assert "OWNER" in roles


@backoff.on_exception(backoff.expo, HttpError, max_time=60)
def test_remove_bucket_default_owner(test_bucket):
test_bucket.acl.user(TEST_EMAIL).grant_owner()
test_bucket.acl.save()

storage_remove_bucket_default_owner.remove_bucket_default_owner(
test_bucket.name, TEST_EMAIL
)

test_bucket.default_object_acl.reload()
roles = test_bucket.default_object_acl.user(TEST_EMAIL).get_roles()
assert "OWNER" not in roles


def test_print_blob_acl(test_blob, capsys):
storage_print_file_acl.print_blob_acl(
test_blob.bucket.name, test_blob.name)
out, _ = capsys.readouterr()
assert out


@backoff.on_exception(backoff.expo, HttpError, max_time=60)
def test_print_blob_acl_for_user(test_blob, capsys):
test_blob.acl.user(TEST_EMAIL).grant_owner()
test_blob.acl.save()

storage_print_file_acl_for_user.print_blob_acl_for_user(
test_blob.bucket.name, test_blob.name, TEST_EMAIL
)

out, _ = capsys.readouterr()
assert "OWNER" in out


@backoff.on_exception(backoff.expo, HttpError, max_time=60)
def test_add_blob_owner(test_blob):
storage_add_file_owner.add_blob_owner(
test_blob.bucket.name, test_blob.name, TEST_EMAIL)

test_blob.acl.reload()
assert "OWNER" in test_blob.acl.user(TEST_EMAIL).get_roles()


@backoff.on_exception(backoff.expo, HttpError, max_time=60)
def test_remove_blob_owner(test_blob):
test_blob.acl.user(TEST_EMAIL).grant_owner()
test_blob.acl.save()

storage_remove_file_owner.remove_blob_owner(
test_blob.bucket.name, test_blob.name, TEST_EMAIL
)

test_blob.acl.reload()
assert "OWNER" not in test_blob.acl.user(TEST_EMAIL).get_roles()
176 changes: 176 additions & 0 deletions samples/snippets/bucket_lock_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# Copyright 2018 Google Inc. All Rights Reserved.
#
# 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.

import time
import uuid

from google.cloud import storage
import pytest

import storage_disable_default_event_based_hold
import storage_enable_default_event_based_hold
import storage_get_default_event_based_hold
import storage_get_retention_policy
import storage_lock_retention_policy
import storage_release_event_based_hold
import storage_release_temporary_hold
import storage_remove_retention_policy
import storage_set_event_based_hold
import storage_set_retention_policy
import storage_set_temporary_hold


BLOB_NAME = "storage_snippets_test_sigil"
BLOB_CONTENT = "Hello, is it me you're looking for?"
# Retention policy for 5 seconds
RETENTION_POLICY = 5


@pytest.fixture
def bucket():
"""Yields a bucket that is deleted after the test completes."""
bucket = None
while bucket is None or bucket.exists():
bucket_name = "bucket-lock-{}".format(uuid.uuid4())
bucket = storage.Client().bucket(bucket_name)
bucket.create()
yield bucket
bucket.delete(force=True)


def test_retention_policy_no_lock(bucket, capsys):
storage_set_retention_policy.set_retention_policy(
bucket.name, RETENTION_POLICY
)
bucket.reload()

assert bucket.retention_period is RETENTION_POLICY
assert bucket.retention_policy_effective_time is not None
assert bucket.retention_policy_locked is None

storage_get_retention_policy.get_retention_policy(bucket.name)
out, _ = capsys.readouterr()
assert "Retention Policy for {}".format(bucket.name) in out
assert "Retention Period: 5" in out
assert "Effective Time: " in out
assert "Retention Policy is locked" not in out

blob = bucket.blob(BLOB_NAME)
blob.upload_from_string(BLOB_CONTENT)

assert blob.retention_expiration_time is not None

storage_remove_retention_policy.remove_retention_policy(bucket.name)
bucket.reload()
assert bucket.retention_period is None

time.sleep(RETENTION_POLICY)


def test_retention_policy_lock(bucket, capsys):
storage_set_retention_policy.set_retention_policy(
bucket.name, RETENTION_POLICY
)
bucket.reload()
assert bucket.retention_policy_locked is None

storage_lock_retention_policy.lock_retention_policy(bucket.name)
bucket.reload()
assert bucket.retention_policy_locked is True

storage_get_retention_policy.get_retention_policy(bucket.name)
out, _ = capsys.readouterr()
assert "Retention Policy is locked" in out


def test_enable_disable_bucket_default_event_based_hold(bucket, capsys):
storage_get_default_event_based_hold.get_default_event_based_hold(
bucket.name
)
out, _ = capsys.readouterr()
assert (
"Default event-based hold is not enabled for {}".format(bucket.name)
in out
)
assert (
"Default event-based hold is enabled for {}".format(bucket.name)
not in out
)

storage_enable_default_event_based_hold.enable_default_event_based_hold(
bucket.name
)
bucket.reload()

assert bucket.default_event_based_hold is True

storage_get_default_event_based_hold.get_default_event_based_hold(
bucket.name
)
out, _ = capsys.readouterr()
assert (
"Default event-based hold is enabled for {}".format(bucket.name) in out
)

# Changes to the bucket will be readable immediately after writing,
# but configuration changes may take time to propagate.
time.sleep(10)

blob = bucket.blob(BLOB_NAME)
blob.upload_from_string(BLOB_CONTENT)
assert blob.event_based_hold is True

storage_release_event_based_hold.release_event_based_hold(
bucket.name, blob.name
)
blob.reload()
assert blob.event_based_hold is False

storage_disable_default_event_based_hold.disable_default_event_based_hold(
bucket.name
)
bucket.reload()
assert bucket.default_event_based_hold is False


def test_enable_disable_temporary_hold(bucket):
blob = bucket.blob(BLOB_NAME)
blob.upload_from_string(BLOB_CONTENT)
assert blob.temporary_hold is None

storage_set_temporary_hold.set_temporary_hold(bucket.name, blob.name)
blob.reload()
assert blob.temporary_hold is True

storage_release_temporary_hold.release_temporary_hold(
bucket.name, blob.name
)
blob.reload()
assert blob.temporary_hold is False


def test_enable_disable_event_based_hold(bucket):
blob = bucket.blob(BLOB_NAME)
blob.upload_from_string(BLOB_CONTENT)
assert blob.event_based_hold is None

storage_set_event_based_hold.set_event_based_hold(bucket.name, blob.name)
blob.reload()
assert blob.event_based_hold is True

storage_release_event_based_hold.release_event_based_hold(
bucket.name, blob.name
)
blob.reload()
assert blob.event_based_hold is False
Loading