'When creating a new secret version in Google Secret Manager, how do I get the version number of the newly created version?

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,
  });
};


Solution 1:[1]

Figured it out.

version is an object that looks like this:

{
   "destroyTime":null,
   "state":"ENABLED",
   "etag":"\"9999999999\"",
   "createTime":{
      "seconds":"9999999999",
      "nanos":9999999999
   },
   "clientSpecifiedPayloadChecksum":false,
   "name":"projects/9999999999/secrets/secret-name/versions/109",
   "replicationStatus":{
      "automatic":{
         "customerManagedEncryption":null
      },
      "replicationStatus":"automatic"
   }
}

So I used this to access the new version number and thus create the old version number:

const newVersionNumber = Number(newVersion.name?.split('/').pop());
const oldVersionNumber = newVersionNumber - 1;

Here is the full code:

const addSecretVersion = async (secretName: string, value: string) => {
  const parent = `projects/my-projects/secrets/${secretName}`;
  const payload = Buffer.from(value, 'utf8');
  // Add the new secret
  const [newVersion] = await secrets.addSecretVersion({
    parent: parent,
    payload: {
      data: payload,
    },
  });
  const newVersionNumber = Number(newVersionName.name?.split('/').pop());
  const oldVersionNumber = newVersionNumber - 1;
  // Destroy the old secret (to avoid billing)
  const oldSecret = `projects/my-projects/secrets/${secretName}/versions/${oldVersionNumber}`;
  await secrets.destroySecretVersion({
    name: oldSecret,
  });
};

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1