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

fix: raise a ValueError in BucketNotification.create() if a topic name is not set #617

Merged
merged 7 commits into from Oct 11, 2021

Conversation

cojenco
Copy link
Contributor

@cojenco cojenco commented Oct 6, 2021

A PubSub topic (topic name) is required to insert a BucketNotification according to the JSON API.

Raise a ValueError in BucketNotification.create() if topic_name = None. This adds a guard check and further prevents requests with invalid NoneType string interpolation.

Add tests and revise conformance tests.

Fixes #616 🦕

@product-auto-label product-auto-label bot added the api: storage Issues related to the googleapis/python-storage API. label Oct 6, 2021
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Oct 6, 2021
@cojenco cojenco changed the title Fix: raise a ValueError in BucketNotification.create() if a topic name is not set fix: raise a ValueError in BucketNotification.create() if a topic name is not set Oct 6, 2021
@cojenco cojenco marked this pull request as ready for review October 6, 2021 20:21
@cojenco cojenco requested review from a team as code owners October 6, 2021 20:21
"""
if self.notification_id is not None:
raise ValueError(
"Notification already exists w/ id: {}".format(self.notification_id)
)

if self.topic_name is None:
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the error from the service currently if the topic name is not included?

This might be fine, but in general we want to avoid overdoing it on client-side validation unless we have a good reason-- we want to let the service itself own most of this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's two parts to answer your question.

If the topic is not included, the server returns a 400 with error message "Invalid Google Cloud Pub/Sub topic. It should look like '//pubsub.googleapis.com/projects/*/topics/*.'"

In our client, on line 270, we preprocess the request body topic through string interpolation. So topic becomes "//pubsub.googleapis.com/projects/<project-identifier>/topics/None" if topic_name=None. Thus we get a 400 for another reason "Cloud Pub/Sub topic '//pubsub.googleapis.com/projects/project/topics/None' not found, ..."

To avoid overdoing client-side validation, I could instead change the logic around preparing/formatting the topic.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah okay-- yeah I think in this case since we are constructing the topic client-side using topic_name, that's a good reason to just do the validation ourselves. It would be confusing if users thought that they had to supply the whole path in the error message you show as topic_name. Let's just try to make as clear a message as possible for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've fixed the logic in constructing the topic. I like the idea of letting the service itself own the validation and aligning with the JSON API. Now the error from the server, if any, will bubble up directly. Will be merging the latest changes if no further questions. Thanks for the review @tritone!

@cojenco cojenco added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Oct 7, 2021
@cojenco cojenco removed the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Oct 11, 2021
@cojenco cojenco merged commit 9dd78df into googleapis:main Oct 11, 2021
cojenco added a commit to cojenco/python-storage that referenced this pull request Oct 13, 2021
…e is not set (googleapis#617)

* fix: topic name is required to create a notification. add check and tests

* revise conf tests

* fix topic formatting and remove extra validation

* blacken lint

* update docstrings
cojenco added a commit to cojenco/python-storage that referenced this pull request Oct 13, 2021
…e is not set (googleapis#617)

* fix: topic name is required to create a notification. add check and tests

* revise conf tests

* fix topic formatting and remove extra validation

* blacken lint

* update docstrings
gcf-merge-on-green bot pushed a commit that referenced this pull request Nov 17, 2021
🤖 I have created a release \*beep\* \*boop\*
---
## [1.43.0](https://www.github.com/googleapis/python-storage/compare/v1.42.3...v1.43.0) (2021-11-15)


### Features

* add ignore_flush parameter to BlobWriter ([#644](https://www.github.com/googleapis/python-storage/issues/644)) ([af9c9dc](https://www.github.com/googleapis/python-storage/commit/af9c9dc83d8582167b74105167af17c9809455de))
* add support for Python 3.10 ([#615](https://www.github.com/googleapis/python-storage/issues/615)) ([f81a2d0](https://www.github.com/googleapis/python-storage/commit/f81a2d054616c1ca1734997a16a8f47f98ab346b))


### Bug Fixes

* raise a ValueError in BucketNotification.create() if a topic name is not set ([#617](https://www.github.com/googleapis/python-storage/issues/617)) ([9dd78df](https://www.github.com/googleapis/python-storage/commit/9dd78df444d21af51af7858e8958b505a26c0b79))


### Documentation

* add contributing and authoring guides under samples/ ([#633](https://www.github.com/googleapis/python-storage/issues/633)) ([420591a](https://www.github.com/googleapis/python-storage/commit/420591a2b71f823dbe80f4a4405d8a514f87e0fb))
* add links to samples and how to guides ([#641](https://www.github.com/googleapis/python-storage/issues/641)) ([49f78b0](https://www.github.com/googleapis/python-storage/commit/49f78b09fed6d9f486639fd0a72542c30a0df084))
* add README to samples subdirectory ([#639](https://www.github.com/googleapis/python-storage/issues/639)) ([58af882](https://www.github.com/googleapis/python-storage/commit/58af882c047c31f59486513c568737082bca6350))
* update samples readme with cli args ([#651](https://www.github.com/googleapis/python-storage/issues/651)) ([75dda81](https://www.github.com/googleapis/python-storage/commit/75dda810e808074d18dfe7915f1403ad01bf2f02))
---


This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
@cojenco cojenco deleted the topic_name branch December 3, 2021 23:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: storage Issues related to the googleapis/python-storage API. cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BucketNotification.create() does not check if a topic is provided
3 participants