0

On the gcloud cli, when listing the pubsub subscriptions of a project, it is possible to filter results by using the --filter flag. Here is an example:

gcloud --project=my-project pubsub subscriptions list --filter=my-filter-string --format='value(name)'

I did not manage to find out how to do this with the python library and its list_subscription method.

It seems to only basically accept a project string and to return all subscriptions in the project. This means I would need to get all the subscriptions in the project and then loop through them to filter them, as follows:

from google.cloud import pubsub_v1

subscriber_client = pubsub_v1.SubscriberClient()

filter = "my-filter-string"

with subscriber_client:
    page_result = subscriber_client.list_subscriptions(
            project="projects/my-project",
        )
    filtered_subscriptions = [
        subscription.name
        for subscription in page_result
        if filter in subscription.name.split("/")[-1]
    ]
    for subscription_name in filtered_subscriptions:
        print(subscription_name)

Is there a more efficient way to do that ?

I have been trying to do this with the metadata: Sequence[Tuple[str, str]] argument on the method, but could not find examples of how to do it.

Emmanuel Sciara
  • 180
  • 1
  • 9

1 Answers1

1

Neither the REST nor RPC API provide a way to filter on the server side, so no there is no more efficient way to do this.

I imagine the gcloud code to do the filter is conceptually similar to what you wrote.

somethingsomething
  • 1,746
  • 6
  • 8