billions-mechanic-26704
09/21/2021, 2:25 PMvariable "org" {
type = string
validation {
condition = length(<http://var.org|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."
}
}
And I use the above variables to name an azure resource group like:
module "resource_group_name" {
source = "gsoft-inc/naming/azurerm//modules/general/resource_group"
name = "main"
prefixes = [<http://var.org|var.org>, var.tenant, var.environment]
}
Is possible to do something similar in pulumi? I saw a similar issue reported here, but looks like this is more under programatically control?prehistoric-activity-61023
09/21/2021, 4:23 PMdef generate_resource_name(name: str, org: str, tenant: str, org: environment):
return f"{org}-{tenant}-{environment}-{name}"
SomeResource(
generate_resource_name("main"),
name=generate_resource_name("main"),
...
)
Notice that you usually have 2 separate names:
• the first argument is the name within pulumi itself
• the second one is the name in cloud provider resource (e.g. name of the GKE cluster)name
as I wrote above and it might cause some issues in case the resource must be recreated)Overriding auto-naming makes your project susceptible to naming collisions. As a result, for resources that may need to be replaced, you should specifyin the resource’s options. This option ensures that old resources are deleted before new ones are created, which will prevent those collisions.deleteBeforeReplace: true
billions-mechanic-26704
09/21/2021, 8:55 PMFor cases that require specific names, you can override auto-naming by specifying a physical name. Most resources have aIf I understood well, I can override those auto-named resources which allow theproperty that you can use to name the resource yourselfname
name
attribute on their API specification, but then when doing so is when naming collisions might be presented (?)
In my case I am using the StorageAccount
resource on python azure API, https://www.pulumi.com/docs/reference/pkg/azure-native/storage/storageaccount/#account_name_python
it does not allow override the property name and the helper function I did was something like this:
def generate_storage_account_name(name: str, number: int, org: str, app: str, env: str):
return f"{name}{number}{org}{app}{env}"
And in the resource I am calling it in this way:
# 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)
It works and my storage account was named in that way
> pulumi up
Previewing update (rhdhv/dev)
View Live: <https://app.pulumi.com/rhdhv/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"
kind : "StorageV2"
location : "westeurope"
resourceGroupName: output<string>
sku : {
name: "Standard_LRS"
}
I am right if I say that in cases when the name
property is not available to be overrided, like this case, so collisions errors names won’t be presented?pulumi up
Previewing update (rhdhv/dev)
View Live: <https://app.pulumi.com/myorg/wmlab-infrastructure/dev/previews/846183e7-9532-4d57-a60a-f11e34d2be07>
Type Name Plan Info
pulumi:pulumi:Stack wmlab-infrastructure-dev
+ ├─ github:index:Repository wmlab-infra create
+- └─ azure-native:storage:StorageAccount main replace [diff: ~accountName]
I am afraid I will have to check this thoroughfully,