Files
Packer-Cn/builder/azure/arm/tempname.go
T

81 lines
2.0 KiB
Go
Raw Normal View History

package arm
import (
"fmt"
2018-07-11 15:32:45 -07:00
"strings"
"github.com/hashicorp/packer/common/random"
)
type TempName struct {
2016-04-21 16:50:03 -07:00
AdminPassword string
CertificatePassword string
ComputeName string
DeploymentName string
KeyVaultName string
ResourceGroupName string
OSDiskName string
DataDiskName string
2018-03-10 11:17:43 -08:00
NicName string
SubnetName string
PublicIPAddressName string
VirtualNetworkName string
NsgName string
}
2020-04-07 02:17:50 +05:30
func NewTempName(p string) *TempName {
tempName := &TempName{}
2020-04-17 00:04:39 +05:30
suffix := random.AlphaNumLower(5)
2020-04-07 02:17:50 +05:30
if p == "" {
2020-04-17 00:17:39 +05:30
p = "pkr"
suffix = random.AlphaNumLower(10)
}
2020-04-17 00:04:39 +05:30
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.DataDiskName = fmt.Sprintf("%sdd%s", p, suffix)
2020-04-17 00:04:39 +05:30
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)
2018-07-11 15:32:45 -07:00
tempName.AdminPassword = generatePassword()
tempName.CertificatePassword = random.AlphaNum(32)
return tempName
}
2018-07-11 15:32:45 -07:00
// generate a password that is acceptable to Azure
// Three of the four items must be met.
// 1. Contains an uppercase character
// 2. Contains a lowercase character
// 3. Contains a numeric digit
// 4. Contains a special character
func generatePassword() string {
var s string
for i := 0; i < 100; i++ {
s := random.AlphaNum(32)
if !strings.ContainsAny(s, random.PossibleNumbers) {
2018-07-11 15:32:45 -07:00
continue
}
if !strings.ContainsAny(s, random.PossibleLowerCase) {
2018-07-11 15:32:45 -07:00
continue
}
if !strings.ContainsAny(s, random.PossibleUpperCase) {
2018-07-11 15:32:45 -07:00
continue
}
return s
}
// if an acceptable password cannot be generated in 100 tries, give up
return s
}