Pulumi python 资源命名约定

Pulumi python resource naming convention

在创建资源名称时,是否有一种预构建的方法可以在资源名称上包含前缀?我正在寻找类似于 terraform 的东西,但我不确定我们是否需要以编程方式创建它...

terraform 我有类似的东西:

variable "org" {
  type = string
  validation {
    condition     = length(var.org) <= 3
    error_message = "The org variable cannot be larger than 3 characters."
  }
}

variable "tenant" {
  type = string
  validation {
    condition     = length(var.tenant) <= 4
    error_message = "The tenant variable cannot be larger than 4 characters."
  }
}

variable "environment" {
  type = string
  validation {
    condition     = length(var.environment) <= 4
    error_message = "The environment variable cannot be larger than 4 characters."
  }
}

我使用上述变量命名 Azure 资源组,例如:

module "resource_group_name" {
  source   = "gsoft-inc/naming/azurerm//modules/general/resource_group"
  name     = "main"
  prefixes = [var.org, var.tenant, var.environment]
}

可以在 Pulumi 中做类似的事情吗?我看到报告了一个类似的问题 here,但看起来这更像是在程序控制下(?)

你可以直接使用Python的格式化函数,比如

resource_group = = azure_native.resources.ResourceGroup("main",
    location="eastus",
    resource_group_name="{0}-{1}-{2}-{3}".format(org, tenant, environment, rgname))

您还可以定义辅助函数并在多个地方使用它。

根据 @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,但是改变这个方案看起来是不可能的,或者至少不推荐,这样做会导致一些问题,资源将被重新创建。

Overriding auto-naming makes your project susceptible to naming collisions. As a result, for resources that may need to be replaced, you should specify deleteBeforeReplace: true in the resource’s options. This option ensures that old resources are deleted before new ones are created, which will prevent those collisions.

如果我理解得很好,我可以覆盖那些允许在 API 规范中使用 name 属性的自动命名资源,但是当这样做时可能会出现命名冲突(? )

在我的例子中,我在 python azure API 上使用 StorageAccount 资源,它不允许覆盖 属性 名称,因此辅助函数运行良好.