nodejs-pubsub
nodejs-pubsub copied to clipboard
topic(setMetadata): messageRetentionDuration not working
Environment details
- OS:
MacOS Monterey v12.4
- Node.js version:
v16.14.2
- npm version:
8.5.0
-
@google-cloud/pubsub
version:3.0.1
Steps to reproduce
Create subscription & setMetadata
const THREE_DAYS_IN_SECONDS = 3 * 24 * 60 * 60
const [topic] = await new PubSub().topic('test').get({ autoCreate: true })
await topic.setMetadata({ messageRetentionDuration: { seconds: THREE_DAYS_IN_SECONDS } })
Error thrown
Error: 3 INVALID_ARGUMENT: Invalid update_mask provided in the UpdateTopicRequest: 'messageRetentionDuration' is not a known Topic field. Note that field paths must be of the form 'push_config' rather than 'pushConfig'.
It seems that messageRetentionDuration is not mapped correctly given this test: https://github.com/googleapis/nodejs-pubsub/blob/e4b8adc22a24ffa61bf6486fd71c05de234e3f51/system-test/pubsub.ts#L537
@ndeitch here is my temporary workaround
the topic.request
is implemented based on the topic.setMetadata()
function. So now you the parameter of setMetadata go to reqOpts.topic, fields are camelCase and in the reqOpts.updateMask.paths. specify field you want to update with _
const topic = client.topic(topicName);
const [topicExists] = await topic.exists();
if (topicExists) {
console.log(`Topic ${topicName} already exists`);
return;
}
try {
await topic.create();
topic.request(
{
client: 'PublisherClient',
method: 'updateTopic',
reqOpts: {
topic: {
name: PubSubClient.getFormattedTopic(topicName, client.projectId),
messageRetentionDuration: { seconds: '604800s' },
},
updateMask: {
paths: ['message_retention_duration'],
},
},
gaxOpts: {},
},
(err, data) => {
if (err) {
console.error(`Error setting topic metadata ${topicName}`, err);
}
},
);
console.log(`Topic ${topicName} created`);
} catch (err) {
console.error(`Error creating topic ${topicName}`, err);
}
Thanks @nguyen-vo for the workaround. I peeked at the code, and it looks like this method (in src/topic.ts) was probably written before we had multi-word topic metadata to update. So it needs to convert the name for the update mask. This shouldn't be a lot of effort, but there's a bunch of work ahead of it in the queue right now. :|