根据@Mikhail Shilkov 的回答,我创建了一个帮助函数来为 azure 上的存储帐户资源的名称提供格式。但是在我使用Pulumi.dev.yaml 的dev 堆栈的配置来读取我想要分配给存储帐户名称的值之前。
以setting and getting configuration values 的方式作为参考,我将其设置为以下值以包含在我的dev 堆栈中:
pulumi config set org rhd
pulumi config set application wmlab
pulumi config set environment dev
只要设置了这些值,我就可以在 Pulumi.dev.yaml 堆栈文件中看到它们:(* Pulumi 将项目名称 wmlab-infrastructure 赋予这些值)
config:
azure-native:location: westeurope # This one was set it up when creating the pulumi python project
wmlab-infrastructure:application: wmlab
wmlab-infrastructure:environment: dev
wmlab-infrastructure:org: rhd
然后从python我使用Config.require通过以这种方式给出键来获取值:
def generate_storage_account_name(name: str, number: int, org: str, app: str, env: str):
return f"{name}{number}{org}{app}{env}"
config = pulumi.Config()
organization = config.require('org')
application = config.require('application')
environment = config.require('environment')
然后在创建存储帐户名称时,我调用了generate_storage_account_name 辅助函数:
(* 我正在使用random.randint(a,b) 函数为存储帐户的名称提供一个整数值,在为其分配名称时会更容易)
# Create an Azure Resource Group
resource_group = azure_native.resources.ResourceGroup(
'resource_group',
resource_group_name="{0}-{1}-{2}".format(organization, application, environment)
)
# Create an Azure resource (Storage Account)
account = storage.StorageAccount(
'main',
resource_group_name=resource_group.name,
account_name=generate_storage_account_name('sa', random.randint(1,100000), organization, application, environment),
sku=storage.SkuArgs(
name=storage.SkuName.STANDARD_LRS,
),
kind=storage.Kind.STORAGE_V2)
而且它有效。创建资源时,存储帐户的名称使用辅助函数:
> pulumi up
Previewing update (rhdhv/dev)
View Live: https://app.pulumi.com/myorg/wmlab-infrastructure/dev/previews/549c2c34-853f-4fe0-b9f2-d5504525b073
Type Name Plan
+ pulumi:pulumi:Stack wmlab-infrastructure-dev create
+ ├─ azure-native:resources:ResourceGroup resource_group create
+ └─ azure-native:storage:StorageAccount main create
Resources:
+ 3 to create
Do you want to perform this update? details
+ pulumi:pulumi:Stack: (create)
[urn=urn:pulumi:dev::wmlab-infrastructure::pulumi:pulumi:Stack::wmlab-infrastructure-dev]
+ azure-native:resources:ResourceGroup: (create)
[urn=urn:pulumi:dev::wmlab-infrastructure::azure-native:resources:ResourceGroup::resource_group]
[provider=urn:pulumi:dev::wmlab-infrastructure::pulumi:providers:azure-native::default_1_29_0::04da6b54-80e4-46f7-96ec-b56ff0331ba9]
location : "westeurope"
resourceGroupName: "rhd-wmlab-dev"
+ azure-native:storage:StorageAccount: (create)
[urn=urn:pulumi:dev::wmlab-infrastructure::azure-native:storage:StorageAccount::main]
[provider=urn:pulumi:dev::wmlab-infrastructure::pulumi:providers:azure-native::default_1_29_0::04da6b54-80e4-46f7-96ec-b56ff0331ba9]
accountName : "sa99180rhdwmlabdev" # HERE THE NAME GENERATED
kind : "StorageV2"
location : "westeurope"
resourceGroupName: output<string>
sku : {
name: "Standard_LRS"
}
阅读更多关于从代码read here访问配置值的信息
Pulumi 有一种自动命名资源的方法,it is explained here,但是改变这个方案看起来是不可能的或者至少不推荐,这样做会导致一些问题并且资源将被重新创建。
覆盖自动命名会使您的项目容易受到命名冲突的影响。因此,对于可能需要替换的资源,您应该在资源的选项中指定 deleteBeforeReplace: true。此选项可确保在创建新资源之前删除旧资源,从而防止这些冲突。
如果我理解得很好,我可以覆盖那些允许在其 API 规范中使用 name 属性的自动命名资源,但是这样做时可能会出现命名冲突 (?)
在我的例子中,我在 python azure API 上使用 StorageAccount 资源,它不允许覆盖属性名称,因此辅助函数运行良好。