![](/img/trans.png)
[英]Pulumi and Pulumi Azure Native not recognised in VS Code
[英]How to set global tags in Pulumi Azure Native
在特定於堆棧的設置文件(即Pulumi.dev.yaml
)中,如果設置了位置(即azure-native:location
),則會自動設置資源組位置,並且資源的位置來自資源組位置。 現在我正在嘗試為所有資源應用通用標簽,即CreatedBy: Pulumi
。 有沒有辦法設置通用/全局標簽,類似於設置文件( Pulumi.dev.yaml
)中的azure-native:location
?
預期:位置和標簽都將從Pulumi.dev.yaml
設置
config:
azure-native:location: japaneast
azure-native:tags:
CreatedBy: Pulumi
var mainRgArgs = config.RequireObject<JsonElement>(KEY_RESOURCE_GROUP_ARGS);
var mainRgName = mainRgArgs.GetProperty(RESOURCE_GROUP_NAME).GetString()!;
var mainRg = new ResourceGroup(RESOURCE_GROUP_MAIN, new ResourceGroupArgs
{
ResourceGroupName = mainRgName
//Location =
//Tags =
});
無法自動設置標簽,因為標簽不是必需的 API 屬性。
location
是 provider 參數的原因是因為每個資源在創建時都需要一個位置。 對於標簽而言,情況並非如此。
但是,它可以自動添加標簽使用是加標簽(這是不是所有的資源)的資源轉化
轉換允許您將屬性注入每個資源,無論您是否已在資源上明確設置該值。 但是,您必須設置可標記資源的列表,因為並非每個 Azure 資源都是可標記的。
在資源上注冊標簽的函數看起來像這樣:
export function registerAutoTags(autoTags: Record<string, string>): void {
pulumi.runtime.registerStackTransformation((args) => {
if (isTaggable(args.type)) {
args.props["tags"] = { ...args.props["tags"], ...autoTags };
return { props: args.props, opts: args.opts };
}
return undefined;
});
}
然后你可以通過調用函數來使用這些標簽:
registerAutoTags({
"user:Project": pulumi.getProject(),
"user:Stack": pulumi.getStack(),
"user:Cost Center": config.require("costCenter"),
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.