在Google Secret Manager中创建新的秘密版本时,如何获得新创建版本的版本号?
我正在尝试编写一个添加新的Google Secret Manager版本的函数,然后破坏以前的旧版本。
我可以轻松添加一个新版本,但是要销毁旧版本,我需要它的版本号。
按这些文档>通过const [version] =等待secrets.addsecretversion()
,然后从中减去1的新秘密版本号。
但是打字稿抱怨版本
不是一个数字:
算术操作的左侧必须是类型为'Any','','','',bigint'或枚举类型的类型。 TS(2362)
这是我的代码添加新版本并删除旧版本:
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,
});
};
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,
});
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
弄清楚了。
版本
是一个看起来像这样的对象:因此,我用它来访问新版本编号,从而创建旧版本编号:
这是完整的代码:
Figured it out.
version
is an object that looks like this:So I used this to access the new version number and thus create the old version number:
Here is the full code:
简单地使用此端点,通过通过以下方式传递您的资源路径:
在GO中,
返回fmt.sprintf(“%s/s/versions/最新”,G.AppSecretPath(name))
您方法的缺点是它不检查版本状态,这意味着如果有3个状态版本,您添加了一个新版本,例如4,第三版本已经处于残疾状态,您的代码断开,因此,您也需要处理该案例。只需确保您正在禁用该秘密的旧版本即可。虽然如果您足够的话,请随时继续前进。
Simple use this endpoint by passing your resource path like below:
In go,
return fmt.Sprintf("%s/versions/latest", g.appSecretPath(name))
The disadvantage of your approach is that it does not check for the version state, which means if there are 3 versions, you add a new version, say 4, and 3rd version is already in DISABLED state, your code breaks, so, you need to handle that case as well. Just make sure you are disabling the old ENABLED version of the secret. Although feel free to go ahead if that is sufficient for you.
名称“:字符串,“ castement”:string,“ destiontime”:string,“ ca”:enum(ca),“ replicationStatus”:{Object(object(replicationStatus)},“ etag”:string“:clientspecifiedpaypayloadchecksum”:
name": string, "createTime": string, "destroyTime": string, "CA": enum (CA), "replicationStatus": { object (ReplicationStatus) }, "etag": string, "clientSpecifiedPayloadChecksum":