pulumi/pkg/resource/autonaming
Mikhail Shilkov fd6853c3f6
Refine autonaming config calculation on provider/resource level (#18034)
Founds the first bug in autonaming configuration... 

Here is my sample program for Azure Native:

```yaml
name: autonaming
description: Autonaming configuration example
runtime: yaml
config:
  pulumi:autonaming:
    value:
      pattern: ${project}-${stack}-${name}
      providers:
        azure-native:
          resources:
            azure-native:storage:StorageAccount:
              pattern: ${project}${stack}${name}${alphanum(6)}
resources:
  rg:
    type: azure-native:resources:ResourceGroup
  sa:
    type: azure-native:storage:StorageAccount
    properties:
      resourceGroupName: ${rg.name}
      sku:
        name: Standard_LRS
      kind: StorageV2
```

Expected result: both resource group and storage account are named as
specified by custom configuration.

Actual result: resource group is named according to our default policy,
not user's configuration.

It starts working correctly if I specify the provider pattern
explicitly:

```yaml
...
pattern: ${project}-${stack}-${name}
  providers:
    azure-native:
      pattern: ${project}-${stack}-${name}
```

It turns out we assign the default config if nothing is specified on the
provider level explicitly, which is wrong: we should use the top-level
"global" config instead.

This PR:

- Changes `resolveNamingConfig` to return a boolean flag of whether an
explicit configuration was found (`false` means we returned a default)
- If `false` is returned on the provider level, then the global
configuration is used
- If `false` is returned on the resource level, then an error is
returned to the user (what were they trying to achieve?)
- While looking at this logic again, I added a check to error if
`enforce` is specified without a `pattern` on any level

Added tests to account for all those use cases, plus a corollary use
case that a user can specify `default` on the provider or resource level
and it will use the default naming, not the parent one.

Fix https://github.com/pulumi/pulumi/issues/18033
2024-12-13 16:01:09 +00:00
..
config.go Refine autonaming config calculation on provider/resource level (#18034) 2024-12-13 16:01:09 +00:00
config_test.go Refine autonaming config calculation on provider/resource level (#18034) 2024-12-13 16:01:09 +00:00
json.go Autonaming configuration in experimental mode (#17916) 2024-12-10 14:03:27 +00:00
pattern.go Autonaming configuration in experimental mode (#17916) 2024-12-10 14:03:27 +00:00
pattern_test.go Autonaming configuration in experimental mode (#17916) 2024-12-10 14:03:27 +00:00
strategy.go Autonaming configuration in experimental mode (#17916) 2024-12-10 14:03:27 +00:00
strategy_test.go Autonaming configuration in experimental mode (#17916) 2024-12-10 14:03:27 +00:00