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

PubSub Client: list_subscriptions returns all subscriptions, not just for that topic #4426

Closed
anorth2 opened this issue Nov 20, 2017 · 5 comments
Assignees
Labels
api: pubsub Issues related to the Pub/Sub API. status: investigating The issue is under investigation, which is determined to be non-trivial.

Comments

@anorth2
Copy link

anorth2 commented Nov 20, 2017

  1. OS type and version
    Any and all

  2. Python version and virtual environment information python --version
    Python 3.6.3

  3. google-cloud-python version pip show google-cloud, pip show google-<service> or pip freeze
    google==1.9.3
    google-api-core==0.1.1
    google-auth==1.2.1
    google-cloud-core==0.28.0
    google-cloud-pubsub==0.29.0
    google-gax==0.15.16
    googleapis-common-protos==1.5.3
    grpc-google-iam-v1==0.11.4

  4. Stacktrace if available

  5. Steps to reproduce
    Run example code for listing subscriptions for a topic from here:
    https://cloud.google.com/pubsub/docs/admin

  6. Code example

def list_subscriptions_in_topic(project, topic_name):
    """Lists all subscriptions for a given topic."""
    subscriber = pubsub_v1.SubscriberClient()
    topic_path = subscriber.topic_path(project, topic_name)

    for subscription in subscriber.list_subscriptions(topic_path):
        print(subscription.name)

topic path being used as shown by topic_path = subscriber.topic_path(project, topic_name):
projects/my-team-dev/topics/testing-topic

Using GitHub flavored markdown can help make your request clearer.
See: https://guides.github.com/features/mastering-markdown/

@anorth2 anorth2 changed the title PubSub Client: List Subscriptions returns all subscriptions, not just for that topic PubSub Client: list_subscriptions returns all subscriptions, not just for that topic Nov 20, 2017
@dhermes dhermes added api: pubsub Issues related to the Pub/Sub API. status: investigating The issue is under investigation, which is determined to be non-trivial. labels Nov 20, 2017
@gylu
Copy link

gylu commented Nov 20, 2017

+1
I'm seeing this also, on google-cloud-pubsub:0.29.0, google-cloud:0.30.0
If the topic_path doesn't adhere to the form projects/project/topics/topic, then an exception is caught, but if it's projects/project/topics/some-topic-name-that-doesnt-exist, then all subscriptions for all topics are listed.

@dhermes
Copy link
Contributor

dhermes commented Dec 15, 2017

So this is an issue, but not really as reported. The problem is that subscriber.list_subscriptions() is meant to be for the entire project:

In [1]: import google.auth

In [2]: from google.cloud import pubsub_v1

In [3]: _, project = google.auth.default()

In [4]: publisher = pubsub_v1.PublisherClient()
E1215 14:50:02.433979430   11896 ev_epollex_linux.cc:1482]   Skipping epollex becuase GRPC_LINUX_EPOLL is not defined.
E1215 14:50:02.434013514   11896 ev_epoll1_linux.cc:1261]    Skipping epoll1 becuase GRPC_LINUX_EPOLL is not defined.
E1215 14:50:02.434020956   11896 ev_epollsig_linux.cc:1761]  Skipping epollsig becuase GRPC_LINUX_EPOLL is not defined.

In [5]: subscriber = pubsub_v1.SubscriberClient()

In [6]: subscriber.list_subscriptions?
Signature: subscriber.list_subscriptions(project, ...)
Docstring:
Lists matching subscriptions.

...

Args:
    project (str): The name of the cloud project that subscriptions belong to.
        Format is ``projects/{project}``.
...

In [7]: project_path = subscriber.project_path(project)

In [8]: LS = list(subscriber.list_subscriptions(project_path))

In [9]: len(LS)
Out[9]: 6

The method you want is publisher.list_topic_subscriptions:

In [10]: topic_path = publisher.topic_path(project, 'abcdef2')

In [11]: LTS = list(publisher.list_topic_subscriptions(topic_path))

In [12]: len(LTS)
Out[12]: 3

In [13]: publisher.list_topic_subscriptions?
Signature: publisher.list_topic_subscriptions(topic, ...)
Docstring:
Lists the name of the subscriptions for this topic.

...

Args:
    topic (str): The name of the topic that subscriptions are attached to.
        Format is ``projects/{project}/topics/{topic}``.

The actual issue is that subscriber.list_subscriptions doesn't reject requests with the topic path (likely because the topic path starts with the project path):

In [14]: topic_path.startswith(project_path)
Out[14]: True

In [15]: LS_ALSO = list(subscriber.list_subscriptions(topic_path))

In [16]: LS_ALSO == LS
Out[16]: True

I think an issue could be filed with the backend to reject those requests with the topic_path where a project_path should be.

/cc @kir-titievsky

@dhermes dhermes closed this as completed Dec 15, 2017
@dhermes
Copy link
Contributor

dhermes commented Dec 15, 2017

I filed https://issuetracker.google.com/issues/70727179 with the backend team.

@anorth2
Copy link
Author

anorth2 commented Jan 8, 2018

@dhermes If this is indeed the issue, then this needs to be updated: https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/pubsub/cloud-client/subscriber.py#L30

The docs state that my method was the way to retrieve by topic.
https://cloud.google.com/pubsub/docs/admin#pubsub-list-subscriptions-python

@dhermes
Copy link
Contributor

dhermes commented Jan 8, 2018

Thanks for finding that @anorth2 (I'm only loosely aware of what's in python-docs-samples). I filed GoogleCloudPlatform/python-docs-samples#1305 against that repo.

parthea pushed a commit that referenced this issue Oct 21, 2023
…)](GoogleCloudPlatform/python-docs-samples#4426)

This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [google-cloud-translate](https://github.com/googleapis/python-translate) | patch | `==2.0.1` -> `==2.0.2` |

---

<details>
<summary>googleapis/python-translate</summary>

[Compare Source](https://github.com/googleapis/python-translate/compare/v2.0.1...v2.0.2)

</details>

---

:date: **Schedule**: At any time (no schedule defined).

:vertical_traffic_light: **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

:recycle: **Rebasing**: Never, or you tick the rebase/retry checkbox.

:no_bell: **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [WhiteSource Renovate](https://renovate.whitesourcesoftware.com). View repository job log [here](https://app.renovatebot.com/dashboard#GoogleCloudPlatform/python-docs-samples).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: pubsub Issues related to the Pub/Sub API. status: investigating The issue is under investigation, which is determined to be non-trivial.
Projects
None yet
Development

No branches or pull requests

3 participants