Skip to content

Commit

Permalink
feat(topic): create setMetadata method (#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
callmehiphop authored and JustinBeckwith committed Mar 9, 2019
1 parent bd727e1 commit 20eb583
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ export {PageOptions, GetSnapshotsCallback, GetSnapshotsResponse, GetSubscription
export {CreateSnapshotCallback, CreateSnapshotResponse, SeekCallback, SeekResponse, Snapshot} from './snapshot';
export {Message} from './subscriber';
export {PushConfig, SubscriptionMetadata, SubscriptionOptions, SubscriptionCloseCallback, CreateSubscriptionOptions, CreateSubscriptionCallback, CreateSubscriptionResponse, GetSubscriptionOptions, GetSubscriptionCallback, GetSubscriptionResponse, GetSubscriptionMetadataCallback, GetSubscriptionMetadataResponse, SetSubscriptionMetadataCallback, SetSubscriptionMetadataResponse, Subscription} from './subscription';
export {CreateTopicCallback, CreateTopicResponse, GetTopicCallback, GetTopicResponse, GetTopicOptions, GetTopicMetadataCallback, GetTopicMetadataResponse, GetTopicSubscriptionsCallback, GetTopicSubscriptionsResponse, Topic} from './topic';
export {CreateTopicCallback, CreateTopicResponse, GetTopicCallback, GetTopicResponse, GetTopicOptions, GetTopicMetadataCallback, GetTopicMetadataResponse, GetTopicSubscriptionsCallback, GetTopicSubscriptionsResponse, SetTopicMetadataCallback, SetTopicMetadataResponse, Topic, TopicMetadata} from './topic';
92 changes: 86 additions & 6 deletions src/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import {EmptyCallback, EmptyResponse, ExistsCallback, ExistsResponse, ObjectStre
import {CreateSubscriptionCallback, CreateSubscriptionOptions, CreateSubscriptionResponse, Subscription, SubscriptionOptions} from './subscription';
import * as util from './util';

type TopicCallback = ResourceCallback<Topic, google.pubsub.v1.ITopic>;
type TopicResponse = [Topic, google.pubsub.v1.ITopic];
export type TopicMetadata = google.pubsub.v1.ITopic;

type TopicCallback = ResourceCallback<Topic, TopicMetadata>;
type TopicResponse = [Topic, TopicMetadata];

export type CreateTopicCallback = TopicCallback;
export type CreateTopicResponse = TopicResponse;
Expand All @@ -38,8 +40,14 @@ export type GetTopicResponse = TopicResponse;

export type GetTopicOptions = CallOptions&{autoCreate?: boolean};

export type GetTopicMetadataCallback = RequestCallback<google.pubsub.v1.ITopic>;
export type GetTopicMetadataResponse = [google.pubsub.v1.ITopic];
type MetadataCallback = RequestCallback<TopicMetadata>;
type MetadataResponse = [TopicMetadata];

export type GetTopicMetadataCallback = MetadataCallback;
export type GetTopicMetadataResponse = MetadataResponse;

export type SetTopicMetadataCallback = MetadataCallback;
export type SetTopicMetadataResponse = MetadataResponse;

export type GetTopicSubscriptionsCallback = RequestCallback<
Subscription, google.pubsub.v1.IListTopicSubscriptionsResponse>;
Expand Down Expand Up @@ -67,7 +75,7 @@ export class Topic {
pubsub: PubSub;
request: typeof PubSub.prototype.request;
iam: IAM;
metadata?: google.pubsub.v1.ITopic;
metadata?: TopicMetadata;
publisher: Publisher;
getSubscriptionsStream = paginator.streamify('getSubscriptions') as() =>
ObjectStream<Subscription>;
Expand Down Expand Up @@ -440,7 +448,7 @@ export class Topic {
topic: this.name,
};

this.request<google.pubsub.v1.ITopic>(
this.request<TopicMetadata>(
{
client: 'PublisherClient',
method: 'getTopic',
Expand Down Expand Up @@ -656,6 +664,78 @@ export class Topic {
return this.publish(data, attributes, callback!);
}

setMetadata(options: TopicMetadata, gaxOpts?: CallOptions):
Promise<SetTopicMetadataResponse>;
setMetadata(options: TopicMetadata, callback: SetTopicMetadataCallback): void;
setMetadata(
options: TopicMetadata, gaxOpts: CallOptions,
callback: SetTopicMetadataCallback): void;
/**
* @typedef {array} SetTopicMetadataResponse
* @property {object} 0 The full API response.
*/
/**
* @callback SetTopicMetadataCallback
* @param {?Error} err Request error, if any.
* @param {object} apiResponse The full API response.
*/
/**
* Updates the topic.
*
* @see [UpdateTopicRequest API Documentation]{@link https://cloud.google.com/pubsub/docs/reference/rest/v1/UpdateTopicRequest}
*
* @param {object} metadata The fields to update. This should be structured
* like a {@link
* https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics#Topic|Topic
* object}.
* @param {object} [gaxOpts] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {SetTopicMetadataCallback} [callback] Callback function.
* @returns {Promise<SetTopicMetadataResponse>}
*
* @example
* const {PubSub} = require('@google-cloud/pubsub');
* const pubsub = new PubSub();
*
* const topic = pubsub.topic('my-topic');
* const metadata = {
* labels: {foo: 'bar'}
* };
*
* topic.setMetadata(metadata, err => {
* if (err) {
* // Error handling omitted.
* }
* });
*
* @example <caption>If the callback is omitted, we'll return a
* Promise.</caption>
* topic.setMetadata(metadata).then((data) => {
* const apiResponse = data[0];
* });
*/
setMetadata(
options: TopicMetadata,
optsOrCallback?: CallOptions|SetTopicMetadataCallback,
callback?: SetTopicMetadataCallback):
void|Promise<SetTopicMetadataResponse> {
const gaxOpts = typeof optsOrCallback === 'object' ? optsOrCallback : {};
callback = typeof optsOrCallback === 'function' ? optsOrCallback : callback;

const topic = Object.assign({name: this.name}, options);
const updateMask = {paths: Object.keys(options)};
const reqOpts = {topic, updateMask};

this.request<TopicMetadata>(
{
client: 'PublisherClient',
method: 'updateTopic',
reqOpts,
gaxOpts,
},
callback!);
}

/**
* Set the publisher options.
*
Expand Down
49 changes: 49 additions & 0 deletions test/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,55 @@ describe('Topic', () => {
});
});

describe('setMetadata', () => {
const METADATA = {
labels: {yee: 'haw'},
};

let requestStub: sinon.SinonStub;

beforeEach(() => {
requestStub = sandbox.stub(topic, 'request');
});

it('should call the correct rpc', () => {
topic.setMetadata(METADATA, assert.ifError);

const [{client, method}] = requestStub.lastCall.args;
assert.strictEqual(client, 'PublisherClient');
assert.strictEqual(method, 'updateTopic');
});

it('should send the correct request options', () => {
topic.setMetadata(METADATA, assert.ifError);

const expectedTopic = Object.assign({name: topic.name}, METADATA);
const expectedUpdateMask = {paths: ['labels']};

const [{reqOpts}] = requestStub.lastCall.args;
assert.deepStrictEqual(reqOpts.topic, expectedTopic);
assert.deepStrictEqual(reqOpts.updateMask, expectedUpdateMask);
});

it('should accept call options', () => {
const callOptions = {};

topic.setMetadata(METADATA, callOptions, assert.ifError);

const [{gaxOpts}] = requestStub.lastCall.args;
assert.strictEqual(gaxOpts, callOptions);
});

it('should pass the user callback to request', () => {
const spy = sandbox.spy();

topic.setMetadata(METADATA, spy);

const [, callback] = requestStub.lastCall.args;
assert.strictEqual(callback, spy);
});
});

describe('setPublishOptions', () => {
it('should call through to Publisher#setOptions', () => {
const fakeOptions = {};
Expand Down

0 comments on commit 20eb583

Please sign in to comment.