diff --git a/lib/pubsub/index.js b/lib/pubsub/index.js index bd7a3b62d90..30ef4715b63 100644 --- a/lib/pubsub/index.js +++ b/lib/pubsub/index.js @@ -154,7 +154,42 @@ function Connection(opts) { }); } -// TODO(jbd): List subscriptions. +/** + * Lists subscriptions. + * @param {string} query.filterByTopic Returns subscriptions that are + * subscribed to the topic provided. + * @param {string} query.pageToken Page token. + * @param {Number} query.maxResults Max number of results to return. + * @param {Function} callback Callback function. + */ +Connection.prototype.listSubscriptions = function(query, callback) { + var that = this; + if (arguments.length < 2) { + callback = query; + query = {}; + } + var q = util.extend({}, query); + q.query = q.filterByTopic + ? 'pubsub.googleapis.com/topic in (' + this.fullTopicName_(q.filterByTopic) + ')' + : 'cloud.googleapis.com/project in (' + this.fullProjectName_() + ')'; + delete q.filterByTopic; + + this.makeReq('GET', 'subscriptions', q, true, function(err, result) { + if (err) { + return callback(err); + } + var items = result.subscription || []; + var subscriptions = items.map(function(item) { + return new Subscription(that, item.name); + }); + var nextQuery = null; + if (result.nextPageToken) { + nextQuery = q; + nextQuery.pageToken = result.nextPageToken; + } + callback(null, subscriptions, nextQuery); + }); +}; /** * Subscribe with the provided options. @@ -219,6 +254,16 @@ Connection.prototype.fullTopicName_ = function(name) { }); }; +/** + * Returns the fully qualified project name. + * Full name is in /projects/ form. + */ +Connection.prototype.fullProjectName_ = function() { + return util.format('/projects/{projectId}', { + projectId: this.id + }); +}; + // TOOD(jbd): Don't duplicate, unify this with bucket.makeReq. Connection.prototype.makeReq = function(method, path, q, body, callback) { var reqOpts = {