Skip to content

Commit 8e50b89

Browse files
authored
Merge pull request #541 from tmeckel/feat/custom_resource_build_prefix
feat: support setting custom_resource_build_prefix via environment variable
2 parents 061d1b8 + 820a152 commit 8e50b89

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

.web-docs/components/builder/arm/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,9 @@ Providing `temp_resource_group_name` or `location` in combination with
523523
- `custom_resource_build_prefix` (string) - specify custom azure resource names during build limited to max 10 characters
524524
this will set the prefix for the resources. The actual resource names will be
525525
`custom_resource_build_prefix` + resourcetype + 5 character random alphanumeric string
526+
527+
You can also set this via the environment variable `PACKER_AZURE_CUSTOM_RESOURCE_BUILD_PREFIX`.
528+
If both the config field and the environment variable are present, the config field takes precedence.
526529

527530
- `license_type` (string) - Specify a license type for the build VM to enable Azure Hybrid Benefit. If not set, Pay-As-You-Go license
528531
model (default) will be used. Valid values are:

builder/azure/arm/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,9 @@ type Config struct {
614614
// specify custom azure resource names during build limited to max 10 characters
615615
// this will set the prefix for the resources. The actual resource names will be
616616
// `custom_resource_build_prefix` + resourcetype + 5 character random alphanumeric string
617+
//
618+
// You can also set this via the environment variable `PACKER_AZURE_CUSTOM_RESOURCE_BUILD_PREFIX`.
619+
// If both the config field and the environment variable are present, the config field takes precedence.
617620
CustomResourcePrefix string `mapstructure:"custom_resource_build_prefix" required:"false"`
618621

619622
// Specify a license type for the build VM to enable Azure Hybrid Benefit. If not set, Pay-As-You-Go license
@@ -1431,6 +1434,12 @@ func assertRequiredParametersSet(c *Config, errs *packersdk.MultiError) {
14311434
}
14321435
}
14331436

1437+
if c.CustomResourcePrefix == "" {
1438+
val, ok := os.LookupEnv("PACKER_AZURE_CUSTOM_RESOURCE_BUILD_PREFIX")
1439+
if ok {
1440+
c.CustomResourcePrefix = val
1441+
}
1442+
}
14341443
if c.CustomResourcePrefix != "" {
14351444
if ok, err := assertResourceNamePrefix(c.CustomResourcePrefix, "custom_resource_build_prefix"); !ok {
14361445
errs = packersdk.MultiErrorAppend(errs, err)

builder/azure/arm/config_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2772,6 +2772,56 @@ func TestConfigShouldAcceptValidCustomResourceBuildPrefix(t *testing.T) {
27722772
}
27732773
}
27742774

2775+
func TestEnvVarSetsCustomResourceBuildPrefix_Invalid(t *testing.T) {
2776+
// Invalid env var should cause validation to fail when field not explicitly set
2777+
t.Setenv("PACKER_AZURE_CUSTOM_RESOURCE_BUILD_PREFIX", "pkr_123456")
2778+
2779+
config := map[string]interface{}{
2780+
"location": "ignore",
2781+
"subscription_id": "ignore",
2782+
"image_offer": "ignore",
2783+
"image_publisher": "ignore",
2784+
"image_sku": "ignore",
2785+
"os_type": "linux",
2786+
"resource_group_name": "ignore",
2787+
"storage_account": "ignore",
2788+
"capture_container_name": "ignore",
2789+
"capture_name_prefix": "ignore",
2790+
}
2791+
2792+
var c Config
2793+
if _, err := c.Prepare(config, getPackerConfiguration()); err == nil {
2794+
t.Fatal("expected config to reject invalid env var value for custom_resource_build_prefix")
2795+
}
2796+
}
2797+
2798+
func TestConfigCustomResourceBuildPrefixTakesPrecedenceOverEnv(t *testing.T) {
2799+
// When both are present, the config value should win
2800+
t.Setenv("PACKER_AZURE_CUSTOM_RESOURCE_BUILD_PREFIX", "pkr-env99")
2801+
2802+
config := map[string]interface{}{
2803+
"location": "ignore",
2804+
"subscription_id": "ignore",
2805+
"image_offer": "ignore",
2806+
"image_publisher": "ignore",
2807+
"image_sku": "ignore",
2808+
"os_type": "linux",
2809+
"resource_group_name": "ignore",
2810+
"storage_account": "ignore",
2811+
"capture_container_name": "ignore",
2812+
"capture_name_prefix": "ignore",
2813+
"custom_resource_build_prefix": "pkr-12345-",
2814+
}
2815+
2816+
var c Config
2817+
if _, err := c.Prepare(config, getPackerConfiguration()); err != nil {
2818+
t.Fatalf("expected config to succeed when both env and config set, got error: %s", err)
2819+
}
2820+
if c.CustomResourcePrefix != "pkr-12345-" {
2821+
t.Fatalf("expected CustomResourcePrefix to be set from config (precedence), got %q", c.CustomResourcePrefix)
2822+
}
2823+
}
2824+
27752825
func TestConfigShouldNormalizeLicenseTypeCase(t *testing.T) {
27762826
config := map[string]string{
27772827
"capture_name_prefix": "ignore",

docs-partials/builder/azure/arm/Config-not-required.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@
385385
- `custom_resource_build_prefix` (string) - specify custom azure resource names during build limited to max 10 characters
386386
this will set the prefix for the resources. The actual resource names will be
387387
`custom_resource_build_prefix` + resourcetype + 5 character random alphanumeric string
388+
389+
You can also set this via the environment variable `PACKER_AZURE_CUSTOM_RESOURCE_BUILD_PREFIX`.
390+
If both the config field and the environment variable are present, the config field takes precedence.
388391

389392
- `license_type` (string) - Specify a license type for the build VM to enable Azure Hybrid Benefit. If not set, Pay-As-You-Go license
390393
model (default) will be used. Valid values are:

0 commit comments

Comments
 (0)