diff --git a/builder/azure/arm/config.go b/builder/azure/arm/config.go index 35b1ee55c..a0d50b01d 100644 --- a/builder/azure/arm/config.go +++ b/builder/azure/arm/config.go @@ -56,6 +56,7 @@ const ( // This is not an exhaustive match, but it should be extremely close. validResourceGroupNameRe = "^[^_\\W][\\w-._\\(\\)]{0,89}$" validManagedDiskName = "^[^_\\W][\\w-._)]{0,79}$" + validResourceNamePrefix = "^[^_\\W][\\w-._)]{0,10}$" ) var ( @@ -65,6 +66,7 @@ var ( reResourceGroupName = regexp.MustCompile(validResourceGroupNameRe) reSnapshotName = regexp.MustCompile(`^[A-Za-z0-9_]{1,79}$`) reSnapshotPrefix = regexp.MustCompile(`^[A-Za-z0-9_]{1,59}$`) + reResourceNamePrefix = regexp.MustCompile(validResourceNamePrefix) ) type PlanInformation struct { @@ -382,6 +384,11 @@ type Config struct { // `virtual_network_name` is not allowed. AllowedInboundIpAddresses []string `mapstructure:"allowed_inbound_ip_addresses"` + // specify custom azure resource names during build limited to max 10 charcters + // this will set the prefix for the resources. The actuall resource names will be + // `custom_resource_build_prefix` + resourcetype + 5 character random alphanumeric string + CustomResourcePrefix string `mapstructure:"custom_resource_build_prefix" required:"false"` + // Runtime Values UserName string `mapstructure-to-hcl2:",skip"` Password string `mapstructure-to-hcl2:",skip"` @@ -629,7 +636,7 @@ func setWinRMCertificate(c *Config) error { } func setRuntimeValues(c *Config) { - var tempName = NewTempName() + var tempName = NewTempName(c.CustomResourcePrefix) c.tmpAdminPassword = tempName.AdminPassword // store so that we can access this later during provisioning @@ -936,6 +943,12 @@ func assertRequiredParametersSet(c *Config, errs *packer.MultiError) { } } + if c.CustomResourcePrefix != "" { + if ok, err := assertResourceNamePrefix(c.CustomResourcePrefix, "custom_resource_build_prefix"); !ok { + errs = packer.MultiErrorAppend(errs, err) + } + } + if c.VirtualNetworkName == "" && c.VirtualNetworkResourceGroupName != "" { errs = packer.MultiErrorAppend(errs, fmt.Errorf("If virtual_network_resource_group_name is specified, so must virtual_network_name")) } @@ -1031,6 +1044,13 @@ func assertManagedImageDataDiskSnapshotName(name, setting string) (bool, error) return true, nil } +func assertResourceNamePrefix(name, setting string) (bool, error) { + if !isValidAzureName(reResourceNamePrefix, name) { + return false, fmt.Errorf("The setting %s must only contain characters from a-z, A-Z, 0-9 and _ and the maximum length is 10 characters", setting) + } + return true, nil +} + func assertAllowedInboundIpAddresses(ipAddresses []string, setting string) (bool, error) { for _, ipAddress := range ipAddresses { if net.ParseIP(ipAddress) == nil { diff --git a/builder/azure/arm/config.hcl2spec.go b/builder/azure/arm/config.hcl2spec.go index 68d83a05f..3d3dceb84 100644 --- a/builder/azure/arm/config.hcl2spec.go +++ b/builder/azure/arm/config.hcl2spec.go @@ -69,6 +69,7 @@ type FlatConfig struct { AdditionalDiskSize []int32 `mapstructure:"disk_additional_size" required:"false" cty:"disk_additional_size"` DiskCachingType *string `mapstructure:"disk_caching_type" required:"false" cty:"disk_caching_type"` AllowedInboundIpAddresses []string `mapstructure:"allowed_inbound_ip_addresses" cty:"allowed_inbound_ip_addresses"` + CustomResourcePrefix *string `mapstructure:"custom_resource_build_prefix" required:"false" cty:"custom_resource_build_prefix"` Type *string `mapstructure:"communicator" cty:"communicator"` PauseBeforeConnect *string `mapstructure:"pause_before_connecting" cty:"pause_before_connecting"` SSHHost *string `mapstructure:"ssh_host" cty:"ssh_host"` @@ -184,6 +185,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "disk_additional_size": &hcldec.AttrSpec{Name: "disk_additional_size", Type: cty.List(cty.Number), Required: false}, "disk_caching_type": &hcldec.AttrSpec{Name: "disk_caching_type", Type: cty.String, Required: false}, "allowed_inbound_ip_addresses": &hcldec.AttrSpec{Name: "allowed_inbound_ip_addresses", Type: cty.List(cty.String), Required: false}, + "custom_resource_build_prefix": &hcldec.AttrSpec{Name: "custom_resource_build_prefix", Type: cty.String, Required: false}, "communicator": &hcldec.AttrSpec{Name: "communicator", Type: cty.String, Required: false}, "pause_before_connecting": &hcldec.AttrSpec{Name: "pause_before_connecting", Type: cty.String, Required: false}, "ssh_host": &hcldec.AttrSpec{Name: "ssh_host", Type: cty.String, Required: false}, diff --git a/builder/azure/arm/tempname.go b/builder/azure/arm/tempname.go index 1471e44ac..246de1152 100644 --- a/builder/azure/arm/tempname.go +++ b/builder/azure/arm/tempname.go @@ -23,21 +23,25 @@ type TempName struct { NsgName string } -func NewTempName() *TempName { +func NewTempName(p string) *TempName { tempName := &TempName{} - suffix := random.AlphaNumLower(10) - tempName.ComputeName = fmt.Sprintf("pkrvm%s", suffix) - tempName.DeploymentName = fmt.Sprintf("pkrdp%s", suffix) - tempName.KeyVaultName = fmt.Sprintf("pkrkv%s", suffix) - tempName.OSDiskName = fmt.Sprintf("pkros%s", suffix) - tempName.DataDiskname = fmt.Sprintf("pkrdd%s", suffix) - tempName.NicName = fmt.Sprintf("pkrni%s", suffix) - tempName.PublicIPAddressName = fmt.Sprintf("pkrip%s", suffix) - tempName.SubnetName = fmt.Sprintf("pkrsn%s", suffix) - tempName.VirtualNetworkName = fmt.Sprintf("pkrvn%s", suffix) - tempName.NsgName = fmt.Sprintf("pkrsg%s", suffix) - tempName.ResourceGroupName = fmt.Sprintf("packer-Resource-Group-%s", suffix) + suffix := random.AlphaNumLower(5) + if p == "" { + p = "pkr" + suffix = random.AlphaNumLower(10) + } + + tempName.ComputeName = fmt.Sprintf("%svm%s", p, suffix) + tempName.DeploymentName = fmt.Sprintf("%sdp%s", p, suffix) + tempName.KeyVaultName = fmt.Sprintf("%skv%s", p, suffix) + tempName.OSDiskName = fmt.Sprintf("%sos%s", p, suffix) + tempName.NicName = fmt.Sprintf("%sni%s", p, suffix) + tempName.PublicIPAddressName = fmt.Sprintf("%sip%s", p, suffix) + tempName.SubnetName = fmt.Sprintf("%ssn%s", p, suffix) + tempName.VirtualNetworkName = fmt.Sprintf("%svn%s", p, suffix) + tempName.NsgName = fmt.Sprintf("%ssg%s", p, suffix) + tempName.ResourceGroupName = fmt.Sprintf("%s-Resource-Group-%s", p, suffix) tempName.AdminPassword = generatePassword() tempName.CertificatePassword = random.AlphaNum(32) diff --git a/builder/azure/arm/tempname_test.go b/builder/azure/arm/tempname_test.go index e6a6fd236..5f7711a82 100644 --- a/builder/azure/arm/tempname_test.go +++ b/builder/azure/arm/tempname_test.go @@ -8,7 +8,7 @@ import ( ) func TestTempNameShouldCreatePrefixedRandomNames(t *testing.T) { - tempName := NewTempName() + tempName := NewTempName("") if strings.Index(tempName.ComputeName, "pkrvm") != 0 { t.Errorf("Expected ComputeName to begin with 'pkrvm', but got '%s'!", tempName.ComputeName) @@ -30,8 +30,8 @@ func TestTempNameShouldCreatePrefixedRandomNames(t *testing.T) { t.Errorf("Expected PublicIPAddressName to begin with 'pkrip', but got '%s'!", tempName.PublicIPAddressName) } - if strings.Index(tempName.ResourceGroupName, "packer-Resource-Group-") != 0 { - t.Errorf("Expected ResourceGroupName to begin with 'packer-Resource-Group-', but got '%s'!", tempName.ResourceGroupName) + if strings.Index(tempName.ResourceGroupName, "pkr-Resource-Group-") != 0 { + t.Errorf("Expected ResourceGroupName to begin with 'pkr-Resource-Group-', but got '%s'!", tempName.ResourceGroupName) } if strings.Index(tempName.SubnetName, "pkrsn") != 0 { @@ -48,7 +48,7 @@ func TestTempNameShouldCreatePrefixedRandomNames(t *testing.T) { } func TestTempAdminPassword(t *testing.T) { - tempName := NewTempName() + tempName := NewTempName("") if !strings.ContainsAny(tempName.AdminPassword, random.PossibleNumbers) { t.Errorf("Expected AdminPassword to contain at least one of '%s'!", random.PossibleNumbers) @@ -62,7 +62,7 @@ func TestTempAdminPassword(t *testing.T) { } func TestTempNameShouldHaveSameSuffix(t *testing.T) { - tempName := NewTempName() + tempName := NewTempName("") suffix := tempName.ComputeName[5:] if strings.HasSuffix(tempName.ComputeName, suffix) != true { @@ -101,3 +101,43 @@ func TestTempNameShouldHaveSameSuffix(t *testing.T) { t.Errorf("Expected NsgName to end with '%s', but the value is '%s'!", suffix, tempName.NsgName) } } + +func TestTempNameShouldCreateCustomPrefix(t *testing.T) { + tempName := NewTempName("CustPrefix") + + if strings.Index(tempName.ComputeName, "CustPrefixvm") != 0 { + t.Errorf("Expected ComputeName to begin with 'CustPrefixvm', but got '%s'!", tempName.ComputeName) + } + + if strings.Index(tempName.DeploymentName, "CustPrefixdp") != 0 { + t.Errorf("Expected ComputeName to begin with 'CustPrefixdp', but got '%s'!", tempName.ComputeName) + } + + if strings.Index(tempName.OSDiskName, "CustPrefixos") != 0 { + t.Errorf("Expected OSDiskName to begin with 'CustPrefixos', but got '%s'!", tempName.OSDiskName) + } + + if strings.Index(tempName.NicName, "CustPrefixni") != 0 { + t.Errorf("Expected NicName to begin with 'CustPrefixni', but got '%s'!", tempName.NicName) + } + + if strings.Index(tempName.PublicIPAddressName, "CustPrefixip") != 0 { + t.Errorf("Expected PublicIPAddressName to begin with 'CustPrefixip', but got '%s'!", tempName.PublicIPAddressName) + } + + if strings.Index(tempName.ResourceGroupName, "CustPrefix-Resource-Group-") != 0 { + t.Errorf("Expected ResourceGroupName to begin with 'packer-Resource-Group-', but got '%s'!", tempName.ResourceGroupName) + } + + if strings.Index(tempName.SubnetName, "CustPrefixsn") != 0 { + t.Errorf("Expected SubnetName to begin with 'pkrip', but got '%s'!", tempName.SubnetName) + } + + if strings.Index(tempName.VirtualNetworkName, "CustPrefixvn") != 0 { + t.Errorf("Expected VirtualNetworkName to begin with 'CustPrefixvn', but got '%s'!", tempName.VirtualNetworkName) + } + + if strings.Index(tempName.NsgName, "CustPrefixsg") != 0 { + t.Errorf("Expected NsgName to begin with 'CustPrefixsg', but got '%s'!", tempName.NsgName) + } +} diff --git a/website/pages/partials/builder/azure/arm/Config-not-required.mdx b/website/pages/partials/builder/azure/arm/Config-not-required.mdx index e57e51119..816dd12ce 100644 --- a/website/pages/partials/builder/azure/arm/Config-not-required.mdx +++ b/website/pages/partials/builder/azure/arm/Config-not-required.mdx @@ -234,6 +234,10 @@ Providing `allowed_inbound_ip_addresses` in combination with `virtual_network_name` is not allowed. +- `custom_resource_build_prefix` (string) - specify custom azure resource names during build limited to max 10 charcters + this will set the prefix for the resources. The actuall resource names will be + `custom_resource_build_prefix` + resourcetype + 5 character random alphanumeric string + - `async_resourcegroup_delete` (bool) - If you want packer to delete the temporary resource group asynchronously set this value. It's a boolean value and defaults to false. Important Setting this true means that