change resource names
This commit is contained in:
parent
e26df90a66
commit
cd41ad1a40
|
@ -56,6 +56,7 @@ const (
|
||||||
// This is not an exhaustive match, but it should be extremely close.
|
// This is not an exhaustive match, but it should be extremely close.
|
||||||
validResourceGroupNameRe = "^[^_\\W][\\w-._\\(\\)]{0,89}$"
|
validResourceGroupNameRe = "^[^_\\W][\\w-._\\(\\)]{0,89}$"
|
||||||
validManagedDiskName = "^[^_\\W][\\w-._)]{0,79}$"
|
validManagedDiskName = "^[^_\\W][\\w-._)]{0,79}$"
|
||||||
|
validResourceNamePrefix = "^[^_\\W][\\w-._)]{0,10}$"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -65,6 +66,7 @@ var (
|
||||||
reResourceGroupName = regexp.MustCompile(validResourceGroupNameRe)
|
reResourceGroupName = regexp.MustCompile(validResourceGroupNameRe)
|
||||||
reSnapshotName = regexp.MustCompile(`^[A-Za-z0-9_]{1,79}$`)
|
reSnapshotName = regexp.MustCompile(`^[A-Za-z0-9_]{1,79}$`)
|
||||||
reSnapshotPrefix = regexp.MustCompile(`^[A-Za-z0-9_]{1,59}$`)
|
reSnapshotPrefix = regexp.MustCompile(`^[A-Za-z0-9_]{1,59}$`)
|
||||||
|
reResourceNamePrefix = regexp.MustCompile(validResourceNamePrefix)
|
||||||
)
|
)
|
||||||
|
|
||||||
type PlanInformation struct {
|
type PlanInformation struct {
|
||||||
|
@ -382,6 +384,9 @@ type Config struct {
|
||||||
// `virtual_network_name` is not allowed.
|
// `virtual_network_name` is not allowed.
|
||||||
AllowedInboundIpAddresses []string `mapstructure:"allowed_inbound_ip_addresses"`
|
AllowedInboundIpAddresses []string `mapstructure:"allowed_inbound_ip_addresses"`
|
||||||
|
|
||||||
|
// specify custom azure resource names
|
||||||
|
CustomResourcePrefix string `mapstructure:"custom_resource_build_prefix" required:"false"`
|
||||||
|
|
||||||
// Runtime Values
|
// Runtime Values
|
||||||
UserName string `mapstructure-to-hcl2:",skip"`
|
UserName string `mapstructure-to-hcl2:",skip"`
|
||||||
Password string `mapstructure-to-hcl2:",skip"`
|
Password string `mapstructure-to-hcl2:",skip"`
|
||||||
|
@ -628,7 +633,7 @@ func setWinRMCertificate(c *Config) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func setRuntimeValues(c *Config) {
|
func setRuntimeValues(c *Config) {
|
||||||
var tempName = NewTempName()
|
var tempName = NewTempName(c.CaptureNamePrefix)
|
||||||
|
|
||||||
c.tmpAdminPassword = tempName.AdminPassword
|
c.tmpAdminPassword = tempName.AdminPassword
|
||||||
// store so that we can access this later during provisioning
|
// 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 != "" {
|
if c.VirtualNetworkName == "" && c.VirtualNetworkResourceGroupName != "" {
|
||||||
errs = packer.MultiErrorAppend(errs, fmt.Errorf("If virtual_network_resource_group_name is specified, so must virtual_network_name"))
|
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
|
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) {
|
func assertAllowedInboundIpAddresses(ipAddresses []string, setting string) (bool, error) {
|
||||||
for _, ipAddress := range ipAddresses {
|
for _, ipAddress := range ipAddresses {
|
||||||
if net.ParseIP(ipAddress) == nil {
|
if net.ParseIP(ipAddress) == nil {
|
||||||
|
|
|
@ -22,20 +22,36 @@ type TempName struct {
|
||||||
NsgName string
|
NsgName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTempName() *TempName {
|
func NewTempName(p string) *TempName {
|
||||||
tempName := &TempName{}
|
tempName := &TempName{}
|
||||||
|
|
||||||
suffix := random.AlphaNumLower(10)
|
if p == "" {
|
||||||
tempName.ComputeName = fmt.Sprintf("pkrvm%s", suffix)
|
suffix := random.AlphaNumLower(10)
|
||||||
tempName.DeploymentName = fmt.Sprintf("pkrdp%s", suffix)
|
tempName.ComputeName = fmt.Sprintf("pkrvm%s", suffix)
|
||||||
tempName.KeyVaultName = fmt.Sprintf("pkrkv%s", suffix)
|
tempName.DeploymentName = fmt.Sprintf("pkrdp%s", suffix)
|
||||||
tempName.OSDiskName = fmt.Sprintf("pkros%s", suffix)
|
tempName.KeyVaultName = fmt.Sprintf("pkrkv%s", suffix)
|
||||||
tempName.NicName = fmt.Sprintf("pkrni%s", suffix)
|
tempName.OSDiskName = fmt.Sprintf("pkros%s", suffix)
|
||||||
tempName.PublicIPAddressName = fmt.Sprintf("pkrip%s", suffix)
|
tempName.NicName = fmt.Sprintf("pkrni%s", suffix)
|
||||||
tempName.SubnetName = fmt.Sprintf("pkrsn%s", suffix)
|
tempName.PublicIPAddressName = fmt.Sprintf("pkrip%s", suffix)
|
||||||
tempName.VirtualNetworkName = fmt.Sprintf("pkrvn%s", suffix)
|
tempName.SubnetName = fmt.Sprintf("pkrsn%s", suffix)
|
||||||
tempName.NsgName = fmt.Sprintf("pkrsg%s", suffix)
|
tempName.VirtualNetworkName = fmt.Sprintf("pkrvn%s", suffix)
|
||||||
tempName.ResourceGroupName = fmt.Sprintf("packer-Resource-Group-%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.AdminPassword = generatePassword()
|
||||||
tempName.CertificatePassword = random.AlphaNum(32)
|
tempName.CertificatePassword = random.AlphaNum(32)
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTempNameShouldCreatePrefixedRandomNames(t *testing.T) {
|
func TestTempNameShouldCreatePrefixedRandomNames(t *testing.T) {
|
||||||
tempName := NewTempName()
|
tempName := NewTempName("")
|
||||||
|
|
||||||
if strings.Index(tempName.ComputeName, "pkrvm") != 0 {
|
if strings.Index(tempName.ComputeName, "pkrvm") != 0 {
|
||||||
t.Errorf("Expected ComputeName to begin with 'pkrvm', but got '%s'!", tempName.ComputeName)
|
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) {
|
func TestTempAdminPassword(t *testing.T) {
|
||||||
tempName := NewTempName()
|
tempName := NewTempName("")
|
||||||
|
|
||||||
if !strings.ContainsAny(tempName.AdminPassword, random.PossibleNumbers) {
|
if !strings.ContainsAny(tempName.AdminPassword, random.PossibleNumbers) {
|
||||||
t.Errorf("Expected AdminPassword to contain at least one of '%s'!", 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) {
|
func TestTempNameShouldHaveSameSuffix(t *testing.T) {
|
||||||
tempName := NewTempName()
|
tempName := NewTempName("")
|
||||||
suffix := tempName.ComputeName[5:]
|
suffix := tempName.ComputeName[5:]
|
||||||
|
|
||||||
if strings.HasSuffix(tempName.ComputeName, suffix) != true {
|
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)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue