I'm trying to write a function that adds a new Google Secret Manager version, and then destroys the previous old version.
I can add a new version easily, but to destroy the old version I need it's version number.
As per these docs I have tried to get the new secret version number via const [version] = await secrets.addSecretVersion()
and then minus 1 from that.
But TypeScript is complaining that version
is not a number:
The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.ts(2362)
Here is my code for adding a new version and deleteing the old version:
const addSecretVersion = async (secretName: string, value: string) => {
const parent = `projects/my-project/secrets/${secretName}`;
const payload = Buffer.from(value, 'utf8');
// Add the new secret
const [version] = await secrets.addSecretVersion({
parent: parent,
payload: {
data: payload,
},
});
const oldVersionNumber = version - 1; //<--- TypeScript error here
// Destroy the old secret (to avoid billing)
const oldSecret = `projects/my-project/secrets/${secretName}/versions/${oldVersionNumber}`;
await secrets.destroySecretVersion({
name: oldSecret,
});
};