From cd41ad1a407f4b6b39acd83b5c54f1354f92a70f Mon Sep 17 00:00:00 2001 From: Vijay Rajah Date: Tue, 7 Apr 2020 02:17:50 +0530 Subject: [PATCH 1/6] change resource names --- builder/azure/arm/config.go | 20 ++++++++++++- builder/azure/arm/tempname.go | 40 ++++++++++++++++++-------- builder/azure/arm/tempname_test.go | 46 ++++++++++++++++++++++++++++-- 3 files changed, 90 insertions(+), 16 deletions(-) diff --git a/builder/azure/arm/config.go b/builder/azure/arm/config.go index 103ebb337..354d7993a 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,9 @@ type Config struct { // `virtual_network_name` is not allowed. AllowedInboundIpAddresses []string `mapstructure:"allowed_inbound_ip_addresses"` + // specify custom azure resource names + CustomResourcePrefix string `mapstructure:"custom_resource_build_prefix" required:"false"` + // Runtime Values UserName string `mapstructure-to-hcl2:",skip"` Password string `mapstructure-to-hcl2:",skip"` @@ -628,7 +633,7 @@ func setWinRMCertificate(c *Config) error { } func setRuntimeValues(c *Config) { - var tempName = NewTempName() + var tempName = NewTempName(c.CaptureNamePrefix) c.tmpAdminPassword = tempName.AdminPassword // store so that we can access this later during provisioning @@ -934,6 +939,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")) } @@ -1029,6 +1040,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/tempname.go b/builder/azure/arm/tempname.go index 45fb97e63..964c8514c 100644 --- a/builder/azure/arm/tempname.go +++ b/builder/azure/arm/tempname.go @@ -22,20 +22,36 @@ 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.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) + if p == "" { + 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.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) + + } else { + suffix := random.AlphaNumLower(5) + 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..538ed8edd 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) @@ -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) + } +} From 53f54000e6946c89659a698a5116597aae339ea3 Mon Sep 17 00:00:00 2001 From: Vijay Rajah Date: Wed, 8 Apr 2020 23:42:49 +0530 Subject: [PATCH 2/6] generate spec & fix typo --- builder/azure/arm/config.go | 2 +- builder/azure/arm/config.hcl2spec.go | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/builder/azure/arm/config.go b/builder/azure/arm/config.go index 354d7993a..8ba8fbe3a 100644 --- a/builder/azure/arm/config.go +++ b/builder/azure/arm/config.go @@ -633,7 +633,7 @@ func setWinRMCertificate(c *Config) error { } func setRuntimeValues(c *Config) { - var tempName = NewTempName(c.CaptureNamePrefix) + var tempName = NewTempName(c.CustomResourcePrefix) c.tmpAdminPassword = tempName.AdminPassword // store so that we can access this later during provisioning diff --git a/builder/azure/arm/config.hcl2spec.go b/builder/azure/arm/config.hcl2spec.go index def5180b7..c34d43373 100644 --- a/builder/azure/arm/config.hcl2spec.go +++ b/builder/azure/arm/config.hcl2spec.go @@ -1,4 +1,4 @@ -// Code generated by "mapstructure-to-hcl2 -type Config,SharedImageGallery,SharedImageGalleryDestination,PlanInformation"; DO NOT EDIT. +// Code generated by "mapstructure-to-hcl2 -type Config,SharedImageGallery,SharedImageGalleryDestination,PlanInformation -output config.hcl2spec.go"; DO NOT EDIT. package arm import ( @@ -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}, From 1e66a1b35521c60941ce4a82b0b0717bcc2f4902 Mon Sep 17 00:00:00 2001 From: Vijay Rajah Date: Thu, 9 Apr 2020 00:00:13 +0530 Subject: [PATCH 3/6] Fix linting & code generation --- builder/azure/arm/config.hcl2spec.go | 2 +- builder/azure/arm/tempname.go | 1 - .../pages/partials/builder/azure/arm/Config-not-required.mdx | 2 ++ 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/builder/azure/arm/config.hcl2spec.go b/builder/azure/arm/config.hcl2spec.go index c34d43373..1fed61165 100644 --- a/builder/azure/arm/config.hcl2spec.go +++ b/builder/azure/arm/config.hcl2spec.go @@ -1,4 +1,4 @@ -// Code generated by "mapstructure-to-hcl2 -type Config,SharedImageGallery,SharedImageGalleryDestination,PlanInformation -output config.hcl2spec.go"; DO NOT EDIT. +// Code generated by "mapstructure-to-hcl2 -type Config,SharedImageGallery,SharedImageGalleryDestination,PlanInformation"; DO NOT EDIT. package arm import ( diff --git a/builder/azure/arm/tempname.go b/builder/azure/arm/tempname.go index 3196b5f86..f5ee844ee 100644 --- a/builder/azure/arm/tempname.go +++ b/builder/azure/arm/tempname.go @@ -26,7 +26,6 @@ type TempName struct { func NewTempName(p string) *TempName { tempName := &TempName{} - if p == "" { suffix := random.AlphaNumLower(10) tempName.ComputeName = fmt.Sprintf("pkrvm%s", suffix) 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..051717d3c 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,8 @@ Providing `allowed_inbound_ip_addresses` in combination with `virtual_network_name` is not allowed. +- `custom_resource_build_prefix` (string) - specify custom azure resource names + - `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 From 9209826bfe8f64e0e974f42f395683d002631db6 Mon Sep 17 00:00:00 2001 From: Vijay Rajah Date: Fri, 10 Apr 2020 00:34:56 +0530 Subject: [PATCH 4/6] Update the doc with more info --- builder/azure/arm/config.go | 4 +++- .../pages/partials/builder/azure/arm/Config-not-required.mdx | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/builder/azure/arm/config.go b/builder/azure/arm/config.go index f6a5da15c..a0d50b01d 100644 --- a/builder/azure/arm/config.go +++ b/builder/azure/arm/config.go @@ -384,7 +384,9 @@ type Config struct { // `virtual_network_name` is not allowed. AllowedInboundIpAddresses []string `mapstructure:"allowed_inbound_ip_addresses"` - // specify custom azure resource names + // 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 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 051717d3c..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,7 +234,9 @@ Providing `allowed_inbound_ip_addresses` in combination with `virtual_network_name` is not allowed. -- `custom_resource_build_prefix` (string) - specify custom azure resource names +- `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 From fff06353b7ace6f1a52d14623de688caf775043d Mon Sep 17 00:00:00 2001 From: vijayrajah Date: Fri, 17 Apr 2020 00:04:39 +0530 Subject: [PATCH 5/6] Update builder/azure/arm/tempname.go refactor the code.. thanks for the suggestion @sylviamoss Co-Authored-By: Sylvia Moss --- builder/azure/arm/tempname.go | 40 ++++++++++++----------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/builder/azure/arm/tempname.go b/builder/azure/arm/tempname.go index f5ee844ee..1f69b88a9 100644 --- a/builder/azure/arm/tempname.go +++ b/builder/azure/arm/tempname.go @@ -26,33 +26,21 @@ type TempName struct { func NewTempName(p string) *TempName { tempName := &TempName{} + suffix := random.AlphaNumLower(5) if p == "" { - 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.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) - - } else { - suffix := random.AlphaNumLower(5) - 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) - - } + 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) From da7382980a356a0ddeaba3b727c867e97d9d8618 Mon Sep 17 00:00:00 2001 From: Vijay Rajah Date: Fri, 17 Apr 2020 00:17:39 +0530 Subject: [PATCH 6/6] Fix failing test & format code --- builder/azure/arm/tempname.go | 7 ++++--- builder/azure/arm/tempname_test.go | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/builder/azure/arm/tempname.go b/builder/azure/arm/tempname.go index 1f69b88a9..246de1152 100644 --- a/builder/azure/arm/tempname.go +++ b/builder/azure/arm/tempname.go @@ -28,9 +28,10 @@ func NewTempName(p string) *TempName { suffix := random.AlphaNumLower(5) if p == "" { - p = "pkr" - suffix = random.AlphaNumLower(10) - } + 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) diff --git a/builder/azure/arm/tempname_test.go b/builder/azure/arm/tempname_test.go index 538ed8edd..5f7711a82 100644 --- a/builder/azure/arm/tempname_test.go +++ b/builder/azure/arm/tempname_test.go @@ -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 {