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

list_subscriptions does not work properly #1485

Closed
bitcpf opened this issue Feb 17, 2016 · 12 comments
Closed

list_subscriptions does not work properly #1485

bitcpf opened this issue Feb 17, 2016 · 12 comments
Assignees
Labels
api: pubsub Issues related to the Pub/Sub API. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@bitcpf
Copy link

bitcpf commented Feb 17, 2016

list_subscriptions() works but list_subscriptions(topic_name = 'topic_name') does not work properly

>>> from gcloud import pubsub
>>> client = pubsub.Client(project = 'my_project')
>>> client.list_subscriptions(topic_name = 'my_topic')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/gcloud/pubsub/client.py", line 131, in list_subscriptions
    for resource in resp['subscriptions']]
  File "/usr/local/lib/python2.7/dist-packages/gcloud/pubsub/subscription.py", line 68, in from_api_repr
    topic_path = resource['topic']
TypeError: string indices must be integers
@dhermes dhermes added the api: pubsub Issues related to the Pub/Sub API. label Feb 17, 2016
@tseaver
Copy link
Contributor

tseaver commented Feb 17, 2016

ATM, topic_name is the third positional argument: you need to call it as a named parameter:

client.list_subscriptions(topic_name='topic_name')

@jgeewax, @dhermes I'm reading this issue as a request to make topic_name the first positional argument. Opinions?

@dhermes
Copy link
Contributor

dhermes commented Feb 17, 2016

👍

@bitcpf
Copy link
Author

bitcpf commented Feb 17, 2016

@tseaver
Sorry, what I really want to say is I wrote the command as:

subscription, next_page_tokens = client.list_subscriptions(topic_name = 'topic_name')

It does not work. And the issue is this line:

        subscriptions = [Subscription.from_api_repr(resource, self,
                                                    topics=topics)
                         for resource in resp.get('subscriptions', ())]

The code pass an empty value of resource (maybe) to subscriptions.

The output is like:

>>> from gcloud import pubsub
>>> client = pubsub.Client(project = 'my_project'')
>>> client.list_subscriptions(topic_name = 'my_topic')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/gcloud/pubsub/client.py", line 131, in list_subscriptions
    for resource in resp['subscriptions']]
  File "/usr/local/lib/python2.7/dist-packages/gcloud/pubsub/subscription.py", line 68, in from_api_repr
    topic_path = resource['topic']
TypeError: string indices must be integers

@dhermes
Copy link
Contributor

dhermes commented Feb 20, 2016

@tseaver Have you reproduced?

@dhermes
Copy link
Contributor

dhermes commented Feb 20, 2016

@tseaver I reproduced:

>>> import gcloud.pubsub
>>> from gcloud import _helpers
>>> from gcloud.environment_vars import TESTS_PROJECT
>>> _helpers.PROJECT = TESTS_PROJECT
>>> C = gcloud.pubsub.Client()
>>> C.project
'FOOOBAR'
>>> C.list_subscriptions()
([], None)
>>> C.list_topics()
([], None)
>>> T = C.topic('hi-mom')
>>> T.create()
>>> S = T.subscription('this-is-mine')
>>> S.create()
>>> C.list_topics()
([<gcloud.pubsub.topic.Topic object at 0x7f7c21812310>], None)
>>> C.list_subscriptions()
([<gcloud.pubsub.subscription.Subscription object at 0x7f7c21812390>], None)
>>> C.list_subscriptions(topic_name=T.name)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "gcloud/pubsub/client.py", line 131, in list_subscriptions
    for resource in resp.get('subscriptions', ())]
  File "gcloud/pubsub/subscription.py", line 71, in from_api_repr
    topic_path = resource['topic']
TypeError: string indices must be integers

@dhermes
Copy link
Contributor

dhermes commented Feb 20, 2016

In [16]: %debug
> .../gcloud-python/gcloud/pubsub/subscription.py(71)from_api_repr()
     69         if topics is None:
     70             topics = {}
---> 71         topic_path = resource['topic']
     72         topic = topics.get(topic_path)
     73         if topic is None:

ipdb> resource
u'projects/omega-moonlight-697/subscriptions/this-is-mine'
ipdb> u
> .../gcloud-python/gcloud/pubsub/client.py(131)list_subscriptions()
    129         subscriptions = [Subscription.from_api_repr(resource, self,
    130                                                     topics=topics)
--> 131                          for resource in resp.get('subscriptions', ())]
    132         return subscriptions, resp.get('nextPageToken')
    133 

ipdb> resp
{u'subscriptions': [u'projects/omega-moonlight-697/subscriptions/this-is-mine']}

@dhermes
Copy link
Contributor

dhermes commented Feb 20, 2016

This is in contrast to what the output looks like without the topic name:

{
  "subscriptions": [
    {
      "topic": "projects/omega-moonlight-697/topics/hi-mom", 
      "ackDeadlineSeconds": 10, 
      "pushConfig": {}, 
      "name": "projects/omega-moonlight-697/subscriptions/this-is-mine"
    }
  ]
}

@tseaver
Copy link
Contributor

tseaver commented Mar 7, 2016

OK, I see the issue: projects.subscriptions/list'](https://cloud.google.com/pubsub/reference/rest/v1/projects.subscriptions/list) is documented to return the full Subscriptions resource, whereas [projects.topics.subscriptions/list` is documented to return only a list of subscription URLs.

The API design choice here is utterly bogus, but we have to work around it.

@tseaver tseaver added the type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. label Mar 7, 2016
@tseaver
Copy link
Contributor

tseaver commented Mar 7, 2016

Given the disparity, I would prefer to handle topic-based subscriptions by adding a list_subscriptions method to the Topic class.

@tseaver
Copy link
Contributor

tseaver commented Mar 7, 2016

@bitcpf Thanks for the report!

@dhermes
Copy link
Contributor

dhermes commented Mar 8, 2016

👍 with separate methods @tseaver

@tseaver
Copy link
Contributor

tseaver commented Mar 8, 2016

@dhermes see #1580 for the implementation.

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. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

3 participants