Skip to content

Commit

Permalink
fix: address incorrect usage of request preconditions (googleapis#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankyn authored and cojenco committed Oct 13, 2021
1 parent e8eac01 commit 4576b83
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
21 changes: 14 additions & 7 deletions google/cloud/storage/bucket.py
Expand Up @@ -2022,6 +2022,9 @@ def rename_blob(
This method will first duplicate the data and then delete the
old blob. This means that with very large objects renaming
could be a very (temporarily) costly or a very slow operation.
If you need more control over the copy and deletion, instead
use `google.cloud.storage.blob.Blob.copy_to` and
`google.cloud.storage.blob.Blob.delete` directly.
:type blob: :class:`google.cloud.storage.blob.Blob`
:param blob: The blob to be renamed.
Expand Down Expand Up @@ -2079,25 +2082,29 @@ def rename_blob(
:param if_source_generation_match: (Optional) Makes the operation
conditional on whether the source
object's generation matches the
given value.
given value. Also used in the
delete request.
:type if_source_generation_not_match: long
:param if_source_generation_not_match: (Optional) Makes the operation
conditional on whether the source
object's generation does not match
the given value.
the given value. Also used in the
delete request.
:type if_source_metageneration_match: long
:param if_source_metageneration_match: (Optional) Makes the operation
conditional on whether the source
object's current metageneration
matches the given value.
matches the given value.Also used in the
delete request.
:type if_source_metageneration_not_match: long
:param if_source_metageneration_not_match: (Optional) Makes the operation
conditional on whether the source
object's current metageneration
does not match the given value.
Also used in the delete request.
:type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
:param retry: (Optional) How to retry the RPC. A None value will disable retries.
Expand Down Expand Up @@ -2139,10 +2146,10 @@ def rename_blob(
blob.delete(
client=client,
timeout=timeout,
if_generation_match=if_generation_match,
if_generation_not_match=if_generation_not_match,
if_metageneration_match=if_metageneration_match,
if_metageneration_not_match=if_metageneration_not_match,
if_generation_match=if_source_generation_match,
if_generation_not_match=if_source_generation_not_match,
if_metageneration_match=if_source_metageneration_match,
if_metageneration_not_match=if_source_metageneration_not_match,
retry=retry,
)
return new_blob
Expand Down
13 changes: 8 additions & 5 deletions tests/unit/test_bucket.py
Expand Up @@ -1639,7 +1639,8 @@ def test_rename_blob_with_generation_match(self):
NEW_BLOB_NAME = "new-blob-name"
DATA = {"name": NEW_BLOB_NAME}
GENERATION_NUMBER = 6
METAGENERATION_NUMBER = 9
SOURCE_GENERATION_NUMBER = 7
SOURCE_METAGENERATION_NUMBER = 9

connection = _Connection(DATA)
client = _Client(connection)
Expand All @@ -1652,7 +1653,8 @@ def test_rename_blob_with_generation_match(self):
client=client,
timeout=42,
if_generation_match=GENERATION_NUMBER,
if_source_metageneration_not_match=METAGENERATION_NUMBER,
if_source_generation_match=SOURCE_GENERATION_NUMBER,
if_source_metageneration_not_match=SOURCE_METAGENERATION_NUMBER,
)

self.assertIs(renamed_blob.bucket, bucket)
Expand All @@ -1668,7 +1670,8 @@ def test_rename_blob_with_generation_match(self):
kw["query_params"],
{
"ifGenerationMatch": GENERATION_NUMBER,
"ifSourceMetagenerationNotMatch": METAGENERATION_NUMBER,
"ifSourceGenerationMatch": SOURCE_GENERATION_NUMBER,
"ifSourceMetagenerationNotMatch": SOURCE_METAGENERATION_NUMBER,
},
)
self.assertEqual(kw["timeout"], 42)
Expand All @@ -1677,10 +1680,10 @@ def test_rename_blob_with_generation_match(self):
blob.delete.assert_called_once_with(
client=client,
timeout=42,
if_generation_match=GENERATION_NUMBER,
if_generation_match=SOURCE_GENERATION_NUMBER,
if_generation_not_match=None,
if_metageneration_match=None,
if_metageneration_not_match=None,
if_metageneration_not_match=SOURCE_METAGENERATION_NUMBER,
retry=DEFAULT_RETRY_IF_GENERATION_SPECIFIED,
)

Expand Down

0 comments on commit 4576b83

Please sign in to comment.